001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.tools;
018:
019: import org.objectweb.speedo.api.SpeedoProperties;
020: import org.objectweb.speedo.mapper.lib.Object2StringSerializer;
021: import org.objectweb.jorm.metainfo.api.Class;
022:
023: import org.objectweb.util.monolog.api.Logger;
024: import org.objectweb.util.monolog.api.BasicLevel;
025: import org.objectweb.util.monolog.api.LoggerFactory;
026: import org.objectweb.util.monolog.Monolog;
027:
028: import javax.jdo.JDOHelper;
029: import javax.jdo.PersistenceManagerFactory;
030: import javax.jdo.PersistenceManager;
031: import java.util.Properties;
032: import java.util.Set;
033: import java.util.Iterator;
034: import java.io.IOException;
035: import java.io.InputStream;
036:
037: /**
038: * It initializes the data structure of persistent classes.
039: *
040: * @author S.Chassande-Barrioz
041: */
042: public class DataStructureCreation {
043:
044: /**
045: * 1 => bad usage
046: * 2 => impossible to load the properties file
047: * 3 => impossible to load the persistent class
048: * @param args an array of (class name | .jdo file) to initialize
049: */
050: public static void main(String[] args) {
051: new DataStructureCreation().execute(args);
052: }
053:
054: private void execute(String[] args) {
055: System.out.println();
056: LoggerFactory lf = Monolog.initialize();
057: Logger logger = lf.getLogger(this .getClass().toString());
058: logger.setLevel(BasicLevel.LEVEL_DEBUG);
059:
060: //Check parameter
061: if (args.length == 0) {
062: System.err.println("Usage: ");
063: System.err
064: .println("\tDataStructureCreation (<class name to initialize> | <.jdo file name>)*");
065: System.err
066: .println("\t\t[-D"
067: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME
068: + "=<driver class name>]");
069: System.err.println("\t\t[-D"
070: + SpeedoProperties.JDO_OPTION_CONNECTION_URL
071: + "=<database url>]");
072: System.err.println("\t\t[-D"
073: + SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME
074: + "=<user name>]");
075: System.err.println("\t\t[-D"
076: + SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD
077: + "=<user password>]");
078: System.err.println("\t\t[-D" + SpeedoProperties.MAPPER_NAME
079: + "=<mapper name>]");
080: System.exit(1);
081: }
082:
083: //Load the properties file from the classpath
084: Properties p = new Properties();
085: String speedoProperties = "speedo-jdo.properties";
086: InputStream is = getClass().getClassLoader()
087: .getResourceAsStream(speedoProperties);
088: if (is == null) {
089: System.err
090: .println("[ERROR] No '"
091: + speedoProperties
092: + "' property file availlable in the classpath (classloader="
093: + getClass().getClassLoader());
094: System.exit(2);
095: }
096: try {
097: p.load(is);
098: } catch (IOException e) {
099: System.err.println("[ERROR] Impossible to load the '"
100: + speedoProperties
101: + "' property file from the classpath: "
102: + e.getMessage());
103: System.exit(2);
104: }
105: //Specify to speedo to create the mapping structure if it does not
106: // already exist
107: p.setProperty(SpeedoProperties.MAPPING_STRUCTURE,
108: SpeedoProperties.MAPPING_STRUCTURE_CIR);
109: boolean useDriverDirectly = false;
110: String s = getDCN();
111: if (s != null) {
112: p.setProperty(
113: SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME,
114: s);
115: useDriverDirectly = true;
116: }
117: s = System
118: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
119: if (s != null) {
120: p
121: .setProperty(
122: SpeedoProperties.JDO_OPTION_CONNECTION_URL,
123: s);
124: useDriverDirectly = true;
125: }
126: s = System
127: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME);
128: if (s != null) {
129: p
130: .setProperty(
131: SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME,
132: s);
133: useDriverDirectly = true;
134: }
135: s = System
136: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD);
137: if (s != null) {
138: p.setProperty(
139: SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, s);
140: useDriverDirectly = true;
141: }
142: s = System.getProperty(SpeedoProperties.MAPPER_NAME);
143: if (s != null) {
144: p.setProperty(SpeedoProperties.MAPPER_NAME, s);
145: }
146:
147: if (useDriverDirectly) {
148: //In case of the user specifies driver properties, the CF name is removed
149: p
150: .remove(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME);
151: }
152:
153: // start Speedo
154: PersistenceManagerFactory pmf = JDOHelper
155: .getPersistenceManagerFactory(p);
156:
157: ClassLoader cl = getClass().getClassLoader();
158: for (int i = 0; i < args.length; i++) {
159: if (args[i].endsWith(".jdo")) {
160: Set mos = null;
161: try {
162: mos = (Set) Object2StringSerializer.deserialize(
163: args[i], cl, logger);
164: } catch (Exception e) {
165: System.err
166: .println("[ERROR] Impossible to load the jorm meta "
167: + "information for the .jmi file '"
168: + Object2StringSerializer
169: .descFileName2ClassName(args[i])
170: + "' (You must use the same Speedo version for the"
171: + " enhancement and for the runtime): ");
172: if (e.getCause() != null) {
173: e.getCause().printStackTrace(System.err);
174: } else {
175: e.printStackTrace(System.err);
176: }
177: continue;
178: }
179: //Parse the meta information in order to initialize each class
180: for (Iterator it = mos.iterator(); it.hasNext();) {
181: Object o = it.next();
182: if (o instanceof Class) {
183: initClass(((Class) o).getFQName(), pmf, cl);
184: }
185: }
186: } else {
187: initClass(args[i], pmf, cl);
188: }
189: }
190: }
191:
192: private static String getDCN() {
193: String strval = System
194: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
195: if (strval != null) {
196: return strval;
197: }
198: strval = System
199: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD);
200: if (strval != null) {
201: System.err
202: .println("WARN: Property "
203: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD
204: + " is deprecated, you must use "
205: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
206: return strval;
207: }
208: strval = System
209: .getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2);
210: if (strval != null) {
211: System.err
212: .println("WARN: Property "
213: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2
214: + " is deprecated, you must use "
215: + SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
216: return strval;
217: }
218: return null;
219: }
220:
221: private void initClass(String className,
222: PersistenceManagerFactory pmf, ClassLoader classLoader) {
223: java.lang.Class persistentClass = null;
224: try {
225: persistentClass = classLoader.loadClass(className);
226: } catch (ClassNotFoundException e) {
227: System.err
228: .println("[ERROR] Impossible to load the persistent class '"
229: + className
230: + "' from the classpath: "
231: + e.getMessage());
232: return;
233: }
234: PersistenceManager pm = pmf.getPersistenceManager();
235: pm.getObjectIdClass(persistentClass);
236: pm.close();
237: System.out.println("[INFO] Class '" + className
238: + "' initialized.");
239: }
240: }
|