KickJava   Java API By Example, From Geeks To Geeks.

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


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: Source.java,v 1.22 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.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import org.apache.velocity.Template;
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.VelocityEngine;
35
36 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
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.EntityJdbcCmp2Desc;
40 import org.objectweb.jonas_ejb.deployment.api.SessionDesc;
41 import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc;
42 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
43
44 import org.objectweb.jonas.common.Log;
45
46 import org.objectweb.util.monolog.api.BasicLevel;
47 import org.objectweb.util.monolog.api.Logger;
48
49 /**
50  * This class allows to generate the source of : the class that implements the
51  * Enterprise bean's remote interface, the class that implements the Enterprise
52  * bean's home interface, the class that implements the Enterprise bean's local
53  * interface, the class that implements the Enterprise bean's localhome
54  * interface, the class of the Entity Handle in case of entity, or the extended
55  * class of the Bean for persistence in case of entity with CMP, via a Velocity
56  * template of the given Enterprise Java Bean.
57  * @author Helene Joanin : Initial developer
58  */

59 class Source {
60
61     /**
62      * home source
63      */

64     static final int HOME = 0;
65     /**
66      * local home source
67      */

68     static final int LOCAL_HOME = 1;
69     /**
70      * remote source
71      */

72     static final int REMOTE = 2;
73     /**
74      * local source
75      */

76     static final int LOCAL = 3;
77     /**
78      * entity handle source
79      */

80     static final int ENTITY_HANDLE = 4;
81     /**
82      * CMP1 or CMP2 entity bean source
83      */

84     static final int ENTITY_CMP_JDBC = 5;
85     /**
86      * interface coherence source for CMP2 entity
87      */

88     static final int ITF_COH_CMP2_ENTITY = 7;
89     /**
90      * cluster home source
91      */

92     static final int CLUSTER_HOME = 8;
93     /**
94      * service endpoint source
95      */

96     static final int SERVICE_ENDPOINT = 9;
97     /**
98      * service endpoint home source
99      */

100     static final int SERVICE_ENDPOINT_HOME = 10;
101
102     /**
103      * Bean Deployment Description
104      */

105     private BeanDesc dd = null;
106
107     /**
108      * Name of the generated source file
109      */

110     private String JavaDoc srcFileName = null;
111
112     /**
113      * Generated source file type
114      */

115     private int srcType;
116
117     /**
118      * Velocity engine
119      */

120     private VelocityEngine vEngine = null;
121
122     /**
123      * Velocity context
124      */

125     private VelocityContext vctx = null;
126
127     /**
128      * logger
129      */

130     private Logger logger = null;
131
132     /**
133      * Source Constructor
134      * @param beanDesc deployment descriptor of the bean
135      * @param fileName name of the source file to generate
136      * @param type source's type (HOME, LOCAL_HOME, REMOTE, ...)
137      * @param ve Velocity engine to use
138      * @exception GenICException In error case
139      */

140     Source(BeanDesc beanDesc, String JavaDoc fileName, int type, VelocityEngine ve) throws GenICException {
141         dd = beanDesc;
142         srcFileName = fileName;
143         srcType = type;
144         vctx = VContextFactory.create(dd, srcType);
145         vEngine = ve;
146         logger = Log.getLogger(Log.JONAS_GENIC_PREFIX);
147         // Trace of the Velocity Context
148
Logger vLogger = Log.getLogger(Log.JONAS_GENIC_VELOCITY_PREFIX);
149         if (vLogger.getCurrentIntLevel() == BasicLevel.DEBUG) {
150             vLogger.log(BasicLevel.DEBUG, "Source(..,fileName=" + fileName + ", type = " + type + ", ..)");
151             vLogger.log(BasicLevel.DEBUG, "VELOCITY CONTEXT = \n(" + VContextFactory.toString(vctx) + "\n)");
152         }
153     }
154
155     /**
156      * Generates the java source
157      * @throws GenICException in error case
158      */

159     void generate() throws GenICException {
160
161         String JavaDoc tmplName = null;
162         Template tmpl = null;
163         FileWriter JavaDoc fwriter = null;
164
165         switch (srcType) {
166             case CLUSTER_HOME:
167                 tmplName = "ClusterRRDistributor.vm";
168                 break;
169
170             case HOME:
171                 if (dd instanceof EntityDesc) {
172                     tmplName = "JEntityHome.vm";
173                 } else if (dd instanceof SessionStatefulDesc) {
174                     tmplName = "JStatefulHome.vm";
175                 } else if (dd instanceof SessionStatelessDesc) {
176                     tmplName = "JStatelessHome.vm";
177                 }
178                 break;
179             case LOCAL_HOME:
180                 if (dd instanceof EntityDesc) {
181                     tmplName = "JEntityLocalHome.vm";
182                 } else if (dd instanceof SessionStatefulDesc) {
183                     tmplName = "JStatefulLocalHome.vm";
184                 } else if (dd instanceof SessionStatelessDesc) {
185                     tmplName = "JStatelessLocalHome.vm";
186                 }
187                 break;
188             case REMOTE:
189                 if (dd instanceof EntityDesc) {
190                     tmplName = "JEntityRemote.vm";
191                 } else if (dd instanceof SessionDesc) {
192                     tmplName = "JSessionRemote.vm";
193                 }
194                 break;
195             case LOCAL:
196                 if (dd instanceof EntityDesc) {
197                     tmplName = "JEntityLocal.vm";
198                 } else if (dd instanceof SessionDesc) {
199                     tmplName = "JSessionLocal.vm";
200                 }
201                 break;
202             case SERVICE_ENDPOINT:
203                 if (dd instanceof SessionStatelessDesc) {
204                     tmplName = "JServiceEndpoint.vm";
205                 }
206                 break;
207             case SERVICE_ENDPOINT_HOME:
208                 if (dd instanceof SessionStatelessDesc) {
209                     tmplName = "JServiceEndpointHome.vm";
210                 }
211                 break;
212             case ENTITY_HANDLE:
213                 if (dd instanceof EntityDesc) {
214                     tmplName = "JEntityHandle.vm";
215                 }
216                 break;
217             case ENTITY_CMP_JDBC:
218                 if (dd instanceof EntityJdbcCmp1Desc) {
219                     tmplName = "JEntityCmpJdbc.vm";
220                 }
221                 if (dd instanceof EntityJdbcCmp2Desc) {
222                     tmplName = "JEntityCmp2.vm";
223                 }
224                 break;
225             case ITF_COH_CMP2_ENTITY:
226                 if (dd instanceof EntityJdbcCmp2Desc) {
227                     tmplName = "JEntityCmp2CoherenceItf.vm";
228                 }
229                 break;
230             default:
231                 break;
232         }
233         if (tmplName == null) {
234             throw new GenICException("No template for '" + srcFileName + " !!!'");
235         }
236
237         try {
238             tmpl = vEngine.getTemplate(tmplName);
239         } catch (Exception JavaDoc e) {
240             throw new GenICException("Cannot get the '" + tmplName + "' template file", e);
241         }
242
243         try {
244             // Create before the parent directory
245
File JavaDoc fs = new File JavaDoc(srcFileName);
246             File JavaDoc fdir = fs.getParentFile();
247             if (fdir != null) {
248                 if (!fdir.exists()) {
249                     if (!fdir.mkdirs()) {
250                         throw new IOException JavaDoc("Cannot create the directory '" + fdir.getPath() + "'");
251                     }
252                 }
253             }
254             fwriter = new FileWriter JavaDoc(srcFileName);
255         } catch (IOException JavaDoc e) {
256             e.printStackTrace();
257             throw new GenICException("Cannot create the '" + srcFileName + "' source file", e);
258         }
259         logger.log(BasicLevel.DEBUG, "Generate the file " + srcFileName);
260         try {
261             tmpl.merge(vctx, fwriter);
262         } catch (Exception JavaDoc e) {
263             throw new GenICException("Cannot generate the '" + srcFileName + "' source from the '" + tmplName
264                     + "' template", e);
265         }
266
267         try {
268             fwriter.flush();
269             fwriter.close();
270         } catch (IOException JavaDoc e) {
271             throw new GenICException("Cannot close the '" + srcFileName + "' source file", e);
272         }
273     }
274
275 }
Popular Tags