KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.netbeans.api.project.FileOwnerQuery;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.modules.j2ee.common.source.GenerationUtils;
28 import org.netbeans.modules.j2ee.dd.api.common.MessageDestination;
29 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
30 import org.netbeans.modules.j2ee.dd.api.ejb.ActivationConfig;
31 import org.netbeans.modules.j2ee.dd.api.ejb.ActivationConfigProperty;
32 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor;
33 import org.netbeans.modules.j2ee.dd.api.ejb.ContainerTransaction;
34 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
35 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
36 import org.netbeans.modules.j2ee.dd.api.ejb.MessageDriven;
37 import org.netbeans.modules.j2ee.dd.api.ejb.Method;
38 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
39 import org.netbeans.modules.j2ee.ejbcore.EjbGenerationUtil;
40 import org.netbeans.modules.j2ee.ejbcore.naming.EJBNameOptions;
41 import org.openide.ErrorManager;
42 import org.openide.filesystems.FileObject;
43
44 /**
45  * Generator of MessageDriven EJBs for EJB 2.1 and 3.0
46  *
47  * @author Martin Adamek
48  */

49 public final class MessageGenerator {
50
51     private static final String JavaDoc EJB21_EJBCLASS = "Templates/J2EE/EJB21/MessageDrivenEjbClass.java"; // NOI18N
52
private static final String JavaDoc EJB30_QUEUE_EJBCLASS = "Templates/J2EE/EJB30/MessageDrivenQueueEjbClass.java"; // NOI18N
53
private static final String JavaDoc EJB30_TOPIC_EJBCLASS = "Templates/J2EE/EJB30/MessageDrivenTopicEjbClass.java"; // NOI18N
54

55     // informations collected in wizard
56
private final FileObject pkg;
57     private final boolean isQueue;
58     private final boolean isSimplified;
59     private final boolean isXmlBased;
60
61     // EJB naming options
62
private final EJBNameOptions ejbNameOptions;
63     private final String JavaDoc ejbName;
64     private final String JavaDoc ejbClassName;
65     private final String JavaDoc displayName;
66     
67     private final String JavaDoc packageName;
68     private final String JavaDoc packageNameWithDot;
69     
70     private final Map JavaDoc<String JavaDoc, String JavaDoc> templateParameters;
71
72     public static MessageGenerator create(String JavaDoc wizardTargetName, FileObject pkg, boolean isQueue, boolean isSimplified, boolean isXmlBased) {
73         return new MessageGenerator(wizardTargetName, pkg, isQueue, isSimplified, isXmlBased);
74     }
75     
76     private MessageGenerator(String JavaDoc wizardTargetName, FileObject pkg, boolean isQueue, boolean isSimplified, boolean isXmlBased) {
77         this.pkg = pkg;
78         this.isQueue = isQueue;
79         this.isSimplified = isSimplified;
80         this.isXmlBased = isXmlBased;
81         this.ejbNameOptions = new EJBNameOptions();
82         this.ejbName = ejbNameOptions.getMessageDrivenEjbNamePrefix() + wizardTargetName + ejbNameOptions.getMessageDrivenEjbNameSuffix();
83         this.ejbClassName = ejbNameOptions.getMessageDrivenEjbClassPrefix() + wizardTargetName + ejbNameOptions.getMessageDrivenEjbClassSuffix();
84         this.displayName = ejbNameOptions.getMessageDrivenDisplayNamePrefix() + wizardTargetName + ejbNameOptions.getMessageDrivenDisplayNameSuffix();
85         this.packageName = EjbGenerationUtil.getSelectedPackageName(pkg);
86         this.packageNameWithDot = packageName + ".";
87         this.templateParameters = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
88         // fill all possible template parameters
89
this.templateParameters.put("package", packageName);
90     }
91     
92     public FileObject generate() throws IOException JavaDoc {
93         FileObject resultFileObject = null;
94         if (isSimplified) {
95             resultFileObject = generateEJB30Classes();
96             if (isXmlBased) {
97                 generateEJB30Xml();
98             }
99         } else {
100             resultFileObject = generateEJB21Classes();
101             if (isXmlBased) {
102                 try {
103                     generateEJB21Xml();
104                 } catch (VersionNotSupportedException ex) {
105                     ErrorManager.getDefault().notify(ex);
106                 }
107             }
108         }
109         return resultFileObject;
110     }
111     
112     private FileObject generateEJB21Classes() throws IOException JavaDoc {
113         FileObject resultFileObject = GenerationUtils.createClass(EJB21_EJBCLASS, pkg, ejbClassName, null, templateParameters);
114         ///
115
Project project = FileOwnerQuery.getOwner(pkg);
116         J2eeModuleProvider pwm = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
117         pwm.getConfigSupport().ensureConfigurationReady();
118         ///
119
return resultFileObject;
120     }
121     
122     private FileObject generateEJB30Classes() throws IOException JavaDoc {
123         String JavaDoc ejbClassTemplate = isQueue ? EJB30_QUEUE_EJBCLASS : EJB30_TOPIC_EJBCLASS;
124         FileObject resultFileObject = GenerationUtils.createClass(ejbClassTemplate, pkg, ejbClassName, null, templateParameters);
125
126         //TODO: RETOUCHE we don't have model for annotations yet
127
// // Create server resources for this bean.
128
// //
129
// // !PW Posted via RequestProcessor for now because the merged annotion provider
130
// // does not have any information for this bean if this is invoked syncronously.
131
// // We need to find a more stable mechanism for the, perhaps new API that directly
132
// // accepts the annotation reference created above. This construct is too fragile.
133
// //
134
// // Note: Even with 1s (1000ms) delay, sometimes the data was still not available.
135
// //
136
// Project project = FileOwnerQuery.getOwner(pkg);
137
// final J2eeModuleProvider pwm = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
138
// RequestProcessor.getDefault().post(new Runnable() {
139
// public void run() {
140
// if(pwm != null) {
141
// pwm.getConfigSupport().ensureResourceDefinedForEjb(ejbName, "message-driven"); //NOI18N
142
// }
143
// }
144
// }, 2000);
145

146         return resultFileObject;
147     }
148     
149     @SuppressWarnings JavaDoc("deprecation") //NOI18N
150
private void generateEJB21Xml() throws IOException JavaDoc, VersionNotSupportedException {
151         org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(pkg);
152         org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJar = DDProvider.getDefault().getMergedDDRoot(ejbModule.getMetadataUnit());
153         EnterpriseBeans beans = ejbJar.getEnterpriseBeans();
154         MessageDriven messageDriven = null;
155         if (beans == null) {
156             beans = ejbJar.newEnterpriseBeans();
157             ejbJar.setEnterpriseBeans(beans);
158         }
159         messageDriven = beans.newMessageDriven();
160         ActivationConfig config = messageDriven.newActivationConfig();
161         ActivationConfigProperty destProp = config.newActivationConfigProperty();
162         destProp.setActivationConfigPropertyName("destinationType"); // NOI18N
163
ActivationConfigProperty ackProp = config.newActivationConfigProperty();
164         ackProp.setActivationConfigPropertyName("acknowledgeMode"); // NOI18N
165
ackProp.setActivationConfigPropertyValue("Auto-acknowledge"); // NOI18N
166
config.addActivationConfigProperty(ackProp);
167         if (isQueue) {
168             String JavaDoc queue = "javax.jms.Queue"; // NOI18N
169
messageDriven.setMessageDestinationType(queue);
170             destProp.setActivationConfigPropertyValue(queue);
171         } else {
172             String JavaDoc topic = "javax.jms.Topic"; // NOI18N
173
messageDriven.setMessageDestinationType(topic);
174             destProp.setActivationConfigPropertyValue(topic);
175             ActivationConfigProperty durabilityProp = config.newActivationConfigProperty();
176             durabilityProp.setActivationConfigPropertyName("subscriptionDurability"); // NOI18N
177
durabilityProp.setActivationConfigPropertyValue("Durable"); // NOI18N
178
config.addActivationConfigProperty(durabilityProp);
179             
180             ActivationConfigProperty clientIdProp = config.newActivationConfigProperty();
181             clientIdProp.setActivationConfigPropertyName("clientId"); // NOI18N
182
clientIdProp.setActivationConfigPropertyValue(ejbName); // NOI18N
183
config.addActivationConfigProperty(clientIdProp);
184             
185             ActivationConfigProperty subscriptionNameProp = config.newActivationConfigProperty();
186             subscriptionNameProp.setActivationConfigPropertyName("subscriptionName"); // NOI18N
187
subscriptionNameProp.setActivationConfigPropertyValue(ejbName); // NOI18N
188
config.addActivationConfigProperty(subscriptionNameProp);
189             
190         }
191         config.addActivationConfigProperty(destProp);
192         messageDriven.setActivationConfig(config);
193         messageDriven.setEjbName(ejbName);
194         messageDriven.setDisplayName(displayName);
195         messageDriven.setEjbClass(packageNameWithDot + ejbClassName);
196         messageDriven.setTransactionType(MessageDriven.TRANSACTION_TYPE_CONTAINER);
197         
198         beans.addMessageDriven(messageDriven);
199         // add transaction requirements
200
AssemblyDescriptor assemblyDescriptor = ejbJar.getSingleAssemblyDescriptor();
201         if (assemblyDescriptor == null) {
202             assemblyDescriptor = ejbJar.newAssemblyDescriptor();
203             ejbJar.setAssemblyDescriptor(assemblyDescriptor);
204         }
205         MessageDestination messageDestination = assemblyDescriptor.newMessageDestination();
206         String JavaDoc destinationLink = ejbName + "Destination"; //NOI18N
207
messageDestination.setDisplayName("Destination for " + displayName);
208         messageDestination.setMessageDestinationName(destinationLink);
209         assemblyDescriptor.addMessageDestination(messageDestination);
210         
211         messageDriven.setMessageDestinationLink(destinationLink);
212         ContainerTransaction containerTransaction = assemblyDescriptor.newContainerTransaction();
213         containerTransaction.setTransAttribute("Required"); //NOI18N
214
Method method = containerTransaction.newMethod();
215         method.setEjbName(ejbName);
216         method.setMethodName("*"); //NOI18N
217
containerTransaction.addMethod(method);
218         assemblyDescriptor.addContainerTransaction(containerTransaction);
219         ejbJar.write(ejbModule.getDeploymentDescriptor());
220         Project project = FileOwnerQuery.getOwner(pkg);
221         J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
222         j2eeModuleProvider.getConfigSupport().ensureResourceDefinedForEjb(ejbName, "message-driven"); //NOI18N
223
}
224     
225     private void generateEJB30Xml() throws IOException JavaDoc {
226         throw new UnsupportedOperationException JavaDoc("Method not implemented yet.");
227     }
228     
229 }
230
Popular Tags