001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2006 Bull S.A.S.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: Source.java 9745 2006-10-16 13:07:08Z japaz $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.genic;
025:
026: import java.io.File;
027: import java.io.FileWriter;
028: import java.io.IOException;
029:
030: import org.apache.velocity.Template;
031: import org.apache.velocity.VelocityContext;
032: import org.apache.velocity.app.VelocityEngine;
033:
034: import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
035: import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
036: import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp1Desc;
037: import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp2Desc;
038: import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc;
039: import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
040:
041: import org.objectweb.jonas.common.Log;
042:
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: /**
047: * This class allows to generate the source of : the class that implements the
048: * Enterprise bean's remote interface, the class that implements the Enterprise
049: * bean's home interface, the class that implements the Enterprise bean's local
050: * interface, the class that implements the Enterprise bean's localhome
051: * interface, the class of the Entity Handle in case of entity, or the extended
052: * class of the Bean for persistence in case of entity with CMP, via a Velocity
053: * template of the given Enterprise Java Bean.
054: * @author Helene Joanin : Initial developer
055: */
056: class Source {
057:
058: /**
059: * home source
060: */
061: static final int HOME = 0;
062: /**
063: * local home source
064: */
065: static final int LOCAL_HOME = 1;
066: /**
067: * remote source
068: */
069: static final int REMOTE = 2;
070: /**
071: * local source
072: */
073: static final int LOCAL = 3;
074: /**
075: * entity handle source
076: */
077: static final int ENTITY_HANDLE = 4;
078: /**
079: * CMP1 or CMP2 entity bean source
080: */
081: static final int ENTITY_CMP_JDBC = 5;
082: /**
083: * interface coherence source for CMP2 entity
084: */
085: static final int ITF_COH_CMP2_ENTITY = 7;
086:
087: /**
088: * cluster home source
089: */
090: static final int CLUSTER_HOME = 8;
091: /**
092: * service endpoint source
093: */
094: static final int SERVICE_ENDPOINT = 9;
095: /**
096: * service endpoint home source
097: */
098: static final int SERVICE_ENDPOINT_HOME = 10;
099:
100: /**
101: * cluster remote source
102: */
103: static final int CLUSTER_REMOTE = 11;
104:
105: /**
106: * cluster home source for SFSB Fail Over
107: */
108: static final int CLUSTER_HOME_SFSB = 12;
109:
110: /**
111: * cluster remote source for SFSB Fail Over
112: */
113: static final int CLUSTER_REMOTE_SFSB = 13;
114:
115: /**
116: * cluster home source for Entity Fail Over
117: */
118: static final int CLUSTER_HOME_ENTITY = 14;
119:
120: /**
121: * cluster remote source for Entity Fail Over
122: */
123: static final int CLUSTER_REMOTE_ENTITY = 15;
124:
125: /**
126: * Disabled the clustering for this bean
127: */
128: static final String CLUSTER_DISABLED = "disabled";
129:
130: /**
131: * Bean Deployment Description
132: */
133: private BeanDesc dd = null;
134:
135: /**
136: * Name of the generated source file
137: */
138: private String srcFileName = null;
139:
140: /**
141: * Generated source file type
142: */
143: private int srcType;
144:
145: /**
146: * Velocity engine
147: */
148: private VelocityEngine vEngine = null;
149:
150: /**
151: * Velocity context
152: */
153: private VelocityContext vctx = null;
154:
155: /**
156: * logger
157: */
158: private Logger logger = null;
159:
160: /**
161: * Source Constructor
162: * @param beanDesc deployment descriptor of the bean
163: * @param fileName name of the source file to generate
164: * @param type source's type (HOME, LOCAL_HOME, REMOTE, ...)
165: * @param ve Velocity engine to use
166: * @exception GenICException In error case
167: */
168: Source(BeanDesc beanDesc, String fileName, int type,
169: VelocityEngine ve) throws GenICException {
170: dd = beanDesc;
171: srcFileName = fileName;
172: srcType = type;
173: vctx = VContextFactory.create(dd, srcType);
174: vEngine = ve;
175: logger = Log.getLogger(Log.JONAS_GENIC_PREFIX);
176: // Trace of the Velocity Context
177: Logger vLogger = Log.getLogger(Log.JONAS_GENIC_VELOCITY_PREFIX);
178: if (vLogger.getCurrentIntLevel() == BasicLevel.DEBUG) {
179: vLogger.log(BasicLevel.DEBUG, "Source(..,fileName="
180: + fileName + ", type = " + type + ", ..)");
181: vLogger.log(BasicLevel.DEBUG, "VELOCITY CONTEXT = \n("
182: + VContextFactory.toString(vctx) + "\n)");
183: }
184: }
185:
186: /**
187: * Generates the java source
188: * @throws GenICException in error case
189: */
190: void generate() throws GenICException {
191:
192: String tmplName = null;
193: Template tmpl = null;
194: FileWriter fwriter = null;
195:
196: switch (srcType) {
197: case CLUSTER_HOME:
198: if (dd.getClusterHomeDistributor() != null) {
199: tmplName = dd.getClusterHomeDistributor();
200: } else {
201: if (dd.isClusterReplicated()) {
202: tmplName = "ClusterHomeSLSBRepDistributor.vm";
203: } else {
204: tmplName = "ClusterHomeDistributor.vm";
205: }
206: }
207: break;
208:
209: case CLUSTER_REMOTE:
210: if (dd.getClusterRemoteDistributor() != null) {
211: tmplName = dd.getClusterRemoteDistributor();
212: } else {
213: if (dd.isClusterReplicated()) {
214: tmplName = "ClusterRemoteSLSBRepDistributor.vm";
215: } else {
216: tmplName = "ClusterRemoteSLSBDistributor.vm";
217: }
218: }
219: break;
220:
221: case CLUSTER_HOME_SFSB:
222:
223: // If the replication is enabled, use by default the right velocity template
224: if (dd.getClusterHomeDistributor() != null) {
225: tmplName = dd.getClusterHomeDistributor();
226: } else {
227: if (dd.isClusterReplicated()) {
228: tmplName = "ClusterHomeSFSBRepDistributor.vm";
229: } else {
230: tmplName = "ClusterHomeDistributor.vm";
231: }
232: }
233: break;
234:
235: case CLUSTER_REMOTE_SFSB:
236: // if the replication is not enabled, the LB/FO are disabled at the remote interface
237: // for the SFSB (sticky session)
238: if (dd.isClusterReplicated()) {
239: if (dd.getClusterRemoteDistributor() != null) {
240: tmplName = dd.getClusterRemoteDistributor();
241: } else {
242: tmplName = "ClusterRemoteSFSBRepDistributor.vm";
243: }
244: } else {
245: tmplName = "";
246: }
247:
248: break;
249:
250: case CLUSTER_HOME_ENTITY:
251:
252: // If the replication is enabled, use by default the right velocity template
253: if (dd.getClusterHomeDistributor() != null) {
254: tmplName = dd.getClusterHomeDistributor();
255: } else {
256: if (dd.isClusterReplicated()) {
257: tmplName = "ClusterHomeEntityRepDistributor.vm";
258: } else {
259: tmplName = "ClusterHomeDistributor.vm";
260: }
261: }
262: break;
263:
264: case CLUSTER_REMOTE_ENTITY:
265: // if the replication is not enabled, the LB/FO are disabled at the remote interface
266: // for the SFSB (sticky session)
267: if (dd.isClusterReplicated()) {
268: if (dd.getClusterRemoteDistributor() != null) {
269: tmplName = dd.getClusterRemoteDistributor();
270: } else {
271: tmplName = "ClusterRemoteEntityRepDistributor.vm";
272: }
273: } else {
274: tmplName = "";
275: }
276:
277: break;
278:
279: case HOME:
280: if (dd instanceof EntityDesc) {
281: tmplName = "JEntityHome.vm";
282: } else if (dd instanceof SessionStatefulDesc) {
283: tmplName = "JStatefulHome.vm";
284: } else if (dd instanceof SessionStatelessDesc) {
285: tmplName = "JStatelessHome.vm";
286: }
287: break;
288: case LOCAL_HOME:
289: if (dd instanceof EntityDesc) {
290: tmplName = "JEntityLocalHome.vm";
291: } else if (dd instanceof SessionStatefulDesc) {
292: tmplName = "JStatefulLocalHome.vm";
293: } else if (dd instanceof SessionStatelessDesc) {
294: tmplName = "JStatelessLocalHome.vm";
295: }
296: break;
297: case REMOTE:
298: if (dd instanceof EntityDesc) {
299: tmplName = "JEntityRemote.vm";
300: } else if (dd instanceof SessionStatefulDesc) {
301: tmplName = "JStatefulRemote.vm";
302: } else if (dd instanceof SessionStatelessDesc) {
303: tmplName = "JStatelessRemote.vm";
304: }
305: break;
306: case LOCAL:
307: if (dd instanceof EntityDesc) {
308: tmplName = "JEntityLocal.vm";
309: } else if (dd instanceof SessionStatefulDesc) {
310: tmplName = "JStatefulLocal.vm";
311: } else if (dd instanceof SessionStatelessDesc) {
312: tmplName = "JStatelessLocal.vm";
313: }
314: break;
315: case SERVICE_ENDPOINT:
316: if (dd instanceof SessionStatelessDesc) {
317: tmplName = "JServiceEndpoint.vm";
318: }
319: break;
320: case SERVICE_ENDPOINT_HOME:
321: if (dd instanceof SessionStatelessDesc) {
322: tmplName = "JServiceEndpointHome.vm";
323: }
324: break;
325: case ENTITY_HANDLE:
326: if (dd instanceof EntityDesc) {
327: tmplName = "JEntityHandle.vm";
328: }
329: break;
330: case ENTITY_CMP_JDBC:
331: if (dd instanceof EntityJdbcCmp1Desc) {
332: tmplName = "JEntityCmpJdbc.vm";
333: }
334: if (dd instanceof EntityJdbcCmp2Desc) {
335: tmplName = "JEntityCmp2.vm";
336: }
337: break;
338: case ITF_COH_CMP2_ENTITY:
339: if (dd instanceof EntityJdbcCmp2Desc) {
340: tmplName = "JEntityCmp2CoherenceItf.vm";
341: }
342: break;
343: default:
344: break;
345: }
346: if (tmplName == null) {
347: throw new GenICException("No template for '" + srcFileName
348: + " !!!'");
349: }
350:
351: try {
352: tmpl = vEngine.getTemplate(tmplName);
353: } catch (Exception e) {
354: throw new GenICException("Cannot get the '" + tmplName
355: + "' template file", e);
356: }
357:
358: try {
359: // Create before the parent directory
360: File fs = new File(srcFileName);
361: File fdir = fs.getParentFile();
362: if (fdir != null) {
363: if (!fdir.exists()) {
364: if (!fdir.mkdirs()) {
365: throw new IOException(
366: "Cannot create the directory '"
367: + fdir.getPath() + "'");
368: }
369: }
370: }
371: fwriter = new FileWriter(srcFileName);
372: } catch (IOException e) {
373: e.printStackTrace();
374: throw new GenICException("Cannot create the '"
375: + srcFileName + "' source file", e);
376: }
377: logger
378: .log(BasicLevel.DEBUG, "Generate the file "
379: + srcFileName);
380: try {
381: tmpl.merge(vctx, fwriter);
382: } catch (Exception e) {
383: throw new GenICException("Cannot generate the '"
384: + srcFileName + "' source from the '" + tmplName
385: + "' template", e);
386: }
387:
388: try {
389: fwriter.flush();
390: fwriter.close();
391: } catch (IOException e) {
392: throw new GenICException("Cannot close the '" + srcFileName
393: + "' source file", e);
394: }
395: }
396:
397: }
|