that must be subclassed for a specific runtime.
| Method from org.apache.openjpa.kernel.AbstractBrokerFactory Detail: |
public void addLifecycleListener(Object listener,
Class[] classes) {
lock();
try {
assertOpen();
if (_lifecycleListeners == null)
_lifecycleListeners = new HashMap(7);
_lifecycleListeners.put(listener, classes);
} finally {
unlock();
}
}
|
protected void addListeners(BrokerImpl broker) {
if (_lifecycleListeners != null && !_lifecycleListeners.isEmpty()) {
Map.Entry entry;
for (Iterator itr = _lifecycleListeners.entrySet().iterator();
itr.hasNext();) {
entry = (Map.Entry) itr.next();
broker.addLifecycleListener(entry.getKey(), (Class[])
entry.getValue());
}
}
if (_transactionListeners != null && !_transactionListeners.isEmpty()) {
for (Iterator itr = _transactionListeners.iterator();
itr.hasNext(); ) {
broker.addTransactionListener(itr.next());
}
}
}
Add factory-registered lifecycle listeners to the broker. |
public void addTransactionListener(Object listener) {
lock();
try {
assertOpen();
if (_transactionListeners == null)
_transactionListeners = new LinkedList();
_transactionListeners.add(listener);
} finally {
unlock();
}
}
|
public void close() {
lock();
try {
assertOpen();
assertNoActiveTransaction();
// remove from factory pool
synchronized (_pool) {
if (_pool.get(_poolKey) == this)
_pool.remove(_poolKey);
}
// close all brokers
Broker broker;
for (Iterator itr = _brokers.iterator(); itr.hasNext();) {
broker = (Broker) itr.next();
// Check for null because _brokers may contain weak references
if ((broker != null) && (!broker.isClosed()))
broker.close();
}
if(_conf.metaDataRepositoryAvailable()) {
// remove metadata repository from listener list
PCRegistry.removeRegisterClassListener
(_conf.getMetaDataRepositoryInstance());
}
_conf.close();
_closed = true;
Log log = _conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
if (log.isTraceEnabled())
_closedException = new IllegalStateException();
} finally {
unlock();
}
}
|
protected void configureBroker(BrokerImpl broker) {
broker.setOptimistic(_conf.getOptimistic());
broker.setNontransactionalRead(_conf.getNontransactionalRead());
broker.setNontransactionalWrite(_conf.getNontransactionalWrite());
broker.setRetainState(_conf.getRetainState());
broker.setRestoreState(_conf.getRestoreStateConstant());
broker.setAutoClear(_conf.getAutoClearConstant());
broker.setIgnoreChanges(_conf.getIgnoreChanges());
broker.setMultithreaded(_conf.getMultithreaded());
broker.setAutoDetach(_conf.getAutoDetachConstant());
broker.setDetachState(_conf.getDetachStateInstance().
getDetachState());
}
Configures the given broker with the current factory option settings. |
protected BrokerImpl findBroker(String user,
String pass,
boolean managed) {
if (managed)
return findTransactionalBroker(user, pass);
return null;
}
Find a pooled broker, or return null if none. If using
managed transactions, looks for a transactional broker;
otherwise returns null by default. This method will be called before
#newStoreManager so that factory subclasses implementing
pooling can return a matching manager before a new StoreManager
is created. |
protected BrokerImpl findTransactionalBroker(String user,
String pass) {
Transaction trans;
ManagedRuntime mr = _conf.getManagedRuntimeInstance();
Object txKey;
try {
trans = mr.getTransactionManager().
getTransaction();
txKey = mr.getTransactionKey();
if (trans == null
|| trans.getStatus() == Status.STATUS_NO_TRANSACTION
|| trans.getStatus() == Status.STATUS_UNKNOWN)
return null;
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new GeneralException(e);
}
Collection brokers = (Collection) _transactional.get(txKey);
if (brokers != null) {
// we don't need to synchronize on brokers since one JTA transaction
// can never be active on multiple concurrent threads.
BrokerImpl broker;
for (Iterator itr = brokers.iterator(); itr.hasNext();) {
broker = (BrokerImpl) itr.next();
if (StringUtils.equals(broker.getConnectionUserName(),
user) && StringUtils.equals
(broker.getConnectionPassword(), pass))
return broker;
}
}
return null;
}
Find a managed runtime broker associated with the
current transaction, or returns null if none. |
public OpenJPAConfiguration getConfiguration() {
return _conf;
}
Return the configuration for this factory. |
protected Object getFactoryInitializationBanner() {
return _loc.get("factory-init", OpenJPAVersion.VERSION_NUMBER);
}
Return an object to be written to the log when this broker factory
initializes. This happens after the configuration is fully loaded. |
public Collection getOpenBrokers() {
return Collections.unmodifiableCollection(_brokers);
}
Returns a set of all the open brokers associated with this factory. The
returned set is unmodifiable, and may contain null references. |
public Object getPoolKey() {
return _poolKey;
}
|
public static AbstractBrokerFactory getPooledFactoryForKey(Object key) {
return (AbstractBrokerFactory) _pool.get(key);
}
Return the pooled factory matching the given key, or null
if none. The key must be of the form created by #getPoolKey . |
public Properties getProperties() {
// required props are VendorName and VersionNumber
Properties props = new Properties();
props.setProperty("VendorName", OpenJPAVersion.VENDOR_NAME);
props.setProperty("VersionNumber", OpenJPAVersion.VERSION_NUMBER);
props.setProperty("VersionId", OpenJPAVersion.VERSION_ID);
return props;
}
Subclasses should override this method to add a Platform
property listing the runtime platform, such as:
OpenJPA JDBC Edition: Oracle Database |
public Object getUserObject(Object key) {
lock();
try {
assertOpen();
return (_userObjects == null) ? null : _userObjects.get(key);
} finally {
unlock();
}
}
|
void initializeBroker(boolean managed,
int connRetainMode,
BrokerImpl broker,
boolean fromDeserialization) {
assertOpen();
makeReadOnly();
// decorate the store manager for data caching and custom
// result object providers; always make sure it's a delegating
// store manager, because it's easier for users to deal with
// that way
StoreManager sm = newStoreManager();
DelegatingStoreManager dsm = null;
if (_conf.getDataCacheManagerInstance().getSystemDataCache()
!= null)
dsm = new DataCacheStoreManager(sm);
dsm = new ROPStoreManager((dsm == null) ? sm : dsm);
broker.initialize(this, dsm, managed, connRetainMode,
fromDeserialization);
if (!fromDeserialization)
addListeners(broker);
// if we're using remote events, register the event manager so
// that it can broadcast commit notifications from the broker
RemoteCommitEventManager remote = _conf.
getRemoteCommitEventManager();
if (remote.areRemoteEventsEnabled())
broker.addTransactionListener(remote);
loadPersistentTypes(broker.getClassLoader());
_brokers.add(broker);
_conf.setReadOnly(Configuration.INIT_STATE_FROZEN);
}
|
public boolean isClosed() {
return _closed;
}
Returns true if this broker factory is closed. |
public void lock() {
_lock.lock();
}
|
public void makeReadOnly() {
if (_readOnly)
return;
lock();
try {
// check again
if (_readOnly)
return;
_readOnly = true;
Log log = _conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
if (log.isInfoEnabled())
log.info(getFactoryInitializationBanner());
if (log.isTraceEnabled()) {
Map props = _conf.toProperties(true);
String lineSep = J2DoPrivHelper.getLineSeparator();
StringBuffer buf = new StringBuffer();
Map.Entry entry;
for (Iterator itr = props.entrySet().iterator();
itr.hasNext();) {
entry = (Map.Entry) itr.next();
buf.append(entry.getKey()).append(": ").
append(entry.getValue());
if (itr.hasNext())
buf.append(lineSep);
}
log.trace(_loc.get("factory-properties", buf.toString()));
}
// setup transient state
setup();
// register the metdata repository to auto-load persistent types
// and make sure types are enhanced
MetaDataRepository repos = _conf.getMetaDataRepositoryInstance();
repos.setValidate(repos.VALIDATE_RUNTIME, true);
repos.setResolve(repos.MODE_MAPPING_INIT, true);
PCRegistry.addRegisterClassListener(repos);
// freeze underlying configuration and eagerly initialize to
// avoid synchronization
_conf.setReadOnly(Configuration.INIT_STATE_FREEZING);
_conf.instantiateAll();
// fire an event for all the broker factory listeners
// registered on the configuration.
_conf.getBrokerFactoryEventManager().fireEvent(
new BrokerFactoryEvent(this,
BrokerFactoryEvent.BROKER_FACTORY_CREATED));
} finally {
unlock();
}
}
Freezes the configuration of this factory. |
public Broker newBroker() {
return newBroker(_conf.getConnectionUserName(),
_conf.getConnectionPassword());
}
|
public Broker newBroker(String user,
String pass) {
return newBroker(user, pass, _conf.isTransactionModeManaged(),
_conf.getConnectionRetainModeConstant());
}
|
public Broker newBroker(boolean managed,
int connRetainMode) {
return newBroker(_conf.getConnectionUserName(),
_conf.getConnectionPassword(), managed, connRetainMode);
}
|
public Broker newBroker(String user,
String pass,
boolean managed,
int connRetainMode) {
return newBroker(user, pass, managed, connRetainMode, true);
}
|
public Broker newBroker(String user,
String pass,
boolean managed,
int connRetainMode,
boolean findExisting) {
try {
assertOpen();
makeReadOnly();
BrokerImpl broker = null;
if (findExisting)
broker = findBroker(user, pass, managed);
if (broker == null) {
broker = newBrokerImpl(user, pass);
initializeBroker(managed, connRetainMode, broker, false);
}
return broker;
} catch (OpenJPAException ke) {
throw ke;
} catch (RuntimeException re) {
throw new GeneralException(re);
}
}
|
protected BrokerImpl newBrokerImpl(String user,
String pass) {
BrokerImpl broker = _conf.newBrokerInstance(user, pass);
if (broker == null)
throw new UserException(_loc.get("no-broker-class",
_conf.getBrokerImpl()));
return broker;
}
Return a broker configured with the proper settings.
By default, this method constructs a new
BrokerImpl of the class set for this factory. |
abstract protected StoreManager newStoreManager()
Return a new StoreManager for this runtime. Note that the instance
returned here may be wrapped before being passed to the
#newBroker method. |
protected static void pool(Object key,
AbstractBrokerFactory factory) {
synchronized(_pool) {
_pool.put(key, factory);
factory.setPoolKey(key);
factory.makeReadOnly();
}
}
Register factory in the pool under key. |
public Object putUserObject(Object key,
Object val) {
lock();
try {
assertOpen();
if (val == null)
return (_userObjects == null) ? null : _userObjects.remove(key);
if (_userObjects == null)
_userObjects = new HashMap();
return _userObjects.put(key, val);
} finally {
unlock();
}
}
|
protected Object readResolve() throws ObjectStreamException {
AbstractBrokerFactory factory = getPooledFactoryForKey(_poolKey);
if (factory != null)
return factory;
// reset these transient fields to empty values
_transactional = new ConcurrentHashMap();
_brokers = newBrokerSet();
makeReadOnly();
return this;
}
Replaces the factory with this JVMs pooled version if it exists. Also
freezes the factory. |
protected void releaseBroker(BrokerImpl broker) {
_brokers.remove(broker);
}
Release broker from any internal data structures. This
is invoked by broker after the broker is fully closed. |
public void removeLifecycleListener(Object listener) {
lock();
try {
assertOpen();
if (_lifecycleListeners != null)
_lifecycleListeners.remove(listener);
} finally {
unlock();
}
}
|
public void removeTransactionListener(Object listener) {
lock();
try {
assertOpen();
if (_transactionListeners != null)
_transactionListeners.remove(listener);
} finally {
unlock();
}
}
|
void setPoolKey(Object key) {
_poolKey = key;
}
Set a key that can be used to obtain this broker factory from the
pool at a later time. |
protected void setup() {
}
Setup transient state used by this factory based on the
current configuration, which will subsequently be locked down. This
method will be called before the first broker is requested,
and will be re-called each time the factory is deserialized into a JVM
that has no configuration for this data store. |
boolean syncWithManagedTransaction(BrokerImpl broker,
boolean begin) {
Transaction trans;
try {
ManagedRuntime mr = broker.getManagedRuntime();
TransactionManager tm = mr.getTransactionManager();
trans = tm.getTransaction();
if (trans != null
&& (trans.getStatus() == Status.STATUS_NO_TRANSACTION
|| trans.getStatus() == Status.STATUS_UNKNOWN))
trans = null;
if (trans == null && begin) {
tm.begin();
trans = tm.getTransaction();
} else if (trans == null)
return false;
// synch broker and trans
trans.registerSynchronization(broker);
// we don't need to synchronize on brokers or guard against multiple
// threads using the same trans since one JTA transaction can never
// be active on multiple concurrent threads.
Object txKey = mr.getTransactionKey();
Collection brokers = (Collection) _transactional.get(txKey);
if (brokers == null) {
brokers = new ArrayList(2);
_transactional.put(txKey, brokers);
trans.registerSynchronization(new RemoveTransactionSync(txKey));
}
brokers.add(broker);
return true;
} catch (OpenJPAException ke) {
throw ke;
} catch (Exception e) {
throw new GeneralException(e);
}
}
Synchronize the given broker with a managed transaction,
optionally starting one if none is in progress. |
protected static Object toPoolKey(Map map) {
Object key = Configurations.getProperty("Id", map);
return ( key != null) ? key : map;
}
Return an internal factory pool key for the given configuration. |
public void unlock() {
_lock.unlock();
}
|