KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > entres > MessageDestinationPanelController


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 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres;
20
21 import java.io.IOException JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.api.project.Project;
25 import org.netbeans.api.project.ProjectUtils;
26 import org.netbeans.api.project.ui.OpenProjects;
27 import org.netbeans.modules.j2ee.dd.api.common.MessageDestination;
28 import org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef;
29 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
30 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor;
31 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
32 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
33 import org.netbeans.modules.j2ee.dd.api.ejb.MessageDriven;
34 import org.openide.ErrorManager;
35
36 /**
37  * Provide controller functionality for MessageDestinationPanel
38  * @author ChrisWebster
39  */

40 public class MessageDestinationPanelController {
41     
42     public ProjectView[] getJMSProjectRepresentation() {
43         Project[] proj = OpenProjects.getDefault().getOpenProjects();
44         // TODO add support for destinations from web projects
45
List JavaDoc<ProjectView> projectViews = new LinkedList JavaDoc<ProjectView>();
46         for (int i=0; i<proj.length; i++) {
47             if (org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(proj[i]).length > 0) {
48                 projectViews.add(new ProjectView(proj[i]));
49             }
50         }
51         return projectViews.toArray(new ProjectView[projectViews.size()]);
52     }
53     
54     public EjbJar getEjbJar(Object JavaDoc item) throws IOException JavaDoc {
55         Project project = ((ProjectView) item).getProject();
56         org.netbeans.modules.j2ee.api.ejbjar.EjbJar apiEjbJar = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project)[0];
57         return org.netbeans.modules.j2ee.dd.api.ejb.DDProvider.getDefault().getMergedDDRoot(apiEjbJar.getMetadataUnit());
58     }
59     
60     public List JavaDoc<JMSDestination> extractEjbProjectDestinations(EjbJar ejbJar, Object JavaDoc selectedItem) {
61         Project project = ((ProjectView) selectedItem).getProject();
62         List JavaDoc<JMSDestination> jmsDestinations = new LinkedList JavaDoc<JMSDestination>();
63         
64         AssemblyDescriptor assemblyDescriptor = ejbJar.getSingleAssemblyDescriptor();
65         if (assemblyDescriptor != null) {
66             try {
67                 MessageDestination[] destination = assemblyDescriptor.getMessageDestination();
68                 if (destination != null) {
69                     for (int i = 0; i < destination.length; i++) {
70                         String JavaDoc name = destination[i].getMessageDestinationName();
71                         String JavaDoc type = findEjbBinding(name, ejbJar);
72                         if (type != null) {
73                             jmsDestinations.add(new JMSDestination(name, project, type));
74                         }
75                     }
76                 }
77             } catch (VersionNotSupportedException vnse) {
78                 ErrorManager.getDefault().notify(vnse);
79             }
80         }
81         
82         return jmsDestinations;
83     }
84     
85     private String JavaDoc findEjbBinding(String JavaDoc destinationName, EjbJar ejbJar) {
86         if (ejbJar.getEnterpriseBeans() == null ||
87                 ejbJar.getEnterpriseBeans().getEjbs() == null) {
88             return null;
89         }
90         // see if a MDB exists with a destination link and matching type
91
MessageDriven[] messageDrivenBeans = ejbJar.getEnterpriseBeans().getMessageDriven();
92         if (messageDrivenBeans != null) {
93             for (int i=0; i < messageDrivenBeans.length; i++) {
94                 try {
95                     if (messageDrivenBeans[i].getMessagingType() == null ||
96                             messageDrivenBeans[i].getMessagingType().equals("javax.jms.MessageListener")) {
97                         if (destinationName.equals(messageDrivenBeans[i].getMessageDestinationLink())) {
98                             return messageDrivenBeans[i].getMessageDestinationType();
99                         }
100                     }
101                 } catch (VersionNotSupportedException vnse) {
102                     ErrorManager.getDefault().notify(vnse);
103                 }
104             }
105         }
106         
107         // look at all destination-refs to see if any consume jms messages
108
Ejb[] ejb = ejbJar.getEnterpriseBeans().getEjbs();
109         for (int i=0; i < ejb.length; i++) {
110             try {
111                 MessageDestinationRef[] refs = ejb[i].getMessageDestinationRef();
112                 String JavaDoc type = findDestinationRef(destinationName,refs);
113                 if (type != null) {
114                     return type;
115                 }
116             } catch (VersionNotSupportedException vnse) {
117                 ErrorManager.getDefault().notify(vnse);
118             }
119         }
120         
121         return null;
122     }
123     
124     private String JavaDoc findDestinationRef(String JavaDoc destination, MessageDestinationRef[] refs) {
125         String JavaDoc type = null;
126         if (refs != null) {
127             for (int i=0; i<refs.length; i++) {
128                 boolean consumes =
129                         org.netbeans.modules.j2ee.dd.api.common.MessageDestinationRef.MESSAGE_DESTINATION_USAGE_CONSUMES.equals(refs[i].getMessageDestinationUsage());
130                 String JavaDoc destinationType = refs[i].getMessageDestinationType();
131                 boolean jms =
132                         "javax.jms.Queue".equals(destinationType) || //NOI18N
133
"javax.jms.Topic".equals(destinationType); //NOI18N
134
if (consumes && jms) {
135                     if (destination.equals(refs[i].getMessageDestinationLink())) {
136                         type = destinationType;
137                     }
138                 }
139             }
140         }
141         return type;
142     }
143     
144     private static class ProjectView {
145         private final Project project;
146         public ProjectView(Project project) {
147             this.project = project;
148         }
149         
150         public String JavaDoc toString() {
151             return ProjectUtils.getInformation(project).getDisplayName();
152         }
153         
154         public Project getProject() {
155             return project;
156         }
157         
158     }
159     
160 }
Popular Tags