1 18 package org.objectweb.speedo.tools; 19 20 import org.objectweb.speedo.api.SpeedoProperties; 21 import org.objectweb.speedo.mapper.lib.Object2StringSerializer; 22 import org.objectweb.jorm.metainfo.api.Class; 23 24 25 import org.objectweb.util.monolog.api.Logger; 26 import org.objectweb.util.monolog.api.BasicLevel; 27 import org.objectweb.util.monolog.api.LoggerFactory; 28 import org.objectweb.util.monolog.Monolog; 29 30 import javax.jdo.JDOHelper; 31 import javax.jdo.PersistenceManagerFactory; 32 import javax.jdo.PersistenceManager; 33 import java.util.Properties ; 34 import java.util.Set ; 35 import java.util.Iterator ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 39 44 public class DataStructureCreation { 45 46 52 public static void main(String [] args) { 53 new DataStructureCreation().execute(args); 54 } 55 56 private void execute(String [] args) { 57 System.out.println(); 58 LoggerFactory lf = Monolog.initialize(); 59 Logger logger = lf.getLogger(this.getClass().toString()); 60 logger.setLevel( BasicLevel.LEVEL_DEBUG ); 61 62 if (args.length == 0) { 64 System.err.println("Usage: "); 65 System.err.println("\tDataStructureCreation (<class name to initialize> | <.jdo file name>)*"); 66 System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME+"=<driver class name>]"); 67 System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_URL+"=<database url>]"); 68 System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME+"=<user name>]"); 69 System.err.println("\t\t[-D"+SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD+"=<user password>]"); 70 System.err.println("\t\t[-D"+SpeedoProperties.MAPPER_NAME+"=<mapper name>]"); 71 System.exit(1); 72 } 73 74 Properties p = new Properties(); 76 String speedoProperties = "speedo.properties"; 77 InputStream is = getClass().getClassLoader().getResourceAsStream(speedoProperties); 78 if (is == null) { 79 System.err.println("[ERROR] No '" + speedoProperties 80 + "' property file availlable in the classpath (classloader=" 81 + getClass().getClassLoader()); 82 System.exit(2); 83 } 84 try { 85 p.load(is); 86 } catch (IOException e) { 87 System.err.println("[ERROR] Impossible to load the '" + speedoProperties 88 + "' property file from the classpath: " + e.getMessage()); 89 System.exit(2); 90 } 91 p.setProperty(SpeedoProperties.MAPPING_STRUCTURE, SpeedoProperties.MAPPING_STRUCTURE_CIR); 94 boolean useDriverDirectly = false; 95 String s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME); 96 if (s != null) { 97 p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME, s); 98 useDriverDirectly = true; 99 } 100 s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL); 101 if (s != null) { 102 p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, s); 103 useDriverDirectly = true; 104 } 105 s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME); 106 if (s != null) { 107 p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, s); 108 useDriverDirectly = true; 109 } 110 s = System.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD); 111 if (s != null) { 112 p.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, s); 113 useDriverDirectly = true; 114 } 115 s = System.getProperty(SpeedoProperties.MAPPER_NAME); 116 if (s != null) { 117 p.setProperty(SpeedoProperties.MAPPER_NAME, s); 118 } 119 120 if (useDriverDirectly) { 121 p.remove(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME); 123 } 124 125 PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p); 127 128 ClassLoader cl = getClass().getClassLoader(); 129 for (int i=0; i<args.length; i++) { 130 if (args[i].endsWith(".jdo")) { 131 Set mos = null; 132 try { 133 mos = (Set ) Object2StringSerializer.deserialize( args[i], cl, logger); 134 } catch (Exception e) { 135 System.err.println("[ERROR] Impossible to load the jorm meta " + 136 "information for the .jmi file '" + Object2StringSerializer.jdoFileName2ClassName(args[i]) 137 + "' (You must use the same Speedo version for the" 138 + " enhancement and for the runtime): "); 139 e.printStackTrace(System.err); 140 continue; 141 } 142 for (Iterator it = mos.iterator(); it.hasNext();) { 144 Object o = it.next(); 145 if (o instanceof Class ) { 146 initClass(((Class ) o).getFQName(), pmf, cl); 147 } 148 } 149 150 } else { 151 initClass(args[i], pmf, cl); 152 } 153 } 154 } 155 156 private void initClass(String className, 157 PersistenceManagerFactory pmf, 158 ClassLoader classLoader) { 159 java.lang.Class persistentClass = null; 160 try { 161 persistentClass = classLoader.loadClass(className); 162 } catch (ClassNotFoundException e) { 163 System.err.println("[ERROR] Impossible to load the persistent class '" 164 + className + "' from the classpath: " + e.getMessage()); 165 return; 166 } 167 PersistenceManager pm = pmf.getPersistenceManager(); 168 pm.getObjectIdClass(persistentClass); 169 pm.close(); 170 System.out.println("[INFO] Class '" + className + "' initialized."); 171 } 172 } 173 | Popular Tags |