KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.enterprise.deploy.model.DDBean JavaDoc;
29 import javax.enterprise.deploy.model.DDBeanRoot JavaDoc;
30 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
31 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
32 import javax.enterprise.deploy.model.XpathListener JavaDoc;
33 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.StyledDocument JavaDoc;
36 import org.netbeans.modules.j2ee.jboss4.config.gen.EjbRef;
37 import org.netbeans.modules.j2ee.jboss4.config.gen.JbossClient;
38 import org.netbeans.modules.j2ee.jboss4.config.gen.ResourceRef;
39 import org.netbeans.modules.j2ee.jboss4.config.gen.ServiceRef;
40 import org.openide.DialogDisplayer;
41 import org.openide.ErrorManager;
42 import org.openide.NotifyDescriptor;
43 import org.openide.cookies.EditorCookie;
44 import org.openide.cookies.SaveCookie;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.util.NbBundle;
49
50 /**
51  *
52  * @author jungi
53  */

54 public class CarDeploymentConfiguration extends JBDeploymentConfiguration
55         implements PropertyChangeListener JavaDoc, XpathListener JavaDoc {
56     
57     private static final String JavaDoc EJB_REF = "/application-client/ejb-ref"; // NOI18N
58
private static final String JavaDoc SERVICE_REF = "/application-client/service-ref"; // NOI18N
59
private static final String JavaDoc RESOURCE_REF = "/application-client/resource-ref"; // NOI18N
60
private static final String JavaDoc DISPLAY_NAME = "/application-client/display-name"; // NOI18N
61

62     private File JavaDoc jbossClientFile;
63     private JbossClient jbossClient;
64     
65     /** Creates a new instance of CarDeploymentConfiguration */
66     public CarDeploymentConfiguration(DeployableObject JavaDoc deployableObject) {
67         super(deployableObject);
68     }
69     
70     /**
71      * CarDeploymentConfiguration initialization. This method should be called before
72      * this class is being used.
73      *
74      * @param file jboss-client.xml file.
75      * @param resourceDir directory containing definition for enterprise resources.
76      */

77     public void init(File JavaDoc file, File JavaDoc resourceDir) {
78         super.init(resourceDir);
79         this.jbossClientFile = file;
80         getJbossClient();
81         if (deploymentDescriptorDO == null) {
82             try {
83                 deploymentDescriptorDO = deploymentDescriptorDO.find(FileUtil.toFileObject(jbossClientFile));
84                 deploymentDescriptorDO.addPropertyChangeListener(this);
85             } catch(DataObjectNotFoundException donfe) {
86                 ErrorManager.getDefault().notify(donfe);
87             }
88         }
89         
90         if (deplObj != null && deplObj.getDDBeanRoot() != null ) {
91             //listen on the resource-ref element
92
DDBeanRoot JavaDoc root = deplObj.getDDBeanRoot();
93             root.addXpathListener(DISPLAY_NAME, this);
94             root.addXpathListener(RESOURCE_REF, this);
95             root.addXpathListener(EJB_REF, this);
96             root.addXpathListener(SERVICE_REF, this);
97         }
98     }
99     
100     /**
101      * Return JbossClient graph. If it was not created yet, load it from the file
102      * and cache it. If the file does not exist, generate it.
103      *
104      * @return JbossWeb graph or null if the jboss-web.xml file is not parseable.
105      */

106     public synchronized JbossClient getJbossClient() {
107         if (jbossClient == null) {
108             try {
109                 if (jbossClientFile.exists()) {
110                     // load configuration if already exists
111
try {
112                         jbossClient = JbossClient.createGraph(jbossClientFile);
113                     } catch (IOException JavaDoc ioe) {
114                         ErrorManager.getDefault().notify(ioe);
115                     } catch (RuntimeException JavaDoc re) {
116                         // jboss-web.xml is not parseable, do nothing
117
}
118                 } else {
119                     // create jboss-web.xml if it does not exist yet
120
jbossClient = generateJbossClient();
121                     writefile(jbossClientFile, jbossClient);
122                 }
123             } catch (ConfigurationException JavaDoc ce) {
124                 ErrorManager.getDefault().notify(ce);
125             }
126         }
127         return jbossClient;
128     }
129
130     /**
131      * Listen to jboss-web.xml document changes.
132      */

133     public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
134         if (evt.getPropertyName() == DataObject.PROP_MODIFIED &&
135                 evt.getNewValue() == Boolean.FALSE) {
136
137             if (evt.getSource() == deploymentDescriptorDO) // dataobject has been modified, jbossWeb graph is out of sync
138
jbossClient = null;
139             else
140                 super.propertyChange(evt);
141         }
142     }
143    
144     public void fireXpathEvent(XpathEvent JavaDoc xpe) {
145         if (!xpe.isAddEvent())
146             return;
147
148         DDBean JavaDoc eventDDBean = xpe.getBean();
149         if (DISPLAY_NAME.equals(eventDDBean.getXpath())) {
150             String JavaDoc name = eventDDBean.getText();
151             try {
152                 setJndiName(name);
153             } catch (ConfigurationException JavaDoc ce) {
154                 ErrorManager.getDefault().notify(ce);
155             }
156         } else if (RESOURCE_REF.equals(eventDDBean.getXpath())) { //a new resource reference added
157
String JavaDoc[] desc = eventDDBean.getText("description"); // NOI18N
158
String JavaDoc[] name = eventDDBean.getText("res-ref-name"); // NOI18N
159
String JavaDoc[] type = eventDDBean.getText("res-type"); // NOI18N
160
if (name.length > 0 && type.length > 0) {
161                 try {
162                     if (desc.length > 0 && "javax.sql.DataSource".equals(type[0])) // NOI18N
163
addResReference(desc[0], name[0]);
164                     else
165                     if ("javax.mail.Session".equals(type[0])) // NOI18N
166
addMailReference(name[0]);
167                     if ("javax.jms.ConnectionFactory".equals(type[0])) // NOI18N
168
addConnectionFactoryReference(name[0]);
169                 } catch (ConfigurationException JavaDoc ce) {
170                     ErrorManager.getDefault().notify(ce);
171                 }
172             }
173         } else if (EJB_REF.equals(eventDDBean.getXpath())) { // a new ejb reference added
174
String JavaDoc[] name = eventDDBean.getText("ejb-ref-name"); // NOI18N
175
String JavaDoc[] type = eventDDBean.getText("ejb-ref-type"); // NOI18N
176
if (name.length > 0 && type.length > 0
177                     && ("Session".equals(type[0]) || "Entity".equals(type[0]))) { // NOI18N
178
try {
179                     addEjbReference(name[0]);
180                 } catch (ConfigurationException JavaDoc ce) {
181                     ErrorManager.getDefault().notify(ce);
182                 }
183             }
184         } else if (SERVICE_REF.equals(eventDDBean.getXpath())) { //a new message destination reference added
185
String JavaDoc[] name = eventDDBean.getText("service-ref-name"); // NOI18N
186
if (name.length > 0) {
187                 try {
188                     addServiceReference(name[0]);
189                 } catch (ConfigurationException JavaDoc ce) {
190                     ErrorManager.getDefault().notify(ce);
191                 }
192             }
193         }
194         
195     }
196
197     // JSR-88 methods ---------------------------------------------------------
198

199     public void save(OutputStream JavaDoc os) throws ConfigurationException JavaDoc {
200         JbossClient jbossClientDD = getJbossClient();
201         if (jbossClientDD == null) {
202             throw new ConfigurationException JavaDoc("Cannot read configuration, it is probably in an inconsistent state."); // NOI18N
203
}
204         try {
205             jbossClientDD.write(os);
206         } catch (IOException JavaDoc ioe) {
207             throw new ConfigurationException JavaDoc(ioe.getLocalizedMessage());
208         }
209     }
210     
211     // private helper methods -------------------------------------------------
212

213     /**
214      * Generate JbossWeb graph.
215      */

216     private JbossClient generateJbossClient() {
217         JbossClient jbossClientDD = new JbossClient();
218         //jbossClientDD.setContextRoot(""); // NOI18N
219
return jbossClientDD;
220     }
221     
222     /**
223      * Add a new resource reference.
224      *
225      * @param desc description
226      * @param name resource reference name
227      */

228     private void addResReference(final String JavaDoc desc, final String JavaDoc name) throws ConfigurationException JavaDoc {
229         modifyJbossClient(new JbossClientModifier() {
230             public void modify(JbossClient modifiedJbossClient) {
231
232                 // check whether resource not already defined
233
ResourceRef resourceRefs[] = modifiedJbossClient.getResourceRef();
234                 for (int i = 0; i < resourceRefs.length; i++) {
235                     String JavaDoc rrn = resourceRefs[i].getResRefName();
236                     if (name.equals(rrn)) {
237                         // already exists
238
return;
239                     }
240                 }
241
242                 //if it doesn't exist yet, create a new one
243
ResourceRef newRR = new ResourceRef();
244                 newRR.setResRefName(name);
245                 newRR.setJndiName(JBOSS4_DATASOURCE_JNDI_PREFIX + name);
246                 modifiedJbossClient.addResourceRef(newRR);
247             }
248         });
249     }
250     
251     /**
252      * Add a new mail service reference.
253      *
254      * @param name mail service name
255      */

256     private void addMailReference(final String JavaDoc name) throws ConfigurationException JavaDoc {
257         modifyJbossClient(new JbossClientModifier() {
258             public void modify(JbossClient modifiedJbossClient) {
259
260                 // check whether mail service not already defined
261
ResourceRef resourceRefs[] = modifiedJbossClient.getResourceRef();
262                 for (int i = 0; i < resourceRefs.length; i++) {
263                     String JavaDoc rrn = resourceRefs[i].getResRefName();
264                     if (name.equals(rrn)) {
265                         // already exists
266
return;
267                     }
268                 }
269
270                 //if it doesn't exist yet, create a new one
271
ResourceRef newRR = new ResourceRef();
272                 newRR.setResRefName(name);
273                 newRR.setJndiName(JBOSS4_MAIL_SERVICE_JNDI_NAME);
274                 modifiedJbossClient.addResourceRef(newRR);
275             }
276         });
277     }
278     
279     /**
280      * Add a new connection factory reference.
281      *
282      * @param name connection factory name
283      */

284     private void addConnectionFactoryReference(final String JavaDoc name) throws ConfigurationException JavaDoc {
285         modifyJbossClient(new JbossClientModifier() {
286             public void modify(JbossClient modifiedJbossClient) {
287
288                 // check whether connection factory not already defined
289
ResourceRef resourceRefs[] = modifiedJbossClient.getResourceRef();
290                 for (int i = 0; i < resourceRefs.length; i++) {
291                     String JavaDoc rrn = resourceRefs[i].getResRefName();
292                     if (name.equals(rrn)) {
293                         // already exists
294
return;
295                     }
296                 }
297
298                 //if it doesn't exist yet, create a new one
299
ResourceRef newRR = new ResourceRef();
300                 newRR.setResRefName(name);
301                 newRR.setJndiName(JBOSS4_CONN_FACTORY_JNDI_NAME);
302                 modifiedJbossClient.addResourceRef(newRR);
303             }
304         });
305     }
306     
307     /**
308      * Add a new ejb reference.
309      *
310      * @param name ejb reference name
311      */

312     private void addEjbReference(final String JavaDoc name) throws ConfigurationException JavaDoc {
313         modifyJbossClient(new JbossClientModifier() {
314             public void modify(JbossClient modifiedJbossClient) {
315
316                 // check whether resource not already defined
317
EjbRef ejbRefs[] = modifiedJbossClient.getEjbRef();
318                 for (int i = 0; i < ejbRefs.length; i++) {
319                     String JavaDoc ern = ejbRefs[i].getEjbRefName();
320                     if (name.equals(ern)) {
321                         // already exists
322
return;
323                     }
324                 }
325
326                 //if it doesn't exist yet, create a new one
327
EjbRef newER = new EjbRef();
328                 newER.setEjbRefName(name);
329                 newER.setJndiName(/*JBOSS4_EJB_JNDI_PREFIX + */name);
330                 modifiedJbossClient.addEjbRef(newER);
331             }
332         });
333     }
334     
335     /**
336      * Add a new jndi-name.
337      *
338      * @param name jndi-name name
339      */

340     private void setJndiName(final String JavaDoc jndiName) throws ConfigurationException JavaDoc {
341         modifyJbossClient(new JbossClientModifier() {
342             public void modify(JbossClient modifiedJbossClient) {
343                 modifiedJbossClient.setJndiName(jndiName);
344             }
345         });
346     }
347     
348     /**
349      * Add a new service reference.
350      *
351      * @param name service reference name
352      */

353     private void addServiceReference(final String JavaDoc name) throws ConfigurationException JavaDoc {
354         modifyJbossClient(new JbossClientModifier() {
355             public void modify(JbossClient modifiedJbossClient) {
356
357                 // check whether resource not already defined
358
ServiceRef serviceRefs[] = modifiedJbossClient.getServiceRef();
359                 for (int i = 0; i < serviceRefs.length; i++) {
360                     String JavaDoc srn = serviceRefs[i].getServiceRefName();
361                     if (name.equals(srn)) {
362                         // already exists
363
return;
364                     }
365                 }
366
367                 //if it doesn't exist yet, create a new one
368
ServiceRef newSR = new ServiceRef();
369                 newSR.setServiceRefName(name);
370                 modifiedJbossClient.addServiceRef(newSR);
371             }
372         });
373     }
374     
375     /**
376      * Perform jbossWeb changes defined by the jbossWeb modifier. Update editor
377      * content and save changes, if appropriate.
378      *
379      * @param modifier
380      */

381     private void modifyJbossClient(JbossClientModifier modifier) throws ConfigurationException JavaDoc {
382         assert deploymentDescriptorDO != null : "DataObject has not been initialized yet"; // NIO18N
383
try {
384             // get the document
385
EditorCookie editor = (EditorCookie)deploymentDescriptorDO.getCookie(EditorCookie.class);
386             StyledDocument JavaDoc doc = editor.getDocument();
387             if (doc == null) {
388                 doc = editor.openDocument();
389             }
390             
391             // get the up-to-date model
392
JbossClient newJbossClient = null;
393             try {
394                 // try to create a graph from the editor content
395
byte[] docString = doc.getText(0, doc.getLength()).getBytes();
396                 newJbossClient = JbossClient.createGraph(new ByteArrayInputStream JavaDoc(docString));
397             } catch (RuntimeException JavaDoc e) {
398                 JbossClient oldJbossClient = getJbossClient();
399                 if (oldJbossClient == null) {
400                     // neither the old graph is parseable, there is not much we can do here
401
// TODO: should we notify the user?
402
throw new ConfigurationException JavaDoc("Configuration data are not parseable cannot perform changes."); // NOI18N
403
}
404                 // current editor content is not parseable, ask whether to override or not
405
NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation(
406                         NbBundle.getMessage(CarDeploymentConfiguration.class, "MSG_jbossClientXmlNotValid"),
407                         NotifyDescriptor.OK_CANCEL_OPTION);
408                 Object JavaDoc result = DialogDisplayer.getDefault().notify(notDesc);
409                 if (result == NotifyDescriptor.CANCEL_OPTION) {
410                     // keep the old content
411
return;
412                 }
413                 // use the old graph
414
newJbossClient = oldJbossClient;
415             }
416             
417             // perform changes
418
modifier.modify(newJbossClient);
419             
420             // save, if appropriate
421
boolean modified = deploymentDescriptorDO.isModified();
422             replaceDocument(doc, newJbossClient);
423             if (!modified) {
424                 SaveCookie cookie = (SaveCookie)deploymentDescriptorDO.getCookie(SaveCookie.class);
425                 cookie.save();
426             }
427             jbossClient = newJbossClient;
428         } catch (BadLocationException JavaDoc ble) {
429             throw (ConfigurationException JavaDoc)(new ConfigurationException JavaDoc().initCause(ble));
430         } catch (IOException JavaDoc ioe) {
431             throw (ConfigurationException JavaDoc)(new ConfigurationException JavaDoc().initCause(ioe));
432         }
433     }
434     
435     // private helper interface -----------------------------------------------
436

437     private interface JbossClientModifier {
438         void modify(JbossClient modifiedJbossClient);
439     }
440 }
441
Popular Tags