1 19 20 package org.netbeans.modules.j2ee.clientproject; 21 22 import java.io.IOException ; 23 import javax.lang.model.element.TypeElement; 24 import org.netbeans.api.java.source.CompilationController; 25 import org.netbeans.api.java.source.JavaSource; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.api.project.ProjectManager; 28 import org.netbeans.api.project.ant.AntArtifact; 29 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer; 30 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AntArtifactChooser; 31 import org.netbeans.modules.j2ee.common.queries.api.InjectionTargetQuery; 32 import org.netbeans.modules.j2ee.common.source.AbstractTask; 33 import org.netbeans.modules.j2ee.dd.api.client.AppClient; 34 import org.netbeans.modules.j2ee.dd.api.client.DDProvider; 35 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef; 36 import org.netbeans.modules.j2ee.dd.api.common.EjbRef; 37 import org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef; 38 import org.netbeans.modules.j2ee.dd.api.common.ResourceRef; 39 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException; 40 import org.netbeans.modules.j2ee.spi.ejbjar.CarImplementation; 41 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.netbeans.spi.project.support.ant.EditableProperties; 44 import org.netbeans.spi.project.support.ant.ReferenceHelper; 45 import org.openide.ErrorManager; 46 import org.openide.filesystems.FileObject; 47 48 52 public class JarContainerImpl implements EnterpriseReferenceContainer { 53 54 private Project webProject; 55 private AntProjectHelper antHelper; 56 private static final String SERVICE_LOCATOR_PROPERTY = "project.serviceLocator.class"; private AppClient webApp; 58 59 60 public JarContainerImpl(Project p, ReferenceHelper helper, AntProjectHelper antHelper) { 61 webProject = p; 62 this.antHelper = antHelper; 63 } 64 65 70 public void setServiceLocatorName(String serviceLocator) throws IOException { 71 EditableProperties ep = 72 antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 73 ep.setProperty(SERVICE_LOCATOR_PROPERTY, serviceLocator); 74 antHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 75 ProjectManager.getDefault().saveProject(webProject); 76 } 77 78 83 public ResourceRef createResourceRef(String className) throws IOException { 84 ResourceRef ref = null; 85 try { 86 ref = (ResourceRef) getAppClient().createBean("ResourceRef"); } catch (ClassNotFoundException cnfe) { 88 IOException ioe = new IOException (); 89 ioe.initCause(cnfe); 90 throw ioe; 91 } 92 return ref; 93 } 94 95 public MessageDestinationRef createDestinationRef(String className) throws IOException { 96 MessageDestinationRef ref = null; 97 try { 98 ref = (MessageDestinationRef) getAppClient().createBean("MessageDestinationRef"); } catch (ClassNotFoundException cnfe) { 100 IOException ioe = new IOException (); 101 ioe.initCause(cnfe); 102 throw ioe; 103 } 104 return ref; 105 } 106 107 114 public String addResourceRef(ResourceRef ref, FileObject referencingFile, String referencingClass) throws IOException { 115 String resourceRefName = ref.getResRefName(); 116 AppClient ac = getAppClient(); 117 if (javax.sql.DataSource .class.getName().equals(ref.getResType())) { 120 ResourceRef[] refs = ac.getResourceRef(); 121 for (int i=0; i < refs.length; i++) { 122 String newDefaultDescription = ref.getDefaultDescription(); 123 String existingDefaultDescription = refs[i].getDefaultDescription(); 124 boolean canCompareDefDesc = (newDefaultDescription != null && existingDefaultDescription != null); 125 if (javax.sql.DataSource .class.getName().equals(refs[i].getResType()) && 126 (canCompareDefDesc ? newDefaultDescription.equals(existingDefaultDescription) : true) && 127 ref.getResRefName().equals(refs[i].getResRefName())) { 128 return refs[i].getResRefName(); 129 } 130 } 131 } 132 if (!isResourceRefUsed(ac, ref)) { 133 resourceRefName = getUniqueName(ac, "ResourceRef", "ResRefName", ref.getResRefName()); ref.setResRefName(resourceRefName); 135 getAppClient().addResourceRef(ref); 136 writeDD(referencingFile, referencingClass); 137 } 138 return resourceRefName; 139 } 140 141 146 public String addEjbLocalReference(EjbLocalRef localRef, FileObject referencingFile, String referencedClassName, AntArtifact target) throws IOException { 147 return addReference(localRef, referencingFile, referencedClassName, target); 148 } 149 150 169 public String addEjbReference(EjbRef ref, FileObject referencingFile, String referenceClassName, AntArtifact target) throws IOException { 170 return addReference(ref, referencingFile, referenceClassName, target); 171 } 172 173 180 public String addDestinationRef(MessageDestinationRef ref, FileObject referencingFile, String referencingClass) throws IOException { 181 String refName = getUniqueName(getAppClient(), "MessageDestinationRef", "MessageDestinationRefName", ref.getMessageDestinationRefName()); 183 ref.setMessageDestinationRefName(refName); 184 try { 185 getAppClient().addMessageDestinationRef(ref); 186 writeDD(referencingFile, referencingClass); 187 } catch (VersionNotSupportedException ex){} 188 return refName; 189 } 190 191 197 public String getServiceLocatorName() { 198 EditableProperties ep = 199 antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 200 return ep.getProperty(SERVICE_LOCATOR_PROPERTY); 201 } 202 203 private AppClient getAppClient() throws IOException { 204 if (webApp==null) { 205 CarImplementation jp = (CarImplementation) webProject.getLookup().lookup(CarImplementation.class); 206 FileObject fo = jp.getDeploymentDescriptor(); 207 webApp = DDProvider.getDefault().getDDRoot(fo); 208 } 209 return webApp; 210 } 211 212 private String getUniqueName(AppClient wa, String beanName, 213 String property, String originalValue) { 214 String proposedValue = originalValue; 215 int index = 1; 216 while (wa.findBeanByName(beanName, property, proposedValue) != null) { 217 proposedValue = originalValue+Integer.toString(index++); 218 } 219 return proposedValue; 220 } 221 222 private void writeDD(FileObject referencingFile, final String referencingClassName) throws IOException { 223 final CarImplementation jp = (CarImplementation) webProject.getLookup().lookup(CarImplementation.class); 224 JavaSource javaSource = JavaSource.forFileObject(referencingFile); 225 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 226 public void run(CompilationController controller) throws Exception { 227 TypeElement typeElement = controller.getElements().getTypeElement(referencingClassName); 228 if (isDescriptorMandatory(jp.getJ2eePlatformVersion()) || 229 !InjectionTargetQuery.isInjectionTarget(controller, typeElement)) { 230 FileObject fo = jp.getDeploymentDescriptor(); 231 getAppClient().write(fo); 232 } 233 } 234 }, true); 235 } 236 237 private String addReference(Object ref, FileObject referencingFile, String referencingClass, AntArtifact target) throws IOException { 238 String refName = null; 239 AppClient webApp = getAppClient(); 240 if (ref instanceof EjbRef) { 241 EjbRef ejbRef = (EjbRef) ref; 242 refName = getUniqueName(getAppClient(), "EjbRef", "EjbRefName", ejbRef.getEjbRefName()); 244 ejbRef.setEjbRefName(refName); 245 try { 247 EjbRef newRef = (EjbRef)webApp.createBean("EjbRef"); try { 249 newRef.setAllDescriptions(ejbRef.getAllDescriptions()); 250 } catch (VersionNotSupportedException ex) { 251 newRef.setDescription(ejbRef.getDefaultDescription()); 252 } 253 newRef.setEjbRefName(ejbRef.getEjbRefName()); 254 newRef.setEjbRefType(ejbRef.getEjbRefType()); 255 newRef.setHome(ejbRef.getHome()); 256 newRef.setRemote(ejbRef.getRemote()); 257 getAppClient().addEjbRef(newRef); 258 } catch (ClassNotFoundException ex){} 259 } else if (ref instanceof EjbLocalRef) { 260 System.err.println("### UNSUPPORTED ###"); 261 282 } 283 284 ProjectClassPathExtender cpExtender = (ProjectClassPathExtender) webProject.getLookup().lookup(ProjectClassPathExtender.class); 285 if (cpExtender != null) { 286 try { 287 AntArtifactChooser.ArtifactItem artifactItems[] = new AntArtifactChooser.ArtifactItem [1]; 288 cpExtender.addAntArtifact(target, target.getArtifactLocations()[0].normalize()); 290 } catch (IOException ioe) { 291 ErrorManager.getDefault().notify(ioe); 292 } 293 } else { 294 ErrorManager.getDefault().log("WebProjectClassPathExtender not found in the project lookup of project: "+webProject.getProjectDirectory().getPath()); } 296 297 writeDD(referencingFile, referencingClass); 298 return refName; 299 } 300 301 private static boolean isDescriptorMandatory(String j2eeVersion) { 302 if ("1.3".equals(j2eeVersion) || "1.4".equals(j2eeVersion)) { 303 return true; 304 } 305 return false; 306 } 307 308 316 private static boolean isResourceRefUsed(AppClient ac, ResourceRef resRef) { 317 String resRefName = resRef.getResRefName(); 318 String resRefType = resRef.getResType(); 319 for (ResourceRef existingRef : ac.getResourceRef()) { 320 if (resRefName.equals(existingRef.getResRefName()) && resRefType.equals(existingRef.getResType())) { 321 return true; 322 } 323 } 324 return false; 325 } 326 327 } 328 | Popular Tags |