001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.jboss4.config;
043:
044: import java.util.Iterator;
045: import java.util.LinkedList;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Set;
049: import org.netbeans.modules.j2ee.jboss4.config.EjbDeploymentConfiguration.BEAN_TYPE;
050: import org.netbeans.modules.j2ee.jboss4.config.gen.EnterpriseBeans;
051: import org.netbeans.modules.j2ee.jboss4.config.gen.Entity;
052: import org.netbeans.modules.j2ee.jboss4.config.gen.Jboss;
053: import org.netbeans.modules.j2ee.jboss4.config.gen.MessageDriven;
054: import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceRef;
055: import org.netbeans.modules.j2ee.jboss4.config.gen.Session;
056:
057: /**
058: * This class implements the core of the jboss.xml file modifications.
059: *
060: * @author lkotouc
061: */
062: final class JbossDataSourceRefModifier {
063:
064: /**
065: * Add a reference to the given resource to the enterprise beans of the given type if it does not exist yet.
066: *
067: * @param modifiedJboss Jboss graph instance being modified
068: * @param resRefName resource reference name
069: * @param beanNames the beans (ejb-name value) which might need to add resource reference specified by resRefName
070: * @param beanType type of bean to add resource reference to
071: * @param jndiName JNDI name of the resource
072: */
073: static void modify(Jboss modifiedJboss, String resRefName,
074: Set beanNames, BEAN_TYPE beanType, String jndiName) {
075:
076: assert (beanNames.size() > 0);
077:
078: if (modifiedJboss.getEnterpriseBeans() == null)
079: modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
080:
081: if (beanType == BEAN_TYPE.SESSION) {
082: addSessionResReference(modifiedJboss, resRefName,
083: beanNames, jndiName);
084: } else if (beanType == BEAN_TYPE.ENTITY) {
085: addEntityResReference(modifiedJboss, resRefName, beanNames,
086: jndiName);
087: }
088: }
089:
090: /**
091: * Add a new resource reference to the session beans without it.
092: *
093: * @param modifiedJboss Jboss instance being modified
094: * @param resRefName resource reference name
095: * @param sessionNames the sessions (ejb-name value) which might need to add resource reference specified by resRefName
096: * @param jndiName JNDI name of the resource
097: */
098: private static void addSessionResReference(Jboss modifiedJboss,
099: String resRefName, Set sessionNames, String jndiName) {
100:
101: List/*<Session>*/sesssionsWithoutReference = new LinkedList();
102:
103: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
104:
105: Session[] sessions = eb.getSession();
106: for (int i = 0; i < sessions.length; i++) {
107: String ejbName = sessions[i].getEjbName();
108: if (sessionNames.contains(ejbName)) { // session found -> check whether it has the resource-ref
109: sessionNames.remove(ejbName); // we don't care about it anymore
110: ResourceRef[] resourceRefs = sessions[i]
111: .getResourceRef();
112: int j = 0;
113: for (; j < resourceRefs.length; j++) {
114: String rrn = resourceRefs[j].getResRefName();
115: if (resRefName.equals(rrn))
116: break; // resource-ref found, continuing with the next session
117: }
118: if (j == resourceRefs.length) // resource-ref not found
119: sesssionsWithoutReference.add(sessions[i]);
120: }
121: }
122:
123: //no session tag yet (sessions.length == 0) or
124: //there are sessions in sessionNames which were not found among the existing ones (those were not removed)
125: for (Iterator it = sessionNames.iterator(); it.hasNext();) {
126: String sessionName = (String) it.next();
127: Session session = new Session();
128: session.setEjbName(sessionName);
129: session.setJndiName(sessionName);
130:
131: //add the new session to enterprise-beans
132: eb.addSession(session);
133:
134: //add the new session to the list of sessions without the resource reference
135: sesssionsWithoutReference.add(session);
136: }
137:
138: //the resource reference will be added to each session without it
139: for (Iterator it = sesssionsWithoutReference.iterator(); it
140: .hasNext();) {
141: ResourceRef newRR = new ResourceRef();
142: newRR.setResRefName(resRefName);
143: newRR.setJndiName(jndiName);
144: Session session = (Session) it.next();
145: session.addResourceRef(newRR);
146: }
147:
148: }
149:
150: /**
151: * Add a new resource reference to the entity beans without it.
152: *
153: * @param modifiedJboss Jboss instance being modified
154: * @param resRefName resource reference name
155: * @param entityNames the entities (ejb-name value) which might need to add resource reference specified by resRefName
156: * @param jndiName JNDI name of the resource
157: */
158: private static void addEntityResReference(Jboss modifiedJboss,
159: String resRefName, Set entityNames, String jndiName) {
160:
161: List/*<Entity>*/entitiesWithoutReference = new LinkedList();
162:
163: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
164:
165: Entity[] entities = eb.getEntity();
166: for (int i = 0; i < entities.length; i++) {
167: String ejbName = entities[i].getEjbName();
168: if (entityNames.contains(ejbName)) { // entity found -> check whether it has the resource-ref
169: entityNames.remove(ejbName); // we don't care about it anymore
170: ResourceRef[] resourceRefs = entities[i]
171: .getResourceRef();
172: int j = 0;
173: for (; j < resourceRefs.length; j++) {
174: String rrn = resourceRefs[j].getResRefName();
175: if (resRefName.equals(rrn))
176: break; // resource-ref found, continuing with the next entity
177: }
178: if (j == resourceRefs.length) // resource-ref not found
179: entitiesWithoutReference.add(entities[i]);
180: }
181: }
182:
183: //no entity tag yet (entities.length == 0) or
184: //there are entities in entityNames which were not found among the existing ones (those were not removed)
185: for (Iterator it = entityNames.iterator(); it.hasNext();) {
186: String entityName = (String) it.next();
187: Entity entity = new Entity();
188: entity.setEjbName(entityName);
189: entity.setJndiName(entityName);
190:
191: //add the new entity to enterprise-beans
192: eb.addEntity(entity);
193:
194: //add the new entity to the list of entities without the resource reference
195: entitiesWithoutReference.add(entity);
196: }
197:
198: //the resource reference will be added to each entity without it
199: for (Iterator it = entitiesWithoutReference.iterator(); it
200: .hasNext();) {
201: ResourceRef newRR = new ResourceRef();
202: newRR.setResRefName(resRefName);
203: newRR.setJndiName(jndiName);
204: Entity entity = (Entity) it.next();
205: entity.addResourceRef(newRR);
206: }
207:
208: }
209:
210: /**
211: * Add a reference to the given resource to the message-driven beans if it does not exist yet.
212: *
213: * @param modifiedJboss Jboss graph instance being modified
214: * @param resRefName resource reference name
215: * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
216: * which might need to add resource reference specified by resRefName
217: * @param jndiName JNDI name of the resource
218: *
219: * @deprecated
220: */
221: static void modifyMsgDrv(Jboss modifiedJboss, String resRefName,
222: Map beans, String jndiName) {
223:
224: assert (beans.size() > 0);
225:
226: if (modifiedJboss.getEnterpriseBeans() == null)
227: modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
228:
229: addMsgDrvResReference(modifiedJboss, resRefName, beans,
230: jndiName);
231: }
232:
233: /**
234: * Add a new resource reference to the message-driven beans without it.
235: *
236: * @param modifiedJboss Jboss instance being modified
237: * @param resRefName resource reference name
238: * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
239: * which might need to add resource reference specified by resRefName
240: * @param jndiName JNDI name of the resource
241: *
242: * @deprecated
243: */
244: private static void addMsgDrvResReference(Jboss modifiedJboss,
245: String resRefName, Map beans, String jndiName) {
246:
247: List/*<Entity>*/msgdrvsWithoutReference = new LinkedList();
248:
249: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
250:
251: MessageDriven[] msgDrivens = eb.getMessageDriven();
252: for (int i = 0; i < msgDrivens.length; i++) {
253: String ejbName = msgDrivens[i].getEjbName();
254: if (beans.containsKey(ejbName)) { // msgdrv found -> check whether it has the resource-ref
255: beans.remove(ejbName); // we don't care about it anymore
256: ResourceRef[] resourceRefs = msgDrivens[i]
257: .getResourceRef();
258: int j = 0;
259: for (; j < resourceRefs.length; j++) {
260: String rrn = resourceRefs[j].getResRefName();
261: if (resRefName.equals(rrn))
262: break; // resource-ref found, continuing with the next mdb
263: }
264: if (j == resourceRefs.length) // resource-ref not found
265: msgdrvsWithoutReference.add(msgDrivens[i]);
266: }
267: }
268:
269: //no message-driven tag yet (msgDrivens.length == 0) or
270: //there are MDBs in beans map which were not found among the existing ones (those were not removed)
271: for (Iterator it = beans.entrySet().iterator(); it.hasNext();) {
272: Map.Entry entry = (Map.Entry) it.next();
273: MessageDriven mdb = new MessageDriven();
274: mdb.setEjbName((String) entry.getKey());
275: mdb.setDestinationJndiName((String) entry.getValue());
276:
277: //add the new mdb to enterprise-beans
278: eb.addMessageDriven(mdb);
279:
280: //add the new mdb to the list of mdbs without the resource reference
281: msgdrvsWithoutReference.add(mdb);
282: }
283:
284: //the resource reference will be added to each mdb without it
285: for (Iterator it = msgdrvsWithoutReference.iterator(); it
286: .hasNext();) {
287: ResourceRef newRR = new ResourceRef();
288: newRR.setResRefName(resRefName);
289: newRR.setJndiName(jndiName);
290: MessageDriven mdb = (MessageDriven) it.next();
291: mdb.addResourceRef(newRR);
292: }
293:
294: }
295:
296: /**
297: * Add a reference to the given resource to the message-driven bean if it does not exist yet.
298: *
299: * @param modifiedJboss Jboss graph instance being modified
300: * @param resRefName resource reference name
301: * @param mdbName the MDB (ejb-name) which might need to add resource reference specified by resRefName
302: * @param jndiName JNDI name of the resource
303: */
304: static void modifyMsgDrv(Jboss modifiedJboss, String resRefName,
305: String mdbName, String jndiName) {
306:
307: if (modifiedJboss.getEnterpriseBeans() == null)
308: modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
309:
310: addMsgDrvResReference(modifiedJboss, resRefName, mdbName,
311: jndiName);
312: }
313:
314: private static void addMsgDrvResReference(Jboss modifiedJboss,
315: String resRefName, String mdbName, String jndiName) {
316:
317: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
318:
319: for (MessageDriven mdb : eb.getMessageDriven()) {
320: String ejbName = mdb.getEjbName();
321: if (mdbName.equals(ejbName)) { // msgdrv found -> check whether it has the resource-ref
322: ResourceRef[] resourceRefs = mdb.getResourceRef();
323: int j = 0;
324: for (; j < resourceRefs.length; j++) {
325: String rrn = resourceRefs[j].getResRefName();
326: if (resRefName.equals(rrn))
327: return; // resource-ref found
328: }
329: if (j == resourceRefs.length) {// resource-ref not found
330: ResourceRef newRR = new ResourceRef();
331: newRR.setResRefName(resRefName);
332: newRR.setJndiName(jndiName);
333: mdb.addResourceRef(newRR);
334: }
335: }
336: }
337: }
338:
339: }
|