KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > genic > BeanSources


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BeanSources.java,v 1.35 2005/07/27 13:36:18 pelletib Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.genic;
27
28 import java.io.File JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import org.apache.velocity.app.VelocityEngine;
34
35 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
36 import org.objectweb.jonas_ejb.deployment.api.EntityCmp2Desc;
37 import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
38 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp1Desc;
39 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
40 import org.objectweb.jonas_ejb.lib.BeanNaming;
41 import org.objectweb.jonas_ejb.lib.RdbMappingBuilder;
42
43 import org.objectweb.jonas.common.Log;
44
45 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
46 import org.objectweb.jorm.compiler.lib.JormCompiler;
47
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.api.Logger;
50
51 /**
52  * This class allows to generate the sources of: the class that implements the
53  * Enterprise bean's remote interface, the class that implements the Enterprise
54  * bean's home interface, the class that implements the Enterprise bean's local
55  * interface, the class that implements the Enterprise bean's localhome
56  * interface, the class of the Entity Handle in case of entity, and the extended
57  * class of the Bean for persistence in case of entity with CMP, of a given
58  * Enterprise Java Bean.
59  * @author Helene Joanin : Initial developer
60  * @author Philippe Durieux
61  * @author Sebastien Chassande
62  */

63 public class BeanSources {
64
65     /**
66      * java file suffix
67      */

68     private static final String JavaDoc JAVA_SUFFIX = ".java";
69
70     /**
71      * mapper name prefix
72      */

73     private static final String JavaDoc RDB_PREFIX = "rdb";
74
75     /**
76      * Cluster suffix
77      */

78     private static final String JavaDoc CLUSTER_SUFFIX = "_Cmi";
79
80     /**
81      * EJBObject (Remote). May be null if no Remote interface.
82      */

83     private String JavaDoc wrpRemoteFileName = null;
84
85     /**
86      * EJBLocalObject (Local). May be null if no Local interface.
87      */

88     private String JavaDoc wrpLocalFileName = null;
89
90     /**
91      * Home. May be null if no Home interface.
92      */

93     private String JavaDoc wrpHomeFileName = null;
94
95     /**
96      * Home cluster file name. May be null if no Home interface.
97      */

98     private String JavaDoc wrpHomeClusterFileName = null;
99
100     /**
101      * LocalHome. May be null if no LocalHome interface.
102      */

103     private String JavaDoc wrpLocalHomeFileName = null;
104
105     /**
106      * Handle (for Entity only)
107      */

108     private String JavaDoc wrpHandleFileName = null;
109
110     /**
111      * Bean (for CMP Entity only)
112      */

113     private String JavaDoc wrpBeanFileName = null;
114
115     /**
116      * Service Endpoint filename
117      */

118     private String JavaDoc wrpServiceEndpointFileName = null;
119
120     /**
121      * Service Endpoint Home filename
122      */

123     private String JavaDoc wrpSEHomeFileName = null;
124
125     /**
126      * Interface for Cmp2 Entity coherence
127      */

128     private String JavaDoc itfCohCmp2Entity = null;
129
130     /**
131      * Source objects used to generate classes
132      */

133     private ArrayList JavaDoc sources;
134
135     /**
136      * Java sources that are not Remote
137      */

138     private ArrayList JavaDoc noRemoteJava;
139
140     /**
141      * directory where to place the generated files
142      */

143     private String JavaDoc outdir = null;
144
145     /**
146      * bean descriptor
147      */

148     private BeanDesc dd = null;
149
150     /**
151      * JORM compiler
152      */

153     private JormCompiler jormCompiler;
154
155     /**
156      * logger
157      */

158     private Logger logger = null;
159
160     /**
161      * BeanSources Constructor for entity bean with CMP2
162      * @param beanDesc deployment descriptor of the bean
163      * @param dirOutputName path of the directory where to place the generated
164      * files (empty string "" if the output directory is the current
165      * directory)
166      * @param ve the Velocity engine
167      * @param jormCompiler the JROM compiler
168      * @exception GenICException In error case
169      */

170     public BeanSources(BeanDesc beanDesc, String JavaDoc dirOutputName, VelocityEngine ve, JormCompiler jormCompiler)
171             throws GenICException {
172         this(beanDesc, dirOutputName, ve);
173         this.jormCompiler = jormCompiler;
174     }
175
176     /**
177      * BeanSources Constructor for bean that is not a entity with CMP2
178      * @param beanDesc deployment descriptor of the bean
179      * @param dirOutputName path of the directory where to place the generated
180      * files (empty string "" if the output directory is the current
181      * directory)
182      * @param ve the Velocity engine
183      * @exception GenICException In error case
184      */

185     public BeanSources(BeanDesc beanDesc, String JavaDoc dirOutputName, VelocityEngine ve) throws GenICException {
186
187         logger = Log.getLogger(Log.JONAS_GENIC_PREFIX);
188         dd = beanDesc;
189         outdir = dirOutputName;
190         sources = new ArrayList JavaDoc();
191         noRemoteJava = new ArrayList JavaDoc();
192
193         // generated Home
194
if (dd.getHomeClass() != null) {
195             if (outdir.length() > 0) {
196                 wrpHomeFileName = outdir + File.separatorChar + BeanNaming.getPath(dd.getFullWrpHomeName());
197             } else {
198                 wrpHomeFileName = dd.getWrpHomeName();
199             }
200             wrpHomeClusterFileName = wrpHomeFileName.concat(CLUSTER_SUFFIX + JAVA_SUFFIX);
201             noRemoteJava.add(wrpHomeClusterFileName);
202             sources.add(new Source(dd, wrpHomeClusterFileName, Source.CLUSTER_HOME, ve));
203
204             wrpHomeFileName = wrpHomeFileName.concat(JAVA_SUFFIX);
205             sources.add(new Source(dd, wrpHomeFileName, Source.HOME, ve));
206
207             // generated Handle for Remote Entity bean
208
if (dd instanceof EntityDesc) {
209                 if (outdir.length() > 0) {
210                     wrpHandleFileName = outdir + File.separatorChar + BeanNaming.getPath(dd.getFullWrpHandleName());
211                 } else {
212                     wrpHandleFileName = dd.getWrpHandleName();
213                 }
214                 wrpHandleFileName = wrpHandleFileName.concat(JAVA_SUFFIX);
215                 noRemoteJava.add(wrpHandleFileName);
216                 sources.add(new Source(dd, wrpHandleFileName, Source.ENTITY_HANDLE, ve));
217             }
218         }
219
220         // generated EJBObject (Remote)
221
if (dd.getRemoteClass() != null) {
222             if (outdir.length() > 0) {
223                 wrpRemoteFileName = outdir + File.separatorChar + BeanNaming.getPath(dd.getFullWrpRemoteName());
224             } else {
225                 wrpRemoteFileName = dd.getWrpRemoteName();
226             }
227             wrpRemoteFileName = wrpRemoteFileName.concat(JAVA_SUFFIX);
228             sources.add(new Source(dd, wrpRemoteFileName, Source.REMOTE, ve));
229         }
230
231         // generated LocalHome
232
if (dd.getLocalHomeClass() != null) {
233             if (outdir.length() > 0) {
234                 wrpLocalHomeFileName = outdir + File.separatorChar + BeanNaming.getPath(dd.getFullWrpLocalHomeName());
235             } else {
236                 wrpLocalHomeFileName = dd.getWrpLocalHomeName();
237             }
238             wrpLocalHomeFileName = wrpLocalHomeFileName.concat(JAVA_SUFFIX);
239             noRemoteJava.add(wrpLocalHomeFileName);
240             sources.add(new Source(dd, wrpLocalHomeFileName, Source.LOCAL_HOME, ve));
241         }
242
243         // generated EJBLocalObject (Local)
244
if (dd.getLocalClass() != null) {
245             if (outdir.length() > 0) {
246                 wrpLocalFileName = outdir + File.separatorChar + BeanNaming.getPath(dd.getFullWrpLocalName());
247             } else {
248                 wrpLocalFileName = dd.getWrpLocalName();
249             }
250             wrpLocalFileName = wrpLocalFileName.concat(JAVA_SUFFIX);
251             noRemoteJava.add(wrpLocalFileName);
252             sources.add(new Source(dd, wrpLocalFileName, Source.LOCAL, ve));
253         }
254
255         // generated ServiceEndpoint and SEHome
256
if (dd instanceof SessionStatelessDesc) {
257             SessionStatelessDesc ssd = (SessionStatelessDesc) dd;
258             if (ssd.getServiceEndpointClass() != null) {
259                 // ServiceEndpoint
260
if (outdir.length() > 0) {
261                     wrpServiceEndpointFileName = outdir + File.separatorChar
262                             + BeanNaming.getPath(ssd.getFullWrpServiceEndpointName());
263                 } else {
264                     wrpServiceEndpointFileName = ssd.getWrpServiceEndpointName();
265                 }
266                 wrpServiceEndpointFileName = wrpServiceEndpointFileName.concat(JAVA_SUFFIX);
267                 //noRemoteJava.add(wrpServiceEndpointFileName); // Accessed locally only
268
sources.add(new Source(dd, wrpServiceEndpointFileName, Source.SERVICE_ENDPOINT, ve));
269                 // ServiceEndpointHome
270
if (outdir.length() > 0) {
271                     wrpSEHomeFileName = outdir + File.separatorChar + BeanNaming.getPath(ssd.getFullWrpSEHomeName());
272                 } else {
273                     wrpSEHomeFileName = ssd.getWrpSEHomeName();
274                 }
275                 wrpSEHomeFileName = wrpSEHomeFileName.concat(JAVA_SUFFIX);
276                 noRemoteJava.add(wrpSEHomeFileName); // Accessed locally only
277
sources.add(new Source(dd, wrpSEHomeFileName, Source.SERVICE_ENDPOINT_HOME, ve));
278             }
279         }
280
281         // generated derived bean class for Entity Bean CMP
282
if (dd instanceof EntityJdbcCmp1Desc) {
283             if (outdir.length() > 0) {
284                 wrpBeanFileName = outdir + File.separatorChar + BeanNaming.getPath(dd.getFullDerivedBeanName());
285             } else {
286                 wrpBeanFileName = dd.getDerivedBeanName();
287             }
288             wrpBeanFileName = wrpBeanFileName.concat(JAVA_SUFFIX);
289             noRemoteJava.add(wrpBeanFileName);
290             sources.add(new Source(dd, wrpBeanFileName, Source.ENTITY_CMP_JDBC, ve));
291         }
292         if (dd instanceof EntityCmp2Desc) {
293             EntityCmp2Desc ecd = (EntityCmp2Desc) dd;
294             if (outdir.length() > 0) {
295                 wrpBeanFileName = outdir + File.separatorChar + BeanNaming.getPath(dd.getFullDerivedBeanName())
296                         + JAVA_SUFFIX;
297                 if (ecd.needJormCoherenceHelper()) {
298                     itfCohCmp2Entity = outdir + File.separatorChar
299                             + BeanNaming.getPath(ecd.getJormCoherenceHelperFQItfName()) + JAVA_SUFFIX;
300                 }
301             } else {
302                 wrpBeanFileName = BeanNaming.getPath(dd.getDerivedBeanName()) + JAVA_SUFFIX;
303                 if (ecd.needJormCoherenceHelper()) {
304                     itfCohCmp2Entity = BeanNaming.getPath(ecd.getJormCoherenceHelperItfName()) + JAVA_SUFFIX;
305                 }
306             }
307             if (ecd.needJormCoherenceHelper()) {
308                 sources.add(new Source(dd, itfCohCmp2Entity, Source.ITF_COH_CMP2_ENTITY, ve));
309                 noRemoteJava.add(itfCohCmp2Entity);
310             }
311             noRemoteJava.add(wrpBeanFileName);
312             sources.add(new Source(dd, wrpBeanFileName, Source.ENTITY_CMP_JDBC, ve));
313         }
314     }
315
316     /**
317      * Generates the java sources
318      * @throws GenICException in error case
319      */

320     public void generate() throws GenICException {
321         // Generates the java sources
322
for (Iterator JavaDoc it = sources.iterator(); it.hasNext();) {
323             Source src = (Source) it.next();
324             src.generate();
325         }
326
327         // In case of Cmp2, call jorm compiler.
328
if (dd instanceof EntityCmp2Desc) {
329             EntityCmp2Desc ecd = (EntityCmp2Desc) dd;
330
331             // Build and fill a JormCompilerParameter dedicated to the bean
332
JormCompilerParameter cp = jormCompiler.getCompilerParameter();
333             cp.setProjectName(RdbMappingBuilder.getProjectName());
334             cp.setKeepSrc(true);
335             cp.setOutput(outdir);
336             cp.setClassMappingInheritance("org.objectweb.jonas_ejb.container.jorm.RdbFactory");
337             switch (ecd.getLockPolicy()) {
338                 case EntityDesc.LOCK_CONTAINER_READ_UNCOMMITTED:
339                     cp.setBindingInheritance("org.objectweb.jonas_ejb.container.JEntitySwitchCRU");
340                     break;
341                 case EntityDesc.LOCK_CONTAINER_SERIALIZED:
342                     cp.setBindingInheritance("org.objectweb.jonas_ejb.container.JEntitySwitchCS");
343                     break;
344                 case EntityDesc.LOCK_CONTAINER_READ_COMMITTED:
345                     cp.setBindingInheritance("org.objectweb.jonas_ejb.container.JEntitySwitchCRC");
346                     break;
347                 case EntityDesc.LOCK_DATABASE:
348                     cp.setBindingInheritance("org.objectweb.jonas_ejb.container.JEntitySwitchDB");
349                     break;
350                 case EntityDesc.LOCK_READ_ONLY:
351                     cp.setBindingInheritance("org.objectweb.jonas_ejb.container.JEntitySwitchRO");
352                     break;
353                 default:
354                     throw new GenICException("Cannot find JEntitySwitch: Unknown lock policy");
355             }
356
357             // Run the Jorm Compiler
358
Collection JavaDoc jormflist = null;
359             try {
360                 // For debug only: Generate the '.pd' Jorm Files
361
if (Log.getLogger("org.objectweb.jonas_ejb.mijorm").isLoggable(BasicLevel.DEBUG)) {
362                     jormCompiler.generateJormFiles(ecd.getJormList());
363                 }
364                 jormflist = jormCompiler.generateFiles(ecd.getJormList());
365             } catch (Exception JavaDoc e) {
366                 throw new GenICException("Problem during jorm generation", e);
367             }
368             // Save the list of Jorm generated files
369
for (Iterator JavaDoc i = jormflist.iterator(); i.hasNext();) {
370                 String JavaDoc file = (String JavaDoc) i.next();
371                 logger.log(BasicLevel.DEBUG, "Jorm generated file: " + file);
372                 noRemoteJava.add(file);
373             }
374         }
375     }
376
377     /**
378      * @return Return the bean's name
379      */

380     public String JavaDoc getEjbName() {
381         return dd.getEjbName();
382     }
383
384     /**
385      * @return Return the file name of the generated source for the Home (null
386      * if none)
387      */

388     public String JavaDoc getWrpHomeFileName() {
389         return wrpHomeFileName;
390     }
391
392     /**
393      * @return Return the file name of the cluster configuration for the Home
394      * (null if none)
395      */

396     public String JavaDoc getWrpHomeClusterFileName() {
397         return wrpHomeClusterFileName;
398     }
399
400     /**
401      * @return Return the file name of the generated source for the Remote (null
402      * if none)
403      */

404     public String JavaDoc getWrpRemoteFileName() {
405         return wrpRemoteFileName;
406     }
407
408     /**
409      * @return Return the class name of the generated source for the Remote
410      * (package included)
411      */

412     public String JavaDoc getWrpRemoteClassName() {
413         return dd.getFullWrpRemoteName();
414     }
415
416     /**
417      * @return Return the file name of the generated source for the
418      * ServiceEndpoint (null if none)
419      */

420     public String JavaDoc getWrpServiceEndpointFileName() {
421         return wrpServiceEndpointFileName;
422     }
423
424     /**
425      * @return Return the file name of the generated source for the
426      * ServiceEndpointHome (null if none)
427      */

428     public String JavaDoc getWrpSEHomeFileName() {
429         return wrpSEHomeFileName;
430     }
431
432     /**
433      * @return Return the class name of the generated source for the
434      * ServiceEndpoint (package included)
435      */

436     public String JavaDoc getWrpServiceEndpointClassName() {
437         if (dd instanceof SessionStatelessDesc) {
438             return ((SessionStatelessDesc) dd).getFullWrpServiceEndpointName();
439         }
440         return null;
441     }
442
443     /**
444      * @return Return the class name of the generated source for the Home
445      * (package included)
446      */

447     public String JavaDoc getWrpHomeClassName() {
448         return dd.getFullWrpHomeName();
449     }
450
451     /**
452      * @return Returns the list of the sources that are not Remote
453      */

454     public Collection JavaDoc getNoRemoteJavas() {
455         return noRemoteJava;
456     }
457 }
Popular Tags