001: package org.apache.ojb.broker.core;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.PBFactoryException;
019: import org.apache.ojb.broker.PBKey;
020: import org.apache.ojb.broker.PersistenceBroker;
021: import org.apache.ojb.broker.PersistenceBrokerInternal;
022: import org.apache.ojb.broker.accesslayer.ConnectionFactoryFactory;
023: import org.apache.ojb.broker.metadata.MetadataManager;
024: import org.apache.ojb.broker.util.BrokerHelper;
025: import org.apache.ojb.broker.util.ClassHelper;
026: import org.apache.ojb.broker.util.configuration.Configuration;
027: import org.apache.ojb.broker.util.configuration.ConfigurationException;
028: import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
029: import org.apache.ojb.broker.util.interceptor.InterceptorFactory;
030: import org.apache.ojb.broker.util.logging.Logger;
031: import org.apache.ojb.broker.util.logging.LoggerFactory;
032:
033: /**
034: * This is an base implementation of the {@link PersistenceBrokerFactoryIF}
035: * interface. Each request ({@link PersistenceBrokerFactoryIF#createPersistenceBroker} or
036: * {@link PersistenceBrokerFactoryIF#defaultPersistenceBroker} call) creates a new
037: * {@link PersistenceBroker} instance. No pooling of broker instances is used.
038: *
039: * @see PersistenceBrokerFactoryDefaultImpl
040: *
041: * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
042: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
043: * @version $Id: PersistenceBrokerFactoryBaseImpl.java,v 1.3.2.3 2005/12/21 22:25:00 tomdz Exp $
044: */
045: public class PersistenceBrokerFactoryBaseImpl implements
046: PersistenceBrokerFactoryIF {
047: private static Logger log = LoggerFactory
048: .getLogger(PersistenceBrokerFactoryBaseImpl.class);
049:
050: private Class implementationClass;
051: private long instanceCount;
052:
053: public PersistenceBrokerFactoryBaseImpl() {
054: configure(OjbConfigurator.getInstance().getConfigurationFor(
055: null));
056: }
057:
058: /**
059: * @see PersistenceBrokerFactoryIF#setDefaultKey
060: */
061: public void setDefaultKey(PBKey key) {
062: try {
063: MetadataManager.getInstance().setDefaultPBKey(key);
064: } catch (Exception e) {
065: throw new PBFactoryException(e);
066: }
067: }
068:
069: /**
070: * @see PersistenceBrokerFactoryIF#getDefaultKey()
071: */
072: public PBKey getDefaultKey() {
073: return MetadataManager.getInstance().getDefaultPBKey();
074: }
075:
076: /**
077: * For internal use! This method creates real new PB instances
078: */
079: protected PersistenceBrokerInternal createNewBrokerInstance(
080: PBKey key) throws PBFactoryException {
081: if (key == null)
082: throw new PBFactoryException(
083: "Could not create new broker with PBkey argument 'null'");
084: // check if the given key really exists
085: if (MetadataManager.getInstance().connectionRepository()
086: .getDescriptor(key) == null) {
087: throw new PBFactoryException("Given PBKey " + key
088: + " does not match in metadata configuration");
089: }
090: if (log.isEnabledFor(Logger.INFO)) {
091: // only count created instances when INFO-Log-Level
092: log
093: .info("Create new PB instance for PBKey "
094: + key
095: + ", already created persistence broker instances: "
096: + instanceCount);
097: // useful for testing
098: ++this .instanceCount;
099: }
100:
101: PersistenceBrokerInternal instance = null;
102: Class[] types = { PBKey.class, PersistenceBrokerFactoryIF.class };
103: Object[] args = { key, this };
104: try {
105: instance = (PersistenceBrokerInternal) ClassHelper
106: .newInstance(implementationClass, types, args);
107: OjbConfigurator.getInstance().configure(instance);
108: instance = (PersistenceBrokerInternal) InterceptorFactory
109: .getInstance().createInterceptorFor(instance);
110: } catch (Exception e) {
111: log.error("Creation of a new PB instance failed", e);
112: throw new PBFactoryException(
113: "Creation of a new PB instance failed", e);
114: }
115: return instance;
116: }
117:
118: /**
119: * Always return a new created {@link PersistenceBroker} instance
120: *
121: * @param pbKey
122: * @return
123: * @throws PBFactoryException
124: */
125: public PersistenceBrokerInternal createPersistenceBroker(PBKey pbKey)
126: throws PBFactoryException {
127: if (log.isDebugEnabled())
128: log
129: .debug("Obtain broker from pool, used PBKey is "
130: + pbKey);
131:
132: /*
133: try to find a valid PBKey, if given key does not full match
134: */
135: pbKey = BrokerHelper.crossCheckPBKey(pbKey);
136:
137: try {
138: return createNewBrokerInstance(pbKey);
139:
140: } catch (Exception e) {
141: throw new PBFactoryException(
142: "Borrow broker from pool failed, using PBKey "
143: + pbKey, e);
144: }
145: }
146:
147: /**
148: * @see PersistenceBrokerFactoryIF#createPersistenceBroker(
149: * String jcdAlias, String user, String password)
150: */
151: public PersistenceBrokerInternal createPersistenceBroker(
152: String jcdAlias, String user, String password)
153: throws PBFactoryException {
154: return this .createPersistenceBroker(new PBKey(jcdAlias, user,
155: password));
156: }
157:
158: /**
159: * @see PersistenceBrokerFactoryIF#createPersistenceBroker(PBKey key)
160: */
161: public PersistenceBrokerInternal defaultPersistenceBroker()
162: throws PBFactoryException {
163: if (getDefaultKey() == null)
164: throw new PBFactoryException(
165: "There was no 'default-connection' attribute"
166: + " enabled in the jdbc connection descriptor");
167: return this .createPersistenceBroker(getDefaultKey());
168: }
169:
170: /*
171: * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
172: */
173: public void configure(Configuration config)
174: throws ConfigurationException {
175: implementationClass = ((PersistenceBrokerConfiguration) config)
176: .getPersistenceBrokerClass();
177: }
178:
179: /**
180: * @see PersistenceBrokerFactoryIF#releaseAllInstances()
181: */
182: public synchronized void releaseAllInstances() {
183: instanceCount = 0;
184: }
185:
186: /**
187: * Not implemented!
188: *
189: * @return always 0
190: */
191: public int activePersistenceBroker() {
192: return 0;
193: }
194:
195: public void shutdown() {
196: try {
197: ConnectionFactoryFactory.getInstance()
198: .createConnectionFactory().releaseAllResources();
199: PersistenceBrokerThreadMapping.shutdown();
200: MetadataManager.getInstance().shutdown();
201: } catch (RuntimeException e) {
202: log.error("Error while shutdown of OJB", e);
203: throw e;
204: }
205: }
206: }
|