KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > JarContainerImpl


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.clientproject;
21
22 import java.io.IOException JavaDoc;
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 /**
49  *
50  * @author jungi
51  */

52 public class JarContainerImpl implements EnterpriseReferenceContainer {
53     
54     private Project webProject;
55     private AntProjectHelper antHelper;
56     private static final String JavaDoc SERVICE_LOCATOR_PROPERTY = "project.serviceLocator.class"; //NOI18N
57
private AppClient webApp;
58     
59     /** Creates a new instance of JarContainerImpl */
60     public JarContainerImpl(Project p, ReferenceHelper helper, AntProjectHelper antHelper) {
61         webProject = p;
62         this.antHelper = antHelper;
63     }
64     
65     /**
66      * set name of service locator fo this project.
67      *
68      * @param serviceLocator used in this project
69      */

70     public void setServiceLocatorName(String JavaDoc serviceLocator) throws IOException JavaDoc {
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     /**
79      * Create resource ref instance based on current project type.
80      *
81      * @param className to determine context from
82      */

83     public ResourceRef createResourceRef(String JavaDoc className) throws IOException JavaDoc {
84         ResourceRef ref = null;
85         try {
86             ref = (ResourceRef) getAppClient().createBean("ResourceRef"); // NOI18N
87
} catch (ClassNotFoundException JavaDoc cnfe) {
88             IOException JavaDoc ioe = new IOException JavaDoc();
89             ioe.initCause(cnfe);
90             throw ioe;
91         }
92         return ref;
93     }
94     
95     public MessageDestinationRef createDestinationRef(String JavaDoc className) throws IOException JavaDoc {
96         MessageDestinationRef ref = null;
97         try {
98             ref = (MessageDestinationRef) getAppClient().createBean("MessageDestinationRef"); // NOI18N
99
} catch (ClassNotFoundException JavaDoc cnfe) {
100             IOException JavaDoc ioe = new IOException JavaDoc();
101             ioe.initCause(cnfe);
102             throw ioe;
103         }
104         return ref;
105     }
106     
107     /**
108      * Add given resource reference into the deployment descriptor.
109      *
110      * @param ref reference to resource used
111      * @param referencingClass class which will use the resource
112      * @return unique jndi name used in deployment descriptor
113      */

114     public String JavaDoc addResourceRef(ResourceRef ref, FileObject referencingFile, String JavaDoc referencingClass) throws IOException JavaDoc {
115         String JavaDoc resourceRefName = ref.getResRefName();
116         AppClient ac = getAppClient();
117         // see if jdbc resource has already been used in the app
118
// this change requested by Ludo
119
if (javax.sql.DataSource JavaDoc.class.getName().equals(ref.getResType())) {
120             ResourceRef[] refs = ac.getResourceRef();
121             for (int i=0; i < refs.length; i++) {
122                 String JavaDoc newDefaultDescription = ref.getDefaultDescription();
123                 String JavaDoc existingDefaultDescription = refs[i].getDefaultDescription();
124                 boolean canCompareDefDesc = (newDefaultDescription != null && existingDefaultDescription != null);
125                 if (javax.sql.DataSource JavaDoc.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()); //NOI18N
134
ref.setResRefName(resourceRefName);
135             getAppClient().addResourceRef(ref);
136             writeDD(referencingFile, referencingClass);
137         }
138         return resourceRefName;
139     }
140     
141     /**
142      *
143      *
144      * @see #addEjbReference(EjbRef, String, AntArtifact)
145      */

146     public String JavaDoc addEjbLocalReference(EjbLocalRef localRef, FileObject referencingFile, String JavaDoc referencedClassName, AntArtifact target) throws IOException JavaDoc {
147         return addReference(localRef, referencingFile, referencedClassName, target);
148     }
149     
150     /**
151      * Add given ejb reference into deployment descriptor. This method should
152      * also ensure that the supplied target is added to the class path (as the
153      * ejb interfaces will be referenced from this class) as well as the
154      * deployed manifest. The deployed manifest is the generic J2EE compliant
155      * strategy, application server specific behavior such as delegating to the
156      * parent class loader could also be used. The main point is not to
157      * include the target in the deployed archive but instead reference the
158      * interface jar (or standard ejb module) included in the J2EE application.
159      *
160      * @param ref -- ejb reference this will include the ejb link which assumes
161      * root packaging in the containing application. The name of this ref should
162      * be considered a hint and made unique within the deployment descriptor.
163      * @param referencedClassName -- name of referenced class, this can be used
164      * to determine where to add the deployment descriptor entry. This class
165      * will be modified with a method or other strategy to obtain the ejb.
166      * @param target to include in the build
167      * @return actual jndi name used in deployment descriptor
168      */

169     public String JavaDoc addEjbReference(EjbRef ref, FileObject referencingFile, String JavaDoc referenceClassName, AntArtifact target) throws IOException JavaDoc {
170         return addReference(ref, referencingFile, referenceClassName, target);
171     }
172     
173     /**
174      * Add given message destination reference into the deployment descriptor
175      *
176      * @param ref to destination
177      * @param referencingClass class using the destination
178      * @return unique jndi name used in the deployment descriptor
179      */

180     public String JavaDoc addDestinationRef(MessageDestinationRef ref, FileObject referencingFile, String JavaDoc referencingClass) throws IOException JavaDoc {
181         String JavaDoc refName = getUniqueName(getAppClient(), "MessageDestinationRef", "MessageDestinationRefName", //NOI18N
182
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     /**
192      *
193      *
194      * @return name of the service locator defined for this project or null
195      * if service locator is not being used
196      */

197     public String JavaDoc 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 JavaDoc {
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 JavaDoc getUniqueName(AppClient wa, String JavaDoc beanName,
213             String JavaDoc property, String JavaDoc originalValue) {
214         String JavaDoc 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 JavaDoc referencingClassName) throws IOException JavaDoc {
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 JavaDoc {
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 JavaDoc addReference(Object JavaDoc ref, FileObject referencingFile, String JavaDoc referencingClass, AntArtifact target) throws IOException JavaDoc {
238         String JavaDoc refName = null;
239         AppClient webApp = getAppClient();
240         if (ref instanceof EjbRef) {
241             EjbRef ejbRef = (EjbRef) ref;
242             refName = getUniqueName(getAppClient(), "EjbRef", "EjbRefName", //NOI18N
243
ejbRef.getEjbRefName());
244             ejbRef.setEjbRefName(refName);
245             // EjbRef can come from Ejb project
246
try {
247                 EjbRef newRef = (EjbRef)webApp.createBean("EjbRef"); //NOI18N
248
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 JavaDoc ex){}
259         } else if (ref instanceof EjbLocalRef) {
260             System.err.println("### UNSUPPORTED ###");
261             /*
262             EjbLocalRef ejbRef = (EjbLocalRef) ref;
263             refName = getUniqueName(getAppClient(), "EjbLocalRef", "EjbRefName", //NOI18N
264                     ejbRef.getEjbRefName());
265             ejbRef.setEjbRefName(refName);
266             // EjbLocalRef can come from Ejb project
267             try {
268                 EjbLocalRef newRef = (EjbLocalRef)webApp.createBean("EjbLocalRef"); //NOI18N
269                 try {
270                     newRef.setAllDescriptions(ejbRef.getAllDescriptions());
271                 } catch (VersionNotSupportedException ex) {
272                     newRef.setDescription(ejbRef.getDefaultDescription());
273                 }
274                 newRef.setEjbLink(ejbRef.getEjbLink());
275                 newRef.setEjbRefName(ejbRef.getEjbRefName());
276                 newRef.setEjbRefType(ejbRef.getEjbRefType());
277                 newRef.setLocal(ejbRef.getLocal());
278                 newRef.setLocalHome(ejbRef.getLocalHome());
279                 getAppClient().addEjbLocalRef(newRef);
280             } catch (ClassNotFoundException ex){}
281              */

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                 //artifactItems[0] = new AntArtifactChooser.ArtifactItem(target, target.getArtifactLocation());
289
cpExtender.addAntArtifact(target, target.getArtifactLocations()[0].normalize());
290             } catch (IOException JavaDoc 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()); //NOI18N
295
}
296         
297         writeDD(referencingFile, referencingClass);
298         return refName;
299     }
300
301     private static boolean isDescriptorMandatory(String JavaDoc j2eeVersion) {
302         if ("1.3".equals(j2eeVersion) || "1.4".equals(j2eeVersion)) {
303             return true;
304         }
305         return false;
306     }
307     
308     /**
309      * Searches for given resource reference in given client module.
310      * Two resource references are considered equal if their names and types are equal.
311      *
312      * @param ac client module where resource reference should be found
313      * @param resRef resource reference to find
314      * @return true id resource reference was found, false otherwise
315      */

316     private static boolean isResourceRefUsed(AppClient ac, ResourceRef resRef) {
317         String JavaDoc resRefName = resRef.getResRefName();
318         String JavaDoc 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