KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > compiler > lib > JormCompilerConfiguratorImpl


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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 of the License, or (at your option) 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 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.compiler.lib;
25
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator;
28 import org.objectweb.jorm.compiler.api.PExceptionCompiler;
29 import org.objectweb.jorm.generator.api.Generator;
30 import org.objectweb.jorm.generator.api.MOPFactory;
31 import org.objectweb.jorm.lib.JormConfiguratorImpl;
32 import org.objectweb.jorm.util.api.Loggable;
33 import org.objectweb.jorm.verifier.api.MappingVerifier;
34 import org.objectweb.jorm.verifier.api.Verifier;
35 import org.objectweb.util.monolog.api.BasicLevel;
36 import org.objectweb.util.monolog.api.LoggerFactory;
37
38 import java.io.File JavaDoc;
39 import java.io.FileInputStream JavaDoc;
40 import java.io.FileNotFoundException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * Used to hold the configuration information related to a JORM compiler
46  * instance.
47  * @author P. Dechamboux
48  */

49 public class JormCompilerConfiguratorImpl extends JormConfiguratorImpl implements JormCompilerConfigurator {
50     
51     private HashMap JavaDoc mapperName2mofFactory = new HashMap JavaDoc();
52     
53     /**
54      * It assignes the logger factory.
55      * @param lf
56      */

57     public void setLoggerFactory(LoggerFactory lf) {
58         loggerFactory = lf;
59         logger = lf.getLogger(JormCompilerConfigurator.LOGGER_NAME);
60     }
61
62     /**
63      * Gets the verifier to be used by JORM.
64      * @return That verifier.
65      */

66     public Verifier getVerifier() throws PException {
67         if (properties == null) {
68             throw new PExceptionCompiler("JORM has not been configured.");
69         }
70         String JavaDoc cn = properties.getProperty("jorm.verifier");
71         if (cn == null) {
72             throw new PException(
73                     "Impossible to load a verifier: not configured:"
74                     + properties);
75         }
76         try {
77             return (Verifier) loader.loadClass(cn).newInstance();
78         } catch (InstantiationException JavaDoc e) {
79             throw new PExceptionCompiler(e, "Problem while instantiating " + cn);
80         } catch (IllegalAccessException JavaDoc e) {
81             throw new PExceptionCompiler(e, "Problem while trying to access " + cn);
82         } catch (ClassNotFoundException JavaDoc e) {
83             throw new PExceptionCompiler(e, "Problem while loading class " + cn);
84         }
85     }
86
87     /**
88      * Gets the generator to be used by JORM.
89      * @return That generator.
90      */

91     public Generator getGenerator() throws PException {
92         if (properties == null) {
93             throw new PExceptionCompiler("JORM has not been configured.");
94         }
95         String JavaDoc cn = properties.getProperty("jorm.generator");
96         if (cn == null) {
97             throw new PException(
98                     "Impossible to load a generator: not configured:"
99                     + properties);
100         }
101         try {
102             Generator res = (Generator) loader.loadClass(cn).newInstance();
103             return res;
104         } catch (InstantiationException JavaDoc e) {
105             throw new PExceptionCompiler(e, "Problem while instantiating " + cn);
106         } catch (IllegalAccessException JavaDoc e) {
107             throw new PExceptionCompiler(e, "Problem while trying to access " + cn);
108         } catch (ClassNotFoundException JavaDoc e) {
109             throw new PExceptionCompiler(e, "Problem while loading class " + cn);
110         }
111     }
112
113     /**
114      * Gets the name to be used for the compiler options file.
115      * @return That name.
116      */

117     public String JavaDoc getJormcOptsFile() throws PException {
118         if (properties == null) {
119             throw new PExceptionCompiler("JORM has not been configured.");
120         }
121         return properties.getProperty("jorm.compiler.optsfile");
122     }
123
124     /**
125      * Gets the MOP factory associated with the given mapper.
126      * @param mappername The name of the concerned mapper.
127      * @return The MOPFactory object.
128      * @throws PException
129      */

130     public MOPFactory getMOPFactory(String JavaDoc mappername) throws PException {
131         MOPFactory mopFactory = (MOPFactory) mapperName2mofFactory.get(mappername);
132         if (mopFactory != null) {
133             return mopFactory;
134         }
135         if (properties == null) {
136             throw new PExceptionCompiler("JORM has not been configured.");
137         }
138         String JavaDoc cn = properties.getProperty("jorm.mapper.mopfactory." + mappername);
139         if (cn == null) {
140             throw new PException(
141                     "Impossible to load a meta object factory: not configured:"
142                     + properties);
143         }
144         try {
145             mopFactory = (MOPFactory) loader.loadClass(cn).newInstance();
146             mapperName2mofFactory.put(mappername, mopFactory);
147             if (mopFactory instanceof Loggable) {
148                 if (loggerFactory != null) {
149                     ((Loggable) mopFactory).setLoggerFactory(loggerFactory);
150                 } else if (logger != null) {
151                     ((Loggable) mopFactory).setLogger(logger);
152                 }
153             }
154             return mopFactory;
155         } catch (InstantiationException JavaDoc e) {
156             throw new PExceptionCompiler(e, "Problem while instantiating " + cn);
157         } catch (IllegalAccessException JavaDoc e) {
158             throw new PExceptionCompiler(e, "Problem while trying to access " + cn);
159         } catch (ClassNotFoundException JavaDoc e) {
160             throw new PExceptionCompiler(e, "Problem while loading class " + cn);
161         }
162     }
163
164     /**
165      * Gets an InputStream for reading the global compiler parameters.
166      * @return The allocated InputStream.
167      * @throws PException
168      */

169     public InputStream JavaDoc getGlobalJormcOptsFile() throws PException {
170         InputStream JavaDoc res = null;
171         String JavaDoc jormcOptsFile = getJormcOptsFile();
172         if (pathToJormcOpts != null) {
173             if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) {
174                 logger.log(BasicLevel.DEBUG, "JormcOptsFile=" + pathToJormcOpts
175                                              + File.separator + jormcOptsFile);
176             }
177             File JavaDoc f = new File JavaDoc(
178                     pathToJormcOpts + File.separator + jormcOptsFile);
179             if (f.exists()) {
180                 try {
181                     res = new FileInputStream JavaDoc(f);
182                 } catch (FileNotFoundException JavaDoc e) {
183                     throw new PException(e, "Cannot find jorm compiler option file.");
184                 }
185             }
186         }
187         if (res == null) {
188             File JavaDoc f = new File JavaDoc(jormcOptsFile);
189             if (f.exists()) {
190                 try {
191                     res = new FileInputStream JavaDoc(f);
192                 } catch (FileNotFoundException JavaDoc e) {
193                     throw new PException(e, "Cannot find jorm compiler option file.");
194                 }
195             } else {
196                 res = loader.getResourceAsStream(jormcOptsFile);
197             }
198             if (res == null) {
199                 throw new PExceptionCompiler("Problem while accessing global " +
200                                              "compiler parameters: unable to locate ["
201                                              + getJormcOptsFile() + "]");
202             }
203         }
204         return res;
205     }
206
207     /**
208      * Gets the verifier associated with the given mapper.
209      * @param mappername The name of the concerned mapper.
210      * @return The MappingVerifier object.
211      * @throws PException
212      */

213     public MappingVerifier getMappingVerifier(String JavaDoc mappername) throws PException {
214         if (properties == null) {
215             throw new PExceptionCompiler("JORM has not been configured.");
216         }
217         String JavaDoc cn = properties.getProperty("jorm.mapper.verifier." + mappername);
218         try {
219             MappingVerifier v = (MappingVerifier) loader.loadClass(cn).newInstance();
220             return v;
221         } catch (InstantiationException JavaDoc e) {
222             throw new PExceptionCompiler(e, "Problem while instantiating " + cn);
223         } catch (IllegalAccessException JavaDoc e) {
224             throw new PExceptionCompiler(e, "Problem while trying to access " + cn);
225         } catch (ClassNotFoundException JavaDoc e) {
226             throw new PExceptionCompiler(e, "Problem while loading class " + cn);
227         }
228     }
229 }
230
Popular Tags