KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > config > JbossMsgDestRefModifier


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.jboss4.config;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.modules.j2ee.jboss4.config.EjbDeploymentConfiguration.BEAN_TYPE;
28 import org.netbeans.modules.j2ee.jboss4.config.gen.EnterpriseBeans;
29 import org.netbeans.modules.j2ee.jboss4.config.gen.Entity;
30 import org.netbeans.modules.j2ee.jboss4.config.gen.Jboss;
31 import org.netbeans.modules.j2ee.jboss4.config.gen.MessageDestinationRef;
32 import org.netbeans.modules.j2ee.jboss4.config.gen.MessageDriven;
33 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceManager;
34 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceManagers;
35 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceRef;
36 import org.netbeans.modules.j2ee.jboss4.config.gen.Session;
37 import org.netbeans.modules.schema2beans.AttrProp;
38 import org.openide.ErrorManager;
39 import org.openide.util.NbBundle;
40
41 /**
42  * This class implements the core of the jboss.xml file modifications.
43  *
44  * @author lkotouc
45  */

46 final class JbossMsgDestRefModifier {
47
48     /**
49      * Add a reference to the given message destination to the enterprise beans of the given type if it does not exist yet.
50      *
51      * @param modifiedJboss Jboss graph instance being modified
52      * @param msgDestRefName message destination reference name
53      * @param beanNames the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
54      * @param beanType type of bean to add message destination reference to
55      * @param destPrefix prefix of the message destination
56      */

57     static void modify(Jboss modifiedJboss, String JavaDoc msgDestRefName, Set JavaDoc beanNames, BEAN_TYPE beanType, String JavaDoc destPrefix) {
58
59         assert(beanNames.size() > 0);
60
61         if (modifiedJboss.getEnterpriseBeans() == null)
62             modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
63
64         if (beanType == BEAN_TYPE.SESSION) {
65             addSessionMsgDestReference(modifiedJboss, msgDestRefName, beanNames, destPrefix);
66         } else
67         if (beanType == BEAN_TYPE.ENTITY) {
68             addEntityMsgDestReference(modifiedJboss, msgDestRefName, beanNames, destPrefix);
69         }
70     }
71     
72     /**
73      * Add a new message destination reference to the session beans without it.
74      *
75      * @param modifiedJboss Jboss instance being modified
76      * @param resRefName message destination reference name
77      * @param sessionNames the sessions (ejb-name value) which might need to add message destination reference specified by msgDestRefName
78      * @param destPrefix prefix of the message destination
79      */

80     private static void addSessionMsgDestReference(Jboss modifiedJboss, String JavaDoc msgDestRefName, Set JavaDoc sessionNames, String JavaDoc destPrefix) {
81
82         List JavaDoc/*<Session>*/ sesssionsWithoutReference = new LinkedList JavaDoc();
83
84         EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
85         
86         Session[] sessions = eb.getSession();
87         for (int i = 0; i < sessions.length; i++) {
88             String JavaDoc ejbName = sessions[i].getEjbName();
89             if (sessionNames.contains(ejbName)) { // session found -> check whether it has the message-destination-ref
90
sessionNames.remove(ejbName); // we don't care about it anymore
91
MessageDestinationRef[] msgDestRefs = sessions[i].getMessageDestinationRef();
92                 int j = 0;
93                 for ( ; j < msgDestRefs.length; j++) {
94                     String JavaDoc mdrn = msgDestRefs[j].getMessageDestinationRefName();
95                     if (msgDestRefName.equals(mdrn))
96                         break; // message-destination-ref found, continuing with the next session
97
}
98                 if (j == msgDestRefs.length) // resource-ref not found
99
sesssionsWithoutReference.add(sessions[i]);
100             }
101         }
102
103         //no session tag yet (sessions.length == 0) or
104
//there are sessions in sessionNames which were not found among the existing ones (those were not removed)
105
for (Iterator JavaDoc it = sessionNames.iterator(); it.hasNext(); ) {
106             String JavaDoc sessionName = (String JavaDoc)it.next();
107             Session session = new Session();
108             session.setEjbName(sessionName);
109             session.setJndiName(sessionName);
110
111             //add the new session to enterprise-beans
112
eb.addSession(session);
113
114             //add the new session to the list of sessions without the resource reference
115
sesssionsWithoutReference.add(session);
116         }
117
118         //the message destination reference will be added to each session without it
119
for (Iterator JavaDoc it = sesssionsWithoutReference.iterator(); it.hasNext(); ) {
120             MessageDestinationRef mdr = new MessageDestinationRef();
121             mdr.setMessageDestinationRefName(msgDestRefName);
122             String JavaDoc jndiName = getJndiName(msgDestRefName, destPrefix);
123             mdr.setJndiName(jndiName);
124             Session session = (Session)it.next();
125             session.addMessageDestinationRef(mdr);
126         }
127
128     }
129     
130     /**
131      * Add a new message destination reference to the entity beans without it.
132      *
133      * @param modifiedJboss Jboss instance being modified
134      * @param resRefName message destination reference name
135      * @param sessionNames the entities (ejb-name value) which might need to add message destination reference specified by msgDestRefName
136      * @param destPrefix prefix of the message destination
137      */

138     private static void addEntityMsgDestReference(Jboss modifiedJboss, String JavaDoc msgDestRefName, Set JavaDoc entityNames, String JavaDoc destPrefix) {
139
140         List JavaDoc/*<Entity>*/ entitiesWithoutReference = new LinkedList JavaDoc();
141
142         EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
143
144         Entity[] entities = eb.getEntity();
145         for (int i = 0; i < entities.length; i++) {
146             String JavaDoc ejbName = entities[i].getEjbName();
147             if (entityNames.contains(ejbName)) { // entity found -> check whether it has the message-destination-ref
148
entityNames.remove(ejbName); // we don't care about it anymore
149
MessageDestinationRef[] msgDestRefs = entities[i].getMessageDestinationRef();
150                 int j = 0;
151                 for ( ; j < msgDestRefs.length; j++) {
152                     String JavaDoc mdrn = msgDestRefs[j].getMessageDestinationRefName();
153                     if (msgDestRefName.equals(mdrn))
154                         break; // message-destination-ref found, continuing with the next session
155
}
156                 if (j == msgDestRefs.length) // resource-ref not found
157
entitiesWithoutReference.add(entities[i]);
158             }
159         }
160
161         //no entity tag yet (entities.length == 0) or
162
//there are entities in entityNames which were not found among the existing ones (those were not removed)
163
for (Iterator JavaDoc it = entityNames.iterator(); it.hasNext(); ) {
164             String JavaDoc entityName = (String JavaDoc)it.next();
165             Entity entity = new Entity();
166             entity.setEjbName(entityName);
167             entity.setJndiName(entityName);
168
169             //add the new entity to enterprise-beans
170
eb.addEntity(entity);
171
172             //add the new entity to the list of entities without the resource reference
173
entitiesWithoutReference.add(entity);
174         }
175
176         //the resource reference will be added to each entity without it
177
for (Iterator JavaDoc it = entitiesWithoutReference.iterator(); it.hasNext(); ) {
178             MessageDestinationRef mdr = new MessageDestinationRef();
179             mdr.setMessageDestinationRefName(msgDestRefName);
180             String JavaDoc jndiName = getJndiName(msgDestRefName, destPrefix);
181             mdr.setJndiName(jndiName);
182             Entity entity = (Entity)it.next();
183             entity.addMessageDestinationRef(mdr);
184         }
185
186     }
187     
188     /**
189      * Add a reference to the given message destination to the message-driven beans if it does not exist yet.
190      *
191      * @param modifiedJboss Jboss graph instance being modified
192      * @param msgDestRefName message destination reference name
193      * @param beans the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
194      * @param destPrefix prefix of the message destination
195      */

196     static void modifyMsgDrv(Jboss modifiedJboss, String JavaDoc msgDestRefName, Map JavaDoc beans, String JavaDoc destPrefix) {
197
198         assert(beans.size() > 0);
199
200         if (modifiedJboss.getEnterpriseBeans() == null)
201             modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
202
203         addMsgDrvMsgDestReference(modifiedJboss, msgDestRefName, beans, destPrefix);
204     }
205     
206     /**
207      * Add a new message destination reference to the message-driven beans without it.
208      *
209      * @param modifiedJboss Jboss instance being modified
210      * @param msgDestRefName message destination reference name
211      * @param beans the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
212      * @param destPrefix prefix of the message destination
213      */

214     private static void addMsgDrvMsgDestReference(Jboss modifiedJboss, String JavaDoc msgDestRefName, Map JavaDoc beans, String JavaDoc destPrefix) {
215
216         List JavaDoc/*<Entity>*/ msgdrvsWithoutReference = new LinkedList JavaDoc();
217
218         EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
219
220         MessageDriven[] msgDrivens = eb.getMessageDriven();
221         for (int i = 0; i < msgDrivens.length; i++) {
222             String JavaDoc ejbName = msgDrivens[i].getEjbName();
223             if (beans.containsKey(ejbName)) { // msgdrv found -> check whether it has the message-destination-ref
224
beans.remove(ejbName); // we don't care about it anymore
225
MessageDestinationRef[] msgDestRefs = msgDrivens[i].getMessageDestinationRef();
226                 int j = 0;
227                 for ( ; j < msgDestRefs.length; j++) {
228                     String JavaDoc mdrn = msgDestRefs[j].getMessageDestinationRefName();
229                     if (msgDestRefName.equals(mdrn))
230                         break; // message-destination-ref found, continuing with the next mdb
231
}
232                 if (j == msgDestRefs.length) // message-destination-ref not found
233
msgdrvsWithoutReference.add(msgDrivens[i]);
234             }
235         }
236
237         //no message-driven tag yet (msgDrivens.length == 0) or
238
//there are MDBs in beans map which were not found among the existing ones (those were not removed)
239
for (Iterator JavaDoc it = beans.keySet().iterator(); it.hasNext(); ) {
240             String JavaDoc ejbName = (String JavaDoc)it.next();
241             MessageDriven mdb = new MessageDriven();
242             mdb.setEjbName(ejbName);
243             String JavaDoc msgDestination = (String JavaDoc)beans.get(ejbName);
244             mdb.setDestinationJndiName(msgDestination);
245                 
246             //add the new mdb to enterprise-beans
247
eb.addMessageDriven(mdb);
248
249             //add the new mdb to the list of mdbs without the resource reference
250
msgdrvsWithoutReference.add(mdb);
251         }
252
253         //the resource reference will be added to each mdb without it
254
for (Iterator JavaDoc it = msgdrvsWithoutReference.iterator(); it.hasNext(); ) {
255             MessageDestinationRef mdr = new MessageDestinationRef();
256             mdr.setMessageDestinationRefName(msgDestRefName);
257             String JavaDoc jndiName = getJndiName(msgDestRefName, destPrefix);
258             mdr.setJndiName(jndiName);
259             MessageDriven mdb = (MessageDriven)it.next();
260             mdb.addMessageDestinationRef(mdr);
261         }
262
263     }
264     
265     private static String JavaDoc getJndiName(String JavaDoc msgDestRefName, String JavaDoc destPrefix) {
266         String JavaDoc jndiName = msgDestRefName;
267         // 'jms/' prefix automatically prepended to the selected message destination during 'Send JMS Message' action
268
if (msgDestRefName.startsWith("jms/")) // NOI18N
269
jndiName = destPrefix + msgDestRefName.substring("jms/".length()); //replace 'jms/' with the correct prefix
270

271         return jndiName;
272     }
273     
274 }
275
Popular Tags