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.MessageDestinationRef;
054: import org.netbeans.modules.j2ee.jboss4.config.gen.MessageDriven;
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 JbossMsgDestRefModifier {
063:
064: /**
065: * Add a reference to the given message destination 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 msgDestRefName message destination reference name
069: * @param beanNames the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
070: * @param beanType type of bean to add message destination reference to
071: * @param destPrefix prefix of the message destination
072: * @param destName message destination name
073: */
074: static void modify(Jboss modifiedJboss, String msgDestRefName,
075: Set beanNames, BEAN_TYPE beanType, String destPrefix,
076: String destName) {
077:
078: assert (beanNames.size() > 0);
079:
080: if (modifiedJboss.getEnterpriseBeans() == null)
081: modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
082:
083: if (beanType == BEAN_TYPE.SESSION) {
084: addSessionMsgDestReference(modifiedJboss, msgDestRefName,
085: beanNames, destPrefix, destName);
086: } else if (beanType == BEAN_TYPE.ENTITY) {
087: addEntityMsgDestReference(modifiedJboss, msgDestRefName,
088: beanNames, destPrefix, destName);
089: }
090: }
091:
092: /**
093: * Add a new message destination reference to the session beans without it.
094: *
095: * @param modifiedJboss Jboss instance being modified
096: * @param resRefName message destination reference name
097: * @param sessionNames the sessions (ejb-name value) which might need to add message destination reference specified by msgDestRefName
098: * @param destPrefix prefix of the message destination
099: * @param destName message destination name
100: */
101: private static void addSessionMsgDestReference(Jboss modifiedJboss,
102: String msgDestRefName, Set sessionNames, String destPrefix,
103: String destName) {
104:
105: List/*<Session>*/sesssionsWithoutReference = new LinkedList();
106:
107: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
108:
109: Session[] sessions = eb.getSession();
110: for (int i = 0; i < sessions.length; i++) {
111: String ejbName = sessions[i].getEjbName();
112: if (sessionNames.contains(ejbName)) { // session found -> check whether it has the message-destination-ref
113: sessionNames.remove(ejbName); // we don't care about it anymore
114: MessageDestinationRef[] msgDestRefs = sessions[i]
115: .getMessageDestinationRef();
116: int j = 0;
117: for (; j < msgDestRefs.length; j++) {
118: String mdrn = msgDestRefs[j]
119: .getMessageDestinationRefName();
120: if (msgDestRefName.equals(mdrn))
121: break; // message-destination-ref found, continuing with the next session
122: }
123: if (j == msgDestRefs.length) // resource-ref not found
124: sesssionsWithoutReference.add(sessions[i]);
125: }
126: }
127:
128: //no session tag yet (sessions.length == 0) or
129: //there are sessions in sessionNames which were not found among the existing ones (those were not removed)
130: for (Iterator it = sessionNames.iterator(); it.hasNext();) {
131: String sessionName = (String) it.next();
132: Session session = new Session();
133: session.setEjbName(sessionName);
134: session.setJndiName(sessionName);
135:
136: //add the new session to enterprise-beans
137: eb.addSession(session);
138:
139: //add the new session to the list of sessions without the resource reference
140: sesssionsWithoutReference.add(session);
141: }
142:
143: //the message destination reference will be added to each session without it
144: for (Iterator it = sesssionsWithoutReference.iterator(); it
145: .hasNext();) {
146: MessageDestinationRef mdr = new MessageDestinationRef();
147: mdr.setMessageDestinationRefName(msgDestRefName);
148: String jndiName = getJndiName(destName, destPrefix);
149: mdr.setJndiName(jndiName);
150: Session session = (Session) it.next();
151: session.addMessageDestinationRef(mdr);
152: }
153:
154: }
155:
156: /**
157: * Add a new message destination reference to the entity beans without it.
158: *
159: * @param modifiedJboss Jboss instance being modified
160: * @param resRefName message destination reference name
161: * @param sessionNames the entities (ejb-name value) which might need to add message destination reference specified by msgDestRefName
162: * @param destPrefix prefix of the message destination
163: * @param destName message destination name
164: */
165: private static void addEntityMsgDestReference(Jboss modifiedJboss,
166: String msgDestRefName, Set entityNames, String destPrefix,
167: String destName) {
168:
169: List/*<Entity>*/entitiesWithoutReference = new LinkedList();
170:
171: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
172:
173: Entity[] entities = eb.getEntity();
174: for (int i = 0; i < entities.length; i++) {
175: String ejbName = entities[i].getEjbName();
176: if (entityNames.contains(ejbName)) { // entity found -> check whether it has the message-destination-ref
177: entityNames.remove(ejbName); // we don't care about it anymore
178: MessageDestinationRef[] msgDestRefs = entities[i]
179: .getMessageDestinationRef();
180: int j = 0;
181: for (; j < msgDestRefs.length; j++) {
182: String mdrn = msgDestRefs[j]
183: .getMessageDestinationRefName();
184: if (msgDestRefName.equals(mdrn))
185: break; // message-destination-ref found, continuing with the next session
186: }
187: if (j == msgDestRefs.length) // resource-ref not found
188: entitiesWithoutReference.add(entities[i]);
189: }
190: }
191:
192: //no entity tag yet (entities.length == 0) or
193: //there are entities in entityNames which were not found among the existing ones (those were not removed)
194: for (Iterator it = entityNames.iterator(); it.hasNext();) {
195: String entityName = (String) it.next();
196: Entity entity = new Entity();
197: entity.setEjbName(entityName);
198: entity.setJndiName(entityName);
199:
200: //add the new entity to enterprise-beans
201: eb.addEntity(entity);
202:
203: //add the new entity to the list of entities without the resource reference
204: entitiesWithoutReference.add(entity);
205: }
206:
207: //the resource reference will be added to each entity without it
208: for (Iterator it = entitiesWithoutReference.iterator(); it
209: .hasNext();) {
210: MessageDestinationRef mdr = new MessageDestinationRef();
211: mdr.setMessageDestinationRefName(msgDestRefName);
212: String jndiName = getJndiName(destName, destPrefix);
213: mdr.setJndiName(jndiName);
214: Entity entity = (Entity) it.next();
215: entity.addMessageDestinationRef(mdr);
216: }
217:
218: }
219:
220: /**
221: * Add a reference to the given message destination to the message-driven beans if it does not exist yet.
222: *
223: * @param modifiedJboss Jboss graph instance being modified
224: * @param msgDestRefName message destination reference name
225: * @param beans the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
226: * @param destPrefix prefix of the message destination
227: *
228: * @deprecated
229: */
230: static void modifyMsgDrv(Jboss modifiedJboss,
231: String msgDestRefName, Map beans, String destPrefix) {
232:
233: assert (beans.size() > 0);
234:
235: if (modifiedJboss.getEnterpriseBeans() == null)
236: modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
237:
238: addMsgDrvMsgDestReference(modifiedJboss, msgDestRefName, beans,
239: destPrefix);
240: }
241:
242: /**
243: * Add a new message destination reference to the message-driven beans without it.
244: *
245: * @param modifiedJboss Jboss instance being modified
246: * @param msgDestRefName message destination reference name
247: * @param beans the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
248: * @param destPrefix prefix of the message destination
249: *
250: * @deprecated
251: */
252: private static void addMsgDrvMsgDestReference(Jboss modifiedJboss,
253: String msgDestRefName, Map beans, String destPrefix) {
254:
255: List/*<Entity>*/msgdrvsWithoutReference = new LinkedList();
256:
257: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
258:
259: MessageDriven[] msgDrivens = eb.getMessageDriven();
260: for (int i = 0; i < msgDrivens.length; i++) {
261: String ejbName = msgDrivens[i].getEjbName();
262: if (beans.containsKey(ejbName)) { // msgdrv found -> check whether it has the message-destination-ref
263: beans.remove(ejbName); // we don't care about it anymore
264: MessageDestinationRef[] msgDestRefs = msgDrivens[i]
265: .getMessageDestinationRef();
266: int j = 0;
267: for (; j < msgDestRefs.length; j++) {
268: String mdrn = msgDestRefs[j]
269: .getMessageDestinationRefName();
270: if (msgDestRefName.equals(mdrn))
271: break; // message-destination-ref found, continuing with the next mdb
272: }
273: if (j == msgDestRefs.length) // message-destination-ref not found
274: msgdrvsWithoutReference.add(msgDrivens[i]);
275: }
276: }
277:
278: //no message-driven tag yet (msgDrivens.length == 0) or
279: //there are MDBs in beans map which were not found among the existing ones (those were not removed)
280: for (Iterator it = beans.entrySet().iterator(); it.hasNext();) {
281: Map.Entry entry = (Map.Entry) it.next();
282: MessageDriven mdb = new MessageDriven();
283: mdb.setEjbName((String) entry.getKey());
284: mdb.setDestinationJndiName((String) entry.getValue());
285:
286: //add the new mdb to enterprise-beans
287: eb.addMessageDriven(mdb);
288:
289: //add the new mdb to the list of mdbs without the resource reference
290: msgdrvsWithoutReference.add(mdb);
291: }
292:
293: //the resource reference will be added to each mdb without it
294: for (Iterator it = msgdrvsWithoutReference.iterator(); it
295: .hasNext();) {
296: MessageDestinationRef mdr = new MessageDestinationRef();
297: mdr.setMessageDestinationRefName(msgDestRefName);
298: String jndiName = getJndiName(msgDestRefName, destPrefix);
299: mdr.setJndiName(jndiName);
300: MessageDriven mdb = (MessageDriven) it.next();
301: mdb.addMessageDestinationRef(mdr);
302: }
303:
304: }
305:
306: /**
307: * Add a reference to the given message destination to the message-driven beans if it does not exist yet.
308: *
309: * @param modifiedJboss Jboss graph instance being modified
310: * @param msgDestRefName message destination reference name
311: * @param mdbName the MDB (ejb-name value) which might need to add message
312: * destination reference specified by msgDestRefName
313: * @param destPrefix prefix of the message destination
314: * @param destName message destination name
315: */
316: static void modifyMsgDrv(Jboss modifiedJboss,
317: String msgDestRefName, String mdbName, String destPrefix,
318: String destName) {
319:
320: if (modifiedJboss.getEnterpriseBeans() == null)
321: modifiedJboss.setEnterpriseBeans(new EnterpriseBeans());
322:
323: addMsgDrvMsgDestReference(modifiedJboss, msgDestRefName,
324: mdbName, destPrefix, destName);
325: }
326:
327: private static void addMsgDrvMsgDestReference(Jboss modifiedJboss,
328: String msgDestRefName, String mdbName, String destPrefix,
329: String destName) {
330:
331: EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
332:
333: for (MessageDriven mdb : eb.getMessageDriven()) {
334: String ejbName = mdb.getEjbName();
335: if (mdbName.equals(ejbName)) { // msgdrv found -> check whether it has the message-destination-ref
336: MessageDestinationRef[] msgDestRefs = mdb
337: .getMessageDestinationRef();
338: int j = 0;
339: for (; j < msgDestRefs.length; j++) {
340: String mdrn = msgDestRefs[j]
341: .getMessageDestinationRefName();
342: if (msgDestRefName.equals(mdrn))
343: return; // message-destination-ref found
344: }
345: if (j == msgDestRefs.length) { // message-destination-ref not found
346: MessageDestinationRef mdr = new MessageDestinationRef();
347: mdr.setMessageDestinationRefName(msgDestRefName);
348: String jndiName = getJndiName(destName, destPrefix);
349: mdr.setJndiName(jndiName);
350: mdb.addMessageDestinationRef(mdr);
351: }
352: }
353: }
354: }
355:
356: private static String getJndiName(String destName, String destPrefix) {
357: return destPrefix + destName;
358: }
359:
360: }
|