1 19 20 package org.netbeans.modules.j2ee.ejbcore.api.codegeneration; 21 22 import java.io.IOException ; 23 import java.util.HashMap ; 24 import java.util.Map ; 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 49 public final class MessageGenerator { 50 51 private static final String EJB21_EJBCLASS = "Templates/J2EE/EJB21/MessageDrivenEjbClass.java"; private static final String EJB30_QUEUE_EJBCLASS = "Templates/J2EE/EJB30/MessageDrivenQueueEjbClass.java"; private static final String EJB30_TOPIC_EJBCLASS = "Templates/J2EE/EJB30/MessageDrivenTopicEjbClass.java"; 55 private final FileObject pkg; 57 private final boolean isQueue; 58 private final boolean isSimplified; 59 private final boolean isXmlBased; 60 61 private final EJBNameOptions ejbNameOptions; 63 private final String ejbName; 64 private final String ejbClassName; 65 private final String displayName; 66 67 private final String packageName; 68 private final String packageNameWithDot; 69 70 private final Map <String , String > templateParameters; 71 72 public static MessageGenerator create(String wizardTargetName, FileObject pkg, boolean isQueue, boolean isSimplified, boolean isXmlBased) { 73 return new MessageGenerator(wizardTargetName, pkg, isQueue, isSimplified, isXmlBased); 74 } 75 76 private MessageGenerator(String 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 <String , String >(); 88 this.templateParameters.put("package", packageName); 90 } 91 92 public FileObject generate() throws IOException { 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 { 113 FileObject resultFileObject = GenerationUtils.createClass(EJB21_EJBCLASS, pkg, ejbClassName, null, templateParameters); 114 Project project = FileOwnerQuery.getOwner(pkg); 116 J2eeModuleProvider pwm = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 117 pwm.getConfigSupport().ensureConfigurationReady(); 118 return resultFileObject; 120 } 121 122 private FileObject generateEJB30Classes() throws IOException { 123 String ejbClassTemplate = isQueue ? EJB30_QUEUE_EJBCLASS : EJB30_TOPIC_EJBCLASS; 124 FileObject resultFileObject = GenerationUtils.createClass(ejbClassTemplate, pkg, ejbClassName, null, templateParameters); 125 126 146 return resultFileObject; 147 } 148 149 @SuppressWarnings ("deprecation") private void generateEJB21Xml() throws IOException , 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"); ActivationConfigProperty ackProp = config.newActivationConfigProperty(); 164 ackProp.setActivationConfigPropertyName("acknowledgeMode"); ackProp.setActivationConfigPropertyValue("Auto-acknowledge"); config.addActivationConfigProperty(ackProp); 167 if (isQueue) { 168 String queue = "javax.jms.Queue"; messageDriven.setMessageDestinationType(queue); 170 destProp.setActivationConfigPropertyValue(queue); 171 } else { 172 String topic = "javax.jms.Topic"; messageDriven.setMessageDestinationType(topic); 174 destProp.setActivationConfigPropertyValue(topic); 175 ActivationConfigProperty durabilityProp = config.newActivationConfigProperty(); 176 durabilityProp.setActivationConfigPropertyName("subscriptionDurability"); durabilityProp.setActivationConfigPropertyValue("Durable"); config.addActivationConfigProperty(durabilityProp); 179 180 ActivationConfigProperty clientIdProp = config.newActivationConfigProperty(); 181 clientIdProp.setActivationConfigPropertyName("clientId"); clientIdProp.setActivationConfigPropertyValue(ejbName); config.addActivationConfigProperty(clientIdProp); 184 185 ActivationConfigProperty subscriptionNameProp = config.newActivationConfigProperty(); 186 subscriptionNameProp.setActivationConfigPropertyName("subscriptionName"); subscriptionNameProp.setActivationConfigPropertyValue(ejbName); 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 AssemblyDescriptor assemblyDescriptor = ejbJar.getSingleAssemblyDescriptor(); 201 if (assemblyDescriptor == null) { 202 assemblyDescriptor = ejbJar.newAssemblyDescriptor(); 203 ejbJar.setAssemblyDescriptor(assemblyDescriptor); 204 } 205 MessageDestination messageDestination = assemblyDescriptor.newMessageDestination(); 206 String destinationLink = ejbName + "Destination"; 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"); Method method = containerTransaction.newMethod(); 215 method.setEjbName(ejbName); 216 method.setMethodName("*"); 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"); } 224 225 private void generateEJB30Xml() throws IOException { 226 throw new UnsupportedOperationException ("Method not implemented yet."); 227 } 228 229 } 230 | Popular Tags |