KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > util > TomcatInstallUtil


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 /*
21  * TomcatInstallUtil.java
22  *
23  * Created on December 9, 2003, 11:14 AM
24  */

25
26 package org.netbeans.modules.tomcat5.util;
27
28 import java.io.*;
29 import org.netbeans.modules.tomcat5.config.gen.Engine;
30 import org.netbeans.modules.tomcat5.config.gen.Host;
31 import org.netbeans.modules.tomcat5.config.gen.Server;
32 import org.netbeans.modules.tomcat5.config.gen.Service;
33
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.*;
36 import org.openide.loaders.*;
37 import org.openide.cookies.EditorCookie;
38 import org.openide.cookies.SaveCookie;
39
40 import org.netbeans.modules.tomcat5.TomcatFactory;
41
42 import org.w3c.dom.Document JavaDoc;
43 import org.apache.xml.serialize.*;
44 import org.openide.modules.InstalledFileLocator;
45
46
47 /**
48  *
49  * @author Martin Grebac
50  */

51 public class TomcatInstallUtil {
52     
53     /** default value of bundled tomcat' http connector uri encoding */
54     private static final String JavaDoc BUNDLED_DEFAULT_URI_ENCODING = "utf-8"; // NOI18N
55
/** default value of bundled tomcat's host autoDeploy attribute */
56     private static final Boolean JavaDoc BUNDLED_DEFAULT_AUTO_DEPLOY = Boolean.FALSE;
57     
58     private static final String JavaDoc ATTR_URI_ENCODING = "URIEncoding"; // NOI18N
59
private static final String JavaDoc ATTR_PORT = "port"; // NOI18N
60
private static final String JavaDoc ATTR_PROTOCOL = "protocol"; // NOI18N
61
private static final String JavaDoc ATTR_AUTO_DEPLOY = "autoDeploy"; // NOI18N
62
private static final String JavaDoc ATTR_SCHEME = "scheme"; // NOI18N
63
private static final String JavaDoc ATTR_SECURE = "secure"; // NOI18N
64

65     private static final String JavaDoc PROP_CONNECTOR = "Connector"; // NOI18N
66

67     private static final String JavaDoc HTTP = "http"; // NOI18N
68
private static final String JavaDoc HTTPS = "https"; // NOI18N
69
private static final String JavaDoc TRUE = "true"; // NOI18N
70

71     /** Creates a new instance of TomcatInstallUtil */
72     private TomcatInstallUtil() {
73     }
74     
75     public static String JavaDoc getShutdownPort(Server server) {
76         String JavaDoc port;
77         
78         port = server.getAttributeValue("port");
79                 
80         if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) {
81             TomcatFactory.getEM ().log ("T5Util.getAdminPort: " + port); // NOI18N
82
}
83         return port != null ? port : String.valueOf(TomcatProperties.DEF_VALUE_SHUTDOWN_PORT);
84     }
85     
86     public static String JavaDoc getPort(Server server) {
87
88         Service service = server.getService(0);
89
90         int defCon = -1;
91         boolean[] connectors = service.getConnector();
92         String JavaDoc port;
93         for (int i=0; i<service.sizeConnector(); i++) {
94             String JavaDoc protocol = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_PROTOCOL);
95             String JavaDoc scheme = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_SCHEME);
96             String JavaDoc secure = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_SECURE);
97             if (isHttpConnector(protocol, scheme, secure)) {
98                 defCon = i;
99                 break;
100             }
101         }
102         
103         if (defCon==-1 && service.sizeConnector() > 0) {
104             defCon=0;
105         }
106         
107         port = service.getAttributeValue(PROP_CONNECTOR, defCon, ATTR_PORT);
108
109         if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) {
110             TomcatFactory.getEM ().log ("T5Util.getPort: " + port); // NOI18N
111
}
112         return port;
113     }
114     
115     public static String JavaDoc getHost(Server server) {
116         String JavaDoc host = null;
117         Service service = server.getService(0);
118         if (service != null) {
119             host = service.getAttributeValue("Engine",0,"defaultHost");
120         }
121        
122         if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) {
123             TomcatFactory.getEM ().log ("T5Util.getHost: " + host); // NOI18N
124
}
125         return host;
126     }
127     
128     /**
129      * Return the CATALINA_HOME directory of the bundled Tomcat
130      *
131      * @return the CATALINA_HOME directory of the bundled Tomcat, <code>null</code>
132      * if the CATALINA_HOME directory does not exist which should never happen.
133      */

134     public static File getBundledHome() {
135         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
136         FileObject fo = fs.findResource(TomcatProperties.BUNDLED_TOMCAT_SETTING);
137         if (fo != null) {
138             InstalledFileLocator ifl = InstalledFileLocator.getDefault();
139             return ifl.locate(fo.getAttribute("bundled_home").toString(), null, false); // NOI18N
140
}
141         return null;
142     }
143     
144     /** @return text (suitable for printing to XML file) for a given XML document.
145      * this method uses org.apache.xml.serialize.XMLSerializer class for printing XML file
146      */

147     public static String JavaDoc getDocumentText(Document JavaDoc doc) {
148         OutputFormat format = new OutputFormat ();
149         format.setPreserveSpace (true);
150         StringWriter sw = new StringWriter();
151         org.w3c.dom.Element JavaDoc rootElement = doc.getDocumentElement();
152         if (rootElement==null) {
153             return null;
154         }
155         try {
156             XMLSerializer ser = new XMLSerializer (sw, format);
157             ser.serialize (rootElement);
158             // Apache serializer also fails to include trailing newline, sigh.
159
sw.write('\n');
160             return sw.toString();
161         }catch(IOException ex) {
162             System.out.println("ex="+ex);
163             //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
164
return rootElement.toString();
165         }
166         finally {
167             try {
168                 sw.close();
169             } catch(IOException ex) {
170                 System.out.println("ex="+ex);
171                 //ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
172
}
173         }
174     }
175     
176     public static void updateDocument(javax.swing.text.Document JavaDoc doc, String JavaDoc newDoc, String JavaDoc prefixMark) throws javax.swing.text.BadLocationException JavaDoc {
177         int origLen = doc.getLength();
178         String JavaDoc origDoc = doc.getText(0, origLen);
179         int prefixInd=0;
180         if (prefixMark!=null) {
181             prefixInd = origDoc.indexOf(prefixMark);
182             if (prefixInd>0) {
183                 origDoc=doc.getText(prefixInd,origLen-prefixInd);
184             }
185             else {
186                 prefixInd=0;
187             }
188             int prefixIndNewDoc = newDoc.indexOf(prefixMark);
189             if (prefixIndNewDoc > 0) {
190                 newDoc=newDoc.substring(prefixIndNewDoc);
191             }
192         }
193         
194         if (origDoc.equals(newDoc)) {
195             // no change in document
196
return;
197         }
198         
199         doc.remove(prefixInd, origLen - prefixInd);
200         doc.insertString(prefixInd, newDoc, null);
201     }
202     
203     private static boolean isHttpConnector(String JavaDoc protocol, String JavaDoc scheme, String JavaDoc secure) {
204         return (protocol == null || protocol.length() == 0 || protocol.toLowerCase().equals(HTTP))
205                 && (scheme == null || !scheme.toLowerCase().equals(HTTPS))
206                 && (secure == null || !secure.toLowerCase().equals(TRUE));
207     }
208     
209     public static boolean setServerPort(int port, File tomcatConf) {
210         FileObject fo = FileUtil.toFileObject(tomcatConf);
211         if (fo != null) {
212             try {
213                 XMLDataObject dobj = (XMLDataObject)DataObject.find(fo);
214                 org.w3c.dom.Document JavaDoc doc = dobj.getDocument();
215                 org.w3c.dom.Element JavaDoc root = doc.getDocumentElement();
216                 org.w3c.dom.NodeList JavaDoc list = root.getElementsByTagName("Service"); //NOI18N
217
int size=list.getLength();
218                 if (size>0) {
219                     org.w3c.dom.Element JavaDoc service=(org.w3c.dom.Element JavaDoc)list.item(0);
220                     org.w3c.dom.NodeList JavaDoc cons = service.getElementsByTagName(PROP_CONNECTOR);
221                     for (int i=0;i<cons.getLength();i++) {
222                         org.w3c.dom.Element JavaDoc con=(org.w3c.dom.Element JavaDoc)cons.item(i);
223                         String JavaDoc protocol = con.getAttribute(ATTR_PROTOCOL);
224                         String JavaDoc scheme = con.getAttribute(ATTR_SCHEME);
225                         String JavaDoc secure = con.getAttribute(ATTR_SECURE);
226                         if (isHttpConnector(protocol, scheme, secure)) {
227                             con.setAttribute(ATTR_PORT, String.valueOf(port));
228                             updateDocument(dobj,doc);
229                             return true;
230                         }
231                     }
232                 }
233             } catch(org.xml.sax.SAXException JavaDoc ex){
234                 org.openide.ErrorManager.getDefault ().notify(ex);
235             } catch(org.openide.loaders.DataObjectNotFoundException ex){
236                 org.openide.ErrorManager.getDefault ().notify(ex);
237             } catch(javax.swing.text.BadLocationException JavaDoc ex){
238                 org.openide.ErrorManager.getDefault ().notify(ex);
239             } catch(java.io.IOException JavaDoc ex){
240                 org.openide.ErrorManager.getDefault ().notify(ex);
241             }
242         }
243         return false;
244     }
245     
246     private static void setServerAttributeValue(Server server, String JavaDoc attribute, String JavaDoc value) {
247         server.setAttributeValue(attribute, value);
248     }
249     
250     private static void setHttpConnectorAttributeValue(Server server, String JavaDoc attribute, String JavaDoc value) {
251         Service services[] = server.getService();
252         if (services != null && services.length > 0) {
253             Service service = services[0];
254             int sizeConnector = service.sizeConnector();
255             for(int i = 0; i < sizeConnector; i++) {
256                 String JavaDoc protocol = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_PROTOCOL);
257                 String JavaDoc scheme = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_SCHEME);
258                 String JavaDoc secure = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_SECURE);
259                 if (isHttpConnector(protocol, scheme, secure)) {
260                     service.setAttributeValue(PROP_CONNECTOR, i, attribute, value);
261                     return;
262                 }
263             }
264         }
265     }
266     
267     private static void setHostAttributeValue(Server server, String JavaDoc attribute, String JavaDoc value) {
268         Service service[] = server.getService();
269         if (service != null) {
270             for(int i = 0; i < service.length; i++) {
271                 Engine engine = service[i].getEngine();
272                 if (engine != null) {
273                     Host host[] = engine.getHost();
274                     if (host != null && host.length > 0) {
275                         host[0].setAttributeValue(attribute, value);
276                         return;
277                     }
278                 }
279             }
280         }
281     }
282     
283     /**
284      * Make some Bundled Tomcat specific changes in server.xml.
285      */

286     public static void patchBundledServerXml(File serverXml) {
287         try {
288             Server server = Server.createGraph(serverXml);
289             setServerAttributeValue(server, ATTR_PORT, String.valueOf(TomcatProperties.DEF_VALUE_BUNDLED_SHUTDOWN_PORT));
290             setHttpConnectorAttributeValue(server, ATTR_PORT, String.valueOf(TomcatProperties.DEF_VALUE_BUNDLED_SERVER_PORT));
291             setHttpConnectorAttributeValue(server, ATTR_URI_ENCODING, BUNDLED_DEFAULT_URI_ENCODING);
292             setHostAttributeValue(server, ATTR_AUTO_DEPLOY, BUNDLED_DEFAULT_AUTO_DEPLOY.toString());
293             server.write(serverXml);
294         } catch (IOException e) {
295             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
296         } catch (RuntimeException JavaDoc e) {
297             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
298         }
299     }
300     
301     public static boolean setShutdownPort(int port, File tomcatConf) {
302         FileObject fo = FileUtil.toFileObject(tomcatConf);
303         if (fo != null) {
304             try {
305                 XMLDataObject dobj = (XMLDataObject)DataObject.find(fo);
306                 org.w3c.dom.Document JavaDoc doc = dobj.getDocument();
307                 org.w3c.dom.Element JavaDoc root = doc.getDocumentElement();
308                 root.setAttribute("port", String.valueOf(port)); //NOI18N
309
updateDocument(dobj,doc);
310                 return true;
311             } catch(org.xml.sax.SAXException JavaDoc ex){
312                 org.openide.ErrorManager.getDefault ().notify(ex);
313             } catch(org.openide.loaders.DataObjectNotFoundException ex){
314                 org.openide.ErrorManager.getDefault ().notify(ex);
315             } catch(javax.swing.text.BadLocationException JavaDoc ex){
316                 org.openide.ErrorManager.getDefault ().notify(ex);
317             } catch(java.io.IOException JavaDoc ex){
318                 org.openide.ErrorManager.getDefault ().notify(ex);
319             }
320         }
321         return false;
322     }
323     
324     public static void updateDocument(DataObject dobj, org.w3c.dom.Document JavaDoc doc)
325         throws javax.swing.text.BadLocationException JavaDoc, java.io.IOException JavaDoc {
326         org.openide.cookies.EditorCookie editor = (EditorCookie)dobj.getCookie(EditorCookie.class);
327         javax.swing.text.Document JavaDoc textDoc = editor.getDocument();
328         if (textDoc == null) {
329             textDoc = editor.openDocument();
330         }
331         TomcatInstallUtil.updateDocument(textDoc,TomcatInstallUtil.getDocumentText(doc),"<Server"); //NOI18N
332
SaveCookie savec = (SaveCookie) dobj.getCookie(SaveCookie.class);
333         if (savec!=null) savec.save();
334     }
335     
336     public static String JavaDoc generatePassword(int length) {
337     int ran2 = 0;
338     String JavaDoc pwd = "";
339     for (int i = 0; i < length; i++) {
340             ran2 = (int)(Math.random()*61);
341             if (ran2 < 10) {
342                 ran2 += 48;
343             } else {
344                 if (ran2 < 35) {
345                     ran2 += 55;
346                 } else {
347                     ran2 += 62;
348                 }
349             }
350             char c = (char) ran2;
351             pwd += c;
352     }
353         return pwd;
354     }
355
356 }
357
Popular Tags