KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > jorm > TestJormHelper


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

23
24 package org.objectweb.medor.jorm;
25
26 import org.apache.tools.ant.types.DTDLocation;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.api.PMapper;
29 import org.objectweb.jorm.metainfo.api.Class;
30 import org.objectweb.jorm.metainfo.api.Manager;
31 import org.objectweb.jorm.metainfo.api.MetaObject;
32 import org.objectweb.medor.api.MedorException;
33 import org.objectweb.medor.lib.Log;
34 import org.objectweb.medor.lib.TestMedorHelper;
35 import org.objectweb.medor.optim.api.QueryRewriter;
36 import org.objectweb.medor.query.api.QueryTree;
37 import org.objectweb.medor.query.jorm.lib.ClassExtent;
38 import org.objectweb.medor.query.lib.QueryTreePrinter;
39 import org.objectweb.jorm.util.io.lib.DirJavaExplorer;
40 import org.objectweb.jorm.compiler.lib.JormCompiler;
41 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.api.Logger;
44 import org.objectweb.util.monolog.api.LoggerFactory;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Collections JavaDoc;
50
51 /**
52  * @author S.Chassande-Barrioz
53  */

54 public abstract class TestJormHelper extends TestMedorHelper {
55     private static String JavaDoc mapperName;
56     private static String JavaDoc projectName;
57     private static String JavaDoc miMappingFactory;
58     private static String JavaDoc mappingParser;
59     private static String JavaDoc pdPath;
60     private static String JavaDoc dtdPath;
61
62     public static final String JavaDoc DEFAULT_PNAME_FIELD_NAME = "object";
63     public static final String JavaDoc PROJECT_NAME = "test";
64
65     static {
66         Logger localLogger = getLoggerFactory()
67                 .getLogger(Log.MEDOR_PREFIX + ".jorm");
68         mapperName = System.getProperty(
69                 "medor.jorm.mapperName", "rdb");
70         localLogger.log(BasicLevel.DEBUG, "MapperName = " + mapperName);
71
72         projectName = System.getProperty(
73                 "medor.jorm.projectName", "");
74         localLogger.log(BasicLevel.DEBUG, "ProjectName = " + projectName);
75
76         miMappingFactory = System.getProperty(
77                 "medor.jorm.miMappingName",
78                 "org.objectweb.jorm.metainfo.rdb.BasicRDBMappingFactory");
79         localLogger.log(
80                 BasicLevel.DEBUG, "MIMappingFactory = " + miMappingFactory);
81
82         mappingParser = System.getProperty(
83                 "medor.jorm.miMappingName",
84                 "org.objectweb.jorm.xml2mi.rdb.RDBParser");
85         localLogger.log(BasicLevel.DEBUG, "MappingParser = " + mappingParser);
86
87         pdPath = System.getProperty(
88                 "medor.jorm.pd.path", "./");
89         localLogger.log(BasicLevel.DEBUG, "pd Path = " + pdPath);
90         dtdPath = System.getProperty(
91                 "medor.jorm.dtd.path", "./");
92         localLogger.log(BasicLevel.DEBUG, "dtd Path = " + dtdPath);
93     }
94
95     public static Manager manager = null;
96     public static PMapper mapper = null;
97     public static JormCompiler compiler = null;
98
99     public class Example {
100         public QueryTree qt;
101         public Class JavaDoc clazz;
102         public ClassExtent extent;
103         public String JavaDoc pnameFieldName;
104     }
105
106     public TestJormHelper(String JavaDoc testName, String JavaDoc loggerName) {
107         super(testName, loggerName);
108         synchronized (getClass()) {
109             if (compiler != null) {
110                 return;
111                 }
112             try {
113                 compiler = new JormCompiler();
114                 compiler.getCompilerConfigurator().configure();
115                 mapper = newMappper();
116                 mapper.setMapperName(getMapperName());
117                 LoggerFactory loggerFactory = getLoggerFactory();
118                 compiler.getCompilerConfigurator().setLoggerFactory(loggerFactory);
119                 compiler.setupLogger();
120                 compiler.setupMIManager();
121                 manager = compiler.getMIManager();
122                 //compiler.setupVerifier(); currently the verifier is not availlable
123
compiler.setupGenerator();
124                 JormCompilerParameter cp = compiler.getCompilerParameter();
125                 DirJavaExplorer exp = new DirJavaExplorer();
126                 exp.addPath(getPDPath());
127                 cp.setClasspath(exp);
128                 cp.setProjectName(getProjectName());
129                 DTDLocation dtdl = new DTDLocation();
130                 dtdl.setPublicId("-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN");
131                 dtdl.setLocation(getDTDPath());
132                 cp.getDtdLocations().add(dtdl);
133                 compiler.setupParser();
134                 compiler.getCompilerParameter().setProjectName(PROJECT_NAME);
135             }
136             catch (PException e) {
137                 fail(e.getMessage());
138             }
139         }
140     }
141
142     public String JavaDoc getPDPath() {
143         return pdPath;
144     }
145
146     public String JavaDoc getDTDPath() {
147         return dtdPath;
148     }
149
150     public String JavaDoc getMapperName() {
151         return mapperName;
152     }
153
154     public String JavaDoc getProjectName() {
155         return projectName;
156     }
157
158     public String JavaDoc getMIMappingFactory() {
159         return miMappingFactory;
160     }
161
162     public String JavaDoc getMappingParser() {
163         return mappingParser;
164     }
165
166     public abstract PMapper newMappper();
167
168     public abstract QueryRewriter createQueryRewriter();
169
170     protected void setUp() {
171     }
172
173     protected void tearDown() {
174     }
175
176     public Example getExtent(Collection JavaDoc fileNames, String JavaDoc className) {
177         return getExtent(fileNames, className, DEFAULT_PNAME_FIELD_NAME, null);
178     }
179
180     public Example getExtent(Collection JavaDoc fileNames,
181                              String JavaDoc fqClassName,
182                              String JavaDoc pnameFieldName,
183                              String JavaDoc[] fieldNames) {
184
185         logger.log(BasicLevel.DEBUG, "getExtentOf the class " + fqClassName);
186
187         logger.log(BasicLevel.DEBUG, "manager=" + manager);
188         Collection JavaDoc mos = null;
189
190         Class JavaDoc c = manager.getClass(fqClassName);
191
192         if (c == null) {
193             try {
194                 mos = loadJormMetaInfo(fileNames);
195                 logger.log(BasicLevel.DEBUG, "Jorm meta information loaded");
196             }
197             catch (PException e) {
198                 e.printStackTrace(System.out);
199                 fail(e.getMessage());
200             }
201         } else {
202             mos = Collections.singletonList(c);
203         }
204
205         for (Iterator JavaDoc it = mos.iterator(); it.hasNext();) {
206             MetaObject mo = (MetaObject) it.next();
207             if (mo instanceof Class JavaDoc
208                     && fqClassName.equals(((Class JavaDoc) mo).getFQName())) {
209                 Example e = new Example();
210                 e.pnameFieldName = pnameFieldName;
211                 e.clazz = (Class JavaDoc) mo;
212                 if (manager == null) {
213                     manager = (Manager) mo.getParent().getParent();
214                     throw new RuntimeException JavaDoc("The test must be changed in according to JORM 2.2");
215                     //mapper.setMetaInfoManager(manager);
216
}
217                 logger.log(BasicLevel.DEBUG, "schema=" + mo.getParent());
218                 logger.log(BasicLevel.DEBUG, "parent(schema)=" + mo.getParent().getParent());
219                 try {
220                     if (fieldNames == null)
221                         e.extent = new ClassExtent(
222                                 e.clazz, e.clazz.getFQName(), pnameFieldName, false);
223                     else
224                         e.extent = new ClassExtent(e.clazz, e.clazz.getFQName(),
225                                 fieldNames, true, pnameFieldName);
226                     logger.log(BasicLevel.DEBUG, "Extent created for the class "
227                             + e.clazz.getName());
228                     e.extent.setPMapper(mapper, PROJECT_NAME);
229
230                     QueryTreePrinter.printQueryTree(e.extent, logger);
231                     return e;
232                 }
233                 catch (MedorException me) {
234                     me.printStackTrace();
235                     fail(me.getMessage());
236                 }
237             }
238         }
239         return null;
240     }
241
242     public Example getRewritten(Example e) {
243         if (e == null)
244             return null;
245         try {
246             //assign mapper and project names to the JormExtent
247
e.extent.setProjectName("test");
248             e.extent.setPMapper(mapper, PROJECT_NAME);
249
250             QueryRewriter qr = createQueryRewriter();
251             logger.log(BasicLevel.DEBUG, "QueryRewriter created");
252
253             e.qt = qr.transform(e.extent);
254             logger.log(BasicLevel.DEBUG, "Extent of the class "
255                     + e.clazz.getName() + " rewritten");
256
257             QueryTreePrinter.printQueryTree(e.qt, logger);
258         }
259         catch (MedorException me) {
260             me.printStackTrace();
261             fail(me.getMessage());
262         }
263         return e;
264     }
265
266     public Collection JavaDoc loadJormMetaInfo(String JavaDoc fileName) throws PException {
267         ArrayList JavaDoc al = new ArrayList JavaDoc(1);
268         al.add(fileName);
269         return loadJormMetaInfo(al);
270     }
271
272     public Collection JavaDoc loadJormMetaInfo(Collection JavaDoc fileNames) throws PException {
273         Collection JavaDoc mos = compiler.parseFiles(fileNames);
274         logger.log(BasicLevel.DEBUG, "Files parsed (" + mos.size() + " item(s) found)");
275
276         mos.iterator();
277         /*
278         MetaInfoPrinter moprint = new MetaInfoPrinter();
279         while (it.hasNext()) {
280             MetaObject mo = (MetaObject) it.next();
281             moprint.print("", mo, new PrintStreamImpl(logger));
282         }
283         */

284         return mos;
285     }
286
287     public String JavaDoc getFieldName(String JavaDoc alias, String JavaDoc fn) {
288         return (alias == null || alias.length() == 0 ? "" : alias + ".") + fn;
289     }
290 }
291
Popular Tags