KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > core > PersistenceBrokerFactoryBaseImpl


1 package org.apache.ojb.broker.core;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
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  * This is an base implementation of the {@link PersistenceBrokerFactoryIF}
35  * interface. Each request ({@link PersistenceBrokerFactoryIF#createPersistenceBroker} or
36  * {@link PersistenceBrokerFactoryIF#defaultPersistenceBroker} call) creates a new
37  * {@link PersistenceBroker} instance. No pooling of broker instances is used.
38  *
39  * @see PersistenceBrokerFactoryDefaultImpl
40  *
41  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler<a>
42  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
43  * @version $Id: PersistenceBrokerFactoryBaseImpl.java,v 1.3.2.3 2005/12/21 22:25:00 tomdz Exp $
44  */

45 public class PersistenceBrokerFactoryBaseImpl implements PersistenceBrokerFactoryIF
46 {
47     private static Logger log = LoggerFactory.getLogger(PersistenceBrokerFactoryBaseImpl.class);
48
49     private Class JavaDoc implementationClass;
50     private long instanceCount;
51
52     public PersistenceBrokerFactoryBaseImpl()
53     {
54         configure(OjbConfigurator.getInstance().getConfigurationFor(null));
55     }
56
57     /**
58      * @see PersistenceBrokerFactoryIF#setDefaultKey
59      */

60     public void setDefaultKey(PBKey key)
61     {
62         try
63         {
64             MetadataManager.getInstance().setDefaultPBKey(key);
65         }
66         catch (Exception JavaDoc e)
67         {
68             throw new PBFactoryException(e);
69         }
70     }
71
72     /**
73      * @see PersistenceBrokerFactoryIF#getDefaultKey()
74      */

75     public PBKey getDefaultKey()
76     {
77         return MetadataManager.getInstance().getDefaultPBKey();
78     }
79
80     /**
81      * For internal use! This method creates real new PB instances
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         // check if the given key really exists
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             // only count created instances when INFO-Log-Level
94
log.info("Create new PB instance for PBKey " + key +
95                     ", already created persistence broker instances: " + instanceCount);
96             // useful for testing
97
++this.instanceCount;
98         }
99
100         PersistenceBrokerInternal instance = null;
101         Class JavaDoc[] types = {PBKey.class, PersistenceBrokerFactoryIF.class};
102         Object JavaDoc[] 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 JavaDoc 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 JavaDoc 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 JavaDoc jcdAlias, String JavaDoc user, String JavaDoc 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 JavaDoc e)
199         {
200             log.error("Error while shutdown of OJB", e);
201             throw e;
202         }
203     }
204 }
205
Popular Tags