KickJava   Java API By Example, From Geeks To Geeks.

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


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: PersistenceXmlFileAnalyzer.java 925 2006-07-25 12:37:03Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence.xml;
27
28 import java.net.URL JavaDoc;
29
30 import javax.persistence.spi.PersistenceProvider;
31
32 import org.objectweb.easybeans.api.EZBArchive;
33 import org.objectweb.easybeans.api.EZBArchiveException;
34 import org.objectweb.easybeans.log.JLog;
35 import org.objectweb.easybeans.log.JLogFactory;
36 import org.objectweb.easybeans.persistence.PersistenceUnitManager;
37
38 /**
39  * Class used to analyze a given archive (by analyzing the persistence.xml file
40  * if any).
41  * @author Florent Benoit
42  */

43 public final class PersistenceXmlFileAnalyzer {
44
45     /**
46      * Directory where persistence.xml file should be.
47      */

48     private static final String JavaDoc DIRECTORY_PERSISTENCE_XML_FILE = "META-INF";
49
50     /**
51      * Name of the persistence.xml file.
52      */

53     private static final String JavaDoc PERSISTENCE_XML_FILE = "persistence.xml";
54
55     /**
56      * Logger.
57      */

58     private static JLog logger = JLogFactory.getLog(PersistenceXmlFileAnalyzer.class);
59
60     /**
61      * Utility class, no public constructor.
62      */

63     private PersistenceXmlFileAnalyzer() {
64
65     }
66
67     /**
68      * Detects and analyze the META-INF/persistence.xml file.
69      * @param archive the file to analyze (or directory) in order to find a
70      * persistence.xml file.
71      * @param classLoader the classloader used to load the persistence provider
72      * class.
73      * @return a persistence unit manager (which can manage the peristence
74      * contexts).
75      * @throws PersistenceXmlFileAnalyzerException if detection or analyze
76      * fails.
77      */

78     public static PersistenceUnitManager analyzePersistenceXmlFile(final EZBArchive archive, final ClassLoader JavaDoc classLoader)
79             throws PersistenceXmlFileAnalyzerException {
80         URL JavaDoc persistenceXmlURL = null;
81         try {
82             persistenceXmlURL = archive.getResource(DIRECTORY_PERSISTENCE_XML_FILE + '/' + PERSISTENCE_XML_FILE);
83         } catch (EZBArchiveException e) {
84             throw new PersistenceXmlFileAnalyzerException("Cannot check if entry '" + DIRECTORY_PERSISTENCE_XML_FILE
85                     + '/' + PERSISTENCE_XML_FILE + "' is present on the file '" + archive.getName() + "'.", e);
86         }
87
88         // Now, do the parsing and fill the structure.
89
JPersistenceUnitInfo[] persistenceUnitInfos = null;
90         if (persistenceXmlURL != null) {
91             try {
92                 persistenceUnitInfos = JPersistenceUnitInfoHelper.getPersistenceUnitInfo(persistenceXmlURL);
93             } catch (JPersistenceUnitInfoException e) {
94                 throw new PersistenceXmlFileAnalyzerException("Cannot parse the URL '" + persistenceXmlURL + "'.", e);
95             }
96
97             for (JPersistenceUnitInfo persistenceUnitInfo : persistenceUnitInfos) {
98                 try {
99                     // sets the archive
100
persistenceUnitInfo.addJarFile(archive.getURL());
101
102                     // Set the root url
103
persistenceUnitInfo.setPersistenceUnitRootUrl(archive.getURL());
104                 } catch (EZBArchiveException e) {
105                     throw new PersistenceXmlFileAnalyzerException("Cannot get the URL on the jar file '" + archive.getName()
106                             + "'.", e);
107                 }
108
109                 // sets the classloader
110
persistenceUnitInfo.setClassLoader(classLoader);
111
112                 // instantiate persistence provider
113
Class JavaDoc persistenceProviderClass;
114                 try {
115                     ClassLoader JavaDoc ctxLoader = Thread.currentThread().getContextClassLoader();
116                     persistenceProviderClass = ctxLoader.loadClass(persistenceUnitInfo
117                             .getPersistenceProviderClassName());
118                 } catch (ClassNotFoundException JavaDoc e) {
119                     throw new PersistenceXmlFileAnalyzerException("Cannot load the persistence provider class '"
120                             + persistenceUnitInfo.getPersistenceProviderClassName() + "'.");
121                 }
122                 PersistenceProvider persistenceProvider;
123                 try {
124                     persistenceProvider = (PersistenceProvider) persistenceProviderClass.newInstance();
125                 } catch (InstantiationException JavaDoc e) {
126                     throw new PersistenceXmlFileAnalyzerException("Cannot instantiate the persistence provider class '"
127                             + persistenceUnitInfo.getPersistenceProviderClassName() + "'.", e);
128                 } catch (IllegalAccessException JavaDoc e) {
129                     throw new PersistenceXmlFileAnalyzerException("Cannot instantiate the persistence provider class '"
130                             + persistenceUnitInfo.getPersistenceProviderClassName() + "'.", e);
131                 }
132
133                 // Set persistence provider
134
persistenceUnitInfo.setPersistenceProvider(persistenceProvider);
135
136             }
137
138             // create persistence unit manager
139
return new PersistenceUnitManager(persistenceUnitInfos);
140         }
141         // nothing found, return nothing(null)
142
return null;
143     }
144
145 }
146
Popular Tags