KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tools > DataStructureCreation


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

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 JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38
39 /**
40  * It initializes the data structure of persistent classes.
41  *
42  * @author S.Chassande-Barrioz
43  */

44 public class DataStructureCreation {
45
46     /**
47      * 1 => bad usage
48      * 2 => impossible to load the properties file
49      * 3 => impossible to load the persistent class
50      * @param args an array of (class name | .jdo file) to initialize
51      */

52     public static void main(String JavaDoc[] args) {
53         new DataStructureCreation().execute(args);
54     }
55
56     private void execute(String JavaDoc[] 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         //Check parameter
63
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         //Load the properties file from the classpath
75
Properties p = new Properties();
76         String JavaDoc speedoProperties = "speedo.properties";
77         InputStream JavaDoc 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 JavaDoc 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         //Specify to speedo to create the mapping structure if it does not
92
// already exist
93
p.setProperty(SpeedoProperties.MAPPING_STRUCTURE, SpeedoProperties.MAPPING_STRUCTURE_CIR);
94         boolean useDriverDirectly = false;
95         String JavaDoc 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             //In case of the user specifies driver properties, the CF name is removed
122
p.remove(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME);
123         }
124
125         // start Speedo
126
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
127
128         ClassLoader JavaDoc cl = getClass().getClassLoader();
129         for (int i=0; i<args.length; i++) {
130             if (args[i].endsWith(".jdo")) {
131                 Set JavaDoc mos = null;
132                 try {
133                     mos = (Set JavaDoc) Object2StringSerializer.deserialize( args[i], cl, logger);
134                 } catch (Exception JavaDoc 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                 //Parse the meta information in order to initialize each class
143
for (Iterator JavaDoc it = mos.iterator(); it.hasNext();) {
144                     Object JavaDoc o = it.next();
145                     if (o instanceof Class JavaDoc) {
146                         initClass(((Class JavaDoc) o).getFQName(), pmf, cl);
147                     }
148                 }
149
150             } else {
151                 initClass(args[i], pmf, cl);
152             }
153         }
154     }
155
156     private void initClass(String JavaDoc className,
157                            PersistenceManagerFactory pmf,
158                            ClassLoader JavaDoc classLoader) {
159         java.lang.Class JavaDoc persistentClass = null;
160         try {
161             persistentClass = classLoader.loadClass(className);
162         } catch (ClassNotFoundException JavaDoc 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