KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > 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 package org.netbeans.modules.j2ee.websphere6.config;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Set JavaDoc;
36 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
37 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
38 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException JavaDoc;
39 import javax.swing.text.BadLocationException JavaDoc;
40 import javax.swing.text.StyledDocument JavaDoc;
41 import javax.xml.namespace.NamespaceContext JavaDoc;
42 import javax.xml.parsers.DocumentBuilder JavaDoc;
43 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
44 import javax.xml.parsers.ParserConfigurationException JavaDoc;
45 import javax.xml.transform.Transformer JavaDoc;
46 import javax.xml.transform.TransformerException JavaDoc;
47 import javax.xml.transform.TransformerFactory JavaDoc;
48 import javax.xml.transform.dom.DOMSource JavaDoc;
49 import javax.xml.transform.stream.StreamResult JavaDoc;
50 import javax.xml.xpath.XPath JavaDoc;
51 import javax.xml.xpath.XPathConstants JavaDoc;
52 import javax.xml.xpath.XPathExpressionException JavaDoc;
53 import javax.xml.xpath.XPathFactory JavaDoc;
54 import javax.xml.xpath.XPathFactoryConfigurationException JavaDoc;
55 import org.netbeans.modules.j2ee.websphere6.config.sync.WarSynchronizer;
56 import org.netbeans.modules.j2ee.websphere6.dd.beans.DDXmiConstants;
57 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSWebBnd;
58 import org.netbeans.modules.j2ee.websphere6.dd.beans.WSWebExt;
59 import org.netbeans.modules.schema2beans.BaseBean;
60 import org.openide.DialogDisplayer;
61 import org.openide.ErrorManager;
62 import org.openide.NotifyDescriptor;
63 import org.openide.filesystems.FileChangeAdapter;
64 import org.openide.filesystems.FileEvent;
65 import org.openide.filesystems.FileObject;
66 import org.openide.filesystems.FileUtil;
67 import org.openide.loaders.*;
68 import org.openide.text.NbDocument;
69 import org.openide.util.NbBundle;
70 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
71 import org.netbeans.api.project.FileOwnerQuery;
72 import org.netbeans.api.project.Project;
73 import org.netbeans.api.project.ui.OpenProjects;
74 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
75 import org.netbeans.modules.j2ee.spi.ejbjar.EjbJarProvider;
76 import org.netbeans.modules.j2ee.websphere6.util.*;
77 import org.netbeans.spi.project.SubprojectProvider;
78 import org.w3c.dom.Attr JavaDoc;
79 import org.w3c.dom.Document JavaDoc;
80 import org.w3c.dom.Element JavaDoc;
81 import org.w3c.dom.Node JavaDoc;
82 import org.w3c.dom.NodeList JavaDoc;
83 import org.xml.sax.SAXException JavaDoc;
84
85 public class WarDeploymentConfiguration extends WSDeploymentConfiguration
86         implements PropertyChangeListener JavaDoc {
87     
88     private File JavaDoc []file;
89     
90     private String JavaDoc contextRoot;
91     WSWebBnd wsWebBnd;
92     WSWebExt wsWebExt;
93     
94     
95     public static final String JavaDoc WEB_APP_ID = "WebApp";
96     /**
97      * Creates a new instance of WarDeploymentConfiguration
98      */

99     public WarDeploymentConfiguration(DeploymentManager JavaDoc dm,DeployableObject JavaDoc deployableObject,InstanceProperties ip) throws InvalidModuleException JavaDoc{
100         super(dm,deployableObject,ip);
101     }
102     
103     /**
104      * WarDeploymentConfiguration initialization. This method should be called before
105      * this class is being used.
106      *
107      *
108      */

109     public void init(File JavaDoc []file) {
110         this.file = file;
111         getWSWebBnd();
112         getWSWebExt();
113         
114         String JavaDoc path;
115         path=(file.length>0)?file[0].getPath():"";
116         path=path.substring(0,path.lastIndexOf(File.separator)+1);
117         File JavaDoc webInfFile = new File JavaDoc(path+"web.xml");
118         File JavaDoc ibmwebbndFile = null;
119         
120         if(file.length<=0) return;
121         if (dataObjects == null) {
122             dataObjects = new DataObject[file.length];
123             try {
124                 for(int i=0;i<dataObjects.length;i++) {
125                     dataObjects [i]= dataObjects[i].find(FileUtil.toFileObject(file[i]));
126                     dataObjects[i].addPropertyChangeListener(this);
127                 }
128             } catch(DataObjectNotFoundException donfe) {
129                 ErrorManager.getDefault().notify(donfe);
130             }
131         }
132         for (int i = 0; i < file.length; i++) {
133             WSDebug.notify(file[i].getName());
134             if (file[i].getName().equals("ibm-web-bnd.xmi")) {
135                 ibmwebbndFile = file[i];
136             }
137         }
138         
139         if(webInfFile.exists()) {
140             String JavaDoc contents = WSUtil.readFile(webInfFile );
141             String JavaDoc ID = "id=\""+WEB_APP_ID+"\"";
142             if(contents!=null && contents.indexOf(ID)==-1) {
143                 String JavaDoc WebAppTagOpen = "<web-app";
144                 String JavaDoc WebAppTagClose = ">";
145                 int startIndex = contents.indexOf(WebAppTagOpen);
146                 if(startIndex!=-1) {
147                     String JavaDoc afterTageOpen = contents.substring(startIndex + WebAppTagOpen.length());
148                     String JavaDoc tag = afterTageOpen.substring(0,afterTageOpen.indexOf(WebAppTagClose));
149                     if(tag.indexOf(ID)==-1) {
150                         WSUtil.writeFile(webInfFile , contents.replaceFirst(WebAppTagOpen + " ",
151                                 WebAppTagOpen + " " + ID +" \n "));
152                     }
153                 }
154             }
155             final WarSynchronizer webSync = new WarSynchronizer (webInfFile, ibmwebbndFile);
156             webSync.addSyncFile(webInfFile);
157         }
158         
159     }
160     
161     /**
162      * Return context path.
163      *
164      * @return context path or null, if the file is not parseable.
165      */

166     public String JavaDoc getContextPath() throws ConfigurationException JavaDoc {
167         return contextRoot;
168     }
169     
170     /**
171      * Listen to websphere.xml document changes.
172      */

173     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
174         if (evt.getPropertyName() == DataObject.PROP_MODIFIED &&
175                 evt.getNewValue() == Boolean.FALSE) {
176             // dataobject has been modified, WSWeb{Ext,Bnd} graph is out of sync
177
wsWebExt = null;
178             wsWebBnd = null;
179         }
180     }
181     
182     /**
183      * Return WSWebBnd graph. If it was not created yet, load it from the file
184      * and cache it. If the file does not exist, generate it.
185      *
186      *
187      * @return WSWebBnd graph or null if the ibm-web-bnd.xml file is not parseable.
188      */

189     public synchronized WSWebBnd getWSWebBnd() {
190         if (wsWebBnd == null) {
191             try {
192                 for(int i=0;i<file.length;i++) {
193                     if(!(file[i].getName().equals("ibm-web-bnd.xmi"))) {
194                         continue;
195                     }
196                     if (file[i].exists()) {
197                         
198                         // load configuration if already exists
199
try {
200                             WSWebBnd wsWebBnd = new WSWebBnd(file[i],false);
201                         } catch (IOException JavaDoc ioe) {
202                             ErrorManager.getDefault().notify(ioe);
203                         } catch (RuntimeException JavaDoc re) {
204                             // websphere.xml is not parseable, do nothing
205
}
206                     } else {
207                         // create websphere.xml if it does not exist yet
208
wsWebBnd = new WSWebBnd();
209                         wsWebBnd.setDefaults();
210                         writefile(file[i], wsWebBnd);
211                     }
212                 }
213             } catch (ConfigurationException JavaDoc ce) {
214                 ErrorManager.getDefault().notify(ce);
215             }
216         }
217         return wsWebBnd;
218     }
219     
220     
221     /**
222      * Return WSWebBnd graph. If it was not created yet, load it from the file
223      * and cache it. If the file does not exist, generate it.
224      *
225      *
226      * @return WSWebBnd graph or null if the ibm-web-bnd.xml file is not parseable.
227      */

228     public synchronized WSWebExt getWSWebExt() {
229         if (wsWebExt == null) {
230             try {
231                 for(int i=0;i<file.length;i++) {
232                     if(!(file[i].getName().equals("ibm-web-ext.xmi"))) {
233                         continue;
234                     }
235                     if (file[i].exists()) {
236                         
237                         // load configuration if already exists
238
try {
239                             WSWebExt wsWebExt = new WSWebExt(file[i],false);
240                         } catch (IOException JavaDoc ioe) {
241                             ErrorManager.getDefault().notify(ioe);
242                         } catch (RuntimeException JavaDoc re) {
243                             // websphere.xml is not parseable, do nothing
244
}
245                     } else {
246                         // create websphere.xml if it does not exist yet
247
wsWebExt = new WSWebExt();
248                         wsWebExt.setDefaults();
249                         writefile(file[i], wsWebExt);
250                     }
251                 }
252             } catch (ConfigurationException JavaDoc ce) {
253                 ErrorManager.getDefault().notify(ce);
254             }
255         }
256         return wsWebExt;
257     }
258     
259     
260     
261     // JSR-88 methods ---------------------------------------------------------
262
/*
263     public void save(OutputStream os) throws ConfigurationException {
264         WSWebBnd wsWebBnd = getWSWebBnd();
265         WSWebExt wsWebExt = getWSWebExt();
266         if (wsWebExt == null || wsWebBnd==null) {
267             throw new ConfigurationException("Cannot read configuration, it is probably in an inconsistent state."); // NOI18N
268         }
269         try {
270             wsWebBnd.write(os);
271             wsWebExt.write(os);
272         } catch (IOException ioe) {
273             throw new ConfigurationException(ioe.getLocalizedMessage());
274         }
275     }
276      
277      
278     /**
279      * Replace the content of the document by the graph.
280      */

281     private void replaceDocument(final StyledDocument JavaDoc doc, BaseBean graph) {
282         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
283         try {
284             graph.write(out);
285         } catch (IOException JavaDoc ioe) {
286             ErrorManager.getDefault().notify(ioe);
287         }
288         NbDocument.runAtomic(doc, new Runnable JavaDoc() {
289             public void run() {
290                 try {
291                     doc.remove(0, doc.getLength());
292                     doc.insertString(0, out.toString(), null);
293                 } catch (BadLocationException JavaDoc ble) {
294                     ErrorManager.getDefault().notify(ble);
295                 }
296             }
297         });
298     }
299     
300     // TODO: this contextPath fix code will be removed, as soon as it will
301
// be moved to the web project
302
private boolean isCorrectCP(String JavaDoc contextPath) {
303         boolean correct=true;
304         if (!contextPath.equals("") && !contextPath.startsWith("/")) correct=false; //NOI18N
305
else if (contextPath.endsWith("/")) correct=false; //NOI18N
306
else if (contextPath.indexOf("//")>=0) correct=false; //NOI18N
307
return correct;
308     }
309     
310     
311     /**
312      * Set context path.
313      */

314     public void setContextPath(String JavaDoc contextPath) {
315         // TODO: this contextPath fix code will be removed, as soon as it will
316
// be moved to the web project
317
if (!isCorrectCP(contextPath)) {
318             String JavaDoc ctxRoot = contextPath;
319             java.util.StringTokenizer JavaDoc tok = new java.util.StringTokenizer JavaDoc(contextPath,"/"); //NOI18N
320
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(); //NOI18N
321
while (tok.hasMoreTokens()) {
322                 buf.append("/"+tok.nextToken()); //NOI18N
323
}
324             ctxRoot = buf.toString();
325             NotifyDescriptor desc = new NotifyDescriptor.Message(
326                     NbBundle.getMessage(WarDeploymentConfiguration.class, "MSG_invalidCP", contextPath),
327                     NotifyDescriptor.Message.INFORMATION_MESSAGE);
328             DialogDisplayer.getDefault().notify(desc);
329             contextPath = ctxRoot;
330         }
331         final String JavaDoc newContextPath = contextPath;
332         contextRoot=contextPath;
333     }
334     
335     // private helper interface -----------------------------------------------
336

337     private interface WSWebBndModifier {
338         void modify(WSWebBnd context);
339     }
340     private interface WSWebExtModifier {
341         void modify(WSWebExt context);
342     }
343     
344     private class WebNSC implements NamespaceContext JavaDoc {
345         public String JavaDoc getPrefix(String JavaDoc namespaceURI) {
346             return "webbnd";
347         }
348         public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
349             return "webbnd.xmi";
350         }
351         public Iterator JavaDoc getPrefixes(String JavaDoc namespaceURI) {
352             return null;
353         }
354     }
355     
356     
357 }
358
Popular Tags