KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > ide > DebugSupport


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.tomcat5.ide;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
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 JavaDoc;
31
32
33 /** Debug support addition for Tomcat5
34  *
35  * @author Martin Grebac
36  */

37 public class DebugSupport {
38
39     private static final String JavaDoc JSP_SERVLET_NAME = "jsp"; //NOI18N
40
private static final String JavaDoc JSP_SERVLET_CLASS = "org.apache.jasper.servlet.JspServlet"; //NOI18N
41

42     private static final String JavaDoc MAPPED_PARAM_NAME = "mappedfile"; //NOI18N
43
private static final String JavaDoc MAPPED_PARAM_VALUE = "true"; //NOI18N
44

45     public static void allowDebugging(TomcatManager tm) throws IOException JavaDoc, SAXException JavaDoc {
46         String JavaDoc url = tm.getUri();
47         
48         // find the web.xml file
49
File JavaDoc webXML = getDefaultWebXML(tm);
50         if (webXML == null) {
51             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception JavaDoc(url));
52             return;
53         }
54         WebApp webApp = DDProvider.getDefault().getDDRoot(webXML);
55         if (webApp == null) {
56             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception JavaDoc(url));
57             return;
58         }
59         boolean needsSave = setMappedProperty(webApp);
60         if (needsSave) {
61             OutputStream JavaDoc os = new FileOutputStream JavaDoc(webXML);
62             try {
63                 webApp.write(os);
64             } finally {
65                 os.close();
66             }
67         }
68     }
69     
70     private static File JavaDoc getDefaultWebXML(TomcatManager tm) {
71         File JavaDoc cb = tm.getTomcatProperties().getCatalinaDir();
72         File JavaDoc webXML = new File JavaDoc(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"); //NOI18N
96
servlet.setServletName(JSP_SERVLET_NAME);
97                 servlet.setServletClass(JSP_SERVLET_CLASS);
98                 InitParam initParam = (InitParam)servlet.createBean("InitParam"); //NOI18N
99
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 JavaDoc 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"); //NOI18N
125
initParam.setParamName(MAPPED_PARAM_NAME);
126                     initParam.setParamValue(MAPPED_PARAM_VALUE);
127                     servlets[i].addInitParam(initParam);
128                     changed=true;
129                 }
130             } catch (ClassNotFoundException JavaDoc ex) {
131                 TomcatManager.ERR.notify(ex);
132             }
133         }
134
135         return changed;
136     }
137
138 }
139
Popular Tags