KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > codegen > CmpCompiler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.codegen;
25
26 import java.io.File JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Collection JavaDoc;
30
31 import java.util.logging.Logger JavaDoc;
32 import java.util.logging.Level JavaDoc;
33
34 import com.sun.logging.LogDomains;
35 import com.sun.enterprise.deployment.EjbDescriptor;
36 import com.sun.enterprise.deployment.Application;
37 import com.sun.enterprise.deployment.EjbBundleDescriptor;
38 import com.sun.enterprise.deployment.EjbCMPEntityDescriptor;
39
40 import com.sun.enterprise.util.io.FileUtils;
41 import com.sun.enterprise.util.i18n.StringManager;
42 import com.sun.enterprise.deployment.runtime.IASEjbExtraDescriptors;
43 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor;
44 import com.sun.enterprise.deployment.runtime.IASPersistenceManagerDescriptor;
45
46
47 /**
48  * Generates concrete impls for CMP beans in an archive.
49  *
50  * @author Nazrul Islam
51  * @since JDK 1.4
52  */

53 class CmpCompiler {
54
55     /**
56      * Constructor!
57      *
58      * @param ctx object encapsulating ejbc runtime information
59      */

60     CmpCompiler(EjbcContext ctx) {
61         this._ejbcCtx = ctx;
62     }
63
64     /**
65      * Generates the concrete impls for all CMPs in the application.
66      *
67      * @throws GeneratorException if this exception was thrown while generating concrete impls
68      * @throws CmpCompilerException if any other error occurs while generating concrete impls
69      */

70     void compile() throws CmpCompilerException, GeneratorException {
71         
72         // deployment descriptor object representation for the archive
73
Application application = null;
74
75         // deployment descriptor object representation for each module
76
EjbBundleDescriptor bundle = null;
77
78         // ejb name
79
String JavaDoc beanName = null;
80
81         // GeneratorException message if any
82
StringBuffer JavaDoc generatorExceptionMsg = null;
83
84         try {
85             // scratchpad variable
86
long time;
87
88             // stubs dir for the current deployment
89
File JavaDoc stubsDir = this._ejbcCtx.getStubsDir();
90
91             application = this._ejbcCtx.getDescriptor();
92
93             _logger.log(Level.FINE, "ejbc.processing_cmp",
94                         application.getRegistrationName());
95
96             Vector JavaDoc cmpFiles = new Vector JavaDoc();
97             final ClassLoader JavaDoc jcl = application.getClassLoader();
98
99             // For each Bundle descriptor generate concrete class.
100
Iterator JavaDoc bundleItr =
101                 application.getEjbBundleDescriptors().iterator();
102
103             while ( bundleItr.hasNext() ) {
104
105                 bundle = (EjbBundleDescriptor)bundleItr.next();
106
107                 if (!bundle.containsCMPEntity()) {
108                     continue;
109                 }
110                 
111                 // If it is a stand alone module then the srcDir is
112
// the ModuleDirectory
113
String JavaDoc archiveUri = (!application.isVirtual()) ?
114                     this._ejbcCtx.getSrcDir().getCanonicalPath()
115                         + File.separator+getModuleDirName(bundle) :
116                     this._ejbcCtx.getSrcDir().getCanonicalPath();
117
118                 if (com.sun.enterprise.util.logging.Debug.enabled) {
119                     _logger.log(Level.FINE,"[CMPC] Module Dir name is "
120                             + archiveUri);
121                 }
122
123                 String JavaDoc generatedXmlsPath = (!application.isVirtual()) ?
124                     this._ejbcCtx.getDeploymentRequest().
125                         getGeneratedXMLDirectory().getCanonicalPath()
126                             + File.separator + getModuleDirName(bundle) :
127                     this._ejbcCtx.getDeploymentRequest().
128                         getGeneratedXMLDirectory().getCanonicalPath();
129
130                 if (com.sun.enterprise.util.logging.Debug.enabled) {
131                     _logger.log(Level.FINE,"[CMPC] Generated XML Dir name is "
132                             + generatedXmlsPath);
133                 }
134
135                 IASPersistenceManagerDescriptor pmDesc =
136                     bundle.getPreferredPersistenceManager();
137
138                 String JavaDoc generatorName = null;
139                 if (null == pmDesc) {
140                     generatorName = IASPersistenceManagerDescriptor.PM_CLASS_GENERATOR_DEFAULT;
141
142                 } else {
143                     generatorName = pmDesc.getPersistenceManagerClassGenerator();
144
145                     // Backward compatability:
146
// Support existing settings that have the old name.
147
if (generatorName.equals(
148             IASPersistenceManagerDescriptor.PM_CLASS_GENERATOR_DEFAULT_OLD)) {
149
150                         generatorName = IASPersistenceManagerDescriptor.PM_CLASS_GENERATOR_DEFAULT;
151                     }
152                 }
153
154                 CMPGenerator gen = null;
155
156                 try {
157             Class JavaDoc generator = getClass().getClassLoader().loadClass(generatorName);
158                     gen = (CMPGenerator)generator.newInstance();
159
160                 } catch (Throwable JavaDoc e) {
161                     String JavaDoc msg = localStrings.getString("cmpc.cmp_generator_class_error",
162                             application.getRegistrationName(),
163                             bundle.getModuleDescriptor().getArchiveUri());
164                     _logger.log(Level.SEVERE, msg, e);
165                     generatorExceptionMsg = addGeneratorExceptionMessage(msg,
166                             generatorExceptionMsg);
167
168                     continue;
169                 }
170
171                 try {
172                     time = now();
173                     gen.init(bundle, _ejbcCtx, archiveUri, generatedXmlsPath);
174                     this._ejbcCtx.getTiming().cmpGeneratorTime += (now() - time);
175                 
176                     Iterator JavaDoc ejbs=bundle.getEjbs().iterator();
177     
178                     while ( ejbs.hasNext() ) {
179     
180                         EjbDescriptor desc = (EjbDescriptor) ejbs.next();
181                         beanName = desc.getName();
182
183                         if (com.sun.enterprise.util.logging.Debug.enabled) {
184                             _logger.log(Level.FINE,"[CMPC] Ejb Class Name: "
185                                                + desc.getEjbClassName());
186                         }
187     
188                         if ( desc instanceof IASEjbCMPEntityDescriptor ) {
189     
190                             // generate concrete CMP class implementation
191
IASEjbCMPEntityDescriptor entd =
192                                 (IASEjbCMPEntityDescriptor)desc;
193     
194                             if (com.sun.enterprise.util.logging.Debug.enabled) {
195                                 _logger.log(Level.FINE,
196                                     "[CMPC] Home Object Impl name is "
197                                     + entd.getLocalHomeImplClassName());
198                             }
199     
200                             // generate persistent class
201
entd.setClassLoader(jcl);
202                     
203                             try {
204                                 time = now();
205                                 gen.generate(entd, stubsDir, stubsDir);
206                                 this._ejbcCtx.getTiming().cmpGeneratorTime +=
207                                                                 (now() - time);
208     
209                             } catch (GeneratorException e) {
210                                 String JavaDoc msg = e.getMessage();
211                                 _logger.log(Level.WARNING, msg);
212                                 generatorExceptionMsg = addGeneratorExceptionMessage(
213                                         msg, generatorExceptionMsg);
214                             }
215
216                         /* WARNING: IASRI 4683195
217                          * JDO Code failed when there was a relationship involved
218                          * because it depends upon the orginal ejbclasname and hence
219                          * this code is shifted to just before the Remote Impl is
220                          * generated.Remote/Home Impl generation depends upon this
221                          * value
222                          */

223     
224                         } else if (desc instanceof EjbCMPEntityDescriptor ) {
225                                 //RI code here
226
}
227
228                     } // end while ejbs.hasNext()
229

230                     beanName = null;
231
232                     time = now();
233                     Collection JavaDoc col = gen.cleanup();
234                     this._ejbcCtx.getTiming().cmpGeneratorTime += (now() - time);
235     
236                     for (Iterator JavaDoc fileIter=col.iterator();fileIter.hasNext();) {
237                         File JavaDoc file=(File JavaDoc)fileIter.next();
238                         String JavaDoc fileName=file.getPath();
239                         _logger.log(Level.FINE,"[CMPC] File name is "+fileName);
240                         cmpFiles.addElement(fileName);
241                     }
242                     
243                 } catch (GeneratorException e) {
244                     String JavaDoc msg = e.getMessage();
245                     _logger.log(Level.WARNING, msg);
246                     generatorExceptionMsg = addGeneratorExceptionMessage(msg,
247                             generatorExceptionMsg);
248                 }
249
250             } // end of bundle
251

252             bundle = null;
253
254             if (generatorExceptionMsg == null) {
255                 // class path for javac
256
String JavaDoc classPath =
257                     getClassPath(this._ejbcCtx.getClasspathUrls(), stubsDir);
258
259                 time = now();
260                 IASEJBC.compileClasses(classPath, cmpFiles, stubsDir,
261                                    stubsDir.getCanonicalPath(),
262                                    this._ejbcCtx.getJavacOptions());
263
264                 this._ejbcCtx.getTiming().javaCompileTime += (now() - time);
265
266                 _logger.log(Level.FINE, "ejbc.done_processing_cmp",
267                         application.getRegistrationName());
268              }
269
270         } catch (GeneratorException e) {
271             _logger.log(Level.WARNING, e.getMessage());
272             throw e;
273
274         } catch (Throwable JavaDoc e) {
275             String JavaDoc eType = e.getClass().getName();
276             String JavaDoc appName = application.getRegistrationName();
277             String JavaDoc exMsg = e.getMessage();
278
279             String JavaDoc msg = null;
280             if (bundle == null) {
281                 // Application or compilation error
282
msg = localStrings.getString("cmpc.cmp_app_error",
283                     eType, appName, exMsg);
284             } else {
285                 String JavaDoc bundleName = bundle.getModuleDescriptor().getArchiveUri();
286                 if (beanName == null) {
287                     // Module processing error
288
msg = localStrings.getString("cmpc.cmp_module_error",
289                         eType, appName, bundleName, exMsg);
290                 } else {
291                     // CMP bean generation error
292
msg = localStrings.getString("cmpc.cmp_bean_error",
293                         new Object JavaDoc[] {eType, beanName, appName, bundleName, exMsg});
294                 }
295             }
296
297             _logger.log(Level.SEVERE, msg, e);
298
299             throw new CmpCompilerException(msg);
300         }
301
302         if (generatorExceptionMsg != null) {
303             // We already logged each separate part.
304
throw new GeneratorException(generatorExceptionMsg.toString());
305         }
306     }
307
308     /**
309      * Helper method. Returns the current system time.
310      *
311      * @return current system time
312      */

313     private long now() {
314         return System.currentTimeMillis();
315     }
316
317     /**
318      * Returns the name of the ejb module for the given bundle descriptor.
319      *
320      * <xmp>
321      * ejb-jar-ic_jar (from ConverterApp example)
322      * </xmp>
323      *
324      * @param bundle ejb bundle descriptor
325      * @return the name of the ejb dir module
326      */

327     private static String JavaDoc getModuleDirName(EjbBundleDescriptor bundle)
328     {
329         String JavaDoc archieveuri = bundle.getModuleDescriptor().getArchiveUri();
330         return FileUtils.makeFriendlyFilename(archieveuri);
331     }
332
333     /**
334      * Helper method - returns the class path as string with path separator.
335      *
336      * @param paths array of class paths
337      * @param other additional directory to be added to the class path
338      *
339      * @return class path for the given application
340      */

341     private static String JavaDoc getClassPath(String JavaDoc[] paths, File JavaDoc other) {
342
343         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
344
345         for (int i=0; i<paths.length; i++) {
346             sb.append(paths[i]+File.pathSeparator);
347         }
348
349         if (other != null) {
350             sb.append(other.toString());
351         }
352
353         return sb.toString();
354     }
355
356     /** Adds GeneratorException message to the buffer.
357      *
358      * @param msg the message text to add to the buffer.
359      * @param buf the buffer to use.
360      * @return the new or updated buffer.
361      */

362     private StringBuffer JavaDoc addGeneratorExceptionMessage(String JavaDoc msg, StringBuffer JavaDoc buf) {
363         StringBuffer JavaDoc rc = buf;
364         if (rc == null)
365             rc = new StringBuffer JavaDoc(msg);
366         else
367             rc.append('\n').append(msg);
368
369         return rc;
370     }
371
372     // ---- VARIABLE(S) - PRIVATE --------------------------------------
373
private EjbcContext _ejbcCtx = null;
374     private static final Logger JavaDoc _logger =
375                 LogDomains.getLogger(LogDomains.DPL_LOGGER);
376     private static final StringManager localStrings =
377                     StringManager.getManager(CmpCompiler.class);
378
379 }
380
Popular Tags