KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > 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.oc4j.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.DeployableObject JavaDoc;
29 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.StyledDocument JavaDoc;
32 import org.netbeans.modules.j2ee.oc4j.config.gen.OrionWebApp;
33 import org.openide.DialogDisplayer;
34 import org.openide.ErrorManager;
35 import org.openide.NotifyDescriptor;
36 import org.openide.cookies.EditorCookie;
37 import org.openide.cookies.SaveCookie;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.util.NbBundle;
42
43 /**
44  * Web module deployment configuration handles creation and updating of the
45  * orion-web.xml configuration file.
46  *
47  * @author sherold
48  */

49 public class WarDeploymentConfiguration extends OC4JDeploymentConfiguration
50         implements PropertyChangeListener JavaDoc {
51     
52     private static final String JavaDoc DEFAULT_CHARSET = "utf-8"; // NOI18N
53
private File JavaDoc file;
54     private OrionWebApp orionWebApp;
55     
56     /**
57      * Creates a new instance of WarDeploymentConfiguration
58      */

59     public WarDeploymentConfiguration(DeployableObject JavaDoc deployableObject) {
60         super(deployableObject);
61     }
62     
63     /**
64      * WarDeploymentConfiguration initialization. This method should be called before
65      * this class is being used.
66      *
67      * @param file orion-web.xml file.
68      */

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

88     public String JavaDoc getContextPath() throws ConfigurationException JavaDoc {
89         OrionWebApp orionWebApp = getOrionWebApp();
90         if (orionWebApp == null) { // graph not parseable
91
throw new ConfigurationException JavaDoc("orion-web.xml is not parseable, cannot read the context path value."); // NOI18N
92
}
93         return orionWebApp.getContextRoot();
94     }
95     
96     /**
97      * Set context path.
98      */

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

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

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

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

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

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

244     private OrionWebApp genereateOrionWebApp() {
245         OrionWebApp orionWebApp = new OrionWebApp();
246         orionWebApp.setContextRoot(""); // NOI18N
247
orionWebApp.setDevelopment("true"); // NOI18N
248
orionWebApp.setDefaultCharset(DEFAULT_CHARSET);
249         return orionWebApp;
250     }
251     
252     // TODO: this contextPath fix code will be removed, as soon as it will
253
// be moved to the web project
254
private boolean isCorrectCP(String JavaDoc contextPath) {
255         boolean correct=true;
256         if (!contextPath.equals("") && !contextPath.startsWith("/")) correct=false; //NOI18N
257
else if (contextPath.endsWith("/")) correct=false; //NOI18N
258
else if (contextPath.indexOf("//")>=0) correct=false; //NOI18N
259
return correct;
260     }
261     
262     
263     // private helper interface -----------------------------------------------
264

265     private interface OrionWebAppModifier {
266         void modify(OrionWebApp context);
267     }
268 }
Popular Tags