KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > ejb > shared > EjbViewController


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.ejbcore.ui.logicalview.ejb.shared;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.lang.model.element.TypeElement;
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.netbeans.api.java.project.JavaProjectConstants;
28 import org.netbeans.api.java.source.CompilationController;
29 import org.netbeans.api.java.source.ElementHandle;
30 import org.netbeans.api.java.source.JavaSource;
31 import org.netbeans.api.java.source.JavaSource.Phase;
32 import org.netbeans.api.project.FileOwnerQuery;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.ant.AntArtifact;
35 import org.netbeans.api.project.ant.AntArtifactQuery;
36 import org.netbeans.modules.j2ee.api.ejbjar.EjbReference;
37 import org.netbeans.modules.j2ee.common.source.AbstractTask;
38 import org.netbeans.modules.j2ee.dd.api.common.MessageDestination;
39 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
40 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor;
41 import org.netbeans.modules.j2ee.dd.api.ejb.ContainerTransaction;
42 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
43 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
44 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
45 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelation;
46 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelationshipRole;
47 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
48 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession;
49 import org.netbeans.modules.j2ee.dd.api.ejb.MessageDriven;
50 import org.netbeans.modules.j2ee.dd.api.ejb.Method;
51 import org.netbeans.modules.j2ee.dd.api.ejb.MethodPermission;
52 import org.netbeans.modules.j2ee.dd.api.ejb.Relationships;
53 import org.openide.ErrorManager;
54 import org.openide.filesystems.FileObject;
55 import org.openide.loaders.DataObject;
56 import org.openide.loaders.DataObjectNotFoundException;
57 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
58 import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.ejb.dnd.EjbReferenceImpl;
59
60 /**
61  * This class provides controller capabilities for ejb logical views. The nodes
62  * should delegate non ui tasks to this class.
63  * @author Chris Webster
64  * @author Martin Adamek
65  */

66 public final class EjbViewController {
67     
68     private final Ejb ejb;
69     private final EjbJar ejbJar;
70     private final Project project;
71     private final ClassPath classPath;
72     
73     public EjbViewController(Ejb ejb, EjbJar ejbJar, ClassPath classPath) {
74         this.ejb = ejb;
75         this.ejbJar = ejbJar;
76         this.classPath = classPath;
77         project = FileOwnerQuery.getOwner(classPath.getRoots()[0]);
78     }
79     
80     public String JavaDoc getDisplayName() {
81         String JavaDoc name = ejb.getDefaultDisplayName();
82         if (name == null) {
83             name = ejb.getEjbName();
84         }
85         return name;
86     }
87     
88     public void delete(boolean deleteClasses) throws IOException JavaDoc {
89         EnterpriseBeans beans = ejbJar.getEnterpriseBeans();
90         J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class);
91         j2eeModuleProvider.getConfigSupport().ensureConfigurationReady();
92         deleteTraces();
93         // for MDBs remove message destination from assembly descriptor
94
if (ejb instanceof MessageDriven) {
95             try {
96                 AssemblyDescriptor assemblyDescriptor = ejbJar.getSingleAssemblyDescriptor();
97                 String JavaDoc mdLinkName = ((MessageDriven) ejb).getMessageDestinationLink();
98                 MessageDestination[] messageDestinations = assemblyDescriptor.getMessageDestination();
99                 for (MessageDestination messageDestination : messageDestinations) {
100                     if (messageDestination.getMessageDestinationName().equals(mdLinkName)) {
101                         assemblyDescriptor.removeMessageDestination(messageDestination);
102                         break;
103                     }
104                 }
105             } catch (VersionNotSupportedException ex) {
106                 ErrorManager.getDefault().notify(ex);
107             }
108         }
109         beans.removeEjb(ejb);
110         writeDD();
111         if (deleteClasses) {
112             deleteClasses();
113         }
114     }
115     
116     public EjbReference createEjbReference() {
117         AntArtifact[] antArtifacts = AntArtifactQuery.findArtifactsByType(project, JavaProjectConstants.ARTIFACT_TYPE_JAR);
118         AntArtifact moduleJarTarget;
119         if (antArtifacts == null || antArtifacts.length == 0) {
120             moduleJarTarget = null;
121         } else {
122             moduleJarTarget = antArtifacts[0];
123         }
124         return new EjbReferenceImpl(moduleJarTarget, (EntityAndSession) ejb);
125     }
126     
127     private FileObject findBeanFo() {
128         return classPath.findResource(ejb.getEjbClass().replace('.','/')+".java"); // NOI18N
129
}
130     
131     public DataObject getBeanDo() {
132         FileObject src = findBeanFo();
133         try {
134             if (src != null) {
135                 return DataObject.find(src);
136             }
137         } catch (DataObjectNotFoundException ex) {
138             ErrorManager.getDefault().notify(ex);
139         }
140         return null;
141     }
142     
143     public ElementHandle<TypeElement> getBeanClass() {
144         try {
145             JavaSource javaSource = JavaSource.forFileObject(findBeanFo());
146             final List JavaDoc<ElementHandle<TypeElement>> result = new ArrayList JavaDoc<ElementHandle<TypeElement>>(1);
147             javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
148                 public void run(CompilationController compilationController) throws IOException JavaDoc {
149                     compilationController.toPhase(Phase.ELEMENTS_RESOLVED);
150                     TypeElement typeElement = compilationController.getElements().getTypeElement(ejb.getEjbClass());
151                     result.add(ElementHandle.create(typeElement));
152                 }
153             }, true);
154             return result.get(0);
155         } catch (IOException JavaDoc ex) {
156             ErrorManager.getDefault().notify(ex);
157             return null;
158         }
159     }
160     
161     /**
162      * gets an ejb reference representation
163      * @return the xml code corresponding to this ejb
164      */

165     public String JavaDoc getRemoteStringRepresentation(String JavaDoc ejbType) {
166         assert ejb instanceof EntityAndSession;
167         EntityAndSession refModel = (EntityAndSession) ejb;
168         String JavaDoc result ="\t<ejb-ref>\n" +
169                 "\t\t<ejb-ref-name>ejb/" + ejb.getEjbName() + "</ejb-ref-name>\n"+
170                 "\t\t<ejb-ref-type>" + ejbType + "</ejb-ref-type>\n"+
171                 "\t\t<home>" + refModel.getHome() + "</home>\n"+
172                 "\t\t<remote>" + refModel.getRemote() + "</remote>\n"+
173                 "\t</ejb-ref>\n";
174         return result;
175     }
176     
177     public String JavaDoc getLocalStringRepresentation(String JavaDoc ejbType) {
178         assert ejb instanceof EntityAndSession;
179         EntityAndSession refModel = (EntityAndSession)ejb;
180         String JavaDoc result ="\t<ejb-local-ref>\n" +
181                 "\t\t<ejb-ref-name>ejb/" + ejb.getEjbName() + "</ejb-ref-name>\n"+
182                 "\t\t<ejb-ref-type>" + ejbType + "</ejb-ref-type>\n"+
183                 "\t\t<local-home>" + refModel.getLocalHome() + "</local-home>\n"+
184                 "\t\t<local>" + refModel.getLocal() + "</local>\n"+
185                 "\t</ejb-local-ref>\n";
186         return result;
187     }
188     
189     
190     private void writeDD() throws IOException JavaDoc {
191         org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(classPath.getRoots()[0]);
192         FileObject ddFile = ejbModule.getDeploymentDescriptor();
193         DDProvider.getDefault().getMergedDDRoot(ejbModule.getMetadataUnit()).write(ddFile);
194     }
195     
196     private boolean isEjbUsed(EjbRelationshipRole role, String JavaDoc ejbName) {
197         return role != null &&
198                 role.getRelationshipRoleSource() != null &&
199                 ejbName.equals(role.getRelationshipRoleSource().getEjbName());
200     }
201     
202     private void deleteRelationships(String JavaDoc ejbName) {
203         Relationships relationships = ejbJar.getSingleRelationships();
204         if (relationships != null) {
205             EjbRelation[] relations = relationships.getEjbRelation();
206             if (relations != null) {
207                 for (EjbRelation ejbRelation : relations) {
208                     if (isEjbUsed(ejbRelation.getEjbRelationshipRole(), ejbName) || isEjbUsed(ejbRelation.getEjbRelationshipRole2(), ejbName)) {
209                         relationships.removeEjbRelation(ejbRelation);
210                     }
211                 }
212                 if (relationships.sizeEjbRelation() == 0) {
213                     ejbJar.setRelationships(null);
214                 }
215             }
216         }
217     }
218     
219     private void deleteTraces() {
220         String JavaDoc ejbName = ejb.getEjbName();
221         String JavaDoc ejbNameCompare = ejbName + "";
222         deleteRelationships(ejbName);
223         AssemblyDescriptor assemblyDescriptor = ejbJar.getSingleAssemblyDescriptor();
224         if (assemblyDescriptor != null) {
225             ContainerTransaction[] containerTransactions = assemblyDescriptor.getContainerTransaction();
226             for (ContainerTransaction containerTransaction : containerTransactions) {
227                 Method[] methods = containerTransaction.getMethod();
228                 methods = methods == null ? new Method[0] : methods;
229                 for (Method method : methods) {
230                     if (ejbNameCompare.equals(method.getEjbName())) {
231                         containerTransaction.removeMethod(method);
232                         if (containerTransaction.sizeMethod() == 0) {
233                             assemblyDescriptor.removeContainerTransaction(containerTransaction);
234                         }
235                     }
236                 }
237             }
238             MethodPermission[] permissions = assemblyDescriptor.getMethodPermission();
239             for (int i =0; i < permissions.length; i++) {
240                 Method[] methods = permissions[i].getMethod();
241                 methods = methods== null ? new Method[0]:methods;
242                 for (int method =0; method < methods.length; method++) {
243                     if (ejbNameCompare.equals(methods[method].getEjbName())) {
244                         permissions[i].removeMethod(methods[method]);
245                         if (permissions[i].sizeMethod() == 0) {
246                             assemblyDescriptor.removeMethodPermission(permissions[i]);
247                         }
248                     }
249                 }
250             }
251         }
252     }
253     
254     private FileObject getFileObject(String JavaDoc className) {
255         assert className != null: "cannot find null className";
256         return classPath.findResource(className.replace('.', '/') + ".java");
257     }
258     
259     private void deleteClasses() {
260         ArrayList JavaDoc<FileObject> classFileObjects = new ArrayList JavaDoc<FileObject>();
261         classFileObjects.add(getFileObject(ejb.getEjbClass()));
262         if (ejb instanceof EntityAndSession) {
263             EntityAndSession entityAndSessionfModel = (EntityAndSession) ejb;
264             if (entityAndSessionfModel.getLocalHome() != null) {
265                 classFileObjects.add(getFileObject(entityAndSessionfModel.getLocalHome()));
266                 classFileObjects.add(getFileObject(entityAndSessionfModel.getLocal()));
267                 classFileObjects.add(getFileObject(entityAndSessionfModel.getLocal() + "Business"));
268             }
269             if (entityAndSessionfModel.getHome() != null) {
270                 classFileObjects.add(getFileObject(entityAndSessionfModel.getHome()));
271                 classFileObjects.add(getFileObject(entityAndSessionfModel.getRemote()));
272                 classFileObjects.add(getFileObject(entityAndSessionfModel.getRemote() + "Business"));
273             }
274         }
275         for (FileObject fileObject : classFileObjects) {
276             if (fileObject != null) {
277                 try {
278                     DataObject dataObject = DataObject.find(fileObject);
279                     assert dataObject != null: ("cannot find DataObject for " + fileObject.getPath());
280                     if (dataObject != null) {
281                         dataObject.delete();
282                     }
283                 } catch (IOException JavaDoc ioe) {
284                     ErrorManager.getDefault().notify(ErrorManager.USER, ioe);
285                 }
286             }
287         }
288     }
289 }
290
Popular Tags