| 1 | |
package org.apache.ojb.broker.core; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 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 | |
|
| 58 | |
|
| 59 | |
|
| 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 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public PBKey getDefaultKey() |
| 76 | |
{ |
| 77 | |
return MetadataManager.getInstance().getDefaultPBKey(); |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 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 | |
|
| 87 | |
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 | |
|
| 94 | |
log.info("Create new PB instance for PBKey " + key + |
| 95 | |
", already created persistence broker instances: " + instanceCount); |
| 96 | |
|
| 97 | |
++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 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 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 | |
|
| 129 | |
|
| 130 | |
|
| 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 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 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 | |
|
| 155 | |
|
| 156 | |
|
| 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 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
public void configure(Configuration config) throws ConfigurationException |
| 168 | |
{ |
| 169 | |
implementationClass = ((PersistenceBrokerConfiguration) config).getPersistenceBrokerClass(); |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public synchronized void releaseAllInstances() |
| 176 | |
{ |
| 177 | |
instanceCount = 0; |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 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 | |
} |