KickJava   Java API By Example, From Geeks To Geeks.

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


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.EjbRef;
29 import org.netbeans.modules.j2ee.jboss4.config.gen.EnterpriseBeans;
30 import org.netbeans.modules.j2ee.jboss4.config.gen.Entity;
31 import org.netbeans.modules.j2ee.jboss4.config.gen.Jboss;
32 import org.netbeans.modules.j2ee.jboss4.config.gen.MessageDriven;
33 import org.netbeans.modules.j2ee.jboss4.config.gen.Session;
34 import org.openide.ErrorManager;
35 import org.openide.util.NbBundle;
36
37 /**
38  * This class implements the core of the jboss.xml file modifications.
39  *
40  * @author lkotouc
41  */

42 final class JbossEjbRefModifier {
43
44     /**
45      * Add a reference to the given ejb to the enterprise beans of the given type if it does not exist yet.
46      *
47      * @param modifiedJboss Jboss graph instance being modified
48      * @param ejbRefName ejb reference name
49      * @param beanNames the beans (ejb-name value) which might need to add ejb reference specified by ejbRefName
50      * @param beanType type of bean to add ejb reference to
51      */

52     static void modify(Jboss modifiedJboss, String JavaDoc ejbRefName, Set JavaDoc beanNames, BEAN_TYPE beanType) {
53
54         assert(beanNames.size() > 0);
55
56         if (modifiedJboss.getEnterpriseBeans() == null)
57             modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
58
59         if (beanType == BEAN_TYPE.SESSION) {
60             addSessionEjbReference(modifiedJboss, ejbRefName, beanNames);
61         } else
62         if (beanType == BEAN_TYPE.ENTITY) {
63             addEntityEjbReference(modifiedJboss, ejbRefName, beanNames);
64         }
65     }
66     
67     /**
68      * Add a new ejb reference to the session beans without it.
69      *
70      * @param modifiedJboss Jboss instance being modified
71      * @param ejbRefName ejb reference name
72      * @param sessionNames the sessions (ejb-name value) which might need to add ejb reference specified by ejbRefName
73      */

74     private static void addSessionEjbReference(Jboss modifiedJboss, String JavaDoc ejbRefName, Set JavaDoc sessionNames) {
75
76         List JavaDoc/*<Session>*/ sesssionsWithoutReference = new LinkedList JavaDoc();
77
78         EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
79         
80         Session[] sessions = eb.getSession();
81         for (int i = 0; i < sessions.length; i++) {
82             String JavaDoc ejbName = sessions[i].getEjbName();
83             if (sessionNames.contains(ejbName)) { // session found -> check whether it has the ejb-ref
84
sessionNames.remove(ejbName); // we don't care about it anymore
85
EjbRef[] ejbRefs = sessions[i].getEjbRef();
86                 int j = 0;
87                 for ( ; j < ejbRefs.length; j++) {
88                     String JavaDoc rrn = ejbRefs[j].getEjbRefName();
89                     if (ejbRefName.equals(rrn))
90                         break; // ejb-ref found, continuing with the next session
91
}
92                 if (j == ejbRefs.length) // ejb-ref not found
93
sesssionsWithoutReference.add(sessions[i]);
94             }
95         }
96
97         //no session tag yet (sessions.length == 0) or
98
//there are sessions in sessionNames which were not found among the existing ones (those were not removed)
99
for (Iterator JavaDoc it = sessionNames.iterator(); it.hasNext(); ) {
100             String JavaDoc sessionName = (String JavaDoc)it.next();
101             Session session = new Session();
102             session.setEjbName(sessionName);
103             session.setJndiName(sessionName);
104
105             //add the new session to enterprise-beans
106
eb.addSession(session);
107
108             //add the new session to the list of sessions without the ejb reference
109
sesssionsWithoutReference.add(session);
110         }
111
112         //the ejb reference will be added to each session without it
113
for (Iterator JavaDoc it = sesssionsWithoutReference.iterator(); it.hasNext(); ) {
114             EjbRef newER = new EjbRef();
115             newER.setEjbRefName(ejbRefName);
116             newER.setJndiName(JBDeploymentConfiguration.JBOSS4_EJB_JNDI_PREFIX + ejbRefName);
117             Session session = (Session)it.next();
118             session.addEjbRef(newER);
119         }
120
121     }
122     
123     /**
124      * Add a new ejb reference to the entity beans without it.
125      *
126      * @param modifiedJboss Jboss instance being modified
127      * @param ejbRefName ejb reference name
128      * @param entityNames the entities (ejb-name value) which might need to add ejb reference specified by ejbRefName
129      */

130     private static void addEntityEjbReference(Jboss modifiedJboss, String JavaDoc ejbRefName, Set JavaDoc entityNames) {
131
132         List JavaDoc/*<Entity>*/ entitiesWithoutReference = new LinkedList JavaDoc();
133
134         EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
135
136         Entity[] entities = eb.getEntity();
137         for (int i = 0; i < entities.length; i++) {
138             String JavaDoc ejbName = entities[i].getEjbName();
139             if (entityNames.contains(ejbName)) { // entity found -> check whether it has the ejb-ref
140
entityNames.remove(ejbName); // we don't care about it anymore
141
EjbRef[] ejbRefs = entities[i].getEjbRef();
142                 int j = 0;
143                 for ( ; j < ejbRefs.length; j++) {
144                     String JavaDoc rrn = ejbRefs[j].getEjbRefName();
145                     if (ejbRefName.equals(rrn))
146                         break; // ejb-ref found, continuing with the next entity
147
}
148                 if (j == ejbRefs.length) // ejb-ref not found
149
entitiesWithoutReference.add(entities[i]);
150             }
151         }
152
153         //no entity tag yet (entities.length == 0) or
154
//there are entities in entityNames which were not found among the existing ones (those were not removed)
155
for (Iterator JavaDoc it = entityNames.iterator(); it.hasNext(); ) {
156             String JavaDoc entityName = (String JavaDoc)it.next();
157             Entity entity = new Entity();
158             entity.setEjbName(entityName);
159             entity.setJndiName(entityName);
160
161             //add the new entity to enterprise-beans
162
eb.addEntity(entity);
163
164             //add the new entity to the list of entities without the ejb reference
165
entitiesWithoutReference.add(entity);
166         }
167
168         //the ejb reference will be added to each entity without it
169
for (Iterator JavaDoc it = entitiesWithoutReference.iterator(); it.hasNext(); ) {
170             EjbRef newER = new EjbRef();
171             newER.setEjbRefName(ejbRefName);
172             newER.setJndiName(JBDeploymentConfiguration.JBOSS4_EJB_JNDI_PREFIX + ejbRefName);
173             Entity entity = (Entity)it.next();
174             entity.addEjbRef(newER);
175         }
176
177     }
178
179     /**
180      * Add a reference to the given ejb to the message-driven beans if it does not exist yet.
181      *
182      * @param modifiedJboss Jboss graph instance being modified
183      * @param ejbRefName ejb reference name
184      * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
185      * which might need to add ejb reference specified by ejbRefName
186      */

187     static void modifyMsgDrv(Jboss modifiedJboss, String JavaDoc ejbRefName, Map JavaDoc beans) {
188
189         assert(beans.size() > 0);
190
191         if (modifiedJboss.getEnterpriseBeans() == null)
192             modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
193
194         addMsgDrvEjbReference(modifiedJboss, ejbRefName, beans);
195     }
196
197     /**
198      * Add a new ejb reference to the message-driven beans without it.
199      *
200      * @param modifiedJboss Jboss instance being modified
201      * @param ejbRefName ejb reference name
202      * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
203      * which might need to add ejb reference specified by ejbRefName
204      */

205     private static void addMsgDrvEjbReference(Jboss modifiedJboss, String JavaDoc ejbRefName, Map JavaDoc beans) {
206
207         List JavaDoc/*<Entity>*/ msgdrvsWithoutReference = new LinkedList JavaDoc();
208
209         EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
210
211         MessageDriven[] msgDrivens = eb.getMessageDriven();
212         for (int i = 0; i < msgDrivens.length; i++) {
213             String JavaDoc ejbName = msgDrivens[i].getEjbName();
214             if (beans.containsKey(ejbName)) { // msgdrv found -> check whether it has the ejb-ref
215
beans.remove(ejbName); // we don't care about it anymore
216
EjbRef[] ejbRefs = msgDrivens[i].getEjbRef();
217                 int j = 0;
218                 for ( ; j < ejbRefs.length; j++) {
219                     String JavaDoc rrn = ejbRefs[j].getEjbRefName();
220                     if (ejbRefName.equals(rrn))
221                         break; // ejb-ref found, continuing with the next entity
222
}
223                 if (j == ejbRefs.length) // resource-ref not found
224
msgdrvsWithoutReference.add(msgDrivens[i]);
225             }
226         }
227
228         //no message-driven tag yet (msgDrivens.length == 0) or
229
//there are MDBs in beans map which were not found among the existing ones (those were not removed)
230
for (Iterator JavaDoc it = beans.keySet().iterator(); it.hasNext(); ) {
231             String JavaDoc ejbName = (String JavaDoc)it.next();
232             MessageDriven mdb = new MessageDriven();
233             mdb.setEjbName(ejbName);
234             String JavaDoc msgDestination = (String JavaDoc)beans.get(ejbName);
235             mdb.setDestinationJndiName(msgDestination);
236                 
237             //add the new mdb to enterprise-beans
238
eb.addMessageDriven(mdb);
239
240             //add the new mdb to the list of mdbs without the resource reference
241
msgdrvsWithoutReference.add(mdb);
242         }
243
244         //the ejb reference will be added to each mdb without it
245
for (Iterator JavaDoc it = msgdrvsWithoutReference.iterator(); it.hasNext(); ) {
246             EjbRef newER = new EjbRef();
247             newER.setEjbRefName(ejbRefName);
248             newER.setJndiName(JBDeploymentConfiguration.JBOSS4_EJB_JNDI_PREFIX + ejbRefName);
249             MessageDriven mdb = (MessageDriven)it.next();
250             mdb.addEjbRef(newER);
251         }
252
253     }
254     
255 }
256
Popular Tags