001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5.ide;
043:
044: import java.io.File;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.io.OutputStream;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import org.netbeans.modules.j2ee.dd.api.web.*;
051: import org.netbeans.modules.j2ee.dd.api.common.InitParam;
052: import org.netbeans.modules.tomcat5.TomcatManager;
053:
054: import org.openide.util.Exceptions;
055: import org.xml.sax.SAXException;
056:
057: /** Debug support addition for Tomcat5
058: *
059: * @author Martin Grebac
060: */
061: public class DebugSupport {
062:
063: private static final String JSP_SERVLET_NAME = "jsp"; //NOI18N
064: private static final String JSP_SERVLET_CLASS = "org.apache.jasper.servlet.JspServlet"; //NOI18N
065:
066: private static final String MAPPED_PARAM_NAME = "mappedfile"; //NOI18N
067: private static final String MAPPED_PARAM_VALUE = "true"; //NOI18N
068:
069: public static void allowDebugging(TomcatManager tm)
070: throws IOException, SAXException {
071: String url = tm.getUri();
072:
073: // find the web.xml file
074: File webXML = getDefaultWebXML(tm);
075: if (webXML == null) {
076: Logger.getLogger(DebugSupport.class.getName()).log(
077: Level.INFO, null, new Exception(url));
078: return;
079: }
080: WebApp webApp = DDProvider.getDefault().getDDRoot(webXML);
081: if (webApp == null) {
082: Logger.getLogger(DebugSupport.class.getName()).log(
083: Level.INFO, null, new Exception(url));
084: return;
085: }
086: boolean needsSave = setMappedProperty(webApp);
087: if (needsSave) {
088: OutputStream os = new FileOutputStream(webXML);
089: try {
090: webApp.write(os);
091: } finally {
092: os.close();
093: }
094: }
095: }
096:
097: private static File getDefaultWebXML(TomcatManager tm) {
098: File cb = tm.getTomcatProperties().getCatalinaDir();
099: File webXML = new File(cb, "conf" + File.separator + "web.xml");
100: if (webXML.exists())
101: return webXML;
102: return null;
103: }
104:
105: private static boolean setMappedProperty(WebApp webApp) {
106:
107: boolean changed = false;
108: boolean isServlet = false;
109:
110: Servlet[] servlets = webApp.getServlet();
111: int i;
112: for (i = 0; i < servlets.length; i++) {
113: if ((servlets[i].getServletName().equals(JSP_SERVLET_NAME))
114: && (servlets[i].getServletClass()
115: .equals(JSP_SERVLET_CLASS))) {
116: isServlet = true;
117: break;
118: }
119: }
120:
121: if (!isServlet) {
122: try {
123: Servlet servlet = (Servlet) webApp
124: .createBean("Servlet"); //NOI18N
125: servlet.setServletName(JSP_SERVLET_NAME);
126: servlet.setServletClass(JSP_SERVLET_CLASS);
127: InitParam initParam = (InitParam) servlet
128: .createBean("InitParam"); //NOI18N
129: initParam.setParamName(MAPPED_PARAM_NAME);
130: initParam.setParamValue(MAPPED_PARAM_VALUE);
131: servlet.addInitParam(initParam);
132: webApp.addServlet(servlet);
133: changed = true;
134: } catch (ClassNotFoundException ex) {
135: Exceptions.printStackTrace(ex);
136: }
137: } else {
138: try {
139: boolean isInitparam = false;
140: InitParam[] initparams = servlets[i].getInitParam();
141: int j;
142: for (j = 0; j < initparams.length; j++) {
143: if ((initparams[j].getParamName()
144: .equals(MAPPED_PARAM_NAME))) {
145: isInitparam = true;
146: break;
147: }
148: }
149: if (isInitparam) {
150: if (!initparams[j].getParamValue().equals(
151: MAPPED_PARAM_VALUE)) {
152: initparams[j].setParamValue(MAPPED_PARAM_VALUE);
153: changed = true;
154: }
155: } else {
156: InitParam initParam = (InitParam) servlets[i]
157: .createBean("InitParam"); //NOI18N
158: initParam.setParamName(MAPPED_PARAM_NAME);
159: initParam.setParamValue(MAPPED_PARAM_VALUE);
160: servlets[i].addInitParam(initParam);
161: changed = true;
162: }
163: } catch (ClassNotFoundException ex) {
164: Exceptions.printStackTrace(ex);
165: }
166: }
167:
168: return changed;
169: }
170:
171: }
|