1 19 20 package org.netbeans.modules.web.project; 21 22 import java.io.IOException ; 23 import java.util.Collections ; 24 import javax.lang.model.element.TypeElement; 25 import javax.lang.model.util.Elements; 26 import org.netbeans.api.java.classpath.ClassPath; 27 import org.netbeans.api.java.source.CancellableTask; 28 import org.netbeans.api.java.source.ClasspathInfo; 29 import org.netbeans.api.java.source.CompilationController; 30 import org.netbeans.api.java.source.JavaSource; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.api.project.ant.AntArtifact; 34 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException; 35 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 36 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef; 37 import org.netbeans.modules.j2ee.dd.api.common.EjbRef; 38 import org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef; 39 import org.netbeans.modules.j2ee.dd.api.common.ResourceRef; 40 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 41 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer; 42 import org.netbeans.modules.j2ee.common.queries.api.InjectionTargetQuery; 43 import org.netbeans.modules.web.project.classpath.ClassPathProviderImpl; 44 import org.netbeans.modules.web.project.classpath.WebProjectClassPathExtender; 45 import org.netbeans.modules.web.project.ui.customizer.AntArtifactChooser; 46 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 47 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation; 48 import org.netbeans.spi.project.support.ant.AntProjectHelper; 49 import org.netbeans.spi.project.support.ant.EditableProperties; 50 import org.netbeans.spi.project.support.ant.ReferenceHelper; 51 import org.openide.ErrorManager; 52 import org.openide.filesystems.FileObject; 53 54 58 class WebContainerImpl implements EnterpriseReferenceContainer { 59 60 private Project webProject; 61 private AntProjectHelper antHelper; 62 private static final String SERVICE_LOCATOR_PROPERTY = "project.serviceLocator.class"; private WebApp webApp; 64 65 public WebContainerImpl(Project p, ReferenceHelper helper, AntProjectHelper antHelper) { 66 webProject = p; 67 this.antHelper = antHelper; 68 } 69 70 public String addEjbLocalReference(EjbLocalRef localRef, FileObject referencingFile, String referencingClass, AntArtifact target) throws IOException { 71 return addReference(localRef, target, referencingFile, referencingClass); 72 } 73 74 public String addEjbReference(EjbRef ref, FileObject referencingFile, String referencingClass, AntArtifact target) throws IOException { 75 return addReference(ref, target, referencingFile, referencingClass); 76 } 77 78 private String addReference(Object ref, AntArtifact target, FileObject referencingFile, String referencingClass) throws IOException { 79 String refName = null; 80 WebApp webApp = getWebApp(); 81 if (ref instanceof EjbRef) { 82 EjbRef ejbRef = (EjbRef) ref; 83 refName = getUniqueName(getWebApp(), "EjbRef", "EjbRefName", ejbRef.getEjbRefName()); 85 ejbRef.setEjbRefName(refName); 86 try { 88 EjbRef newRef = (EjbRef)webApp.createBean("EjbRef"); try { 90 newRef.setAllDescriptions(ejbRef.getAllDescriptions()); 91 } catch (VersionNotSupportedException ex) { 92 newRef.setDescription(ejbRef.getDefaultDescription()); 93 } 94 newRef.setEjbRefName(ejbRef.getEjbRefName()); 95 newRef.setEjbRefType(ejbRef.getEjbRefType()); 96 newRef.setHome(ejbRef.getHome()); 97 newRef.setRemote(ejbRef.getRemote()); 98 getWebApp().addEjbRef(newRef); 99 } catch (ClassNotFoundException ex){} 100 } else if (ref instanceof EjbLocalRef) { 101 EjbLocalRef ejbRef = (EjbLocalRef) ref; 102 refName = getUniqueName(getWebApp(), "EjbLocalRef", "EjbRefName", ejbRef.getEjbRefName()); 104 ejbRef.setEjbRefName(refName); 105 try { 107 EjbLocalRef newRef = (EjbLocalRef)webApp.createBean("EjbLocalRef"); try { 109 newRef.setAllDescriptions(ejbRef.getAllDescriptions()); 110 } catch (VersionNotSupportedException ex) { 111 newRef.setDescription(ejbRef.getDefaultDescription()); 112 } 113 newRef.setEjbLink(ejbRef.getEjbLink()); 114 newRef.setEjbRefName(ejbRef.getEjbRefName()); 115 newRef.setEjbRefType(ejbRef.getEjbRefType()); 116 newRef.setLocal(ejbRef.getLocal()); 117 newRef.setLocalHome(ejbRef.getLocalHome()); 118 getWebApp().addEjbLocalRef(newRef); 119 } catch (ClassNotFoundException ex){} 120 } 121 122 WebProjectClassPathExtender cpExtender = (WebProjectClassPathExtender) webProject.getLookup().lookup(WebProjectClassPathExtender.class); 123 if (cpExtender != null) { 124 try { 125 AntArtifactChooser.ArtifactItem artifactItems[] = new AntArtifactChooser.ArtifactItem [1]; 126 artifactItems[0] = new AntArtifactChooser.ArtifactItem(target, target.getArtifactLocation()); 127 cpExtender.addAntArtifacts(WebProjectProperties.JAVAC_CLASSPATH, artifactItems, WebProjectProperties.TAG_WEB_MODULE_LIBRARIES); 128 } catch (IOException ioe) { 129 ErrorManager.getDefault().notify(ioe); 130 } 131 } else { 132 ErrorManager.getDefault().log("WebProjectClassPathExtender not found in the project lookup of project: "+webProject.getProjectDirectory().getPath()); } 134 135 writeDD(referencingFile, referencingClass); 136 return refName; 137 } 138 139 public String getServiceLocatorName() { 140 EditableProperties ep = 141 antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 142 return ep.getProperty(SERVICE_LOCATOR_PROPERTY); 143 } 144 145 public void setServiceLocatorName(String serviceLocator) throws IOException { 146 EditableProperties ep = 147 antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 148 ep.setProperty(SERVICE_LOCATOR_PROPERTY, serviceLocator); 149 antHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 150 ProjectManager.getDefault().saveProject(webProject); 151 } 152 153 private WebApp getWebApp() throws IOException { 154 if (webApp==null) { 155 WebModuleImplementation jp = (WebModuleImplementation) webProject.getLookup().lookup(WebModuleImplementation.class); 156 FileObject fo = jp.getDeploymentDescriptor(); 157 webApp = DDProvider.getDefault().getDDRoot(fo); 158 } 159 return webApp; 160 } 161 162 private void writeDD(FileObject referencingFile, final String referencingClass) throws IOException { 163 ClassPathProviderImpl cppImpl = webProject.getLookup().lookup(ClassPathProviderImpl.class); 164 ClasspathInfo classpathInfo = ClasspathInfo.create( 165 cppImpl.getProjectSourcesClassPath(ClassPath.BOOT), 166 cppImpl.getProjectSourcesClassPath(ClassPath.COMPILE), 167 cppImpl.getProjectSourcesClassPath(ClassPath.SOURCE) 168 ); 169 JavaSource javaSource = JavaSource.create(classpathInfo, Collections.<FileObject>emptyList()); 170 WebModuleImplementation jp = (WebModuleImplementation) webProject.getLookup().lookup(WebModuleImplementation.class); 171 172 final boolean[] isInjectionTarget = {false}; 174 CancellableTask task = new CancellableTask<CompilationController>() { 175 public void run(CompilationController controller) throws IOException { 176 Elements elements = controller.getElements(); 177 TypeElement thisElement = elements.getTypeElement(referencingClass); 178 if (thisElement!=null) 179 isInjectionTarget[0] = InjectionTargetQuery.isInjectionTarget(controller, thisElement); 180 } 181 public void cancel() {} 182 }; 183 JavaSource refFile = JavaSource.forFileObject(referencingFile); 184 if (refFile!=null) { 185 refFile.runUserActionTask(task, true); 186 } 187 188 boolean shouldWrite = isDescriptorMandatory(jp.getJ2eePlatformVersion()) || !isInjectionTarget[0]; 189 if (shouldWrite) { 190 FileObject fo = jp.getDeploymentDescriptor(); 191 getWebApp().write(fo); 192 } 193 } 194 195 public String addResourceRef(ResourceRef ref, FileObject referencingFile, String referencingClass) throws IOException { 196 WebApp wa = getWebApp(); 197 String resourceRefName = ref.getResRefName(); 198 if (javax.sql.DataSource .class.getName().equals(ref.getResType())) { 201 ResourceRef[] refs = wa.getResourceRef(); 202 for (int i=0; i < refs.length; i++) { 203 String newDefaultDescription = ref.getDefaultDescription(); 204 String existingDefaultDescription = refs[i].getDefaultDescription(); 205 boolean canCompareDefDesc = (newDefaultDescription != null && existingDefaultDescription != null); 206 if (javax.sql.DataSource .class.getName().equals(refs[i].getResType()) && 207 (canCompareDefDesc ? newDefaultDescription.equals(existingDefaultDescription) : true) && 208 ref.getResRefName().equals(refs[i].getResRefName())) { 209 return refs[i].getResRefName(); 210 } 211 } 212 } 213 if (!isResourceRefUsed(wa, ref)) { 214 resourceRefName = getUniqueName(wa, "ResourceRef", "ResRefName", ref.getResRefName()); ref.setResRefName(resourceRefName); 216 wa.addResourceRef(ref); 217 writeDD(referencingFile, referencingClass); 218 } 219 return resourceRefName; 220 } 221 222 public ResourceRef createResourceRef(String className) throws IOException { 223 ResourceRef ref = null; 224 try { 225 ref = (ResourceRef) getWebApp().createBean("ResourceRef"); 226 } catch (ClassNotFoundException cnfe) { 227 IOException ioe = new IOException (); 228 ioe.initCause(cnfe); 229 throw ioe; 230 } 231 return ref; 232 } 233 234 private String getUniqueName(WebApp wa, String beanName, 235 String property, String originalValue) { 236 String proposedValue = originalValue; 237 int index = 1; 238 while (wa.findBeanByName(beanName, property, proposedValue) != null) { 239 proposedValue = originalValue+Integer.toString(index++); 240 } 241 return proposedValue; 242 } 243 244 public String addDestinationRef(MessageDestinationRef ref, FileObject referencingFile, String referencingClass) throws IOException { 245 try { 246 for (MessageDestinationRef mdRef : getWebApp().getMessageDestinationRef()){ 248 if (mdRef.getMessageDestinationRefName().equals(ref.getMessageDestinationRefName())){ 249 return mdRef.getMessageDestinationRefName(); 250 } 251 } 252 } catch (VersionNotSupportedException ex) { 253 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 254 } 255 256 String refName = getUniqueName(getWebApp(), "MessageDestinationRef", "MessageDestinationRefName", ref.getMessageDestinationRefName()); 258 259 ref.setMessageDestinationRefName(refName); 260 try { 261 getWebApp().addMessageDestinationRef(ref); 262 writeDD(referencingFile, referencingClass); 263 } catch (VersionNotSupportedException ex){ 264 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 265 } 266 return refName; 267 } 268 269 public MessageDestinationRef createDestinationRef(String className) throws IOException { 270 MessageDestinationRef ref = null; 271 try { 272 ref = (MessageDestinationRef) getWebApp().createBean("MessageDestinationRef"); 273 } catch (ClassNotFoundException cnfe) { 274 IOException ioe = new IOException (); 275 ioe.initCause(cnfe); 276 throw ioe; 277 } 278 return ref; 279 } 280 281 private static boolean isDescriptorMandatory(String j2eeVersion) { 282 if ("1.3".equals(j2eeVersion) || "1.4".equals(j2eeVersion)) { 283 return true; 284 } 285 return false; 286 } 287 288 296 private static boolean isResourceRefUsed(WebApp webApp, ResourceRef resRef) { 297 String resRefName = resRef.getResRefName(); 298 String resRefType = resRef.getResType(); 299 for (ResourceRef existingRef : webApp.getResourceRef()) { 300 if (resRefName.equals(existingRef.getResRefName()) && resRefType.equals(existingRef.getResType())) { 301 return true; 302 } 303 } 304 return false; 305 } 306 307 } 308 | Popular Tags |