1 25 26 package org.objectweb.jonas_ejb.genic; 27 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.io.IOException ; 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 59 class Source { 60 61 64 static final int HOME = 0; 65 68 static final int LOCAL_HOME = 1; 69 72 static final int REMOTE = 2; 73 76 static final int LOCAL = 3; 77 80 static final int ENTITY_HANDLE = 4; 81 84 static final int ENTITY_CMP_JDBC = 5; 85 88 static final int ITF_COH_CMP2_ENTITY = 7; 89 92 static final int CLUSTER_HOME = 8; 93 96 static final int SERVICE_ENDPOINT = 9; 97 100 static final int SERVICE_ENDPOINT_HOME = 10; 101 102 105 private BeanDesc dd = null; 106 107 110 private String srcFileName = null; 111 112 115 private int srcType; 116 117 120 private VelocityEngine vEngine = null; 121 122 125 private VelocityContext vctx = null; 126 127 130 private Logger logger = null; 131 132 140 Source(BeanDesc beanDesc, String 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 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 159 void generate() throws GenICException { 160 161 String tmplName = null; 162 Template tmpl = null; 163 FileWriter 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 e) { 240 throw new GenICException("Cannot get the '" + tmplName + "' template file", e); 241 } 242 243 try { 244 File fs = new File (srcFileName); 246 File fdir = fs.getParentFile(); 247 if (fdir != null) { 248 if (!fdir.exists()) { 249 if (!fdir.mkdirs()) { 250 throw new IOException ("Cannot create the directory '" + fdir.getPath() + "'"); 251 } 252 } 253 } 254 fwriter = new FileWriter (srcFileName); 255 } catch (IOException 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 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 e) { 271 throw new GenICException("Cannot close the '" + srcFileName + "' source file", e); 272 } 273 } 274 275 } | Popular Tags |