KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > config > WarDeploymentConfiguration


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.weblogic9.config;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
30 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.StyledDocument JavaDoc;
33 import org.netbeans.modules.j2ee.weblogic9.config.gen.WeblogicWebApp;
34 import org.netbeans.modules.schema2beans.AttrProp;
35 import org.netbeans.modules.schema2beans.BaseBean;
36 import org.openide.DialogDisplayer;
37 import org.openide.ErrorManager;
38 import org.openide.NotifyDescriptor;
39 import org.openide.cookies.EditorCookie;
40 import org.openide.cookies.SaveCookie;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.loaders.DataObject;
43 import org.openide.loaders.DataObjectNotFoundException;
44 import org.openide.text.NbDocument;
45 import org.openide.util.NbBundle;
46
47 /**
48  * Web module deployment configuration handles creation and updating of the
49  * weblogic.xml configuration file.
50  *
51  * @author sherold
52  */

53 public class WarDeploymentConfiguration extends WLDeploymentConfiguration
54         implements PropertyChangeListener JavaDoc {
55     
56     private File JavaDoc file;
57     private WeblogicWebApp webLogicWebApp;
58     
59     /**
60      * Creates a new instance of WarDeploymentConfiguration
61      */

62     public WarDeploymentConfiguration(DeployableObject JavaDoc deployableObject) {
63         super(deployableObject);
64     }
65     
66     /**
67      * WarDeploymentConfiguration initialization. This method should be called before
68      * this class is being used.
69      *
70      * @param file weblogic.xml file.
71      */

72     public void init(File JavaDoc file) {
73         this.file = file;
74         getWeblogicWebApp();
75         if (dataObject == null) {
76             try {
77                 dataObject = dataObject.find(FileUtil.toFileObject(file));
78                 dataObject.addPropertyChangeListener(this);
79             } catch(DataObjectNotFoundException donfe) {
80                 ErrorManager.getDefault().notify(donfe);
81             }
82         }
83     }
84     
85     /**
86      * Return context path.
87      *
88      * @return context path or null, if the file is not parseable.
89      */

90     public String JavaDoc getContextPath() throws ConfigurationException JavaDoc {
91         WeblogicWebApp webLogicWebApp = getWeblogicWebApp();
92         if (webLogicWebApp == null) { // graph not parseable
93
throw new ConfigurationException JavaDoc("weblogic.xml is not parseable, cannot read the context path value."); // NOI18N
94
}
95         return webLogicWebApp.getContextRoot(0);
96     }
97     
98     /**
99      * Set context path.
100      */

101     public void setContextPath(String JavaDoc contextPath) throws ConfigurationException JavaDoc {
102         // TODO: this contextPath fix code will be removed, as soon as it will
103
// be moved to the web project
104
if (!isCorrectCP(contextPath)) {
105             String JavaDoc ctxRoot = contextPath;
106             java.util.StringTokenizer JavaDoc tok = new java.util.StringTokenizer JavaDoc(contextPath,"/"); //NOI18N
107
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(); //NOI18N
108
while (tok.hasMoreTokens()) {
109                 buf.append("/"+tok.nextToken()); //NOI18N
110
}
111             ctxRoot = buf.toString();
112             NotifyDescriptor desc = new NotifyDescriptor.Message(
113                     NbBundle.getMessage (WarDeploymentConfiguration.class, "MSG_invalidCP", contextPath),
114                     NotifyDescriptor.Message.INFORMATION_MESSAGE);
115             DialogDisplayer.getDefault().notify(desc);
116             contextPath = ctxRoot;
117         }
118         final String JavaDoc newContextPath = contextPath;
119         modifyWeblogicWebApp(new WeblogicWebAppModifier() {
120             public void modify(WeblogicWebApp webLogicWebApp) {
121                 webLogicWebApp.setContextRoot(new String JavaDoc [] {newContextPath});
122             }
123         });
124     }
125     
126     /**
127      * Listen to weblogic.xml document changes.
128      */

129     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
130         if (evt.getPropertyName() == DataObject.PROP_MODIFIED &&
131                 evt.getNewValue() == Boolean.FALSE) {
132             // dataobject has been modified, webLogicWebApp graph is out of sync
133
webLogicWebApp = null;
134         }
135     }
136    
137     /**
138      * Return WeblogicWebApp graph. If it was not created yet, load it from the file
139      * and cache it. If the file does not exist, generate it.
140      *
141      * @return WeblogicWebApp graph or null if the weblogic.xml file is not parseable.
142      */

143     public synchronized WeblogicWebApp getWeblogicWebApp() {
144         if (webLogicWebApp == null) {
145             try {
146                 if (file.exists()) {
147                     // load configuration if already exists
148
try {
149                         webLogicWebApp = WeblogicWebApp.createGraph(file);
150                     } catch (IOException JavaDoc ioe) {
151                         ErrorManager.getDefault().notify(ioe);
152                     } catch (RuntimeException JavaDoc re) {
153                         // weblogic.xml is not parseable, do nothing
154
}
155                 } else {
156                     // create weblogic.xml if it does not exist yet
157
webLogicWebApp = genereateWeblogicWebApp();
158                     writefile(file, webLogicWebApp);
159                 }
160             } catch (ConfigurationException JavaDoc ce) {
161                 ErrorManager.getDefault().notify(ce);
162             }
163         }
164         return webLogicWebApp;
165     }
166     
167     // JSR-88 methods ---------------------------------------------------------
168

169     public void save(OutputStream JavaDoc os) throws ConfigurationException JavaDoc {
170         WeblogicWebApp webLogicWebApp = getWeblogicWebApp();
171         if (webLogicWebApp == null) {
172             throw new ConfigurationException JavaDoc("Cannot read configuration, it is probably in an inconsistent state."); // NOI18N
173
}
174         try {
175             webLogicWebApp.write(os);
176         } catch (IOException JavaDoc ioe) {
177             throw new ConfigurationException JavaDoc(ioe.getLocalizedMessage());
178         }
179     }
180     
181     // private helper methods -------------------------------------------------
182

183     /**
184      * Perform webLogicWebApp changes defined by the webLogicWebApp modifier. Update editor
185      * content and save changes, if appropriate.
186      *
187      * @param modifier
188      */

189     private void modifyWeblogicWebApp(WeblogicWebAppModifier modifier) throws ConfigurationException JavaDoc {
190         assert dataObject != null : "DataObject has not been initialized yet"; // NIO18N
191
try {
192             // get the document
193
EditorCookie editor = (EditorCookie)dataObject.getCookie(EditorCookie.class);
194             StyledDocument JavaDoc doc = editor.getDocument();
195             if (doc == null) {
196                 doc = editor.openDocument();
197             }
198             
199             // get the up-to-date model
200
WeblogicWebApp newWeblogicWebApp = null;
201             try {
202                 // try to create a graph from the editor content
203
byte[] docString = doc.getText(0, doc.getLength()).getBytes();
204                 newWeblogicWebApp = WeblogicWebApp.createGraph(new ByteArrayInputStream JavaDoc(docString));
205             } catch (RuntimeException JavaDoc e) {
206                 WeblogicWebApp oldWeblogicWebApp = getWeblogicWebApp();
207                 if (oldWeblogicWebApp == null) {
208                     // neither the old graph is parseable, there is not much we can do here
209
// TODO: should we notify the user?
210
throw new ConfigurationException JavaDoc("Configuration data are not parseable cannot perform changes."); // NOI18N
211
}
212                 // current editor content is not parseable, ask whether to override or not
213
NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation(
214                         NbBundle.getMessage(WarDeploymentConfiguration.class, "MSG_weblogicXmlNotValid"),
215                         NotifyDescriptor.OK_CANCEL_OPTION);
216                 Object JavaDoc result = DialogDisplayer.getDefault().notify(notDesc);
217                 if (result == NotifyDescriptor.CANCEL_OPTION) {
218                     // keep the old content
219
return;
220                 }
221                 // use the old graph
222
newWeblogicWebApp = oldWeblogicWebApp;
223             }
224             
225             // perform changes
226
modifier.modify(newWeblogicWebApp);
227             
228             // save, if appropriate
229
boolean modified = dataObject.isModified();
230             replaceDocument(doc, newWeblogicWebApp);
231             if (!modified) {
232                 SaveCookie cookie = (SaveCookie)dataObject.getCookie(SaveCookie.class);
233                 if (cookie != null) {
234                     cookie.save();
235                 }
236             }
237             webLogicWebApp = newWeblogicWebApp;
238         } catch (BadLocationException JavaDoc ble) {
239             throw (ConfigurationException JavaDoc)(new ConfigurationException JavaDoc().initCause(ble));
240         } catch (IOException JavaDoc ioe) {
241             throw (ConfigurationException JavaDoc)(new ConfigurationException JavaDoc().initCause(ioe));
242         }
243     }
244     
245     /**
246      * Genereate Context graph.
247      */

248     private WeblogicWebApp genereateWeblogicWebApp() {
249         WeblogicWebApp webLogicWebApp = new WeblogicWebApp();
250         webLogicWebApp.createAttribute("xmlns:j2ee", "xmlns:j2ee", AttrProp.CDATA | AttrProp.IMPLIED, null, null);
251         webLogicWebApp.setAttributeValue("xmlns:j2ee", "http://java.sun.com/xml/ns/j2ee");
252         webLogicWebApp.setAttributeValue("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); // NOI18N
253
webLogicWebApp.setAttributeValue("xsi:schemaLocation", "http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd"); // NOI18N
254
webLogicWebApp.setContextRoot(new String JavaDoc [] {""}); // NOI18N
255
return webLogicWebApp;
256     }
257     
258     /**
259      * Replace the content of the document by the graph.
260      */

261     private void replaceDocument(final StyledDocument JavaDoc doc, BaseBean graph) {
262         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
263         try {
264             graph.write(out);
265         } catch (IOException JavaDoc ioe) {
266             ErrorManager.getDefault().notify(ioe);
267         }
268         NbDocument.runAtomic(doc, new Runnable JavaDoc() {
269             public void run() {
270                 try {
271                     doc.remove(0, doc.getLength());
272                     doc.insertString(0, out.toString(), null);
273                 } catch (BadLocationException JavaDoc ble) {
274                     ErrorManager.getDefault().notify(ble);
275                 }
276             }
277         });
278     }
279     
280     // TODO: this contextPath fix code will be removed, as soon as it will
281
// be moved to the web project
282
private boolean isCorrectCP(String JavaDoc contextPath) {
283         boolean correct=true;
284         if (!contextPath.equals("") && !contextPath.startsWith("/")) correct=false; //NOI18N
285
else if (contextPath.endsWith("/")) correct=false; //NOI18N
286
else if (contextPath.indexOf("//")>=0) correct=false; //NOI18N
287
return correct;
288     }
289     
290     
291     // private helper interface -----------------------------------------------
292

293     private interface WeblogicWebAppModifier {
294         void modify(WeblogicWebApp context);
295     }
296 }
297
Popular Tags