1 19 20 package org.netbeans.modules.j2ee.ejbcore.api.codegeneration; 21 22 import com.sun.source.tree.ClassTree; 23 import org.netbeans.modules.j2ee.ejbcore.EjbGenerationUtil; 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import org.netbeans.api.java.source.JavaSource; 28 import org.netbeans.api.java.source.WorkingCopy; 29 import org.netbeans.api.project.FileOwnerQuery; 30 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 31 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans; 32 import org.netbeans.modules.j2ee.dd.api.ejb.Session; 33 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.modules.j2ee.common.source.AbstractTask; 36 import org.netbeans.modules.j2ee.common.source.GenerationUtils; 37 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor; 38 import org.netbeans.modules.j2ee.dd.api.ejb.ContainerTransaction; 39 import org.netbeans.modules.j2ee.ejbcore.naming.EJBNameOptions; 40 import org.openide.filesystems.FileObject; 41 42 47 public final class SessionGenerator { 48 49 public static final String EJB21_EJBCLASS = "Templates/J2EE/EJB21/SessionEjbClass.java"; public static final String EJB21_LOCAL = "Templates/J2EE/EJB21/SessionLocal.java"; public static final String EJB21_LOCALHOME = "Templates/J2EE/EJB21/SessionLocalHome.java"; public static final String EJB21_REMOTE = "Templates/J2EE/EJB21/SessionRemote.java"; public static final String EJB21_REMOTEHOME = "Templates/J2EE/EJB21/SessionRemoteHome.java"; 55 public static final String EJB30_STATELESS_EJBCLASS = "Templates/J2EE/EJB30/StatelessEjbClass.java"; public static final String EJB30_STATEFUL_EJBCLASS = "Templates/J2EE/EJB30/StatefulEjbClass.java"; public static final String EJB30_LOCAL = "Templates/J2EE/EJB30/SessionLocal.java"; public static final String EJB30_REMOTE = "Templates/J2EE/EJB30/SessionRemote.java"; 60 private final FileObject pkg; 62 private final boolean hasRemote; 63 private final boolean hasLocal; 64 private final boolean isStateful; 65 private final boolean isSimplified; 66 private final boolean isXmlBased; 68 69 private final EJBNameOptions ejbNameOptions; 71 private final String ejbName; 72 private final String ejbClassName; 73 private final String remoteName; 74 private final String remoteHomeName; 75 private final String localName; 76 private final String localHomeName; 77 private final String displayName; 78 79 private final String packageName; 80 private final String packageNameWithDot; 81 82 private final Map <String , String > templateParameters; 83 84 public static SessionGenerator create(String wizardTargetName, FileObject pkg, boolean hasRemote, boolean hasLocal, 85 boolean isStateful, boolean isSimplified, boolean hasBusinessInterface, boolean isXmlBased) { 86 return new SessionGenerator(wizardTargetName, pkg, hasRemote, hasLocal, isStateful, isSimplified, hasBusinessInterface, isXmlBased); 87 } 88 89 private SessionGenerator(String wizardTargetName, FileObject pkg, boolean hasRemote, boolean hasLocal, 90 boolean isStateful, boolean isSimplified, boolean hasBusinessInterface, boolean isXmlBased) { 91 this.pkg = pkg; 92 this.hasRemote = hasRemote; 93 this.hasLocal = hasLocal; 94 this.isStateful = isStateful; 95 this.isSimplified = isSimplified; 96 this.isXmlBased = isXmlBased; 98 this.ejbNameOptions = new EJBNameOptions(); 99 this.ejbName = ejbNameOptions.getSessionEjbNamePrefix() + wizardTargetName + ejbNameOptions.getSessionEjbNameSuffix(); 100 this.ejbClassName = ejbNameOptions.getSessionEjbClassPrefix() + wizardTargetName + ejbNameOptions.getSessionEjbClassSuffix(); 101 this.remoteName = ejbNameOptions.getSessionRemotePrefix() + wizardTargetName + ejbNameOptions.getSessionRemoteSuffix(); 102 this.remoteHomeName = ejbNameOptions.getSessionRemoteHomePrefix() + wizardTargetName + ejbNameOptions.getSessionRemoteHomeSuffix(); 103 this.localName = ejbNameOptions.getSessionLocalPrefix() + wizardTargetName + ejbNameOptions.getSessionLocalSuffix(); 104 this.localHomeName = ejbNameOptions.getSessionLocalHomePrefix() + wizardTargetName + ejbNameOptions.getSessionLocalHomeSuffix(); 105 this.displayName = ejbNameOptions.getSessionDisplayNamePrefix() + wizardTargetName + ejbNameOptions.getSessionDisplayNameSuffix(); 106 this.packageName = EjbGenerationUtil.getSelectedPackageName(pkg); 107 this.packageNameWithDot = packageName + "."; 108 this.templateParameters = new HashMap <String , String >(); 109 this.templateParameters.put("package", packageName); 111 this.templateParameters.put("localInterface", packageNameWithDot + localName); 112 this.templateParameters.put("remoteInterface", packageNameWithDot + remoteName); 113 } 114 115 public FileObject generate() throws IOException { 116 FileObject resultFileObject = null; 117 if (isSimplified) { 118 resultFileObject = generateEJB30Classes(); 119 120 Project project = FileOwnerQuery.getOwner(pkg); 125 J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class); 126 j2eeModuleProvider.getConfigSupport().ensureConfigurationReady(); 127 128 if (isXmlBased) { 129 generateEJB30Xml(); 130 } 131 } else { 132 resultFileObject = generateEJB21Classes(); 133 134 Project project = FileOwnerQuery.getOwner(pkg); 139 J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class); 140 j2eeModuleProvider.getConfigSupport().ensureConfigurationReady(); 141 142 if (isXmlBased) { 143 generateEJB21Xml(); 144 } 145 } 146 return resultFileObject; 147 } 148 149 private FileObject generateEJB21Classes() throws IOException { 150 FileObject ejbClassFO = GenerationUtils.createClass(EJB21_EJBCLASS, pkg, ejbClassName, null, templateParameters); 151 if (hasRemote) { 152 GenerationUtils.createClass(EJB21_REMOTE, pkg, remoteName, null, templateParameters); 153 GenerationUtils.createClass(EJB21_REMOTEHOME, pkg, remoteHomeName, null, templateParameters); 154 } 155 if (hasLocal) { 156 GenerationUtils.createClass(EJB21_LOCAL, pkg, localName, null, templateParameters); 157 GenerationUtils.createClass(EJB21_LOCALHOME, pkg, localHomeName, null, templateParameters); 158 } 159 return ejbClassFO; 160 } 161 162 private FileObject generateEJB30Classes() throws IOException { 163 String ejbClassTemplateName = isStateful ? EJB30_STATEFUL_EJBCLASS : EJB30_STATELESS_EJBCLASS; 164 FileObject ejbClassFO = GenerationUtils.createClass(ejbClassTemplateName, pkg, ejbClassName, null, templateParameters); 165 if (hasRemote) { 166 GenerationUtils.createClass(EJB30_REMOTE, pkg, remoteName, null, templateParameters); 167 } 168 if (hasLocal) { 169 GenerationUtils.createClass(EJB30_LOCAL, pkg, localName, null, templateParameters); 170 } 171 JavaSource javaSource = JavaSource.forFileObject(ejbClassFO); 172 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 173 public void run(WorkingCopy workingCopy) throws IOException { 174 workingCopy.toPhase(JavaSource.Phase.RESOLVED); 175 GenerationUtils generationUtils = GenerationUtils.newInstance(workingCopy); 176 ClassTree classTree = generationUtils.getClassTree(); 177 ClassTree newClassTree = classTree; 178 if (hasRemote) { 179 newClassTree = generationUtils.addImplementsClause(newClassTree, packageNameWithDot + remoteName); 180 } 181 if (hasLocal) { 182 newClassTree = generationUtils.addImplementsClause(newClassTree, packageNameWithDot + localName); 183 } 184 workingCopy.rewrite(classTree, newClassTree); 185 } 186 }).commit(); 187 return ejbClassFO; 188 } 189 190 private void generateEJB21Xml() throws IOException { 191 org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(pkg); 192 org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJar = DDProvider.getDefault().getMergedDDRoot(ejbModule.getMetadataUnit()); 193 EnterpriseBeans beans = ejbJar.getEnterpriseBeans(); 194 Session session = null; 195 if (beans == null) { 196 beans = ejbJar.newEnterpriseBeans(); 197 ejbJar.setEnterpriseBeans(beans); 198 } 199 session = beans.newSession(); 200 session.setEjbName(ejbName); 201 session.setDisplayName(displayName); 202 session.setEjbClass(packageNameWithDot + ejbClassName); 203 204 if (hasRemote) { 205 session.setRemote(packageNameWithDot + remoteName); 206 session.setHome(packageNameWithDot + remoteHomeName); 207 } 208 if (hasLocal) { 209 session.setLocal(packageNameWithDot + localName); 210 session.setLocalHome(packageNameWithDot + localHomeName); 211 } 212 String sessionType = Session.SESSION_TYPE_STATELESS; 213 if (isStateful) { 214 sessionType = Session.SESSION_TYPE_STATEFUL; 215 } 216 session.setSessionType(sessionType); 217 session.setTransactionType("Container"); beans.addSession(session); 219 AssemblyDescriptor assemblyDescriptor = ejbJar.getSingleAssemblyDescriptor(); 221 if (assemblyDescriptor == null) { 222 assemblyDescriptor = ejbJar.newAssemblyDescriptor(); 223 ejbJar.setAssemblyDescriptor(assemblyDescriptor); 224 } 225 ContainerTransaction containerTransaction = assemblyDescriptor.newContainerTransaction(); 226 containerTransaction.setTransAttribute("Required"); org.netbeans.modules.j2ee.dd.api.ejb.Method method = containerTransaction.newMethod(); 228 method.setEjbName(ejbName); 229 method.setMethodName("*"); containerTransaction.addMethod(method); 231 assemblyDescriptor.addContainerTransaction(containerTransaction); 232 ejbJar.write(ejbModule.getDeploymentDescriptor()); 233 } 234 235 private void generateEJB30Xml() throws IOException { 236 throw new UnsupportedOperationException ("Method not implemented yet."); 237 } 238 239 260 } 261 | Popular Tags |