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.1 2007-08-24 22:17:35 ewestfal Exp $
044 */
045 public class PersistenceBrokerFactoryBaseImpl implements PersistenceBrokerFactoryIF
046 {
047 private static Logger log = LoggerFactory.getLogger(PersistenceBrokerFactoryBaseImpl.class);
048
049 private Class implementationClass;
050 private long instanceCount;
051
052 public PersistenceBrokerFactoryBaseImpl()
053 {
054 configure(OjbConfigurator.getInstance().getConfigurationFor(null));
055 }
056
057 /**
058 * @see PersistenceBrokerFactoryIF#setDefaultKey
059 */
060 public void setDefaultKey(PBKey key)
061 {
062 try
063 {
064 MetadataManager.getInstance().setDefaultPBKey(key);
065 }
066 catch (Exception e)
067 {
068 throw new PBFactoryException(e);
069 }
070 }
071
072 /**
073 * @see PersistenceBrokerFactoryIF#getDefaultKey()
074 */
075 public PBKey getDefaultKey()
076 {
077 return MetadataManager.getInstance().getDefaultPBKey();
078 }
079
080 /**
081 * For internal use! This method creates real new PB instances
082 */
083 protected PersistenceBrokerInternal createNewBrokerInstance(PBKey key) throws PBFactoryException
084 {
085 if (key == null) throw new PBFactoryException("Could not create new broker with PBkey argument 'null'");
086 // check if the given key really exists
087 if (MetadataManager.getInstance().connectionRepository().getDescriptor(key) == null)
088 {
089 throw new PBFactoryException("Given PBKey " + key + " does not match in metadata configuration");
090 }
091 if (log.isEnabledFor(Logger.INFO))
092 {
093 // only count created instances when INFO-Log-Level
094 log.info("Create new PB instance for PBKey " + key +
095 ", already created persistence broker instances: " + instanceCount);
096 // useful for testing
097 ++this.instanceCount;
098 }
099
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 * Always return a new created {@link PersistenceBroker} instance
119 *
120 * @param pbKey
121 * @return
122 * @throws PBFactoryException
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 try to find a valid PBKey, if given key does not full match
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 * @see PersistenceBrokerFactoryIF#createPersistenceBroker(
146 * String jcdAlias, String user, String password)
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 * @see PersistenceBrokerFactoryIF#createPersistenceBroker(PBKey key)
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 * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
166 */
167 public void configure(Configuration config) throws ConfigurationException
168 {
169 implementationClass = ((PersistenceBrokerConfiguration) config).getPersistenceBrokerClass();
170 }
171
172 /**
173 * @see PersistenceBrokerFactoryIF#releaseAllInstances()
174 */
175 public synchronized void releaseAllInstances()
176 {
177 instanceCount = 0;
178 }
179
180 /**
181 * Not implemented!
182 *
183 * @return always 0
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 }