1 package org.apache.ojb.broker.core; 2 3 17 18 import org.apache.ojb.broker.PBFactoryException; 19 import org.apache.ojb.broker.PBKey; 20 import org.apache.ojb.broker.PersistenceBroker; 21 import org.apache.ojb.broker.PersistenceBrokerInternal; 22 import org.apache.ojb.broker.accesslayer.ConnectionFactoryFactory; 23 import org.apache.ojb.broker.metadata.MetadataManager; 24 import org.apache.ojb.broker.util.BrokerHelper; 25 import org.apache.ojb.broker.util.ClassHelper; 26 import org.apache.ojb.broker.util.configuration.Configuration; 27 import org.apache.ojb.broker.util.configuration.ConfigurationException; 28 import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator; 29 import org.apache.ojb.broker.util.interceptor.InterceptorFactory; 30 import org.apache.ojb.broker.util.logging.Logger; 31 import org.apache.ojb.broker.util.logging.LoggerFactory; 32 33 45 public class PersistenceBrokerFactoryBaseImpl implements PersistenceBrokerFactoryIF 46 { 47 private static Logger log = LoggerFactory.getLogger(PersistenceBrokerFactoryBaseImpl.class); 48 49 private Class implementationClass; 50 private long instanceCount; 51 52 public PersistenceBrokerFactoryBaseImpl() 53 { 54 configure(OjbConfigurator.getInstance().getConfigurationFor(null)); 55 } 56 57 60 public void setDefaultKey(PBKey key) 61 { 62 try 63 { 64 MetadataManager.getInstance().setDefaultPBKey(key); 65 } 66 catch (Exception e) 67 { 68 throw new PBFactoryException(e); 69 } 70 } 71 72 75 public PBKey getDefaultKey() 76 { 77 return MetadataManager.getInstance().getDefaultPBKey(); 78 } 79 80 83 protected PersistenceBrokerInternal createNewBrokerInstance(PBKey key) throws PBFactoryException 84 { 85 if (key == null) throw new PBFactoryException("Could not create new broker with PBkey argument 'null'"); 86 if (MetadataManager.getInstance().connectionRepository().getDescriptor(key) == null) 88 { 89 throw new PBFactoryException("Given PBKey " + key + " does not match in metadata configuration"); 90 } 91 if (log.isEnabledFor(Logger.INFO)) 92 { 93 log.info("Create new PB instance for PBKey " + key + 95 ", already created persistence broker instances: " + instanceCount); 96 ++this.instanceCount; 98 } 99 100 PersistenceBrokerInternal instance = null; 101 Class [] types = {PBKey.class, PersistenceBrokerFactoryIF.class}; 102 Object [] args = {key, this}; 103 try 104 { 105 instance = (PersistenceBrokerInternal) ClassHelper.newInstance(implementationClass, types, args); 106 OjbConfigurator.getInstance().configure(instance); 107 instance = (PersistenceBrokerInternal) InterceptorFactory.getInstance().createInterceptorFor(instance); 108 } 109 catch (Exception e) 110 { 111 log.error("Creation of a new PB instance failed", e); 112 throw new PBFactoryException("Creation of a new PB instance failed", e); 113 } 114 return instance; 115 } 116 117 124 public PersistenceBrokerInternal createPersistenceBroker(PBKey pbKey) throws PBFactoryException 125 { 126 if (log.isDebugEnabled()) log.debug("Obtain broker from pool, used PBKey is " + pbKey); 127 128 131 pbKey = BrokerHelper.crossCheckPBKey(pbKey); 132 133 try 134 { 135 return createNewBrokerInstance(pbKey); 136 137 } 138 catch (Exception e) 139 { 140 throw new PBFactoryException("Borrow broker from pool failed, using PBKey " + pbKey, e); 141 } 142 } 143 144 148 public PersistenceBrokerInternal createPersistenceBroker(String jcdAlias, String user, String password) 149 throws PBFactoryException 150 { 151 return this.createPersistenceBroker(new PBKey(jcdAlias, user, password)); 152 } 153 154 157 public PersistenceBrokerInternal defaultPersistenceBroker() throws PBFactoryException 158 { 159 if (getDefaultKey() == null) throw new PBFactoryException("There was no 'default-connection' attribute" + 160 " enabled in the jdbc connection descriptor"); 161 return this.createPersistenceBroker(getDefaultKey()); 162 } 163 164 167 public void configure(Configuration config) throws ConfigurationException 168 { 169 implementationClass = ((PersistenceBrokerConfiguration) config).getPersistenceBrokerClass(); 170 } 171 172 175 public synchronized void releaseAllInstances() 176 { 177 instanceCount = 0; 178 } 179 180 185 public int activePersistenceBroker() 186 { 187 return 0; 188 } 189 190 public void shutdown() 191 { 192 try 193 { 194 ConnectionFactoryFactory.getInstance().createConnectionFactory().releaseAllResources(); 195 PersistenceBrokerThreadMapping.shutdown(); 196 MetadataManager.getInstance().shutdown(); 197 } 198 catch(RuntimeException e) 199 { 200 log.error("Error while shutdown of OJB", e); 201 throw e; 202 } 203 } 204 } 205 | Popular Tags |