001: /*
002: * Speedo: an implementation of JDO compliant personality on top of JORM
003: * generic I/O sub-system. Copyright (C) 2001-2004 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Release: 1.0
020: *
021: * Created on Apr 19, 2004 @author franck.milleville@cgey.com
022: *
023: */
024: package org.objectweb.speedo.j2eedo.test;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.util.Properties;
029:
030: import javax.jdo.JDOException;
031: import javax.jdo.JDOHelper;
032: import javax.jdo.PersistenceManagerFactory;
033:
034: import org.objectweb.speedo.api.SpeedoProperties;
035: import org.objectweb.speedo.j2eedo.bo.DatabaseImpl;
036: import org.objectweb.speedo.j2eedo.common.PMHolder;
037: import org.objectweb.util.monolog.Monolog;
038: import org.objectweb.util.monolog.api.BasicLevel;
039: import org.objectweb.util.monolog.api.Logger;
040: import org.objectweb.util.monolog.api.LoggerFactory;
041:
042: /**
043: * This class is used for basic test implementation in order to
044: * highlight some errors or behaviours. The developper needs to create a new class extended of
045: * this class and writes its own test method doTest()
046: *
047: * @author franck.milleville@cgey.com
048: */
049: public class MainLauncher {
050: static Logger logger = null;
051: static {
052: LoggerFactory lf = Monolog.initialize();
053: logger = lf.getLogger(MainLauncher.class.getName());
054: }
055:
056: final static Properties p = new Properties();
057:
058: public void initPMF() throws JDOException, Exception {
059: //Load the properties file from the classpath
060: String speedoProperties = "speedo-jdo.properties";
061: InputStream is = DatabaseImpl.class.getClassLoader()
062: .getResourceAsStream(speedoProperties);
063: if (is == null) {
064: System.err
065: .println("[ERROR] No '"
066: + speedoProperties
067: + "' property file availlable in the classpath (classloader="
068: + DatabaseImpl.class.getClassLoader());
069: System.exit(2);
070: }
071: try {
072: p.load(is);
073: } catch (IOException e) {
074: System.err.println("[ERROR] Impossible to load the '"
075: + speedoProperties
076: + "' property file from the classpath: "
077: + e.getMessage());
078: System.exit(2);
079: }
080: //Specify to speedo to create the mapping structure if it does not
081: // already exist
082: p.setProperty(SpeedoProperties.MAPPING_STRUCTURE,
083: SpeedoProperties.MAPPING_STRUCTURE_CIR);
084: boolean useDriverDirectly = false;
085: String s = System
086: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
087: if (s != null) {
088: p.setProperty(
089: SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME,
090: s);
091: useDriverDirectly = true;
092: }
093: s = System
094: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
095: if (s != null) {
096: p
097: .setProperty(
098: SpeedoProperties.JDO_OPTION_CONNECTION_URL,
099: s);
100: useDriverDirectly = true;
101: }
102: s = System
103: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME);
104: if (s != null) {
105: p
106: .setProperty(
107: SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME,
108: s);
109: useDriverDirectly = true;
110: }
111: s = System
112: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD);
113: if (s != null) {
114: p.setProperty(
115: SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, s);
116: useDriverDirectly = true;
117: }
118: s = System.getProperty(SpeedoProperties.MAPPER_NAME);
119: if (s != null) {
120: p.setProperty(SpeedoProperties.MAPPER_NAME, s);
121: }
122:
123: if (useDriverDirectly) {
124: //In case of the user specifies driver properties, the CF name is
125: // removed
126: p
127: .remove(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME);
128: }
129:
130: PersistenceManagerFactory pmf = JDOHelper
131: .getPersistenceManagerFactory(p);
132: PMHolder pmHolder = new PMHolder(pmf);
133: DatabaseImpl.initTestData(pmHolder);
134: }
135:
136: /**
137: * This method connects to the database using the default speedo properties
138: * file and calls the test method doTest
139: *
140: * @param args not used
141: * @throws JDOException
142: * @throws Exception
143: * @see #doTest()
144: */
145: public static void main(String[] args) throws JDOException,
146: Exception {
147: MainLauncher ml = new MainLauncher();
148: ml.initPMF();
149: ml.doTest();
150: }
151:
152: /**
153: * This method is the basic test sample. It describes how to get the
154: * persistence manager factory.
155: * <p>
156: * This method must be rewritten by the include your own tests as shown by the
157: * class {@link ShowConcurrencyErrors ShowConcurrencyErrors}
158: */
159: public void doTest() throws JDOException, Exception {
160: // get the persistence manager factory
161: PersistenceManagerFactory pmf = JDOHelper
162: .getPersistenceManagerFactory(p);
163: logger.log(BasicLevel.INFO, "Show connection properties:\n\t"
164: + pmf.getProperties());
165: }
166: }
|