KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > spi > ejbjar > support > EjbEnterpriseReferenceContainerSupport


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.spi.ejbjar.support;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.api.project.ProjectManager;
25 import org.netbeans.api.project.ant.AntArtifact;
26 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef;
27 import org.netbeans.modules.j2ee.dd.api.common.EjbRef;
28 import org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef;
29 import org.netbeans.modules.j2ee.dd.api.common.ResourceRef;
30 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
31 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer;
32 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
33 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
34 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
35 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
36 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
37 import org.netbeans.spi.project.support.ant.AntProjectHelper;
38 import org.netbeans.spi.project.support.ant.EditableProperties;
39 import org.openide.ErrorManager;
40 import org.openide.filesystems.FileObject;
41 import org.openide.util.NbBundle;
42
43
44 /** Default implementation of {@link org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer}.
45  *
46  * @author Chris Webster
47  */

48 public final class EjbEnterpriseReferenceContainerSupport {
49     
50     private EjbEnterpriseReferenceContainerSupport() {
51     }
52     
53     public static EnterpriseReferenceContainer createEnterpriseReferenceContainer(Project project, AntProjectHelper helper) {
54         return new ERC(project, helper);
55     }
56     
57     private static class ERC implements EnterpriseReferenceContainer {
58         
59         private Project ejbProject;
60         private AntProjectHelper antHelper;
61         private static final String JavaDoc SERVICE_LOCATOR_PROPERTY = "project.serviceLocator.class"; //NOI18N
62

63         private ERC(Project p, AntProjectHelper helper) {
64             ejbProject = p;
65             antHelper = helper;
66         }
67         
68         public String JavaDoc addEjbLocalReference(EjbLocalRef localRef, FileObject referencingFile, String JavaDoc referencingClass, AntArtifact target) throws IOException JavaDoc {
69             return addReference(localRef, referencingFile, referencingClass, target);
70         }
71         
72         public String JavaDoc addEjbReference(EjbRef ref, FileObject referencingFile, String JavaDoc referencingClass, AntArtifact target) throws IOException JavaDoc {
73             return addReference(ref, referencingFile, referencingClass, target);
74         }
75         
76         private String JavaDoc addReference(Object JavaDoc ref, FileObject referencingFile, String JavaDoc referencingClass, AntArtifact target) throws IOException JavaDoc {
77             String JavaDoc refName = null;
78             Ejb model = findEjbForClass(referencingClass);
79             // XXX: target may be null (for example for a freeform project which doesn't have jar outputs set)
80
// that's the reason of the check for target == null
81
boolean fromSameProject = (target == null || ejbProject.equals(target.getProject()));
82             org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJars [] = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(ejbProject);
83             assert ejbJars.length > 0;
84             // try to write to deployment descriptor anly if there is any
85
// in case of metadata written in annotations, model shoud be updatet automaticaly
86
if (ejbJars[0].getDeploymentDescriptor() != null) {
87                 if (model == null) {
88                     if (ref instanceof EjbRef) {
89                         return ((EjbRef) ref).getEjbRefName();
90                     } else {
91                         return ((EjbLocalRef) ref).getEjbRefName();
92                     }
93                 }
94                 // XXX could use visitor here to remove conditional logic
95
if (ref instanceof EjbRef) {
96                     org.netbeans.modules.j2ee.dd.api.common.EjbRef ejbRef =
97                             (org.netbeans.modules.j2ee.dd.api.common.EjbRef) ref;
98                     refName = getUniqueName(model, Ejb.EJB_REF,
99                             ejbRef.EJB_REF_NAME, ejbRef.getEjbRefName());
100                     ejbRef.setEjbRefName(refName);
101                     if (fromSameProject) {
102                         ejbRef.setEjbLink(stripModuleName(ejbRef.getEjbLink()));
103                     }
104                     model.addEjbRef(ejbRef);
105                 } else {
106                     org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ejbRef =
107                             (org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef) ref;
108                     refName = getUniqueName(model, Ejb.EJB_LOCAL_REF,
109                             ejbRef.EJB_REF_NAME, ejbRef.getEjbRefName());
110                     ejbRef.setEjbRefName(refName);
111                     if (fromSameProject) {
112                         ejbRef.setEjbLink(stripModuleName(ejbRef.getEjbLink()));
113                     }
114                     model.addEjbLocalRef(ejbRef);
115                 }
116                 writeDD();
117             }
118             
119             if(!fromSameProject) {
120                 try {
121                     ProjectClassPathExtender pcpe = ejbProject.getLookup().lookup(ProjectClassPathExtender.class);
122                     assert pcpe != null;
123                     pcpe.addAntArtifact(target, target.getArtifactLocations()[0]);
124                 } catch (IOException JavaDoc ioe) {
125                     ErrorManager.getDefault().notify(ioe);
126                 }
127             }
128             
129             ProjectManager.getDefault().saveProject(ejbProject);
130             return refName;
131         }
132         
133         public String JavaDoc getServiceLocatorName() {
134             EditableProperties ep =
135                     antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
136             return ep.getProperty(SERVICE_LOCATOR_PROPERTY);
137         }
138         
139         public void setServiceLocatorName(String JavaDoc serviceLocator) throws IOException JavaDoc {
140             EditableProperties ep = antHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
141             ep.setProperty(SERVICE_LOCATOR_PROPERTY, serviceLocator);
142             antHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
143             ProjectManager.getDefault().saveProject(ejbProject);
144         }
145         
146         private String JavaDoc stripModuleName(String JavaDoc ejbLink) {
147             int index = ejbLink.indexOf('#');
148             return ejbLink.substring(index+1);
149         }
150         
151         private Ejb findEjbForClass(String JavaDoc className) throws IOException JavaDoc {
152             EjbJar dd = findDD();
153             Ejb ejb = null;
154             if (dd != null) {
155                 EnterpriseBeans beans = dd.getEnterpriseBeans();
156                 if (beans != null) {
157                     ejb = (Ejb) beans.findBeanByName(EnterpriseBeans.SESSION, Ejb.EJB_CLASS, className);
158                     if (ejb == null) {
159                         ejb = (Ejb) beans.findBeanByName(EnterpriseBeans.ENTITY, Ejb.EJB_CLASS, className);
160                     }
161                     if (ejb == null) {
162                         ejb = (Ejb) beans.findBeanByName(EnterpriseBeans.MESSAGE_DRIVEN, Ejb.EJB_CLASS, className);
163                     }
164                 }
165             }
166             return ejb;
167         }
168         
169         private EjbJar findDD() throws IOException JavaDoc {
170             org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJars [] = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(ejbProject);
171             assert ejbJars.length > 0;
172             return DDProvider.getDefault().getMergedDDRoot(ejbJars[0].getMetadataUnit());
173         }
174         
175         private void writeDD() throws IOException JavaDoc {
176             org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJars [] = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(ejbProject);
177             assert ejbJars.length > 0;
178             if (isDescriptorMandatory(ejbJars[0].getJ2eePlatformVersion())) {
179                 FileObject fo = ejbJars[0].getDeploymentDescriptor();
180                 if (fo != null){
181                     DDProvider.getDefault().getMergedDDRoot(ejbJars[0].getMetadataUnit()).write(fo);
182                 }
183             }
184         }
185         
186         public String JavaDoc addResourceRef(ResourceRef ref, FileObject referencingFile, String JavaDoc referencingClass) throws IOException JavaDoc {
187             Ejb ejb = findEjbForClass(referencingClass);
188             if (ejb == null) {
189                 return ref.getResRefName();
190             }
191             String JavaDoc resourceRefName = ref.getResRefName();
192             if (javax.sql.DataSource JavaDoc.class.getName().equals(ref.getResType())) {
193                 if (!isJdbcConnectionAlreadyUsed(ejb, ref)) {
194                     resourceRefName = getUniqueName(ejb, Ejb.RESOURCE_REF, org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_REF_NAME, ref.getResRefName());
195                     ref.setResRefName(resourceRefName);
196                     ejb.addResourceRef((org.netbeans.modules.j2ee.dd.api.common.ResourceRef)ref);
197                     writeDD();
198                 }
199             } else {
200                 if (!isResourceRefUsed(ejb, ref)) {
201                     resourceRefName = getUniqueName(ejb, Ejb.RESOURCE_REF, org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_REF_NAME, ref.getResRefName());
202                     ref.setResRefName(resourceRefName);
203                     ejb.addResourceRef((org.netbeans.modules.j2ee.dd.api.common.ResourceRef)ref);
204                     writeDD();
205                 }
206             }
207             return resourceRefName;
208         }
209         
210         private boolean isJdbcConnectionAlreadyUsed(Ejb ejb, ResourceRef ref) throws IOException JavaDoc {
211             if (javax.sql.DataSource JavaDoc.class.getName().equals(ref.getResType())) {
212                 for (ResourceRef existingRef : ejb.getResourceRef()) {
213                     String JavaDoc newDefaultDescription = ref.getDefaultDescription();
214                     String JavaDoc existingDefaultDescription = existingRef.getDefaultDescription();
215                     boolean canCompareDefDesc = (newDefaultDescription != null && existingDefaultDescription != null);
216                     if (javax.sql.DataSource JavaDoc.class.getName().equals(existingRef.getResType()) &&
217                             (canCompareDefDesc ? newDefaultDescription.equals(existingDefaultDescription) : true) &&
218                             ref.getResRefName().equals(existingRef.getResRefName())) {
219                         return true;
220                     }
221                 }
222             }
223             return false;
224         }
225         
226         /**
227          * Searches for given resource reference in given EJB.
228          * Two resource references are considered equal if their names and types are equal.
229          *
230          * @param ejb EJB where resource reference should be found
231          * @param resRef resource reference to find
232          * @return true id resource reference was found, false otherwise
233          */

234         private static boolean isResourceRefUsed(Ejb ejb, ResourceRef resRef) {
235             String JavaDoc resRefName = resRef.getResRefName();
236             String JavaDoc resRefType = resRef.getResType();
237             for (ResourceRef existingRef : ejb.getResourceRef()) {
238                 if (resRefName.equals(existingRef.getResRefName()) && resRefType.equals(existingRef.getResType())) {
239                     return true;
240                 }
241             }
242             return false;
243         }
244         
245         private String JavaDoc getUniqueName(Ejb bean, String JavaDoc beanName,
246                 String JavaDoc property, String JavaDoc originalValue) {
247             String JavaDoc proposedValue = originalValue;
248             int index = 1;
249             while (bean.findBeanByName(beanName, property, proposedValue) != null) {
250                 proposedValue = originalValue+Integer.toString(index++);
251             }
252             return proposedValue;
253         }
254         
255         public ResourceRef createResourceRef(String JavaDoc className) throws IOException JavaDoc {
256             ResourceRef ref = null;
257             Ejb ejb = findEjbForClass(className);
258             if (ejb != null) {
259                 ref = ejb.newResourceRef();
260             } else {
261                 try {
262                     EjbJar ejbJar = findDD();
263                     if (ejbJar != null) {
264                         ref = (ResourceRef) ejbJar.createBean("ResourceRef");
265                     } else {
266                         ErrorManager.getDefault().log(ErrorManager.USER,
267                                 NbBundle.getMessage(EjbEnterpriseReferenceContainerSupport.class, "MSG_MissingMetadata"));
268                     }
269                 } catch (ClassNotFoundException JavaDoc cnfe) {
270                     IOException JavaDoc ioe = new IOException JavaDoc();
271                     ioe.initCause(cnfe);
272                     throw ioe;
273                 }
274             }
275             return ref;
276         }
277         
278         public String JavaDoc addDestinationRef(MessageDestinationRef ref, FileObject referencingFile, String JavaDoc referencingClass) throws IOException JavaDoc {
279             Ejb ejb = findEjbForClass(referencingClass);
280             if (ejb == null) {
281                 return ref.getMessageDestinationRefName();
282             }
283             try {
284                 // do not add if there is already an existing destination ref (see #85673)
285
for (MessageDestinationRef mdRef : ejb.getMessageDestinationRef()){
286                     if (mdRef.getMessageDestinationRefName().equals(ref.getMessageDestinationRefName())){
287                         return mdRef.getMessageDestinationRefName();
288                     }
289                 }
290             } catch (VersionNotSupportedException ex) {
291                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
292             }
293             
294             String JavaDoc destinationRefName = getUniqueName(ejb, Ejb.MESSAGE_DESTINATION_REF,
295                     org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef.MESSAGE_DESTINATION_REF_NAME,
296                     ref.getMessageDestinationRefName());
297             ref.setMessageDestinationRefName(destinationRefName);
298             try {
299                 ejb.addMessageDestinationRef((org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef)ref);
300             } catch (VersionNotSupportedException vnse) {
301                 // this exception should not be generated
302
}
303             writeDD();
304             return destinationRefName;
305         }
306         
307         public MessageDestinationRef createDestinationRef(String JavaDoc className) throws IOException JavaDoc {
308             Ejb ejb = findEjbForClass(className);
309             MessageDestinationRef ref = null;
310             if (ejb != null) {
311                 try {
312                     ref = ejb.newMessageDestinationRef();
313                 } catch (VersionNotSupportedException vnse) {
314                     IOException JavaDoc ioe = new IOException JavaDoc();
315                     ioe.initCause(vnse);
316                     throw ioe;
317                 }
318             } else {
319                 try {
320                     ref = (MessageDestinationRef) findDD().createBean("MessageDestinationRef");
321                 } catch (ClassNotFoundException JavaDoc cnfe) {
322                     IOException JavaDoc ioe = new IOException JavaDoc();
323                     ioe.initCause(cnfe);
324                     throw ioe;
325                 }
326             }
327             return ref;
328         }
329         
330     }
331     
332     private static boolean isDescriptorMandatory(String JavaDoc j2eeVersion) {
333         if ("1.3".equals(j2eeVersion) || "1.4".equals(j2eeVersion)) {
334             return true;
335         }
336         return false;
337     }
338     
339 }
340
Popular Tags