KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > invoice > applications > JormInit


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 examples.invoice.applications;
25
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.api.PMapper;
28 import org.objectweb.jorm.lib.MapperJCA;
29 import org.objectweb.jorm.mapper.rdb.lib.ConnectionSpecJDBC;
30 import org.objectweb.jorm.mapper.rdb.lib.MapperJDBC;
31 import org.objectweb.jorm.util.api.Loggable;
32 import org.objectweb.perseus.connector.ra.fos.FosAttributeControler;
33 import org.objectweb.perseus.connector.ra.fos.FosManagedConnectionFactory;
34 import org.objectweb.perseus.cache.api.CacheManager;
35 import org.objectweb.util.monolog.Monolog;
36 import org.objectweb.util.monolog.api.BasicLevel;
37 import org.objectweb.util.monolog.api.Logger;
38 import org.objectweb.util.monolog.api.LoggerFactory;
39
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Properties JavaDoc;
43
44 /**
45  * @author P. Dechamboux
46  */

47 public class JormInit {
48     // JormInit related constants and variables
49
public static final String JavaDoc LOGGER_FACTORY_CLASS_NAME
50             = "loggerFactoryClassName";
51     private static JormInit singleton = new JormInit();
52     private static CacheManager cacheManager = new TrivialCache();
53     protected LoggerFactory loggerFactory = null;
54     protected Logger logger = null;
55     // Mapper related constants and variables
56
private HashMap JavaDoc mapperMap = new HashMap JavaDoc();
57     private HashMap JavaDoc urlMap = new HashMap JavaDoc();
58     // RA related constants and variables
59
public static final String JavaDoc RESOURCE_JDBC_SUBMAPPER
60             = "jdbc.submapper";
61     public static final String JavaDoc RESOURCE_FOS_MCF_CLASSNAME
62             = "fos.resource.mcf.classname";
63     public static final String JavaDoc DEFAULT_FOS_URL = "fos.ConnectionURL";
64
65     /**
66      * Retrieves the JormInit singleton.
67      */

68     public static JormInit getInstance() {
69         return singleton;
70     }
71
72     /**
73      * Retrieves the LoggerFactory
74      */

75     public LoggerFactory getLoggerFactory() {
76         return loggerFactory;
77     }
78
79     /**
80      * Initializes the log system.
81      */

82     public void initLogSystem(Properties JavaDoc prop) throws Exception JavaDoc {
83         loggerFactory = Monolog.initialize();
84         logger = loggerFactory.getLogger("examples.invoice.applications.JormInit");
85         logger.log(BasicLevel.INFO, "Log system initialized.");
86     }
87
88     /**
89      * Initializes the RA environment
90      */

91     public void initResourceAdapter(Properties JavaDoc prop) throws Exception JavaDoc {
92         initJDBCResourceAdapter(prop);
93         initFOSResourceAdapter(prop);
94     }
95
96     private void initJDBCResourceAdapter(Properties JavaDoc prop) throws Exception JavaDoc {
97         // Initializes JDBC environment
98
PMapper mapper = new MapperJDBC();
99         mapper.setConnectionFactory(
100                 new ConnectionSpecJDBC(
101                         prop.getProperty("ConnectionURL"),
102                         prop.getProperty("DriverClassName"),
103                         prop.getProperty("UserName"),
104                         prop.getProperty("Password")));
105         mapper.setMapperName("rdb." + prop.getProperty(RESOURCE_JDBC_SUBMAPPER));
106         ((Loggable) mapper).setLoggerFactory(loggerFactory);
107         mapper.start();
108         mapperMap.put(mapper.getMapperName(), mapper);
109         urlMap.put(mapper.getMapperName(), prop.getProperty("ConnectionURL"));
110         logger.log(BasicLevel.INFO, "Mapper " + mapper.getMapperName()
111                                     + " instancied and initialised.");
112     }
113
114     private void initFOSResourceAdapter(Properties JavaDoc prop) throws Exception JavaDoc {
115         PMapper mapper;
116         // Initializes FOS environment
117
String JavaDoc fosMcfCN = prop.getProperty(RESOURCE_FOS_MCF_CLASSNAME);
118         if (fosMcfCN == null) {
119             logger.log(BasicLevel.INFO,
120                        "Cannot initialize FOS RA environment: MCF=" + fosMcfCN);
121             return;
122         }
123         FosAttributeControler ra
124                 = (FosAttributeControler) Class.forName(fosMcfCN).newInstance();
125         logger.log(BasicLevel.DEBUG, "FOS Ra=" + ra);
126         // configure it
127
ra.setConnectionURL(prop.getProperty(DEFAULT_FOS_URL));
128         ra.setLoggerFactory(loggerFactory);
129         ra.setInitializeAtStartUp(false);
130         ra.setLoggerBaseName("examples.invoice.ra");
131         ((FosManagedConnectionFactory) ra).start();
132         mapper = new MapperJCA();
133         mapper.setConnectionFactory(((FosManagedConnectionFactory) ra).createConnectionFactory());
134         mapper.setMapperName("fos");
135         ((Loggable) mapper).setLoggerFactory(loggerFactory);
136         mapper.start();
137         mapperMap.put(mapper.getMapperName(), mapper);
138         urlMap.put(mapper.getMapperName(), prop.getProperty(DEFAULT_FOS_URL));
139         logger.log(BasicLevel.INFO, "Mapper " + mapper.getMapperName()
140                                     + " instancied and initialised.");
141     }
142
143     public PMapper getMapper(String JavaDoc name) throws PException {
144         return (PMapper) mapperMap.get(name);
145     }
146
147     public Iterator JavaDoc registeredMapper() {
148         return mapperMap.values().iterator();
149     }
150
151     public String JavaDoc getURL(PMapper m) {
152         return (String JavaDoc) urlMap.get(m.getMapperName());
153     }
154
155     public CacheManager getCacheManager() {
156         return cacheManager;
157     }
158 }
159
Popular Tags