KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.enterprise.deploy.model.DDBean JavaDoc;
33 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
34 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
35 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
36 import javax.enterprise.deploy.model.XpathListener JavaDoc;
37 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
38 import javax.swing.text.BadLocationException JavaDoc;
39 import javax.swing.text.StyledDocument JavaDoc;
40 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException;
41 import org.netbeans.modules.j2ee.jboss4.config.gen.EnterpriseBeans;
42 import org.netbeans.modules.j2ee.jboss4.config.gen.Jboss;
43 import org.netbeans.modules.j2ee.jboss4.config.gen.MessageDriven;
44 import org.openide.DialogDisplayer;
45 import org.openide.ErrorManager;
46 import org.openide.NotifyDescriptor;
47 import org.openide.cookies.EditorCookie;
48 import org.openide.cookies.SaveCookie;
49 import org.openide.filesystems.FileUtil;
50 import org.openide.loaders.DataObject;
51 import org.openide.loaders.DataObjectNotFoundException;
52 import org.openide.util.NbBundle;
53
54
55 /**
56  * EJB module deployment configuration handles jboss.xml configuration file creation.
57  *
58  * @author sherold, lkotouc
59  */

60 public class EjbDeploymentConfiguration extends JBDeploymentConfiguration
61         implements PropertyChangeListener JavaDoc, XpathListener JavaDoc {
62     
63     private static final String JavaDoc SESSION_RESOURCE_REF = "/ejb-jar/enterprise-beans/session/resource-ref"; // NOI18N
64
private static final String JavaDoc ENTITY_RESOURCE_REF = "/ejb-jar/enterprise-beans/entity/resource-ref"; //NOI1*N
65
private static final String JavaDoc MSGDRV_RESOURCE_REF = "/ejb-jar/enterprise-beans/message-driven/resource-ref"; // NOI18N
66
private static final String JavaDoc SESSION_EJB_REF = "/ejb-jar/enterprise-beans/session/ejb-ref"; // NOI18N
67
private static final String JavaDoc ENTITY_EJB_REF = "/ejb-jar/enterprise-beans/entity/ejb-ref"; // NOI18N
68
private static final String JavaDoc MSGDRV_EJB_REF = "/ejb-jar/enterprise-beans/message-driven/ejb-ref"; // NOI18N
69
private static final String JavaDoc MSGDRV = "/ejb-jar/enterprise-beans/message-driven"; // NOI18N
70
private static final String JavaDoc MSGDRV_MSG_DEST = MSGDRV + "/message-destination-link"; // NOI18N
71
private static final String JavaDoc SESSION_MSG_DEST_REF = "/ejb-jar/enterprise-beans/session/message-destination-ref"; // NOI18N
72
private static final String JavaDoc ENTITY_MSG_DEST_REF = "/ejb-jar/enterprise-beans/entity/message-destination-ref"; // NOI18N
73
private static final String JavaDoc MSGDRV_MSG_DEST_REF = "/ejb-jar/enterprise-beans/message-driven/message-destination-ref"; // NOI18N
74

75     static class BEAN_TYPE {
76         private String JavaDoc type;
77         
78         private BEAN_TYPE(String JavaDoc type) { this.type = type; }
79         String JavaDoc getType() { return type; }
80         
81         static final BEAN_TYPE SESSION = new BEAN_TYPE("session"); // NOI18N
82
static final BEAN_TYPE ENTITY = new BEAN_TYPE("entity"); // NOI18N
83
static final BEAN_TYPE MSGDRV = new BEAN_TYPE("message-driven"); // NOI18N
84
}
85     
86     private File JavaDoc jbossFile;
87     private Jboss jboss;
88     
89     // stores ejb-name between MSGDRV Xpath event and MSGDRV_MSG_DEST are fired
90
private String JavaDoc tempEjbName;
91     
92     private final boolean isEJB3;
93         
94     /**
95      * Creates a new instance of EjbDeploymentConfiguration
96      */

97     public EjbDeploymentConfiguration(DeployableObject JavaDoc deployableObject) {
98         super(deployableObject);
99         if (deployableObject != null && deployableObject.getDDBeanRoot() != null &&
100             "3.0".equals(deployableObject.getDDBeanRoot().getDDBeanRootVersion())) { // NOI81N
101
isEJB3 = true;
102         }
103         else {
104             isEJB3 = false;
105         }
106     }
107     
108     /**
109      * EjbDeploymentConfiguration initialization. This method should be called before
110      * this class is being used.
111      *
112      * @param file jboss.xml file.
113      * @param resourceDir directory containing definition for enterprise resources.
114      */

115     public void init(File JavaDoc file, File JavaDoc resourceDir) {
116         super.init(resourceDir);
117         this.jbossFile = file;
118         getJboss();
119         if (deploymentDescriptorDO == null) {
120             try {
121                 deploymentDescriptorDO = deploymentDescriptorDO.find(FileUtil.toFileObject(jbossFile));
122                 deploymentDescriptorDO.addPropertyChangeListener(this);
123             } catch(DataObjectNotFoundException donfe) {
124                 ErrorManager.getDefault().notify(donfe);
125             }
126         }
127         
128         if (deplObj != null && deplObj.getDDBeanRoot() != null ) {
129             //listen on the resource-ref element
130
DDBeanRoot JavaDoc root = deplObj.getDDBeanRoot();
131             root.addXpathListener(SESSION_RESOURCE_REF, this);
132             root.addXpathListener(ENTITY_RESOURCE_REF, this);
133             root.addXpathListener(SESSION_EJB_REF, this);
134             root.addXpathListener(ENTITY_EJB_REF, this);
135             root.addXpathListener(MSGDRV_RESOURCE_REF, this);
136             root.addXpathListener(MSGDRV_EJB_REF, this);
137             root.addXpathListener(MSGDRV, this);
138             root.addXpathListener(MSGDRV_MSG_DEST, this);
139             root.addXpathListener(SESSION_MSG_DEST_REF, this);
140             root.addXpathListener(ENTITY_MSG_DEST_REF, this);
141             root.addXpathListener(MSGDRV_MSG_DEST_REF, this);
142         }
143     }
144        
145     /**
146      * Return jboss graph. If it was not created yet, load it from the file
147      * and cache it. If the file does not exist, generate it.
148      *
149      * @return jboss graph or null if the jboss.xml file is not parseable.
150      */

151     public synchronized Jboss getJboss() {
152         if (jboss == null) {
153             try {
154                 if (jbossFile.exists()) {
155                     // load configuration if already exists
156
try {
157                         jboss = jboss.createGraph(jbossFile);
158                     } catch (IOException JavaDoc ioe) {
159                         ErrorManager.getDefault().notify(ioe);
160                     } catch (RuntimeException JavaDoc re) {
161                         // jboss.xml is not parseable, do nothing
162
}
163                 } else {
164                     // create jboss.xml if it does not exist yet
165
jboss = generateJboss();
166                     writefile(jbossFile, jboss);
167                 }
168             } catch (ConfigurationException JavaDoc ce) {
169                 ErrorManager.getDefault().notify(ce);
170             }
171         }
172         return jboss;
173     }
174     
175     /**
176      * Listen to jboss.xml document changes.
177      */

178     public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
179         if (evt.getPropertyName() == DataObject.PROP_MODIFIED &&
180             evt.getNewValue() == Boolean.FALSE) {
181             
182             if (evt.getSource() == deploymentDescriptorDO) // dataobject has been modified, jboss graph is out of sync
183
jboss = null;
184             else
185                 super.propertyChange(evt);
186         }
187     }
188    
189     public void fireXpathEvent(XpathEvent JavaDoc xpe) {
190         
191         if (!xpe.isAddEvent())
192             return;
193         
194         DDBean JavaDoc eventDDBean = xpe.getBean();
195         if (SESSION_RESOURCE_REF.equals(eventDDBean.getXpath()) ||
196             ENTITY_RESOURCE_REF.equals(eventDDBean.getXpath()) ||
197             MSGDRV_RESOURCE_REF.equals(eventDDBean.getXpath()))
198         {
199             String JavaDoc[] desc = eventDDBean.getText("description"); // NOI18N
200
String JavaDoc[] name = eventDDBean.getText("res-ref-name"); // NOI18N
201
String JavaDoc[] type = eventDDBean.getText("res-type"); // NOI18N
202
if (name.length > 0 && type.length > 0) {
203                 try {
204                     //we don't know which bean contains the created resource-ref,
205
//thus we must find all beans containing the same resource-ref
206
//as the one just created
207
BEAN_TYPE beanType = (SESSION_RESOURCE_REF.equals(eventDDBean.getXpath()) ?
208                                           BEAN_TYPE.SESSION : (ENTITY_RESOURCE_REF.equals(eventDDBean.getXpath()) ?
209                                           BEAN_TYPE.ENTITY : BEAN_TYPE.MSGDRV));
210
211                     if (beanType != BEAN_TYPE.MSGDRV) { //sessions and entities
212
Set JavaDoc beanNames = null;
213                         if (desc.length > 0 && "javax.sql.DataSource".equals(type[0])) { // NOI18N
214
beanNames = getRelevantBeansDataRef(desc[0], name[0], eventDDBean.getRoot(), beanType);
215                             addResReference(desc[0], name[0], beanNames, beanType);
216                         }
217                         else
218                         if ("javax.mail.Session".equals(type[0])) { // NOI18N
219
beanNames = getRelevantBeansMailRef(name[0], eventDDBean.getRoot(), beanType);
220                             addMailReference(name[0], beanNames, beanType);
221                         }
222                         else
223                         if ("javax.jms.ConnectionFactory".equals(type[0])) { // NOI18N
224
beanNames = getRelevantBeansConnectionFactoryRef(name[0], eventDDBean.getRoot(), beanType);
225                             addConnectionFactoryReference(name[0], beanNames, beanType);
226                         }
227                     }
228                     else { // message-driven beans
229
Map JavaDoc beans = null;
230                         if (desc.length > 0 && "javax.sql.DataSource".equals(type[0])) { // NOI18N
231
beans = getRelevantMsgDrvBeansDataRef(desc[0], name[0], eventDDBean.getRoot());
232                             addMsgDrvResReference(desc[0], name[0], beans);
233                         }
234                         if ("javax.mail.Session".equals(type[0])) { // NOI18N
235
beans = getRelevantMsgDrvBeansMailRef(name[0], eventDDBean.getRoot());
236                             addMsgDrvMailReference(name[0], beans);
237                         }
238                         else
239                         if ("javax.jms.ConnectionFactory".equals(type[0])) { // NOI18N
240
beans = getRelevantMsgDrvBeansConnectionFactoryRef(name[0], eventDDBean.getRoot());
241                             addMsgDrvConnectionFactoryReference(name[0], beans);
242                         }
243                     }
244
245                 } catch (ConfigurationException JavaDoc ce) {
246                     ErrorManager.getDefault().notify(ce);
247                 }
248             }
249         }
250         else if (SESSION_EJB_REF.equals(eventDDBean.getXpath()) ||
251                  ENTITY_EJB_REF.equals(eventDDBean.getXpath()) ||
252                  MSGDRV_EJB_REF.equals(eventDDBean.getXpath()))
253         {
254             String JavaDoc[] name = eventDDBean.getText("ejb-ref-name"); // NOI18N
255
String JavaDoc[] type = eventDDBean.getText("ejb-ref-type"); // NOI18N
256
if (name.length > 0 && type.length > 0
257                     && ("Session".equals(type[0]) || "Entity".equals(type[0]))) { // NOI18N
258
try {
259                     //we don't know which bean contains the created ejb-ref,
260
//thus we must find all beans containing the same ejb-ref
261
//as the one just created
262
BEAN_TYPE beanType = (SESSION_EJB_REF.equals(eventDDBean.getXpath()) ?
263                                           BEAN_TYPE.SESSION : (ENTITY_EJB_REF.equals(eventDDBean.getXpath()) ?
264                                           BEAN_TYPE.ENTITY : BEAN_TYPE.MSGDRV));
265
266                     if (beanType != BEAN_TYPE.MSGDRV) { //sessions and entities
267
Set JavaDoc beanNames = getRelevantBeansEjbRef(name[0], eventDDBean.getRoot(), beanType);
268                         addEjbReference(name[0], beanNames, beanType);
269                     }
270                     else { // message-driven beans
271
Map JavaDoc beans = getRelevantMsgDrvBeansEjbRef(name[0], eventDDBean.getRoot());
272                         addMsgDrvEjbReference(name[0], beans);
273                     }
274
275                 } catch (ConfigurationException JavaDoc ce) {
276                     ErrorManager.getDefault().notify(ce);
277                 }
278             }
279         }
280         else if (MSGDRV.equals(eventDDBean.getXpath())) {
281             if (isEJB3) { // do not generate MDB destination reference for EJB3 modules (issue #82452)
282
return;
283             }
284             
285             String JavaDoc[] name = eventDDBean.getText("ejb-name"); // NOI18N
286
String JavaDoc[] type = eventDDBean.getText("message-destination-type"); // NOI18N
287
String JavaDoc[] dest = eventDDBean.getText("message-destination-link"); // NOI18N
288

289             if (name.length == 0)
290                 return;
291             
292             if (dest.length == 0) {
293                 tempEjbName = name[0];
294             } else {
295                 try {
296                     addMDB(name[0], dest[0]);
297                 } catch (ConfigurationException JavaDoc ce) {
298                     ErrorManager.getDefault().notify(ce);
299                 }
300             }
301         }
302         else if (MSGDRV_MSG_DEST.equals(eventDDBean.getXpath())) { //is fired right after the MSGDRV Xpath event, too
303
if (isEJB3) { // do not generate MDB destination reference for EJB3 modules (issue #82452)
304
return;
305             }
306
307             if (tempEjbName == null) // MSGDRV was not fired before
308
return;
309             
310             try {
311                 String JavaDoc dest = xpe.getBean().getText();
312                 addMDB(tempEjbName, dest);
313             } catch (ConfigurationException JavaDoc ce) {
314                 ErrorManager.getDefault().notify(ce);
315             } finally {
316                 tempEjbName = null;
317             }
318         }
319         
320         else if (SESSION_MSG_DEST_REF.equals(eventDDBean.getXpath()) ||
321                  ENTITY_MSG_DEST_REF.equals(eventDDBean.getXpath()) ||
322                  MSGDRV_MSG_DEST_REF.equals(eventDDBean.getXpath()))
323         {
324             String JavaDoc[] name = eventDDBean.getText("message-destination-ref-name"); // NOI18N
325
String JavaDoc[] type = eventDDBean.getText("message-destination-type"); // NOI18N
326

327             if (name.length > 0) {
328                 
329                 String JavaDoc destPrefix = "";
330                 if (type.length > 0) {
331                     if (type[0].equals("javax.jms.Queue")) // NOI18N
332
destPrefix = JBOSS4_MSG_QUEUE_JNDI_PREFIX;
333                     else
334                     if (type[0].equals("javax.jms.Topic")) // NOI18N
335
destPrefix = JBOSS4_MSG_TOPIC_JNDI_PREFIX;
336                 }
337                 
338                 try {
339                     //we don't know which bean contains the created resource-ref,
340
//thus we must find all beans containing the same resource-ref
341
//as the one just created
342
BEAN_TYPE beanType = (SESSION_MSG_DEST_REF.equals(eventDDBean.getXpath()) ?
343                                           BEAN_TYPE.SESSION : (ENTITY_MSG_DEST_REF.equals(eventDDBean.getXpath()) ?
344                                           BEAN_TYPE.ENTITY : BEAN_TYPE.MSGDRV));
345
346                     if (beanType != BEAN_TYPE.MSGDRV) { //sessions and entities
347
Set JavaDoc beanNames = getRelevantBeansMsgDestRef(name[0], eventDDBean.getRoot(), beanType);
348                         addMsgDestReference(name[0], destPrefix, beanNames, beanType);
349                     }
350                     else { // message-driven beans
351
Map JavaDoc beans = getRelevantMsgDrvBeansMsgDestRef(name[0], eventDDBean.getRoot());
352                         addMsgDrvMsgDestReference(name[0], destPrefix, beans);
353                     }
354                 
355                 } catch (ConfigurationException JavaDoc ce) {
356                     ErrorManager.getDefault().notify(ce);
357                 }
358             }
359         }
360     }
361     
362     /**
363      * Searches for the beans of the give type referring to the given data source.
364      * It returns the names for the beans found.
365      *
366      * @param desc searched data source description tag value
367      * @param resRefName searched data source (res-ref-name tag value)
368      * @param root root bean to search from
369      * @param beanType type of bean to search for
370      *
371      * @return set of the names (ejb-name) of the beans refering to the resource-ref
372      * with the same description and res-ref-name
373      */

374     private Set JavaDoc getRelevantBeansDataRef(String JavaDoc desc, String JavaDoc resRefName, DDBeanRoot JavaDoc root, BEAN_TYPE beanType) {
375         
376         Set JavaDoc beanNames = new HashSet JavaDoc();
377         DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/" + beanType.getType()); // NOI18N
378
for (int i = 0; i < beans.length; i++) {
379             DDBean JavaDoc bean = beans[i];
380             DDBean JavaDoc[] resRefs = bean.getChildBean("resource-ref");
381             for (int j = 0; resRefs != null && j < resRefs.length; j++) {
382                 String JavaDoc[] descs = resRefs[j].getText("description"); // NOI18N
383
String JavaDoc[] names = resRefs[j].getText("res-ref-name"); // NOI18N
384
String JavaDoc[] types = resRefs[j].getText("res-type"); // NOI18N
385
if (descs.length > 0 && names.length > 0 && types.length > 0 &&
386                     descs[0].equals(desc) && names[0].equals(resRefName) && "javax.sql.DataSource".equals(types[0])) { // NOI18N
387
//store bean's ejb-name tag value
388
beanNames.add(bean.getChildBean("ejb-name")[0].getText()); // NOI18N
389
break;
390                 }
391             }
392         }
393         
394         return beanNames;
395     }
396     
397     /**
398      * Searches for the message-driven beans referring to the given resource.
399      * It returns the names and message destination links for the beans found.
400      *
401      * @param desc searched data source description tag value
402      * @param resRefName searched res-ref-name tag value
403      * @param root root bean to search from
404      *
405      * @return map where the keys are the bean names (ejb-name) and
406      * the values are the message destinations (message-destination-link)
407      */

408     private Map JavaDoc getRelevantMsgDrvBeansDataRef(String JavaDoc desc, String JavaDoc resRefName, DDBeanRoot JavaDoc root) {
409         
410         HashMap JavaDoc/*<String, String>*/ beanMap = new HashMap JavaDoc(); // maps ejb-name to message-destination-link
411
DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/message-driven"); // NOI18N
412
for (int i = 0; i < beans.length; i++) {
413             DDBean JavaDoc bean = beans[i];
414             DDBean JavaDoc[] resRefs = bean.getChildBean("resource-ref");
415             for (int j = 0; resRefs != null && j < resRefs.length; j++) {
416                 String JavaDoc[] descs = resRefs[j].getText("description"); // NOI18N
417
String JavaDoc[] names = resRefs[j].getText("res-ref-name"); // NOI18N
418
String JavaDoc[] types = resRefs[j].getText("res-type"); // NOI18N
419
if (descs.length > 0 && names.length > 0 && types.length > 0 &&
420                     descs[0].equals(desc) && names[0].equals(resRefName) && "javax.sql.DataSource".equals(types[0])) { // NOI18N
421
//store bean's ejb-name and message-destination-link tag values
422
String JavaDoc key = bean.getChildBean("ejb-name")[0].getText();
423                     String JavaDoc value = "";
424                     if (bean.getChildBean("message-destination-link").length > 0) // NOI18N
425
value = bean.getChildBean("message-destination-link")[0].getText();
426                     beanMap.put(key, value); // NOI18N
427
break;
428                 }
429             }
430         }
431         
432         return beanMap;
433     }
434     
435     /**
436      * @param ejbRefName searched ejb-ref-name tag value
437      * @param root root bean to search from
438      * @param beanType type of bean to search for
439      *
440      * @return set of the names (ejb-name) of the beans refering to the ejb-ref
441      * with the same ejb-ref-name
442      */

443     private Set JavaDoc getRelevantBeansEjbRef(String JavaDoc ejbRefName, DDBeanRoot JavaDoc root, BEAN_TYPE beanType) {
444         
445         Set JavaDoc beanNames = new HashSet JavaDoc();
446         DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/" + beanType.getType()); // NOI18N
447
for (int i = 0; i < beans.length; i++) {
448             DDBean JavaDoc bean = beans[i];
449             DDBean JavaDoc[] ejbRefs = bean.getChildBean("ejb-ref");
450             for (int j = 0; ejbRefs != null && j < ejbRefs.length; j++) {
451                 String JavaDoc[] names = ejbRefs[j].getText("ejb-ref-name"); // NOI18N
452
String JavaDoc[] types = ejbRefs[j].getText("ejb-ref-type"); // NOI18N
453
if (names.length > 0 && types.length > 0 && names[0].equals(ejbRefName)
454                     && ("Session".equals(types[0]) || "Entity".equals(types[0]))) { // NOI18N
455
//store bean's ejb-name tag value
456
beanNames.add(bean.getChildBean("ejb-name")[0].getText()); // NOI18N
457
break;
458                 }
459             }
460         }
461         
462         return beanNames;
463     }
464     
465     interface JbossModifier {
466         public void modify(Jboss modifiedJboss);
467     }
468     
469     /**
470      * Searches for the message-driven beans referring to the given ejb.
471      * It returns the names and message destination links for the beans found.
472      *
473      * @param ejbRefName searched ejb-ref-name tag value
474      * @param root root bean to search from
475      *
476      * @return map where the keys are the bean names (ejb-name) and
477      * the values are the message destinations (message-destination-link)
478      */

479     private Map JavaDoc getRelevantMsgDrvBeansEjbRef(String JavaDoc ejbRefName, DDBeanRoot JavaDoc root) {
480         
481         HashMap JavaDoc/*<String, String>*/ beanMap = new HashMap JavaDoc(); // maps ejb-name to message-destination-link
482
DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/message-driven"); // NOI18N
483
for (int i = 0; i < beans.length; i++) {
484             DDBean JavaDoc bean = beans[i];
485             DDBean JavaDoc[] ejbRefs = bean.getChildBean("ejb-ref");
486             for (int j = 0; ejbRefs != null && j < ejbRefs.length; j++) {
487                 String JavaDoc[] names = ejbRefs[j].getText("ejb-ref-name"); // NOI18N
488
String JavaDoc[] types = ejbRefs[j].getText("ejb-ref-type"); // NOI18N
489
if (names.length > 0 && types.length > 0 && names[0].equals(ejbRefName)
490                     && ("Session".equals(types[0]) || "Entity".equals(types[0]))) { // NOI18N
491
//store bean's ejb-name and message-destination-link tag values
492
String JavaDoc key = bean.getChildBean("ejb-name")[0].getText();
493                     String JavaDoc value = "";
494                     if (bean.getChildBean("message-destination-link").length > 0) // NOI18N
495
value = bean.getChildBean("message-destination-link")[0].getText();
496                     beanMap.put(key, value); // NOI18N
497
break;
498                 }
499             }
500         }
501         
502         return beanMap;
503     }
504     
505     /**
506      * Searches for the beans of the give type referring to the given mail service.
507      * It returns the names for the beans found.
508      *
509      * @param resRefName searched mail service (res-ref-name tag value)
510      * @param root root bean to search from
511      * @param beanType type of bean to search for
512      *
513      * @return set of the names (ejb-name) of the beans refering to the resource-ref
514      * with the same description and res-ref-name
515      */

516     private Set JavaDoc getRelevantBeansMailRef(String JavaDoc resRefName, DDBeanRoot JavaDoc root, BEAN_TYPE beanType) {
517         
518         Set JavaDoc beanNames = new HashSet JavaDoc();
519         DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/" + beanType.getType()); // NOI18N
520
for (int i = 0; i < beans.length; i++) {
521             DDBean JavaDoc bean = beans[i];
522             DDBean JavaDoc[] resRefs = bean.getChildBean("resource-ref");
523             for (int j = 0; resRefs != null && j < resRefs.length; j++) {
524                 String JavaDoc[] names = resRefs[j].getText("res-ref-name"); // NOI18N
525
String JavaDoc[] types = resRefs[j].getText("res-type"); // NOI18N
526
if (names.length > 0 && types.length > 0 &&
527                     names[0].equals(resRefName) && "javax.mail.Session".equals(types[0])) { // NOI18N
528
//store bean's ejb-name tag value
529
beanNames.add(bean.getChildBean("ejb-name")[0].getText()); // NOI18N
530
break;
531                 }
532             }
533         }
534         
535         return beanNames;
536     }
537     
538     /**
539      * Searches for the message-driven beans referring to the given mail service.
540      * It returns the names and message destination links for the beans found.
541      *
542      * @param resRefName searched res-ref-name tag value
543      * @param root root bean to search from
544      *
545      * @return map where the keys are the bean names (ejb-name) and
546      * the values are the message destinations (message-destination-link)
547      */

548     private Map JavaDoc getRelevantMsgDrvBeansMailRef(String JavaDoc resRefName, DDBeanRoot JavaDoc root) {
549         
550         HashMap JavaDoc/*<String, String>*/ beanMap = new HashMap JavaDoc(); // maps ejb-name to message-destination-link
551
DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/message-driven"); // NOI18N
552
for (int i = 0; i < beans.length; i++) {
553             DDBean JavaDoc bean = beans[i];
554             DDBean JavaDoc[] resRefs = bean.getChildBean("resource-ref");
555             for (int j = 0; resRefs != null && j < resRefs.length; j++) {
556                 String JavaDoc[] names = resRefs[j].getText("res-ref-name"); // NOI18N
557
String JavaDoc[] types = resRefs[j].getText("res-type"); // NOI18N
558
if (names.length > 0 && types.length > 0 &&
559                     names[0].equals(resRefName) && "javax.mail.Session".equals(types[0])) { // NOI18N
560
//store bean's ejb-name and message-destination-link tag values
561
String JavaDoc key = bean.getChildBean("ejb-name")[0].getText();
562                     String JavaDoc value = "";
563                     if (bean.getChildBean("message-destination-link").length > 0) // NOI18N
564
value = bean.getChildBean("message-destination-link")[0].getText();
565                     beanMap.put(key, value); // NOI18N
566
break;
567                 }
568             }
569         }
570         
571         return beanMap;
572     }
573     
574     /**
575      * Searches for the beans of the given type referring to the given connection factory.
576      * It returns the names for the beans found.
577      *
578      * @param resRefName searched connection factory (res-ref-name tag value)
579      * @param root root bean to search from
580      * @param beanType type of bean to search for
581      *
582      * @return set of the names (ejb-name) of the beans refering to the resource-ref
583      * with the same res-ref-name
584      */

585     private Set JavaDoc getRelevantBeansConnectionFactoryRef(String JavaDoc resRefName, DDBeanRoot JavaDoc root, BEAN_TYPE beanType) {
586         
587         Set JavaDoc beanNames = new HashSet JavaDoc();
588         DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/" + beanType.getType()); // NOI18N
589
for (int i = 0; i < beans.length; i++) {
590             DDBean JavaDoc bean = beans[i];
591             DDBean JavaDoc[] resRefs = bean.getChildBean("resource-ref");
592             for (int j = 0; resRefs != null && j < resRefs.length; j++) {
593                 String JavaDoc[] names = resRefs[j].getText("res-ref-name"); // NOI18N
594
String JavaDoc[] types = resRefs[j].getText("res-type"); // NOI18N
595
if (names.length > 0 && types.length > 0 &&
596                     names[0].equals(resRefName) && "javax.jms.ConnectionFactory".equals(types[0])) { // NOI18N
597
//store bean's ejb-name tag value
598
beanNames.add(bean.getChildBean("ejb-name")[0].getText()); // NOI18N
599
break;
600                 }
601             }
602         }
603         
604         return beanNames;
605     }
606
607     /**
608      * Searches for the message-driven beans referring to the given connection factory.
609      * It returns the names and message destination links for the beans found.
610      *
611      * @param resRefName searched res-ref-name tag value
612      * @param root root bean to search from
613      *
614      * @return map where the keys are the bean names (ejb-name) and
615      * the values are the message destinations (message-destination-link)
616      */

617     private Map JavaDoc getRelevantMsgDrvBeansConnectionFactoryRef(String JavaDoc resRefName, DDBeanRoot JavaDoc root) {
618         
619         HashMap JavaDoc/*<String, String>*/ beanMap = new HashMap JavaDoc(); // maps ejb-name to message-destination-link
620
DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/message-driven"); // NOI18N
621
for (int i = 0; i < beans.length; i++) {
622             DDBean JavaDoc bean = beans[i];
623             DDBean JavaDoc[] resRefs = bean.getChildBean("resource-ref");
624             for (int j = 0; resRefs != null && j < resRefs.length; j++) {
625                 String JavaDoc[] names = resRefs[j].getText("res-ref-name"); // NOI18N
626
String JavaDoc[] types = resRefs[j].getText("res-type"); // NOI18N
627
if (names.length > 0 && types.length > 0 &&
628                     names[0].equals(resRefName) && "javax.jms.ConnectionFactory".equals(types[0])) { // NOI18N
629
//store bean's ejb-name and message-destination-link tag values
630
String JavaDoc key = bean.getChildBean("ejb-name")[0].getText();
631                     String JavaDoc value = "";
632                     if (bean.getChildBean("message-destination-link").length > 0) // NOI18N
633
value = bean.getChildBean("message-destination-link")[0].getText();
634                     beanMap.put(key, value); // NOI18N
635
break;
636                 }
637             }
638         }
639         
640         return beanMap;
641     }
642     
643     /**
644      * @param msgDestRefName searched message-destination-ref tag value
645      * @param root root bean to search from
646      * @param beanType type of bean to search for
647      *
648      * @return set of the names (ejb-name) of the beans refering to the message-destination-ref
649      * with the same message-destination-ref-name
650      */

651     private Set JavaDoc getRelevantBeansMsgDestRef(String JavaDoc msgDestRefName, DDBeanRoot JavaDoc root, BEAN_TYPE beanType) {
652         
653         Set JavaDoc beanNames = new HashSet JavaDoc();
654         DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/" + beanType.getType()); // NOI18N
655
for (int i = 0; i < beans.length; i++) {
656             DDBean JavaDoc bean = beans[i];
657             DDBean JavaDoc[] msgDestRefs = bean.getChildBean("message-destination-ref"); // NOI18N
658
for (int j = 0; msgDestRefs != null && j < msgDestRefs.length; j++) {
659                 String JavaDoc[] names = msgDestRefs[j].getText("message-destination-ref-name"); // NOI18N
660
if (names.length > 0 && names[0].equals(msgDestRefName)) {
661                     //store bean's ejb-name tag value
662
beanNames.add(bean.getChildBean("ejb-name")[0].getText()); // NOI18N
663
break;
664                 }
665             }
666         }
667         
668         return beanNames;
669     }
670     
671     /**
672      * @param msgDestRefName searched message-destination-ref tag value
673      * @param root root bean to search from
674      * @param beanType type of bean to search for
675      *
676      * @return set of the names (ejb-name) of the beans refering to the message-destination-ref
677      * with the same message-destination-ref-name
678      */

679     private Map JavaDoc getRelevantMsgDrvBeansMsgDestRef(String JavaDoc msgDestRefName, DDBeanRoot JavaDoc root) {
680         
681         HashMap JavaDoc/*<String, String>*/ beanMap = new HashMap JavaDoc(); // maps ejb-name to message-destination-link
682
DDBean JavaDoc[] beans = root.getChildBean("/ejb-jar/enterprise-beans/message-driven"); // NOI18N
683
for (int i = 0; i < beans.length; i++) {
684             DDBean JavaDoc bean = beans[i];
685             DDBean JavaDoc[] msgDestRefs = bean.getChildBean("message-destination-ref"); // NOI18N
686
for (int j = 0; msgDestRefs != null && j < msgDestRefs.length; j++) {
687                 String JavaDoc[] names = msgDestRefs[j].getText("message-destination-ref-name"); // NOI18N
688
if (names.length > 0 && names[0].equals(msgDestRefName)) {
689                     //store bean's ejb-name tag value
690
String JavaDoc key = bean.getChildBean("ejb-name")[0].getText();
691                     String JavaDoc value = "";
692                     if (bean.getChildBean("message-destination-link").length > 0) // NOI18N
693
value = bean.getChildBean("message-destination-link")[0].getText();
694                     beanMap.put(key, value); // NOI18N
695
break;
696                 }
697             }
698         }
699         
700         return beanMap;
701     }
702     
703     /**
704      * Add a new data source reference to the beans of the given type without it.
705      *
706      * @param desc data source description
707      * @param resRefName data source reference name
708      * @param beanNames the beans (ejb-name value) which might need to add data source reference specified by resRefName
709      * @param beanType type of bean to add data source reference to
710      */

711     private void addResReference(String JavaDoc desc, final String JavaDoc resRefName, final Set JavaDoc beanNames, final BEAN_TYPE beanType)
712     throws ConfigurationException JavaDoc
713     {
714         modifyJboss(new JbossModifier() {
715            public void modify(Jboss modifiedJboss) {
716                String JavaDoc jndiName = JBDeploymentConfiguration.JBOSS4_DATASOURCE_JNDI_PREFIX + resRefName;
717                JbossDataSourceRefModifier.modify(modifiedJboss, resRefName, beanNames, beanType, jndiName);
718            }
719         });
720     }
721
722     /**
723      * Add a new resource reference to the message-driven beans without it.
724      *
725      * @param desc data source description
726      * @param resRefName resource reference name
727      * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
728      * which might need to add resource reference specified by resRefName
729      */

730     private void addMsgDrvResReference(String JavaDoc desc, final String JavaDoc resRefName, final Map JavaDoc beans)
731     throws ConfigurationException JavaDoc
732     {
733         modifyJboss(new JbossModifier() {
734            public void modify(Jboss modifiedJboss) {
735                String JavaDoc jndiName = JBDeploymentConfiguration.JBOSS4_DATASOURCE_JNDI_PREFIX + resRefName;
736                JbossDataSourceRefModifier.modifyMsgDrv(modifiedJboss, resRefName, beans, jndiName);
737            }
738         });
739     }
740
741     /**
742      * Add a new ejb reference to the beans of the given type without it.
743      *
744      * @param ejbRefName ejb reference name
745      * @param beanNames the beans (ejb-name value) which might need to add ejb reference specified by ejbRefName
746      * @param beanType type of bean to add ejb reference to
747      */

748     private void addEjbReference(final String JavaDoc ejbRefName, final Set JavaDoc beanNames, final BEAN_TYPE beanType)
749     throws ConfigurationException JavaDoc
750     {
751         modifyJboss(new JbossModifier() {
752            public void modify(Jboss modifiedJboss) {
753                JbossEjbRefModifier.modify(modifiedJboss, ejbRefName, beanNames, beanType);
754            }
755         });
756     }
757
758     /**
759      * Add a new ejb reference to the message-driven beans without it.
760      *
761      * @param ejbRefName ejb reference name
762      * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
763      * which might need to add ejb reference specified by ejbRefName
764      */

765     private void addMsgDrvEjbReference(final String JavaDoc ejbRefName, final Map JavaDoc beans)
766     throws ConfigurationException JavaDoc
767     {
768         modifyJboss(new JbossModifier() {
769            public void modify(Jboss modifiedJboss) {
770                JbossEjbRefModifier.modifyMsgDrv(modifiedJboss, ejbRefName, beans);
771            }
772         });
773     }
774
775     /**
776      * Add a new mail service reference to the beans of the given type without it.
777      *
778      * @param resRefName mail service reference name
779      * @param beanNames the beans (ejb-name value) which might need to add mail service reference specified by resRefName
780      * @param beanType type of bean to add mail service reference to
781      */

782     private void addMailReference(final String JavaDoc resRefName, final Set JavaDoc beanNames, final BEAN_TYPE beanType)
783     throws ConfigurationException JavaDoc
784     {
785         modifyJboss(new JbossModifier() {
786            public void modify(Jboss modifiedJboss) {
787                String JavaDoc jndiName = JBOSS4_MAIL_SERVICE_JNDI_NAME;
788                JbossDataSourceRefModifier.modify(modifiedJboss, resRefName, beanNames, beanType, jndiName);
789            }
790         });
791     }
792
793     /**
794      * Add a new mail service reference to the message-driven beans without it.
795      *
796      * @param resRefName mail service reference name
797      * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
798      * which might need to add mail service reference specified by resRefName
799      */

800     private void addMsgDrvMailReference(final String JavaDoc resRefName, final Map JavaDoc beans)
801     throws ConfigurationException JavaDoc
802     {
803         modifyJboss(new JbossModifier() {
804            public void modify(Jboss modifiedJboss) {
805                String JavaDoc jndiName = JBOSS4_MAIL_SERVICE_JNDI_NAME;
806                JbossDataSourceRefModifier.modifyMsgDrv(modifiedJboss, resRefName, beans, jndiName);
807            }
808         });
809     }
810
811     /**
812      * Add MDB record.
813      *
814      * @param name MDB name (ejb-name)
815      * @param dest MDB destination (message-destination-link)
816      */

817     private void addMDB(final String JavaDoc name, final String JavaDoc dest) throws ConfigurationException JavaDoc {
818         modifyJboss(new JbossModifier() {
819             public void modify(Jboss modifiedJboss) {
820
821                 EnterpriseBeans eb = modifiedJboss.getEnterpriseBeans();
822                 if (eb == null) {
823                     eb = new EnterpriseBeans();
824                     modifiedJboss.setEnterpriseBeans(eb);
825                 }
826                 
827                 // check whether mdb not already defined
828
MessageDriven[] mdbs = eb.getMessageDriven();
829                 for (int i = 0; i < mdbs.length; i++) {
830                     String JavaDoc en = mdbs[i].getEjbName();
831                     if (name.equals(en)) {
832                         // already exists
833
return;
834                     }
835                 }
836
837                 //if it doesn't exist yet, create a new one
838
MessageDriven mdb = new MessageDriven();
839                 mdb.setEjbName(name);
840                 mdb.setDestinationJndiName(dest); // NOI18N
841
eb.addMessageDriven(mdb);
842             }
843         });
844     }
845
846     /**
847      * Add a new connection factory reference to the beans of the given type without it.
848      *
849      * @param resRefName connection factory reference name
850      * @param beanNames the beans (ejb-name value) which might need to add connection factory reference specified by resRefName
851      * @param beanType type of bean to add connection factory reference to
852      */

853     private void addConnectionFactoryReference(final String JavaDoc resRefName, final Set JavaDoc beanNames, final BEAN_TYPE beanType)
854     throws ConfigurationException JavaDoc
855     {
856         modifyJboss(new JbossModifier() {
857            public void modify(Jboss modifiedJboss) {
858                String JavaDoc jndiName = JBOSS4_CONN_FACTORY_JNDI_NAME;
859                JbossDataSourceRefModifier.modify(modifiedJboss, resRefName, beanNames, beanType, jndiName);
860            }
861         });
862     }
863
864     /**
865      * Add a new connection factory reference to the message-driven beans without it.
866      *
867      * @param resRefName connection factory reference name
868      * @param beans the bean names (ejb-name) mapped to the message destinations (message-destination-link)
869      * which might need to add connection factory reference specified by resRefName
870      */

871     private void addMsgDrvConnectionFactoryReference(final String JavaDoc resRefName, final Map JavaDoc beans)
872     throws ConfigurationException JavaDoc
873     {
874         modifyJboss(new JbossModifier() {
875            public void modify(Jboss modifiedJboss) {
876                String JavaDoc jndiName = JBOSS4_CONN_FACTORY_JNDI_NAME;
877                JbossDataSourceRefModifier.modifyMsgDrv(modifiedJboss, resRefName, beans, jndiName);
878            }
879         });
880     }
881     
882     /**
883      * Add a new message destination reference to the beans of the given type without it.
884      *
885      * @param msgDestRefName message destination reference name
886      * @param beanNames the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
887      * @param beanType type of bean to add message destination reference to
888      */

889     private void addMsgDestReference(final String JavaDoc msgDestRefName, final String JavaDoc destPrefix,
890                                      final Set JavaDoc beanNames, final BEAN_TYPE beanType) throws ConfigurationException JavaDoc
891     {
892         modifyJboss(new JbossModifier() {
893            public void modify(Jboss modifiedJboss) {
894                JbossMsgDestRefModifier.modify(modifiedJboss, msgDestRefName, beanNames, beanType, destPrefix);
895            }
896         });
897     }
898
899     /**
900      * Add a new message destination reference to the message driven beans without it.
901      *
902      * @param msgDestRefName message destination reference name
903      * @param beanNames the beans (ejb-name value) which might need to add message destination reference specified by msgDestRefName
904      * @param beanType type of bean to add message destination reference to
905      */

906     private void addMsgDrvMsgDestReference(final String JavaDoc msgDestRefName, final String JavaDoc destPrefix,
907                                      final Map JavaDoc beans) throws ConfigurationException JavaDoc
908     {
909         modifyJboss(new JbossModifier() {
910            public void modify(Jboss modifiedJboss) {
911                JbossMsgDestRefModifier.modifyMsgDrv(modifiedJboss, msgDestRefName, beans, destPrefix);
912            }
913         });
914     }
915
916     /**
917      * Perform jboss changes defined by the jboss modifier. Update editor
918      * content and save changes, if appropriate.
919      *
920      * @param modifier
921      */

922     private void modifyJboss(JbossModifier modifier)
923     throws ConfigurationException JavaDoc
924     {
925         assert deploymentDescriptorDO != null : "DataObject has not been initialized yet"; // NIO18N
926
try {
927             // get the document
928
EditorCookie editor = (EditorCookie)deploymentDescriptorDO.getCookie(EditorCookie.class);
929             StyledDocument JavaDoc doc = editor.getDocument();
930             if (doc == null) {
931                 doc = editor.openDocument();
932             }
933             
934             // get the up-to-date model
935
Jboss newJboss = null;
936             try {
937                 // try to create a graph from the editor content
938
byte[] docString = doc.getText(0, doc.getLength()).getBytes();
939                 newJboss = Jboss.createGraph(new ByteArrayInputStream JavaDoc(docString));
940             } catch (RuntimeException JavaDoc e) {
941                 Jboss oldJboss = getJboss();
942                 if (oldJboss == null) {
943                     // neither the old graph is parseable, there is not much we can do here
944
// TODO: should we notify the user?
945
throw new ConfigurationException JavaDoc(
946                             NbBundle.getMessage(JBDeploymentConfiguration.class, "MSG_jbossXmlCannotParse", "jboss.xml")); // NOI18N
947
}
948                 // current editor content is not parseable, ask whether to override or not
949
NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation(
950                         NbBundle.getMessage(WarDeploymentConfiguration.class, "MSG_jbossXmlNotValid", "jboss.xml"),
951                         NotifyDescriptor.OK_CANCEL_OPTION);
952                 Object JavaDoc result = DialogDisplayer.getDefault().notify(notDesc);
953                 if (result == NotifyDescriptor.CANCEL_OPTION) {
954                     // keep the old content
955
return;
956                 }
957                 // use the old graph
958
newJboss = oldJboss;
959             }
960             
961             // perform changes
962
modifier.modify(newJboss);
963             
964             // save, if appropriate
965
boolean modified = deploymentDescriptorDO.isModified();
966             replaceDocument(doc, newJboss);
967             if (!modified) {
968                 SaveCookie cookie = (SaveCookie)deploymentDescriptorDO.getCookie(SaveCookie.class);
969                 cookie.save();
970             }
971             jboss = newJboss;
972         } catch (BadLocationException JavaDoc ble) {
973             throw (ConfigurationException JavaDoc)(new ConfigurationException JavaDoc().initCause(ble));
974         } catch (IOException JavaDoc ioe) {
975             throw (ConfigurationException JavaDoc)(new ConfigurationException JavaDoc().initCause(ioe));
976         }
977     }
978     
979     // JSR-88 methods ---------------------------------------------------------
980

981     public void save(OutputStream JavaDoc os) throws ConfigurationException JavaDoc {
982         Jboss jboss = getJboss();
983         if (jboss == null) {
984             throw new ConfigurationException JavaDoc("Cannot read configuration, it is probably in an inconsistent state."); // NOI18N
985
}
986         try {
987             jboss.write(os);
988         } catch (IOException JavaDoc ioe) {
989             throw new ConfigurationException JavaDoc(ioe.getLocalizedMessage());
990         }
991     }
992     
993     // private helper methods -------------------------------------------------
994

995     /**
996      * Generate Jboss graph.
997      */

998     private Jboss generateJboss() {
999         return new Jboss();
1000    }
1001    
1002}
1003
Popular Tags