Source.java

00001 
00026 package org.objectweb.jonas_ejb.genic;
00027 
00028 import java.io.File;
00029 import java.io.FileWriter;
00030 import java.io.IOException;
00031 
00032 import org.apache.velocity.Template;
00033 import org.apache.velocity.VelocityContext;
00034 import org.apache.velocity.app.VelocityEngine;
00035 
00036 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
00037 import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
00038 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp1Desc;
00039 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp2Desc;
00040 import org.objectweb.jonas_ejb.deployment.api.SessionDesc;
00041 import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc;
00042 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
00043 
00044 import org.objectweb.jonas.common.Log;
00045 
00046 import org.objectweb.util.monolog.api.BasicLevel;
00047 import org.objectweb.util.monolog.api.Logger;
00048 
00059 class Source {
00060 
00064     static final int HOME = 0;
00068     static final int LOCAL_HOME = 1;
00072     static final int REMOTE = 2;
00076     static final int LOCAL = 3;
00080     static final int ENTITY_HANDLE = 4;
00084     static final int ENTITY_CMP_JDBC = 5;
00088     static final int ITF_COH_CMP2_ENTITY = 7;
00092     static final int CLUSTER_HOME = 8;
00096     static final int SERVICE_ENDPOINT = 9;
00100     static final int SERVICE_ENDPOINT_HOME = 10;
00101 
00105     private BeanDesc dd = null;
00106 
00110     private String srcFileName = null;
00111 
00115     private int srcType;
00116 
00120     private VelocityEngine vEngine = null;
00121 
00125     private VelocityContext vctx = null;
00126 
00130     private Logger logger = null;
00131 
00140     Source(BeanDesc beanDesc, String fileName, int type, VelocityEngine ve) throws GenICException {
00141         dd = beanDesc;
00142         srcFileName = fileName;
00143         srcType = type;
00144         vctx = VContextFactory.create(dd, srcType);
00145         vEngine = ve;
00146         logger = Log.getLogger(Log.JONAS_GENIC_PREFIX);
00147         // Trace of the Velocity Context
00148         Logger vLogger = Log.getLogger(Log.JONAS_GENIC_VELOCITY_PREFIX);
00149         if (vLogger.getCurrentIntLevel() == BasicLevel.DEBUG) {
00150             vLogger.log(BasicLevel.DEBUG, "Source(..,fileName=" + fileName + ", type = " + type + ", ..)");
00151             vLogger.log(BasicLevel.DEBUG, "VELOCITY CONTEXT = \n(" + VContextFactory.toString(vctx) + "\n)");
00152         }
00153     }
00154 
00159     void generate() throws GenICException {
00160 
00161         String tmplName = null;
00162         Template tmpl = null;
00163         FileWriter fwriter = null;
00164 
00165         switch (srcType) {
00166             case CLUSTER_HOME:
00167                 if (dd instanceof EntityDesc) {
00168                     tmplName = "JEntityClusterHome.vm";
00169                 } else if (dd instanceof SessionStatefulDesc) {
00170                     tmplName = "JStatefulClusterHome.vm";
00171                 } else if (dd instanceof SessionStatelessDesc) {
00172                     tmplName = "JStatelessClusterHome.vm";
00173                 }
00174                 break;
00175             case HOME:
00176                 if (dd instanceof EntityDesc) {
00177                     tmplName = "JEntityHome.vm";
00178                 } else if (dd instanceof SessionStatefulDesc) {
00179                     tmplName = "JStatefulHome.vm";
00180                 } else if (dd instanceof SessionStatelessDesc) {
00181                     tmplName = "JStatelessHome.vm";
00182                 }
00183                 break;
00184             case LOCAL_HOME:
00185                 if (dd instanceof EntityDesc) {
00186                     tmplName = "JEntityLocalHome.vm";
00187                 } else if (dd instanceof SessionStatefulDesc) {
00188                     tmplName = "JStatefulLocalHome.vm";
00189                 } else if (dd instanceof SessionStatelessDesc) {
00190                     tmplName = "JStatelessLocalHome.vm";
00191                 }
00192                 break;
00193             case REMOTE:
00194                 if (dd instanceof EntityDesc) {
00195                     tmplName = "JEntityRemote.vm";
00196                 } else if (dd instanceof SessionDesc) {
00197                     tmplName = "JSessionRemote.vm";
00198                 }
00199                 break;
00200             case LOCAL:
00201                 if (dd instanceof EntityDesc) {
00202                     tmplName = "JEntityLocal.vm";
00203                 } else if (dd instanceof SessionDesc) {
00204                     tmplName = "JSessionLocal.vm";
00205                 }
00206                 break;
00207             case SERVICE_ENDPOINT:
00208                 if (dd instanceof SessionStatelessDesc) {
00209                     tmplName = "JServiceEndpoint.vm";
00210                 }
00211                 break;
00212             case SERVICE_ENDPOINT_HOME:
00213                 if (dd instanceof SessionStatelessDesc) {
00214                     tmplName = "JServiceEndpointHome.vm";
00215                 }
00216                 break;
00217             case ENTITY_HANDLE:
00218                 if (dd instanceof EntityDesc) {
00219                     tmplName = "JEntityHandle.vm";
00220                 }
00221                 break;
00222             case ENTITY_CMP_JDBC:
00223                 if (dd instanceof EntityJdbcCmp1Desc) {
00224                     tmplName = "JEntityCmpJdbc.vm";
00225                 }
00226                 if (dd instanceof EntityJdbcCmp2Desc) {
00227                     tmplName = "JEntityCmp2.vm";
00228                 }
00229                 break;
00230             case ITF_COH_CMP2_ENTITY:
00231                 if (dd instanceof EntityJdbcCmp2Desc) {
00232                     tmplName = "JEntityCmp2CoherenceItf.vm";
00233                 }
00234                 break;
00235             default:
00236                 break;
00237         }
00238         if (tmplName == null) {
00239             throw new GenICException("No template for  '" + srcFileName + " !!!'");
00240         }
00241 
00242         try {
00243             tmpl = vEngine.getTemplate(tmplName);
00244         } catch (Exception e) {
00245             throw new GenICException("Cannot get the '" + tmplName + "' template file", e);
00246         }
00247 
00248         try {
00249             // Create before the parent directory
00250             File fs = new File(srcFileName);
00251             File fdir = fs.getParentFile();
00252             if (fdir != null) {
00253                 if (!fdir.exists()) {
00254                     if (!fdir.mkdirs()) {
00255                         throw new IOException("Cannot create the directory '" + fdir.getPath() + "'");
00256                     }
00257                 }
00258             }
00259             fwriter = new FileWriter(srcFileName);
00260         } catch (IOException e) {
00261             e.printStackTrace();
00262             throw new GenICException("Cannot create the '" + srcFileName + "' source file", e);
00263         }
00264         logger.log(BasicLevel.DEBUG, "Generate the file " + srcFileName);
00265         try {
00266             tmpl.merge(vctx, fwriter);
00267         } catch (Exception e) {
00268             throw new GenICException("Cannot generate the '" + srcFileName + "' source from the '" + tmplName
00269                     + "' template", e);
00270         }
00271 
00272         try {
00273             fwriter.flush();
00274             fwriter.close();
00275         } catch (IOException e) {
00276             throw new GenICException("Cannot close the '" + srcFileName + "' source file", e);
00277         }
00278     }
00279 
00280 }

Generated on Tue Feb 15 15:05:48 2005 for JOnAS by  doxygen 1.3.9.1