KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebContainerImpl


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.web.project;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
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 /**
55  *
56  * @author Chris Webster
57  */

58 class WebContainerImpl implements EnterpriseReferenceContainer {
59     
60     private Project webProject;
61     private AntProjectHelper antHelper;
62     private static final String JavaDoc SERVICE_LOCATOR_PROPERTY = "project.serviceLocator.class"; //NOI18N
63
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 JavaDoc addEjbLocalReference(EjbLocalRef localRef, FileObject referencingFile, String JavaDoc referencingClass, AntArtifact target) throws IOException JavaDoc {
71         return addReference(localRef, target, referencingFile, referencingClass);
72     }
73     
74     public String JavaDoc addEjbReference(EjbRef ref, FileObject referencingFile, String JavaDoc referencingClass, AntArtifact target) throws IOException JavaDoc {
75         return addReference(ref, target, referencingFile, referencingClass);
76     }
77     
78     private String JavaDoc addReference(Object JavaDoc ref, AntArtifact target, FileObject referencingFile, String JavaDoc referencingClass) throws IOException JavaDoc {
79         String JavaDoc refName = null;
80         WebApp webApp = getWebApp();
81         if (ref instanceof EjbRef) {
82             EjbRef ejbRef = (EjbRef) ref;
83             refName = getUniqueName(getWebApp(), "EjbRef", "EjbRefName", //NOI18N
84
ejbRef.getEjbRefName());
85             ejbRef.setEjbRefName(refName);
86             // EjbRef can come from Ejb project
87
try {
88                 EjbRef newRef = (EjbRef)webApp.createBean("EjbRef"); //NOI18N
89
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 JavaDoc ex){}
100         } else if (ref instanceof EjbLocalRef) {
101             EjbLocalRef ejbRef = (EjbLocalRef) ref;
102             refName = getUniqueName(getWebApp(), "EjbLocalRef", "EjbRefName", //NOI18N
103
ejbRef.getEjbRefName());
104             ejbRef.setEjbRefName(refName);
105             // EjbLocalRef can come from Ejb project
106
try {
107                 EjbLocalRef newRef = (EjbLocalRef)webApp.createBean("EjbLocalRef"); //NOI18N
108
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 JavaDoc 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 JavaDoc 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()); //NOI18N
133
}
134         
135         writeDD(referencingFile, referencingClass);
136         return refName;
137     }
138     
139     public String JavaDoc 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 JavaDoc serviceLocator) throws IOException JavaDoc {
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 JavaDoc {
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 JavaDoc referencingClass) throws IOException JavaDoc {
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         // test if referencing class is injection target
173
final boolean[] isInjectionTarget = {false};
174         CancellableTask task = new CancellableTask<CompilationController>() {
175                 public void run(CompilationController controller) throws IOException JavaDoc {
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 JavaDoc addResourceRef(ResourceRef ref, FileObject referencingFile, String JavaDoc referencingClass) throws IOException JavaDoc {
196         WebApp wa = getWebApp();
197         String JavaDoc resourceRefName = ref.getResRefName();
198         // see if jdbc resource has already been used in the app
199
// this change requested by Ludo
200
if (javax.sql.DataSource JavaDoc.class.getName().equals(ref.getResType())) {
201             ResourceRef[] refs = wa.getResourceRef();
202             for (int i=0; i < refs.length; i++) {
203                 String JavaDoc newDefaultDescription = ref.getDefaultDescription();
204                 String JavaDoc existingDefaultDescription = refs[i].getDefaultDescription();
205                 boolean canCompareDefDesc = (newDefaultDescription != null && existingDefaultDescription != null);
206                 if (javax.sql.DataSource JavaDoc.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()); //NOI18N
215
ref.setResRefName(resourceRefName);
216             wa.addResourceRef(ref);
217             writeDD(referencingFile, referencingClass);
218         }
219         return resourceRefName;
220     }
221     
222     public ResourceRef createResourceRef(String JavaDoc className) throws IOException JavaDoc {
223         ResourceRef ref = null;
224         try {
225             ref = (ResourceRef) getWebApp().createBean("ResourceRef");
226         } catch (ClassNotFoundException JavaDoc cnfe) {
227             IOException JavaDoc ioe = new IOException JavaDoc();
228             ioe.initCause(cnfe);
229             throw ioe;
230         }
231         return ref;
232     }
233     
234     private String JavaDoc getUniqueName(WebApp wa, String JavaDoc beanName,
235             String JavaDoc property, String JavaDoc originalValue) {
236         String JavaDoc 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 JavaDoc addDestinationRef(MessageDestinationRef ref, FileObject referencingFile, String JavaDoc referencingClass) throws IOException JavaDoc {
245         try {
246             // do not add if there is already an existing destination ref (see #85673)
247
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 JavaDoc refName = getUniqueName(getWebApp(), "MessageDestinationRef", "MessageDestinationRefName", //NOI18N
257
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 JavaDoc className) throws IOException JavaDoc {
270         MessageDestinationRef ref = null;
271         try {
272             ref = (MessageDestinationRef) getWebApp().createBean("MessageDestinationRef");
273         } catch (ClassNotFoundException JavaDoc cnfe) {
274             IOException JavaDoc ioe = new IOException JavaDoc();
275             ioe.initCause(cnfe);
276             throw ioe;
277         }
278         return ref;
279     }
280     
281     private static boolean isDescriptorMandatory(String JavaDoc j2eeVersion) {
282         if ("1.3".equals(j2eeVersion) || "1.4".equals(j2eeVersion)) {
283             return true;
284         }
285         return false;
286     }
287     
288     /**
289      * Searches for given resource reference in given web module.
290      * Two resource references are considered equal if their names and types are equal.
291      *
292      * @param webApp web module where resource reference should be found
293      * @param resRef resource reference to find
294      * @return true id resource reference was found, false otherwise
295      */

296     private static boolean isResourceRefUsed(WebApp webApp, ResourceRef resRef) {
297         String JavaDoc resRefName = resRef.getResRefName();
298         String JavaDoc 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