KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > lib > JormConfiguratorImpl


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.lib;
25
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.api.PMappingStructuresManager;
28 import org.objectweb.jorm.api.JormConfigurator;
29 import org.objectweb.jorm.metainfo.api.Manager;
30 import org.objectweb.jorm.metainfo.api.MappingFactory;
31 import org.objectweb.jorm.xml2mi.api.MappingParser;
32 import org.objectweb.jorm.xml2mi.api.Parser;
33 import org.objectweb.jorm.mi2xml.api.MappingDomtreeBuilder;
34 import org.objectweb.jorm.mi2xml.api.Writer;
35 import org.objectweb.jorm.util.api.Loggable;
36 import org.objectweb.util.monolog.Monolog;
37 import org.objectweb.util.monolog.api.Logger;
38 import org.objectweb.util.monolog.api.LoggerFactory;
39 import org.objectweb.util.monolog.api.MonologFactory;
40
41 import java.io.File JavaDoc;
42 import java.io.FileInputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Properties JavaDoc;
48 import java.util.StringTokenizer JavaDoc;
49 import java.util.Collection JavaDoc;
50 import java.util.HashMap JavaDoc;
51
52 /**
53  * Used to hold the configuration information related to the JORM runtime.
54  * @author P. Dechamboux
55  */

56 public class JormConfiguratorImpl implements JormConfigurator {
57     protected Properties JavaDoc properties = null;
58     protected Properties JavaDoc logProperties = null;
59     protected String JavaDoc pathToJormcOpts = null;
60     protected HashMap JavaDoc mappers = new HashMap JavaDoc();
61     protected LoggerFactory loggerFactory = null;
62     protected Logger logger = null;
63     protected ClassLoader JavaDoc loader = null;
64
65     /**
66      * Configures the JORM compiler by loading information related to the
67      * different mappers supported by this instance of JORM.
68      * @throws PException
69      */

70     public void configure(String JavaDoc propertyfile) throws PException {
71         if (properties != null) {
72             throw new PException("JORM already configured.");
73         }
74         try {
75             File JavaDoc pf = new File JavaDoc(propertyfile);
76             properties = new Properties JavaDoc();
77             properties.load(new FileInputStream JavaDoc(pf));
78             configure(properties);
79             pathToJormcOpts = pf.getParent();
80         } catch (IOException JavaDoc e) {
81             InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream(propertyfile);
82             if (is == null) {
83                 throw new PException(e, "Problem while configuring the JORM: unable to locate [" + propertyfile + "]");
84             }
85             try {
86                 properties = new Properties JavaDoc();
87                 properties.load(is);
88                 configure(properties);
89             } catch (Exception JavaDoc e2) {
90                 properties = null;
91                 throw new PException(e2, "Problem while configuring the JORM: unable to locate [" + propertyfile + "]");
92             }
93         }
94     }
95
96     public void configure() throws PException {
97         configure(DEFAULT_JORM_CONFIGURATION_FILE);
98     }
99
100     /**
101      * Configures the JORM compiler by loading information related to the
102      * different mappers supported by this instance of JORM.
103      * @param p is the properties containing the jorm configuration
104      */

105     public void configure(Properties JavaDoc p) {
106         properties = p;
107         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(p.getProperty("jorm.mapper.list").trim(), ",");
108         while (st.hasMoreTokens()) {
109             String JavaDoc mappername = st.nextToken();
110             String JavaDoc sms = properties.getProperty("jorm.mapper.submappers." + mappername);
111             if (sms == null) {
112                 continue;
113             }
114             ArrayList JavaDoc sml = new ArrayList JavaDoc();
115             mappers.put(mappername, sml);
116             StringTokenizer JavaDoc st2 = new StringTokenizer JavaDoc(sms.trim(), ",", false);
117             while (st2.hasMoreTokens()) {
118                 sml.add(st2.nextToken());
119             }
120         }
121
122         // use context classloader ?
123
String JavaDoc useCtxLoader = properties.getProperty(USE_CONTEXT_CLASSLOADER, "false").trim();
124         if ("false".equals(useCtxLoader)) {
125             loader = getClass().getClassLoader();
126         } else {
127             loader = Thread.currentThread().getContextClassLoader();
128         }
129     }
130
131     /**
132      * Configures the logger factory object with a properties object.
133      * @param propertyfile a properties object
134      */

135     public void configureLog(String JavaDoc propertyfile) throws PException {
136         if (loggerFactory == null) {
137             loggerFactory = Monolog.getMonologFactory(propertyfile);
138         } else {
139             Properties JavaDoc p = new Properties JavaDoc();
140             try {
141                 File JavaDoc pf = new File JavaDoc(propertyfile);
142                 p.load(new FileInputStream JavaDoc(pf));
143             } catch (IOException JavaDoc e) {
144                 try {
145                     p.load(loader.getResourceAsStream(propertyfile));
146                 } catch (Exception JavaDoc e2) {
147                     throw new PException(e, "Problem while configuring the JORM: unable to locate [" + propertyfile + "]");
148                 }
149             }
150             Monolog.loadMonologConfiguration(p, (MonologFactory) loggerFactory);
151         }
152     }
153
154     /**
155      * Lists the names of supported mappers.
156      * @return This list Iterator.
157      */

158     public Iterator JavaDoc knownMappers() throws PException {
159         if (properties == null) {
160             throw new PException("JORM has not been configured.");
161         }
162         return mappers.keySet().iterator();
163     }
164
165     /**
166      * It assignes the logger factory.
167      * @param lf
168      */

169     public void setLoggerFactory(LoggerFactory lf) {
170         loggerFactory = lf;
171         logger = lf.getLogger(LOGGER_NAME);
172     }
173
174     /**
175      * Gets the logger factory associated with this instance of JORM.
176      * @return The logger factory.
177      */

178     public LoggerFactory getLoggerFactory() throws PException {
179         if (loggerFactory == null) {
180             throw new PException("JORM has not been configured.");
181         }
182         return loggerFactory;
183     }
184
185     /**
186      * Gets the MI manager to be used by JORM.
187      * @return That MI manager.
188      */

189     public Manager getMIManager() throws PException {
190         if (properties == null) {
191             throw new PException("JORM has not been configured.");
192         }
193         String JavaDoc cn = properties.getProperty("jorm.mimanager");
194         if (cn == null) {
195             throw new PException(
196                     "Impossible to load a meta info manager: not configured:"
197                     + properties);
198         }
199         try {
200             Manager m = (Manager) loader.loadClass(cn).newInstance();
201             if (loggerFactory != null && m instanceof Loggable) {
202                 ((Loggable) m).setLoggerFactory(loggerFactory);
203             }
204             return m;
205         } catch (InstantiationException JavaDoc e) {
206             throw new PException(e, "Problem while instantiating " + cn);
207         } catch (IllegalAccessException JavaDoc e) {
208             throw new PException(e, "Problem while trying to access " + cn);
209         } catch (ClassNotFoundException JavaDoc e) {
210             throw new PException(e, "Problem while loading class " + cn);
211         }
212     }
213
214     /**
215      * Gets the parser to be used by JORM.
216      * @return That parser.
217      */

218     public Parser getParser() throws PException {
219         if (properties == null) {
220             throw new PException("JORM has not been configured.");
221         }
222         String JavaDoc cn = properties.getProperty("jorm.parser");
223         if (cn == null) {
224             throw new PException(
225                     "Impossible to load a parser: not configured:"
226                     + properties);
227         }
228         try {
229             return (Parser) loader.loadClass(cn).newInstance();
230         } catch (InstantiationException JavaDoc e) {
231             throw new PException(e, "Problem while instantiating " + cn);
232         } catch (IllegalAccessException JavaDoc e) {
233             throw new PException(e, "Problem while trying to access " + cn);
234         } catch (ClassNotFoundException JavaDoc e) {
235             throw new PException(e, "Problem while loading class " + cn);
236         }
237     }
238
239     /**
240      * Gets the MI factory associated with the given mapper.
241      * @param mappername The name of the concerned mapper.
242      * @return The MappingFactory object.
243      * @throws PException
244      */

245     public MappingFactory getMIFactory(String JavaDoc mappername) throws PException {
246         if (properties == null) {
247             throw new PException("JORM has not been configured.");
248         }
249         String JavaDoc cn = properties.getProperty("jorm.mapper.mifactory." + mappername);
250         if (cn == null) {
251             throw new PException(
252                     "Impossible to load a meta info factory: not configured:"
253                     + "jorm.mapper.mifactory." + mappername);
254         }
255         try {
256             MappingFactory mf = (MappingFactory) loader.loadClass(cn).newInstance();
257             return mf;
258         } catch (InstantiationException JavaDoc e) {
259             throw new PException(e, "Problem while instantiating " + cn);
260         } catch (IllegalAccessException JavaDoc e) {
261             throw new PException(e, "Problem while trying to access " + cn);
262         } catch (ClassNotFoundException JavaDoc e) {
263             throw new PException(e, "Problem while loading class " + cn);
264         }
265     }
266
267     /**
268      * Gets the schema manager associated with the given mapper.
269      * @param mappername The name of the concerned mapper.
270      * @return The PMappingStructuresManager object.
271      * @throws PException
272      */

273     public PMappingStructuresManager getSchMgr(String JavaDoc mappername) throws PException {
274         if (properties == null) {
275             throw new PException("JORM has not been configured.");
276         }
277         String JavaDoc cn = properties.getProperty("jorm.mapper.schmgr." + mappername);
278         if (cn == null) {
279             throw new PException(
280                     "Impossible to load a schema manager: not configured:"
281                     + properties);
282         }
283         try {
284             PMappingStructuresManager mf = (PMappingStructuresManager) loader.loadClass(cn).newInstance();
285             return mf;
286         } catch (InstantiationException JavaDoc e) {
287             throw new PException(e, "Problem while instantiating " + cn);
288         } catch (IllegalAccessException JavaDoc e) {
289             throw new PException(e, "Problem while trying to access " + cn);
290         } catch (ClassNotFoundException JavaDoc e) {
291             throw new PException(e, "Problem while loading class " + cn);
292         }
293     }
294
295     /**
296      * Gets the class name of the GenClassMapping associated with the given mapper.
297      * @param mappername The name of the concerned mapper.
298      * @return The string giving the class name.
299      * @throws PException
300      */

301     public Class JavaDoc getGcmClass(String JavaDoc mappername) throws PException {
302         if (properties == null) {
303             throw new PException("JORM has not been configured.");
304         }
305         String JavaDoc cn = properties.getProperty("jorm.mapper.gcmapping." + mappername);
306         if (cn == null) {
307             throw new PException(
308                     "Impossible to load a generic class mapping: not configured:"
309                     + properties);
310         }
311         try {
312             Class JavaDoc res = loader.loadClass(cn);
313             res.newInstance();
314             return res;
315         } catch (InstantiationException JavaDoc e) {
316             throw new PException(e, "Problem while trying to instantiate " + cn);
317         } catch (IllegalAccessException JavaDoc e) {
318             throw new PException(e, "Problem while trying to access " + cn);
319         } catch (ClassNotFoundException JavaDoc e) {
320             throw new PException(e, "Problem while loading class " + cn);
321         }
322     }
323
324     /**
325      * Gets the parser associated with the given mapper for mapping infos.
326      * @param mappername The name of the concerned mapper.
327      * @return The MappingParser object.
328      * @throws PException
329      */

330     public MappingParser getMappingParser(String JavaDoc mappername) throws PException {
331         if (properties == null) {
332             throw new PException("JORM has not been configured.");
333         }
334         String JavaDoc cn = properties.getProperty("jorm.mapper.parser." + mappername);
335         try {
336             MappingParser p = (MappingParser) loader.loadClass(cn).newInstance();
337             return p;
338         } catch (InstantiationException JavaDoc e) {
339             throw new PException(e, "Problem while instantiating " + cn);
340         } catch (IllegalAccessException JavaDoc e) {
341             throw new PException(e, "Problem while trying to access " + cn);
342         } catch (ClassNotFoundException JavaDoc e) {
343             throw new PException(e, "Problem while loading class " + cn);
344         }
345     }
346
347     /**
348      * Gets the BasicDomWriter object.
349      * @return The BasicDomWriter object.
350      * @throws PException
351      */

352     public Writer getWriter() throws PException {
353         if (properties == null) {
354             throw new PException("JORM has not been configured.");
355         }
356         String JavaDoc cn = properties.getProperty("jorm.writer");
357         try {
358             return (Writer) loader.loadClass(cn).newInstance();
359         } catch (InstantiationException JavaDoc e) {
360             throw new PException(e, "Problem while instantiating " + cn);
361         } catch (IllegalAccessException JavaDoc e) {
362             throw new PException(e, "Problem while trying to access " + cn);
363         } catch (ClassNotFoundException JavaDoc e) {
364             throw new PException(e, "Problem while loading class " + cn);
365         }
366     }
367
368     /**
369      * Gets the MappingDomTreeBuilder object associated with the given mapper.
370      * @param mappername The name of the concerned mapper.
371      * @return The MappingDomTreeBuilder object.
372      * @throws PException
373      */

374     public MappingDomtreeBuilder getMappingDomtreeBuilder(String JavaDoc mappername)
375             throws PException {
376         if (properties == null) {
377             throw new PException("JORM has not been configured.");
378         }
379         String JavaDoc cn = properties.getProperty("jorm.mapper.writer." + mappername);
380         try {
381             MappingDomtreeBuilder mdg = (MappingDomtreeBuilder) loader.loadClass(cn).newInstance();
382             return mdg;
383         } catch (InstantiationException JavaDoc e) {
384             throw new PException(e, "Problem while instantiating " + cn);
385         } catch (IllegalAccessException JavaDoc e) {
386             throw new PException(e, "Problem while trying to access " + cn);
387         } catch (ClassNotFoundException JavaDoc e) {
388             throw new PException(e, "Problem while loading class " + cn);
389         }
390     }
391
392     /**
393      * Gets the submappers associated with a given mapper.
394      * @param mappername The concerned mapper.
395      * @return The collection of the names of submappers.
396      * @throws PException
397      */

398     public Collection JavaDoc getSubMappers(String JavaDoc mappername) throws PException {
399         if (properties == null) {
400             throw new PException("JORM has not been configured.");
401         }
402         return (Collection JavaDoc) mappers.get(mappername);
403     }
404
405     /**
406      * Removes this mapper from the list of the ones used for generation.
407      * @param mn The name of the concerned mapper.
408      */

409     public void removeMapper(String JavaDoc mn) {
410         mappers.remove(mn);
411     }
412
413     /**
414      * Removes all mappers from the list of the ones used for generation.
415      */

416     public void removeAllMappers() {
417         mappers.clear();
418     }
419
420     /**
421      * Removes this submapper from the list of the ones used for generation.
422      * @param mn The concerned mapper.
423      * @param smn The submapper to be removed.
424      */

425     public void removeSubMapper(String JavaDoc mn, String JavaDoc smn) {
426         ArrayList JavaDoc sms = (ArrayList JavaDoc) mappers.get(mn);
427         if (sms == null) {
428             return;
429         }
430         int pos = sms.indexOf(smn);
431         if (pos == -1) {
432             return;
433         }
434         sms.remove(pos);
435     }
436
437     /**
438      * Adds this submapper to the list of the ones used for generation.
439      * @param mn The concerned mapper.
440      * @param smn The submapper to be added.
441      */

442     public void addSubMapper(String JavaDoc mn, String JavaDoc smn) {
443         ArrayList JavaDoc sms = (ArrayList JavaDoc) mappers.get(mn);
444         if (sms == null) {
445             sms = new ArrayList JavaDoc();
446             mappers.put(mn, sms);
447         }
448         int pos = sms.indexOf(smn);
449         if (pos == -1) {
450             sms.add(smn);
451         }
452     }
453 }
454
Popular Tags