KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > xml > JPersistenceUnitInfoHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JPersistenceUnitInfoHelper.java 463 2006-05-17 15:00:58Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence.xml;
27
28 import java.io.IOException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import javax.sql.DataSource JavaDoc;
39
40 import org.objectweb.easybeans.log.JLog;
41 import org.objectweb.easybeans.log.JLogFactory;
42
43 /**
44  * Class used to fill PersistenceInfo implementation class.
45  * @author Florent Benoit
46  */

47 public final class JPersistenceUnitInfoHelper {
48
49     /**
50      * Default XML configuration.
51      */

52     private static final String JavaDoc DEFAULT_PERSISTENCE_CONFIG = "org/objectweb/easybeans/persistence/conf/default-persistence.xml";
53
54     /**
55      * Logger.
56      */

57     private static JLog logger = JLogFactory.getLog(JPersistenceUnitInfoHelper.class);
58
59     /**
60      * Default configuration of persistence.xml (it defines the default
61      * persistence provider and links to external config, one for each provider
62      * class name.
63      */

64     private static JPersistenceUnitInfo defaultPersistenceunitInfo = null;
65
66     /**
67      * Defines the default configuration (properties) for each persistence
68      * provider.
69      */

70     private static Map JavaDoc<String JavaDoc, JPersistenceUnitInfo> providersInfo = null;
71
72     /**
73      * Utility class, no public constructor.
74      */

75     private JPersistenceUnitInfoHelper() {
76
77     }
78
79     /**
80      * Parses the given XML and complete the PersistenceUnitInfos structure
81      * before returning it.
82      * @param url the URL of the the Reader of the XML file.
83      * @return array of jPersistenceInfo which are implementation of
84      * PersistenceUnitInfo object
85      * @throws JPersistenceUnitInfoException if values are incorrect.
86      */

87     public static JPersistenceUnitInfo[] getPersistenceUnitInfo(final URL JavaDoc url) throws JPersistenceUnitInfoException {
88         // load default values if not already loaded.
89
loadDefaultValues();
90
91         JPersistenceUnitInfo[] jPersistenceunitInfos = JPersistenceUnitInfoLoader.loadPersistenceUnitInfoImpl(url);
92
93         for (JPersistenceUnitInfo jPersistenceunitInfo : jPersistenceunitInfos) {
94             // analyze jta datasource
95
String JavaDoc jtaDsName = jPersistenceunitInfo.getJtaDataSourceName();
96
97             // try to get this datasource (if not null)
98
if (jtaDsName != null && !jtaDsName.equals("")) {
99                 DataSource JavaDoc ds = null;
100                 try {
101                     ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup(jtaDsName);
102                 } catch (NamingException JavaDoc e) {
103                     // TODO: Remove
104
// try with jdbc_1
105
try {
106                         logger.warn("Datasource named '" + jtaDsName
107                                 + "' was not found, use instead the default jndi name jdbc_1");
108                         ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("jdbc_1");
109                     } catch (NamingException JavaDoc ne) {
110                         throw new JPersistenceUnitInfoException("Cannot get jta DataSource with the JNDI name '"
111                                 + jtaDsName + "'.", ne);
112                     }
113                 }
114                 jPersistenceunitInfo.setJtaDataSource(ds);
115             }
116
117             // analyze non jta datasource
118
String JavaDoc nonJtaDsName = jPersistenceunitInfo.getNonJtaDataSourceName();
119             // try to get this datasource (if not null)
120
if (nonJtaDsName != null && !nonJtaDsName.equals("")) {
121                 DataSource JavaDoc ds = null;
122                 try {
123                     ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup(nonJtaDsName);
124                 } catch (NamingException JavaDoc e) {
125                     // TODO: Remove
126
// try with jdbc_1
127
try {
128                         logger.warn("Datasource named '" + jtaDsName
129                                 + "' was not found, use instead the default jndi name jdbc_1");
130                         ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("jdbc_1");
131                     } catch (NamingException JavaDoc ne) {
132                         throw new JPersistenceUnitInfoException("Cannot get non jta DataSource with the JNDI name '"
133                                 + nonJtaDsName + "'.", ne);
134                     }
135                 }
136                 jPersistenceunitInfo.setNonJtaDataSource(ds);
137             }
138
139             // Persistence Provider
140
if (jPersistenceunitInfo.getPersistenceProviderClassName() == null
141                     || jPersistenceunitInfo.getPersistenceProviderClassName().equals("")) {
142                 logger.info("No persistence provider was set, set to value {0}.", defaultPersistenceunitInfo
143                         .getPersistenceProviderClassName());
144                 jPersistenceunitInfo.setPersistenceProviderClassName(defaultPersistenceunitInfo
145                         .getPersistenceProviderClassName());
146             }
147
148             // add properties for the given persistence provider
149
JPersistenceUnitInfo providerDefaultConf = providersInfo.get(jPersistenceunitInfo
150                     .getPersistenceProviderClassName());
151             if (providerDefaultConf == null) {
152                 logger.info("No default configuration for the persistence provider {0}", jPersistenceunitInfo
153                         .getPersistenceProviderClassName());
154             } else {
155                 logger.info("Found a default configuration for the persistence provider {0}", jPersistenceunitInfo
156                         .getPersistenceProviderClassName());
157                 Properties JavaDoc defaultProperties = providerDefaultConf.getProperties();
158                 Enumeration JavaDoc providerPropertiesEnum = defaultProperties.propertyNames();
159                 while (providerPropertiesEnum.hasMoreElements()) {
160                     String JavaDoc key = (String JavaDoc) providerPropertiesEnum.nextElement();
161                     String JavaDoc value = defaultProperties.getProperty(key);
162                     // set the value on the provider info
163
if (jPersistenceunitInfo.getProperties().getProperty(key) == null) {
164                         jPersistenceunitInfo.getProperties().setProperty(key, value);
165                         logger.info("Setting the property {0} with value {1}", key, value);
166                     }
167                 }
168
169             }
170         }
171         return jPersistenceunitInfos;
172     }
173
174     /**
175      * Loads the default values (global configuration) and then configuration of
176      * each persistence provider.
177      * @throws JPersistenceUnitInfoException if default configuration is not found
178      */

179     private static synchronized void loadDefaultValues() throws JPersistenceUnitInfoException {
180
181         // already loaded, return.
182
if (defaultPersistenceunitInfo != null && providersInfo != null) {
183             return;
184         }
185         defaultPersistenceunitInfo = new JPersistenceUnitInfo();
186         providersInfo = new HashMap JavaDoc<String JavaDoc, JPersistenceUnitInfo>();
187
188         // Load default values for a persistence INFO
189
ClassLoader JavaDoc currentCL = Thread.currentThread().getContextClassLoader();
190         Enumeration JavaDoc<URL JavaDoc> urlsConfig = null;
191         try {
192             urlsConfig = currentCL.getResources(DEFAULT_PERSISTENCE_CONFIG);
193         } catch (IOException JavaDoc e) {
194             throw new JPersistenceUnitInfoException("Cannot get resources with the name '" + DEFAULT_PERSISTENCE_CONFIG
195                     + "' in the context classloader '" + currentCL + "'.");
196         }
197
198         // reverse the list of URLs as we want to override the values
199
LinkedList JavaDoc<URL JavaDoc> lstURLs = new LinkedList JavaDoc<URL JavaDoc>();
200         while (urlsConfig.hasMoreElements()) {
201             lstURLs.addFirst(urlsConfig.nextElement());
202         }
203
204         // For each URL, analyze properties (only one persistence unit is
205
// expected by module)
206
for (URL JavaDoc tmpURL : lstURLs) {
207             JPersistenceUnitInfo[] jPersistenceunitInfos = JPersistenceUnitInfoLoader
208                     .loadPersistenceUnitInfoImpl(tmpURL);
209             // use first unit name
210
if (jPersistenceunitInfos.length != 1) {
211                 throw new JPersistenceUnitInfoException(
212                         "Each default config file should have only one persistence unit '" + tmpURL + "'.");
213             }
214             JPersistenceUnitInfo pInfo = jPersistenceunitInfos[0];
215             defaultPersistenceunitInfo.setPersistenceProviderClassName(pInfo.getPersistenceProviderClassName());
216
217             // Now extract configuration of each persistence provider
218
Properties JavaDoc providersProperties = pInfo.getProperties();
219             Enumeration JavaDoc providerNames = providersProperties.propertyNames();
220             while (providerNames.hasMoreElements()) {
221                 String JavaDoc providerName = (String JavaDoc) providerNames.nextElement();
222
223                 // check if there is an existing provider, else create it
224
JPersistenceUnitInfo existingProviderInfo = providersInfo.get(providerName);
225                 if (existingProviderInfo == null) {
226                     existingProviderInfo = new JPersistenceUnitInfo();
227                     providersInfo.put(providerName, existingProviderInfo);
228                 }
229
230                 Enumeration JavaDoc<URL JavaDoc> urlsProviderConf = null;
231                 try {
232                     urlsProviderConf = currentCL.getResources(providersProperties.getProperty(providerName));
233                 } catch (IOException JavaDoc e) {
234                     throw new JPersistenceUnitInfoException("Cannot get resources with the name '" + providerName
235                             + "' in the context classloader '" + currentCL + "'.");
236                 }
237                 // reverse order : used to override values
238
LinkedList JavaDoc<URL JavaDoc> reverseProviderConfURLs = new LinkedList JavaDoc<URL JavaDoc>();
239                 while (urlsProviderConf.hasMoreElements()) {
240                     reverseProviderConfURLs.addFirst(urlsProviderConf.nextElement());
241                 }
242
243                 if (reverseProviderConfURLs.size() == 0) {
244                     logger.warn("No default properties for persistence provider class named {0}", providerName);
245                 }
246
247                 // for each info for a provider, set the values
248
for (URL JavaDoc providerURLConf : reverseProviderConfURLs) {
249                     JPersistenceUnitInfo[] providerPersistenceunitInfos = JPersistenceUnitInfoLoader
250                             .loadPersistenceUnitInfoImpl(providerURLConf);
251                     // use first unit name
252
if (providerPersistenceunitInfos.length != 1) {
253                         throw new JPersistenceUnitInfoException(
254                                 "Each default config file should have only one persistence unit '" + providerURLConf
255                                         + "'.");
256                     }
257                     JPersistenceUnitInfo providerInfo = providerPersistenceunitInfos[0];
258
259                     // get provider info and set values
260
Properties JavaDoc providerProperties = providerInfo.getProperties();
261                     Enumeration JavaDoc providerPropertiesEnum = providerProperties.propertyNames();
262                     while (providerPropertiesEnum.hasMoreElements()) {
263                         String JavaDoc key = (String JavaDoc) providerPropertiesEnum.nextElement();
264                         String JavaDoc value = providerProperties.getProperty(key);
265                         // set the value on the provider info
266
existingProviderInfo.getProperties().setProperty(key, value);
267                     }
268
269                 }
270             }
271
272         }
273
274         logger.info("Default persistence provider set to value {0}.", defaultPersistenceunitInfo
275                 .getPersistenceProviderClassName());
276     }
277
278 }
279
Popular Tags