1 19 20 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 ; 43 import org.apache.xml.serialize.*; 44 import org.openide.modules.InstalledFileLocator; 45 46 47 51 public class TomcatInstallUtil { 52 53 54 private static final String BUNDLED_DEFAULT_URI_ENCODING = "utf-8"; 56 private static final Boolean BUNDLED_DEFAULT_AUTO_DEPLOY = Boolean.FALSE; 57 58 private static final String ATTR_URI_ENCODING = "URIEncoding"; private static final String ATTR_PORT = "port"; private static final String ATTR_PROTOCOL = "protocol"; private static final String ATTR_AUTO_DEPLOY = "autoDeploy"; private static final String ATTR_SCHEME = "scheme"; private static final String ATTR_SECURE = "secure"; 65 private static final String PROP_CONNECTOR = "Connector"; 67 private static final String HTTP = "http"; private static final String HTTPS = "https"; private static final String TRUE = "true"; 71 72 private TomcatInstallUtil() { 73 } 74 75 public static String getShutdownPort(Server server) { 76 String port; 77 78 port = server.getAttributeValue("port"); 79 80 if (TomcatFactory.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 81 TomcatFactory.getEM ().log ("T5Util.getAdminPort: " + port); } 83 return port != null ? port : String.valueOf(TomcatProperties.DEF_VALUE_SHUTDOWN_PORT); 84 } 85 86 public static String getPort(Server server) { 87 88 Service service = server.getService(0); 89 90 int defCon = -1; 91 boolean[] connectors = service.getConnector(); 92 String port; 93 for (int i=0; i<service.sizeConnector(); i++) { 94 String protocol = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_PROTOCOL); 95 String scheme = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_SCHEME); 96 String 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); } 112 return port; 113 } 114 115 public static String getHost(Server server) { 116 String 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); } 125 return host; 126 } 127 128 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); } 141 return null; 142 } 143 144 147 public static String getDocumentText(Document doc) { 148 OutputFormat format = new OutputFormat (); 149 format.setPreserveSpace (true); 150 StringWriter sw = new StringWriter(); 151 org.w3c.dom.Element 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 sw.write('\n'); 160 return sw.toString(); 161 }catch(IOException ex) { 162 System.out.println("ex="+ex); 163 return rootElement.toString(); 165 } 166 finally { 167 try { 168 sw.close(); 169 } catch(IOException ex) { 170 System.out.println("ex="+ex); 171 } 173 } 174 } 175 176 public static void updateDocument(javax.swing.text.Document doc, String newDoc, String prefixMark) throws javax.swing.text.BadLocationException { 177 int origLen = doc.getLength(); 178 String 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 return; 197 } 198 199 doc.remove(prefixInd, origLen - prefixInd); 200 doc.insertString(prefixInd, newDoc, null); 201 } 202 203 private static boolean isHttpConnector(String protocol, String scheme, String 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 doc = dobj.getDocument(); 215 org.w3c.dom.Element root = doc.getDocumentElement(); 216 org.w3c.dom.NodeList list = root.getElementsByTagName("Service"); int size=list.getLength(); 218 if (size>0) { 219 org.w3c.dom.Element service=(org.w3c.dom.Element )list.item(0); 220 org.w3c.dom.NodeList cons = service.getElementsByTagName(PROP_CONNECTOR); 221 for (int i=0;i<cons.getLength();i++) { 222 org.w3c.dom.Element con=(org.w3c.dom.Element )cons.item(i); 223 String protocol = con.getAttribute(ATTR_PROTOCOL); 224 String scheme = con.getAttribute(ATTR_SCHEME); 225 String 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 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 ex){ 238 org.openide.ErrorManager.getDefault ().notify(ex); 239 } catch(java.io.IOException ex){ 240 org.openide.ErrorManager.getDefault ().notify(ex); 241 } 242 } 243 return false; 244 } 245 246 private static void setServerAttributeValue(Server server, String attribute, String value) { 247 server.setAttributeValue(attribute, value); 248 } 249 250 private static void setHttpConnectorAttributeValue(Server server, String attribute, String 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 protocol = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_PROTOCOL); 257 String scheme = service.getAttributeValue(PROP_CONNECTOR, i, ATTR_SCHEME); 258 String 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 attribute, String 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 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 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 doc = dobj.getDocument(); 307 org.w3c.dom.Element root = doc.getDocumentElement(); 308 root.setAttribute("port", String.valueOf(port)); updateDocument(dobj,doc); 310 return true; 311 } catch(org.xml.sax.SAXException 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 ex){ 316 org.openide.ErrorManager.getDefault ().notify(ex); 317 } catch(java.io.IOException 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 doc) 325 throws javax.swing.text.BadLocationException , java.io.IOException { 326 org.openide.cookies.EditorCookie editor = (EditorCookie)dobj.getCookie(EditorCookie.class); 327 javax.swing.text.Document textDoc = editor.getDocument(); 328 if (textDoc == null) { 329 textDoc = editor.openDocument(); 330 } 331 TomcatInstallUtil.updateDocument(textDoc,TomcatInstallUtil.getDocumentText(doc),"<Server"); SaveCookie savec = (SaveCookie) dobj.getCookie(SaveCookie.class); 333 if (savec!=null) savec.save(); 334 } 335 336 public static String generatePassword(int length) { 337 int ran2 = 0; 338 String 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 |