KickJava   Java API By Example, From Geeks To Geeks.

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


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.MessageDriven;
32 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceManager;
33 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceManagers;
34 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceRef;
35 import org.netbeans.modules.j2ee.jboss4.config.gen.Session;
36 import org.netbeans.modules.schema2beans.AttrProp;
37 import org.openide.ErrorManager;
38 import org.openide.util.NbBundle;
39
40 /**
41  * This class implements the core of the jboss.xml file modifications.
42  *
43  * @author lkotouc
44  */

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

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

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

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

194     static void modifyMsgDrv(Jboss modifiedJboss, String JavaDoc resRefName, Map JavaDoc beans, String JavaDoc jndiName) {
195
196         assert(beans.size() > 0);
197
198         if (modifiedJboss.getEnterpriseBeans() == null)
199             modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
200
201         addMsgDrvResReference(modifiedJboss, resRefName, beans, jndiName);
202     }
203     
204     /**
205      * Add a new resource reference to the message-driven beans without it.
206      *
207      * @param modifiedJboss Jboss instance being modified
208      * @param resRefName resource reference name
209      * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
210      * which might need to add resource reference specified by resRefName
211      * @param jndiName JNDI name of the resource
212      */

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