1 19 20 package org.netbeans.modules.tomcat5.ide; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import org.netbeans.modules.j2ee.dd.api.web.*; 26 import org.netbeans.modules.j2ee.dd.api.common.InitParam; 27 import org.netbeans.modules.tomcat5.TomcatManager; 28 29 import org.openide.ErrorManager; 30 import org.xml.sax.SAXException ; 31 32 33 37 public class DebugSupport { 38 39 private static final String JSP_SERVLET_NAME = "jsp"; private static final String JSP_SERVLET_CLASS = "org.apache.jasper.servlet.JspServlet"; 42 private static final String MAPPED_PARAM_NAME = "mappedfile"; private static final String MAPPED_PARAM_VALUE = "true"; 45 public static void allowDebugging(TomcatManager tm) throws IOException , SAXException { 46 String url = tm.getUri(); 47 48 File webXML = getDefaultWebXML(tm); 50 if (webXML == null) { 51 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception (url)); 52 return; 53 } 54 WebApp webApp = DDProvider.getDefault().getDDRoot(webXML); 55 if (webApp == null) { 56 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception (url)); 57 return; 58 } 59 boolean needsSave = setMappedProperty(webApp); 60 if (needsSave) { 61 OutputStream os = new FileOutputStream (webXML); 62 try { 63 webApp.write(os); 64 } finally { 65 os.close(); 66 } 67 } 68 } 69 70 private static File getDefaultWebXML(TomcatManager tm) { 71 File cb = tm.getTomcatProperties().getCatalinaDir(); 72 File webXML = new File (cb, "conf" + File.separator + "web.xml"); 73 if (webXML.exists()) 74 return webXML; 75 return null; 76 } 77 78 private static boolean setMappedProperty(WebApp webApp) { 79 80 boolean changed=false; 81 boolean isServlet=false; 82 83 Servlet[] servlets = webApp.getServlet(); 84 int i; 85 for(i=0;i<servlets.length;i++) { 86 if ((servlets[i].getServletName().equals(JSP_SERVLET_NAME)) && 87 (servlets[i].getServletClass().equals(JSP_SERVLET_CLASS))) { 88 isServlet=true; 89 break; 90 } 91 } 92 93 if (!isServlet) { 94 try { 95 Servlet servlet = (Servlet)webApp.createBean("Servlet"); servlet.setServletName(JSP_SERVLET_NAME); 97 servlet.setServletClass(JSP_SERVLET_CLASS); 98 InitParam initParam = (InitParam)servlet.createBean("InitParam"); initParam.setParamName(MAPPED_PARAM_NAME); 100 initParam.setParamValue(MAPPED_PARAM_VALUE); 101 servlet.addInitParam(initParam); 102 webApp.addServlet(servlet); 103 changed=true; 104 } catch (ClassNotFoundException ex) { 105 TomcatManager.ERR.notify(ex); 106 } 107 } else { 108 try { 109 boolean isInitparam = false; 110 InitParam[] initparams = servlets[i].getInitParam(); 111 int j; 112 for (j=0;j<initparams.length;j++) { 113 if ((initparams[j].getParamName().equals(MAPPED_PARAM_NAME))) { 114 isInitparam=true; 115 break; 116 } 117 } 118 if (isInitparam) { 119 if (!initparams[j].getParamValue().equals(MAPPED_PARAM_VALUE)) { 120 initparams[j].setParamValue(MAPPED_PARAM_VALUE); 121 changed=true; 122 } 123 } else { 124 InitParam initParam = (InitParam)servlets[i].createBean("InitParam"); initParam.setParamName(MAPPED_PARAM_NAME); 126 initParam.setParamValue(MAPPED_PARAM_VALUE); 127 servlets[i].addInitParam(initParam); 128 changed=true; 129 } 130 } catch (ClassNotFoundException ex) { 131 TomcatManager.ERR.notify(ex); 132 } 133 } 134 135 return changed; 136 } 137 138 } 139 | Popular Tags |