KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > j2eedo > test > MainLauncher


1 /*
2  * Speedo: an implementation of JDO compliant personality on top of JORM
3  * generic I/O sub-system. Copyright (C) 2001-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Release: 1.0
20  *
21  * Created on Apr 19, 2004 @author franck.milleville@cgey.com
22  *
23  */

24 package org.objectweb.speedo.j2eedo.test;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.jdo.JDOException;
31 import javax.jdo.JDOHelper;
32 import javax.jdo.PersistenceManagerFactory;
33
34 import org.objectweb.speedo.api.SpeedoProperties;
35 import org.objectweb.speedo.j2eedo.bo.DatabaseImpl;
36 import org.objectweb.speedo.j2eedo.common.PMHolder;
37 import org.objectweb.util.monolog.Monolog;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40 import org.objectweb.util.monolog.api.LoggerFactory;
41
42 /**
43  * This class is used for basic test implementation in order to
44  * highlight some errors or behaviours. The developper needs to create a new class extended of
45  * this class and writes its own test method doTest()
46  *
47  * @author franck.milleville@cgey.com
48  */

49 public class MainLauncher {
50     static Logger logger = null;
51     static {
52         LoggerFactory lf = Monolog.initialize();
53         logger = lf.getLogger(MainLauncher.class.getName());
54     }
55     
56     final static Properties JavaDoc p = new Properties JavaDoc();
57     public void initPMF() throws JDOException, Exception JavaDoc {
58         //Load the properties file from the classpath
59
String JavaDoc speedoProperties = "speedo.properties";
60         InputStream JavaDoc is =
61             DatabaseImpl.class.getClassLoader().getResourceAsStream(
62                     speedoProperties);
63         if (is == null) {
64             System.err.println(
65                     "[ERROR] No '"
66                     + speedoProperties
67                     + "' property file availlable in the classpath (classloader="
68                     + DatabaseImpl.class.getClassLoader());
69             System.exit(2);
70         }
71         try {
72             p.load(is);
73         } catch (IOException JavaDoc e) {
74             System.err.println(
75                     "[ERROR] Impossible to load the '"
76                     + speedoProperties
77                     + "' property file from the classpath: "
78                     + e.getMessage());
79             System.exit(2);
80         }
81         //Specify to speedo to create the mapping structure if it does not
82
// already exist
83
p.setProperty(
84                 SpeedoProperties.MAPPING_STRUCTURE,
85                 SpeedoProperties.MAPPING_STRUCTURE_CIR);
86         boolean useDriverDirectly = false;
87         String JavaDoc s =
88             System.getProperty(
89                     SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
90         if (s != null) {
91             p.setProperty(
92                     SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME,
93                     s);
94             useDriverDirectly = true;
95         }
96         s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
97         if (s != null) {
98             p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, s);
99             useDriverDirectly = true;
100         }
101         s =
102             System.getProperty(
103                     SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME);
104         if (s != null) {
105             p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, s);
106             useDriverDirectly = true;
107         }
108         s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD);
109         if (s != null) {
110             p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, s);
111             useDriverDirectly = true;
112         }
113         s = System.getProperty(SpeedoProperties.MAPPER_NAME);
114         if (s != null) {
115             p.setProperty(SpeedoProperties.MAPPER_NAME, s);
116         }
117
118         if (useDriverDirectly) {
119             //In case of the user specifies driver properties, the CF name is
120
// removed
121
p.remove(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME);
122         }
123
124         PersistenceManagerFactory pmf =
125             JDOHelper.getPersistenceManagerFactory(p);
126         PMHolder pmHolder = new PMHolder(pmf);
127         DatabaseImpl.initTestData(pmHolder);
128     }
129
130     /**
131      * This method connects to the database using the default speedo properties
132      * file and calls the test method doTest
133      *
134      * @param args not used
135      * @throws JDOException
136      * @throws Exception
137      * @see #doTest()
138      */

139     public static void main(String JavaDoc[] args) throws JDOException, Exception JavaDoc {
140         MainLauncher ml = new MainLauncher();
141         ml.initPMF();
142         ml.doTest();
143     }
144
145     /**
146      * This method is the basic test sample. It describes how to get the
147      * persistence manager factory.
148      * <p>
149      * This method must be rewritten by the include your own tests as shown by the
150      * class {@link ShowConcurrencyErrors ShowConcurrencyErrors}
151      */

152     public void doTest() throws JDOException, Exception JavaDoc {
153         // get the persistence manager factory
154
PersistenceManagerFactory pmf =
155             JDOHelper.getPersistenceManagerFactory(p);
156         logger.log(BasicLevel.INFO, "Show connection properties:\n\t" + pmf.getProperties());
157     }
158 }
159
Popular Tags