KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > api > codegeneration > SessionGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
43  * Generator of Session EJBs for EJB 2.1 and 3.0
44  *
45  * @author Martin Adamek
46  */

47 public final class SessionGenerator {
48     
49     public static final String JavaDoc EJB21_EJBCLASS = "Templates/J2EE/EJB21/SessionEjbClass.java"; // NOI18N
50
public static final String JavaDoc EJB21_LOCAL = "Templates/J2EE/EJB21/SessionLocal.java"; // NOI18N
51
public static final String JavaDoc EJB21_LOCALHOME = "Templates/J2EE/EJB21/SessionLocalHome.java"; // NOI18N
52
public static final String JavaDoc EJB21_REMOTE = "Templates/J2EE/EJB21/SessionRemote.java"; // NOI18N
53
public static final String JavaDoc EJB21_REMOTEHOME = "Templates/J2EE/EJB21/SessionRemoteHome.java"; // NOI18N
54

55     public static final String JavaDoc EJB30_STATELESS_EJBCLASS = "Templates/J2EE/EJB30/StatelessEjbClass.java"; // NOI18N
56
public static final String JavaDoc EJB30_STATEFUL_EJBCLASS = "Templates/J2EE/EJB30/StatefulEjbClass.java"; // NOI18N
57
public static final String JavaDoc EJB30_LOCAL = "Templates/J2EE/EJB30/SessionLocal.java"; // NOI18N
58
public static final String JavaDoc EJB30_REMOTE = "Templates/J2EE/EJB30/SessionRemote.java"; // NOI18N
59

60     // informations collected in wizard
61
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 hasBusinessInterface;
67
private final boolean isXmlBased;
68     
69     // EJB naming options
70
private final EJBNameOptions ejbNameOptions;
71     private final String JavaDoc ejbName;
72     private final String JavaDoc ejbClassName;
73     private final String JavaDoc remoteName;
74     private final String JavaDoc remoteHomeName;
75     private final String JavaDoc localName;
76     private final String JavaDoc localHomeName;
77     private final String JavaDoc displayName;
78     
79     private final String JavaDoc packageName;
80     private final String JavaDoc packageNameWithDot;
81
82     private final Map JavaDoc<String JavaDoc, String JavaDoc> templateParameters;
83
84     public static SessionGenerator create(String JavaDoc 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 JavaDoc 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.hasBusinessInterface = hasBusinessInterface;
97
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 JavaDoc<String JavaDoc, String JavaDoc>();
109         // fill all possible template parameters
110
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 JavaDoc {
116         FileObject resultFileObject = null;
117         if (isSimplified) {
118             resultFileObject = generateEJB30Classes();
119             
120             //put these lines in a common function at the appropriate place after EA1
121
//something like public EjbJar getEjbJar()
122
//This method will be used whereever we construct/get DD object graph to ensure
123
//corresponding config listners attached to it.
124
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             //put these lines in a common function at the appropriate place after EA1
135
//something like public EjbJar getEjbJar()
136
//This method will be used whereever we construct/get DD object graph to ensure
137
//corresponding config listners attached to it.
138
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 JavaDoc {
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 JavaDoc {
163         String JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc sessionType = Session.SESSION_TYPE_STATELESS;
213         if (isStateful) {
214             sessionType = Session.SESSION_TYPE_STATEFUL;
215         }
216         session.setSessionType(sessionType);
217         session.setTransactionType("Container"); // NOI18N
218
beans.addSession(session);
219         // add transaction requirements
220
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"); //NOI18N
227
org.netbeans.modules.j2ee.dd.api.ejb.Method method = containerTransaction.newMethod();
228         method.setEjbName(ejbName);
229         method.setMethodName("*"); //NOI18N
230
containerTransaction.addMethod(method);
231         assemblyDescriptor.addContainerTransaction(containerTransaction);
232         ejbJar.write(ejbModule.getDeploymentDescriptor());
233     }
234     
235     private void generateEJB30Xml() throws IOException JavaDoc {
236         throw new UnsupportedOperationException JavaDoc("Method not implemented yet.");
237     }
238     
239       //TODO: RETOUCHE WS
240
// /**
241
// * Special case for generating a Session implementation bean for web services
242
// */
243
// public String generateWebServiceImplBean(String ejbName, FileObject pkg, Project project, String delegateData) throws java.io.IOException {
244
// String pkgName = EjbGenerationUtil.getSelectedPackageName(pkg, project);
245
// Bean b = genUtil.getDefaultBean();
246
// b.setCommentDataEjbName(ejbName);
247
// b.setClassname(true);
248
// b.setClassnameName(EjbGenerationUtil.getBeanClassName(ejbName)); //NOI18N
249
// b.setDelegateData(delegateData);
250
// if (pkgName!=null) {
251
// b.setClassnamePackage(pkgName);
252
// }
253
//
254
// // generate bean class
255
// EjbJar ejbModule = EjbJar.getEjbJar(pkg);
256
// boolean simplified = ejbModule.getJ2eePlatformVersion().equals(J2eeModule.JAVA_EE_5);
257
// return null;//genUtil.generateBeanClass(simplified ? SESSION_TEMPLATE_WS_JAVAEE5 : SESSION_TEMPLATE, b, pkgName, pkg);
258
// }
259

260 }
261
Popular Tags