Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/enableit/db/daf/DataAbstractionFacadeFactory.java


1   package org.enableit.db.daf;
2   
3   // Java imports 
4   import java.io.InputStreamReader ; 
5   import java.net.URL ; 
6   import java.util.Enumeration ; 
7   
8   // Log4J Imports
9   import org.apache.log4j.Category;
10  
11  // Castor imports
12  import org.exolab.castor.xml.Unmarshaller ;
13  
14  // My imports
15  import org.enableit.db.daf.conf.* ; 
16  import org.enableit.db.daf.castor.CastorJdoDaf ; 
17  import org.enableit.db.daf.jdbc.JdbcDaf ; 
18  import org.enableit.db.daf.ejb.EntityEjbDaf ; 
19  
20  /** 
21   * Factory for <code>DataAbstrationFacades</code>. 
22   * This class relies on an XML resource file named data-abstraction.xml 
23   * to map registered Object types to data persistence capabilities of 
24   * classes implementing the DataAbstractionFacade interface.
25   * 
26   * @author default 
27   */
28  public class DataAbstractionFacadeFactory
29  {
30      /**  
31       * Default Constructor. 
32       */
33      protected  DataAbstractionFacadeFactory ()
34          throws UnconfiguredException
35      {
36          logger.info("METHOD_ENTRY: constructor");
37          
38          if (config==null) {
39              init() ; 
40          }
41  
42          logger.info("METHOD_EXIT: constructor");
43          
44      }
45  
46      /**
47       * Initialise the Factory from the configuration file  
48       * received as an URL parameter.
49       * 
50       * @param config 
51       */
52      public static void init (java.net.URL configUrl)
53          throws UnconfiguredException
54      {
55          logger.info("METHOD_ENTRY: init");
56          logger.info("... with params configUrl=" + configUrl) ; 
57         
58          try {
59              InputStreamReader reader = new InputStreamReader(configUrl.openStream()) ; 
60            config = (DataAbstraction)Unmarshaller.unmarshal(DataAbstraction.class, reader) ; 
61          } catch (Exception e) {
62              logger.fatal(e) ;         
63              throw new UnconfiguredException(e.getClass().getName() + 
64                      ":" + e.getMessage()) ; 
65          }
66  
67          logger.info("METHOD_EXIT: init");
68      }
69     
70      /**  
71       * Initialise the Factory from the configuration file obtained 
72       * as a resource from this class' ClassLoader.
73       */
74      protected void init ()
75          throws UnconfiguredException
76      {
77          logger.info("METHOD_ENTRY: init");
78         
79          URL url = DataAbstractionFacadeFactory.class.getResource(DataAbstractionFacadeFactory.CONFIG_FILENAME) ; 
80      
81          init(url) ; 
82  
83          logger.info("METHOD_EXIT: init");
84      }
85     
86      /**  
87       * Return the appropriate instance of <code>DataAbstractionFacade</code>
88       * for the <code>Object</code> type received.
89       * 
90       * @param bean 
91       * A JavaBean, whose type is registered in the data-abstraction.xml.
92       * 
93       * @throws UnconfiguredException
94       * If the factory cannot find the configuration file as a resource
95       * and one has not been set using the public <code>init</code> method.
96       * <br>
97       * @throws TypeNotFoundException
98       * If the factory <em>is</em> configured but the type of the JavaBean 
99       * supplied is not registered.
100      */
101     public static DataAbstractionFacade getInstance (Object bean)
102         throws UnconfiguredException, TypeUnconfiguredException
103     {
104         logger.info("METHOD_ENTRY: getInstance");
105         logger.info("... with params bean=" + bean) ; 
106            
107         // check have a configuration 
108         if (me==null) {
109             me = new DataAbstractionFacadeFactory() ; 
110         }
111 
112         logger.info("METHOD_EXIT: getInstance");
113         return getInstance(bean.getClass()) ; 
114     }  
115 
116     /**  
117      * Return the appropriate instance of <code>DataAbstractionFacade</code>
118      * for the <code>Class</code> received.
119      * 
120      * @param bean 
121      * A JavaBean, whose type is registered in the data-abstraction.xml.
122      * 
123      * @throws UnconfiguredException
124      * If the factory cannot find the configuration file as a resource
125      * and one has not been set using the public <code>init</code> method.
126      * <br>
127      * @throws TypeNotFoundException
128      * If the factory <em>is</em> configured but the type of the JavaBean 
129      * supplied is not registered.
130      */
131     public static DataAbstractionFacade getInstance (Class beanType)
132         throws UnconfiguredException, TypeUnconfiguredException
133     {
134         logger.info("METHOD_ENTRY: getInstance");
135         logger.info("... with params beanType=" + beanType) ; 
136            
137         // check have a configuration 
138         if (me==null) {
139             me = new DataAbstractionFacadeFactory() ; 
140         }
141 
142         // Get config for the requested Bean 
143         String type = beanType.getName() ; 
144         String persistenceProviderRef = null ; 
145         for (Enumeration enum=config.enumeratePersistentClass() ; enum.hasMoreElements() ; ) {
146         
147             PersistentClass pc = (PersistentClass)enum.nextElement() ; 
148             if (pc.getType().equals(type)) {
149                 persistenceProviderRef = pc.getPersistenceProviderRef() ; 
150                 break ; 
151             }
152         }
153 
154         if (persistenceProviderRef==null) {
155             throw new TypeUnconfiguredException(type + " is not registered") ; 
156         }
157 
158         ProviderDetails pd = null ;
159         for (Enumeration enum=config.enumeratePersistenceProvider() ; enum.hasMoreElements() ; ) {
160             PersistenceProvider pp = (PersistenceProvider)enum.nextElement() ; 
161             if (pp.getName().equals(persistenceProviderRef)) {
162                 pd = pp.getProviderDetails() ;
163             }
164         } 
165 
166         if (pd==null) {
167             throw new TypeUnconfiguredException(type + "is registered as using " + 
168                     persistenceProviderRef + " which is not configured") ; 
169         }
170 
171         logger.info("METHOD_EXIT: getInstance");
172         return getInstance(pd) ;         
173     }
174 
175     /**  
176      * Return the appropriate instance of DataAbstractionFacade
177      * using the <code>PersistenceProviderDetails</code> received.
178      * 
179      * @param ppd
180      * A <code>PersistenceProviderDetails</code>.
181      */
182     public static DataAbstractionFacade getInstance (ProviderDetails pd)
183     {
184         logger.info("METHOD_ENTRY: getInstance");
185            
186         DataAbstractionFacade daf = null ; 
187         if (pd.getJdbc()!=null) {
188             daf = new JdbcDaf(pd) ; 
189         } else if (pd.getEntityEjb()!=null) {
190             daf = new EntityEjbDaf(pd) ; 
191         } else if (pd.getCastor()!=null) {
192             daf = new CastorJdoDaf(pd) ; 
193         }
194         
195         logger.info("METHOD_EXIT: getInstance");
196         return daf ; 
197     }
198 
199 /*
200  * Member properties 
201  */
202     protected static DataAbstractionFacadeFactory me ; 
203 
204     protected static DataAbstraction config ; 
205 
206     /** 
207      * The Log4J <code>Category</code> doing the logging.
208      */
209     private static Category logger = Category.getInstance(DataAbstractionFacadeFactory.class);
210 
211 /*
212  * Member constants 
213  */
214 
215     public static final String CONFIG_FILENAME = "data-abstraction.xml" ; 
216 }