/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.tools;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.mapper.lib.Object2StringSerializer;
import org.objectweb.jorm.metainfo.api.Class;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.LoggerFactory;
import org.objectweb.util.monolog.Monolog;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.PersistenceManager;
import java.util.Properties;
import java.util.Set;
import java.util.Iterator;
import java.io.IOException;
import java.io.InputStream;
/**
* It initializes the data structure of persistent classes.
*
* @author S.Chassande-Barrioz
*/
public class DataStructureCreation {
/**
* 1 => bad usage
* 2 => impossible to load the properties file
* 3 => impossible to load the persistent class
* @param args an array of (class name | .jdo file) to initialize
*/
public static void main(String[] args) {
new DataStructureCreation().execute(args);
}
private void execute(String[] args) {
System.out.println();
LoggerFactory lf = Monolog.initialize();
Logger logger = lf.getLogger(this.getClass().toString());
logger.setLevel( BasicLevel.LEVEL_DEBUG );
//Check parameter
if (args.length == 0) {
System.err.println("Usage: ");
System.err.println("\tDataStructureCreation (<class name to initialize> | <.jdo file name>)*");
System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME+"=<driver class name>]");
System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_URL+"=<database url>]");
System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME+"=<user name>]");
System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD+"=<user password>]");
System.err.println("\t\t[-D"+SpeedoProperties.MAPPER_NAME+"=<mapper name>]");
System.exit(1);
}
//Load the properties file from the classpath
Properties p = new Properties();
String speedoProperties = "speedo-jdo.properties";
InputStream is = getClass().getClassLoader().getResourceAsStream(speedoProperties);
if (is == null) {
System.err.println("[ERROR] No '" + speedoProperties
+ "' property file availlable in the classpath (classloader="
+ getClass().getClassLoader());
System.exit(2);
}
try {
p.load(is);
} catch (IOException e) {
System.err.println("[ERROR] Impossible to load the '" + speedoProperties
+ "' property file from the classpath: " + e.getMessage());
System.exit(2);
}
//Specify to speedo to create the mapping structure if it does not
// already exist
p.setProperty(SpeedoProperties.MAPPING_STRUCTURE, SpeedoProperties.MAPPING_STRUCTURE_CIR);
boolean useDriverDirectly = false;
String s = getDCN();
if (s != null) {
p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME, s);
useDriverDirectly = true;
}
s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
if (s != null) {
p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, s);
useDriverDirectly = true;
}
s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME);
if (s != null) {
p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, s);
useDriverDirectly = true;
}
s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD);
if (s != null) {
p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, s);
useDriverDirectly = true;
}
s = System.getProperty(SpeedoProperties.MAPPER_NAME);
if (s != null) {
p.setProperty(SpeedoProperties.MAPPER_NAME, s);
}
if (useDriverDirectly) {
//In case of the user specifies driver properties, the CF name is removed
p.remove(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME);
}
// start Speedo
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
ClassLoader cl = getClass().getClassLoader();
for (int i=0; i<args.length; i++) {
if (args[i].endsWith(".jdo")) {
Set mos = null;
try {
mos = (Set) Object2StringSerializer.deserialize( args[i], cl, logger);
} catch (Exception e) {
System.err.println("[ERROR] Impossible to load the jorm meta " +
"information for the .jmi file '" + Object2StringSerializer.descFileName2ClassName(args[i])
+ "' (You must use the same Speedo version for the"
+ " enhancement and for the runtime): ");
if (e.getCause() != null) {
e.getCause().printStackTrace(System.err);
} else {
e.printStackTrace(System.err);
}
continue;
}
//Parse the meta information in order to initialize each class
for (Iterator it = mos.iterator(); it.hasNext();) {
Object o = it.next();
if (o instanceof Class) {
initClass(((Class) o).getFQName(), pmf, cl);
}
}
} else {
initClass(args[i], pmf, cl);
}
}
}
private static String getDCN() {
String strval = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
if (strval != null) {
return strval;
}
strval = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD);
if (strval != null) {
System.err.println("WARN: Property "
+ SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD
+ " is deprecated, you must use "
+ SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
return strval;
}
strval = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2);
if (strval != null) {
System.err.println("WARN: Property "
+ SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME_OLD2
+ " is deprecated, you must use "
+ SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
return strval;
}
return null;
}
private void initClass(String className,
PersistenceManagerFactory pmf,
ClassLoader classLoader) {
java.lang.Class persistentClass = null;
try {
persistentClass = classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
System.err.println("[ERROR] Impossible to load the persistent class '"
+ className + "' from the classpath: " + e.getMessage());
return;
}
PersistenceManager pm = pmf.getPersistenceManager();
pm.getObjectIdClass(persistentClass);
pm.close();
System.out.println("[INFO] Class '" + className + "' initialized.");
}
}