Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 74191 invoked from network); 27 Jun 2006 10:36:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 27 Jun 2006 10:36:22 -0000 Received: (qmail 39151 invoked by uid 500); 27 Jun 2006 10:36:21 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 39077 invoked by uid 500); 27 Jun 2006 10:36:20 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 39064 invoked by uid 99); 27 Jun 2006 10:36:20 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Jun 2006 03:36:20 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Jun 2006 03:36:19 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id E11351A983A; Tue, 27 Jun 2006 03:35:58 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r417407 - in /cocoon/trunk: ./ core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/ tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/... Date: Tue, 27 Jun 2006 10:35:57 -0000 To: cvs@cocoon.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060627103558.E11351A983A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: cziegeler Date: Tue Jun 27 03:35:56 2006 New Revision: 417407 URL: http://svn.apache.org/viewvc?rev=417407&view=rev Log: Add web.xml rewriter to deploy tool which will add the paranoid classloader Fix include bug in paranoid class loader Added: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidListener.java (with props) cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/WebApplicationRewriter.java (with props) cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/XMLUtils.java (with props) Removed: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidHttpSessionListener.java cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidServletContextListener.java Modified: cocoon/trunk/README.txt cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/AbstractParanoidListener.java cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/DefaultClassLoader.java cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/AbstractDeployMojo.java Modified: cocoon/trunk/README.txt URL: http://svn.apache.org/viewvc/cocoon/trunk/README.txt?rev=417407&r1=417406&r2=417407&view=diff ============================================================================== --- cocoon/trunk/README.txt (original) +++ cocoon/trunk/README.txt Tue Jun 27 03:35:56 2006 @@ -74,9 +74,13 @@ Go to core/cocoon-webapp: $ mvn cocoon:deploy - $ mvn jetty6:run-exploded + $ mvn jetty6:run Point your browser to http://localhost:8888/ + +(Don't use jetty6:run-exploded as in this case the jetty6 plugin will + alter the webapp build by the Cocoon deployer again!) + HOW TO START THE COCOON WEBAPP (OSGI MODE) Modified: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/AbstractParanoidListener.java URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/AbstractParanoidListener.java?rev=417407&r1=417406&r2=417407&view=diff ============================================================================== --- cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/AbstractParanoidListener.java (original) +++ cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/AbstractParanoidListener.java Tue Jun 27 03:35:56 2006 @@ -75,6 +75,7 @@ while ( i.hasNext() ) { final Object listener = i.next(); try { + this.invokeListener(identifier, listener, event); } catch (Exception e) { throw new RuntimeException("Cannot invoke listener " + listener, e); } Added: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidListener.java URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidListener.java?rev=417407&view=auto ============================================================================== --- cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidListener.java (added) +++ cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidListener.java Tue Jun 27 03:35:56 2006 @@ -0,0 +1,288 @@ +/* + * Copyright 1999-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.bootstrap.servlet; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.StringTokenizer; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextAttributeEvent; +import javax.servlet.ServletContextAttributeListener; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.ServletException; +import javax.servlet.http.HttpSessionActivationListener; +import javax.servlet.http.HttpSessionAttributeListener; +import javax.servlet.http.HttpSessionBindingEvent; +import javax.servlet.http.HttpSessionBindingListener; +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; + +/** + * This listener can be used as a wrapper around "real" listeners to + * support the paranoid class loader. + * + * @version $Id$ + */ +public class ParanoidListener + implements HttpSessionListener, + ServletContextListener, + HttpSessionActivationListener, + HttpSessionAttributeListener, + HttpSessionBindingListener, + ServletContextAttributeListener { + + private static final String SERVLET_CONTEXT_CREATED = "CLC"; + + private static final String SERVLET_CONTEXT_DESTROYED = "CLD"; + + private static final String SESSION_CREATED = "SEC"; + + private static final String SESSION_DESTROYED = "SED"; + + private static final String SESSION_ACTIVATED = "SEAC"; + + private static final String SESSION_PASSIVATE = "SEDE"; + + private static final String VALUE_BOUND = "VB"; + + private static final String VALUE_UNBOUND = "VUB"; + + private static final String ATTR_REPLACED = "ARE"; + + private static final String ATTR_REMOVED = "ADE"; + + private static final String ATTR_ADDED = "AAD"; + + private static final String CONTEXT_ATTR_REPLACED = "CARE"; + + private static final String CONTEXT_ATTR_REMOVED = "CADE"; + + private static final String CONTEXT_ATTR_ADDED = "CAAD"; + + protected ClassLoader classloader; + + protected List httpSessionListeners = new ArrayList(); + + protected List servletContextListeners = new ArrayList(); + + protected List httpSessionActivationListeners = new ArrayList(); + + protected List httpSessionBindingListeners = new ArrayList(); + + protected List servletContextAttributeListeners = new ArrayList(); + + protected List httpSessionAttributeListeners = new ArrayList(); + + protected void init(ServletContext context) { + // Get the classloader + try { + this.classloader = BootstrapClassLoaderManager.getClassLoader(context); + } catch (ServletException se) { + throw new RuntimeException("Unable to create bootstrap classloader.", se); + } + + // Create the listeners + final ClassLoader old = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(this.classloader); + final String listenersConfig = context.getInitParameter(ParanoidListener.class.getName()); + if ( listenersConfig != null ) { + final StringTokenizer st = new StringTokenizer(listenersConfig, " \t\r\n\f;,", false); + while ( st.hasMoreTokens() ) { + final String className = st.nextToken(); + try { + Class listenerClass = this.classloader.loadClass(className); + final Object listener = listenerClass.newInstance(); + if ( listener instanceof HttpSessionListener ) { + this.httpSessionListeners.add(listener); + } + if ( listener instanceof ServletContextListener ) { + this.servletContextListeners.add(listener); + } + if ( listener instanceof HttpSessionActivationListener ) { + this.httpSessionActivationListeners.add(listener); + } + if ( listener instanceof HttpSessionAttributeListener ) { + this.httpSessionAttributeListeners.add(listener); + } + if ( listener instanceof HttpSessionBindingListener ) { + this.httpSessionBindingListeners.add(listener); + } + if ( listener instanceof ServletContextAttributeListener ) { + this.servletContextAttributeListeners.add(listener); + } + } catch (Exception e) { + throw new RuntimeException("Cannot load listener " + className, e); + } + } + } + } finally { + Thread.currentThread().setContextClassLoader(old); + } + } + + protected void invoke(List listeners, String identifier, Object event) { + if ( this.classloader != null ) { + final ClassLoader old = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(this.classloader); + final Iterator i = listeners.iterator(); + while ( i.hasNext() ) { + final Object listener = i.next(); + try { + if ( ParanoidListener.SERVLET_CONTEXT_CREATED.equals(identifier) ) { + ((ServletContextListener)listener).contextInitialized((ServletContextEvent)event); + } else if ( ParanoidListener.SERVLET_CONTEXT_DESTROYED.equals(identifier) ) { + ((ServletContextListener)listener).contextDestroyed((ServletContextEvent)event); + } else if ( ParanoidListener.SESSION_CREATED.equals(identifier) ) { + ((HttpSessionListener)listener).sessionCreated((HttpSessionEvent)event); + } else if ( ParanoidListener.SESSION_DESTROYED.equals(identifier) ) { + ((HttpSessionListener)listener).sessionDestroyed((HttpSessionEvent)event); + } else if ( ParanoidListener.VALUE_BOUND.equals(identifier) ) { + ((HttpSessionBindingListener)listener).valueBound((HttpSessionBindingEvent)event); + } else if ( ParanoidListener.VALUE_UNBOUND.equals(identifier) ) { + ((HttpSessionBindingListener)listener).valueUnbound((HttpSessionBindingEvent)event); + } else if ( ParanoidListener.ATTR_ADDED.equals(identifier) ) { + ((HttpSessionAttributeListener)listener).attributeAdded((HttpSessionBindingEvent)event); + } else if ( ParanoidListener.ATTR_REMOVED.equals(identifier) ) { + ((HttpSessionAttributeListener)listener).attributeRemoved((HttpSessionBindingEvent)event); + } else if ( ParanoidListener.ATTR_REPLACED.equals(identifier) ) { + ((HttpSessionAttributeListener)listener).attributeReplaced((HttpSessionBindingEvent)event); + } else if ( ParanoidListener.CONTEXT_ATTR_ADDED.equals(identifier) ) { + ((ServletContextAttributeListener)listener).attributeAdded((ServletContextAttributeEvent)event); + } else if ( ParanoidListener.CONTEXT_ATTR_REMOVED.equals(identifier) ) { + ((ServletContextAttributeListener)listener).attributeRemoved((ServletContextAttributeEvent)event); + } else if ( ParanoidListener.CONTEXT_ATTR_REPLACED.equals(identifier) ) { + ((ServletContextAttributeListener)listener).attributeReplaced((ServletContextAttributeEvent)event); + } else if ( ParanoidListener.SESSION_ACTIVATED.equals(identifier) ) { + ((HttpSessionActivationListener)listener).sessionDidActivate((HttpSessionEvent)event); + } else if ( ParanoidListener.SESSION_PASSIVATE.equals(identifier) ) { + ((HttpSessionActivationListener)listener).sessionWillPassivate((HttpSessionEvent)event); + } + } catch (Exception e) { + throw new RuntimeException("Cannot invoke listener " + listener, e); + } + } + } finally { + Thread.currentThread().setContextClassLoader(old); + } + } + } + + /** + * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) + */ + public void contextDestroyed(ServletContextEvent contextEvent) { + this.invoke(this.servletContextListeners, ParanoidListener.SERVLET_CONTEXT_DESTROYED, contextEvent); + } + + /** + * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) + */ + public void contextInitialized(ServletContextEvent contextEvent) { + final ServletContext context = contextEvent.getServletContext(); + this.init(context); + + this.invoke(this.servletContextListeners, ParanoidListener.SERVLET_CONTEXT_CREATED, contextEvent); + } + + /** + * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent) + */ + public void sessionCreated(HttpSessionEvent event) { + this.invoke(this.httpSessionListeners, ParanoidListener.SESSION_CREATED, event); + } + + /** + * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent) + */ + public void sessionDestroyed(HttpSessionEvent event) { + this.invoke(this.httpSessionListeners, ParanoidListener.SESSION_DESTROYED, event); + } + + /** + * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent) + */ + public void valueBound(HttpSessionBindingEvent event) { + this.invoke(this.httpSessionBindingListeners, ParanoidListener.VALUE_BOUND, event); + } + + /** + * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent) + */ + public void valueUnbound(HttpSessionBindingEvent event) { + this.invoke(this.httpSessionBindingListeners, ParanoidListener.VALUE_UNBOUND, event); + } + + /** + * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent) + */ + public void attributeAdded(HttpSessionBindingEvent event) { + this.invoke(this.httpSessionAttributeListeners, ParanoidListener.ATTR_ADDED, event); + } + + /** + * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent) + */ + public void attributeRemoved(HttpSessionBindingEvent event) { + this.invoke(this.httpSessionAttributeListeners, ParanoidListener.ATTR_REMOVED, event); + } + + /** + * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent) + */ + public void attributeReplaced(HttpSessionBindingEvent event) { + this.invoke(this.httpSessionAttributeListeners, ParanoidListener.ATTR_REPLACED, event); + } + + /** + * @see javax.servlet.http.HttpSessionActivationListener#sessionDidActivate(javax.servlet.http.HttpSessionEvent) + */ + public void sessionDidActivate(HttpSessionEvent event) { + this.invoke(this.httpSessionActivationListeners, ParanoidListener.SESSION_ACTIVATED, event); + } + + /** + * @see javax.servlet.http.HttpSessionActivationListener#sessionWillPassivate(javax.servlet.http.HttpSessionEvent) + */ + public void sessionWillPassivate(HttpSessionEvent event) { + this.invoke(this.httpSessionActivationListeners, ParanoidListener.SESSION_PASSIVATE, event); + } + + /** + * @see javax.servlet.ServletContextAttributeListener#attributeAdded(javax.servlet.ServletContextAttributeEvent) + */ + public void attributeAdded(ServletContextAttributeEvent event) { + this.invoke(this.servletContextAttributeListeners, ParanoidListener.CONTEXT_ATTR_ADDED, event); + } + + /** + * @see javax.servlet.ServletContextAttributeListener#attributeRemoved(javax.servlet.ServletContextAttributeEvent) + */ + public void attributeRemoved(ServletContextAttributeEvent event) { + this.invoke(this.servletContextAttributeListeners, ParanoidListener.CONTEXT_ATTR_REMOVED, event); + } + + /** + * @see javax.servlet.ServletContextAttributeListener#attributeReplaced(javax.servlet.ServletContextAttributeEvent) + */ + public void attributeReplaced(ServletContextAttributeEvent event) { + this.invoke(this.servletContextAttributeListeners, ParanoidListener.CONTEXT_ATTR_REPLACED, event); + } +} \ No newline at end of file Propchange: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidListener.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/bootstrap/servlet/ParanoidListener.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/DefaultClassLoader.java URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/DefaultClassLoader.java?rev=417407&r1=417406&r2=417407&view=diff ============================================================================== --- cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/DefaultClassLoader.java (original) +++ cocoon/trunk/core/cocoon-bootstrap/src/main/java/org/apache/cocoon/classloader/DefaultClassLoader.java Tue Jun 27 03:35:56 2006 @@ -63,7 +63,7 @@ boolean tryHere; // If no explicit includes, try here - if (this.includes == null) { + if (this.includes == null || this.includes.size() == 0) { tryHere = true; } else { // See if it matches include patterns @@ -77,7 +77,7 @@ } // Go through the exclusion list - if (tryHere && excludes != null) { + if (tryHere && this.excludes != null && this.excludes.size() > 0) { for (int i = 0; i < this.excludes.size(); i++) { if (WildcardMatcherHelper.match((String)excludes.get(i), name) != null) { tryHere = false; Modified: cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/AbstractDeployMojo.java URL: http://svn.apache.org/viewvc/cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/AbstractDeployMojo.java?rev=417407&r1=417406&r2=417407&view=diff ============================================================================== --- cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/AbstractDeployMojo.java (original) +++ cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/AbstractDeployMojo.java Tue Jun 27 03:35:56 2006 @@ -16,7 +16,9 @@ package org.apache.cocoon.maven.deployer; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; @@ -26,6 +28,8 @@ import org.apache.cocoon.maven.deployer.monolithic.DevelopmentBlock; import org.apache.cocoon.maven.deployer.monolithic.DevelopmentProperty; import org.apache.cocoon.maven.deployer.monolithic.MonolithicCocoonDeployer; +import org.apache.cocoon.maven.deployer.utils.WebApplicationRewriter; +import org.apache.cocoon.maven.deployer.utils.XMLUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -34,14 +38,15 @@ import org.apache.maven.plugin.war.AbstractWarMojo; import org.apache.maven.project.MavenProject; import org.apache.maven.project.artifact.MavenMetadataSource; +import org.w3c.dom.Document; /** * Create a Cocoon web application based on a block deployment descriptor. * * @version $Id$ */ -abstract class AbstractDeployMojo extends AbstractWarMojo -{ +abstract class AbstractDeployMojo extends AbstractWarMojo { + /** * Artifact factory, needed to download source jars for inclusion in classpath. * @@ -137,7 +142,14 @@ */ private String webXml; - /** + /** + * Use paranoid classloading + * + * @parameter expression="${maven.war.paranoidclassloader}" + */ + private boolean useParanoidClassloader = true; + + /** * Deploy a monolithic Cocoon web application. This means it doesn't use * the features that the blocks-fw offers. */ @@ -159,7 +171,23 @@ } // take care of paranoid classloading - // TBD + if ( this.useParanoidClassloader ) { + String webXmlLocation = this.getWebXml(); + if ( webXmlLocation == null ) { + webXmlLocation = getWarSourceDirectory().getAbsolutePath() + File.separatorChar + "WEB-INF" + File.separatorChar + "web.xml"; + } + this.getLog().info("Adding paranoid classloader configuration."); + this.getLog().info("Reading web.xml: " + webXmlLocation); + try { + final Document webAppDoc = XMLUtils.parseXml(new FileInputStream(new File(webXmlLocation))); + WebApplicationRewriter.rewrite(webAppDoc); + final String dest = webappDirectory_.getAbsolutePath() + File.separatorChar + "WEB-INF" + File.separatorChar + "web.xml"; + this.getLog().info("Writing web.xml: " + dest); + XMLUtils.write(webAppDoc, new FileOutputStream(dest)); + } catch (Exception e) { + throw new MojoExecutionException("Unable to read web.xml from " + webXmlLocation, e); + } + } } /** Added: cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/WebApplicationRewriter.java URL: http://svn.apache.org/viewvc/cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/WebApplicationRewriter.java?rev=417407&view=auto ============================================================================== --- cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/WebApplicationRewriter.java (added) +++ cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/WebApplicationRewriter.java Tue Jun 27 03:35:56 2006 @@ -0,0 +1,117 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.maven.deployer.utils; + +import java.util.Iterator; +import java.util.List; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +/** + * @version $Id$ + */ +public class WebApplicationRewriter { + + public static final String SERVLET_CLASS = "org.apache.cocoon.bootstrap.servlet.ParanoidServlet"; + + public static final String LISTENER_CLASS = "org.apache.cocoon.bootstrap.servlet.ParanoidListener"; + + public static boolean rewrite(Document webAppDoc) { + boolean rewritten = false; + final Element rootElement = webAppDoc.getDocumentElement(); + // first rewrite servlets + final List servlets = XMLUtils.getChildNodes(rootElement, "servlet"); + Iterator i = servlets.iterator(); + while ( i.hasNext() ) { + final Element servletElement = (Element)i.next(); + final Element servletClassElement = XMLUtils.getChildNode(servletElement, "servlet-class"); + if ( servletClassElement != null ) { + final String className = XMLUtils.getValue(servletClassElement); + XMLUtils.setValue(servletClassElement, SERVLET_CLASS); + // create init-param with real servlet class + final Element initParamElem = webAppDoc.createElementNS(null, "init-param"); + final Element initParamNameElem = webAppDoc.createElementNS(null, "param-name"); + final Element initParamValueElem = webAppDoc.createElementNS(null, "param-value"); + initParamElem.appendChild(initParamNameElem); + initParamElem.appendChild(initParamValueElem); + XMLUtils.setValue(initParamNameElem, "servlet-class"); + XMLUtils.setValue(initParamValueElem, className); + servletElement.appendChild(initParamElem); + rewritten = true; + } + } + // now rewrite listeners + final List listeners = XMLUtils.getChildNodes(rootElement, "listener"); + i = listeners.iterator(); + boolean hasListener = false; + final StringBuffer rewrittenListeners = new StringBuffer(); + while ( i.hasNext() ) { + final Element listenerElement = (Element)i.next(); + final Element listenerClassElement = XMLUtils.getChildNode(listenerElement, "listener-class"); + if ( listenerClassElement != null ) { + final String className = XMLUtils.getValue(listenerClassElement); + if ( rewrittenListeners.length() > 0 ) { + rewrittenListeners.append(','); + } + rewrittenListeners.append(className); + if ( hasListener ) { + rootElement.removeChild(listenerElement); + } else { + XMLUtils.setValue(listenerClassElement, LISTENER_CLASS); + hasListener = true; + } + rewritten = true; + } + } + // remove old parameter + i = XMLUtils.getChildNodes(rootElement, "context-param").iterator(); + while ( i.hasNext() ) { + final Element child = (Element)i.next(); + if ( LISTENER_CLASS.equals(XMLUtils.getValue(XMLUtils.getChildNode(child, "param-name")))) { + rootElement.removeChild(child); + } + } + if ( hasListener ) { + addContextParameter(rootElement, LISTENER_CLASS, rewrittenListeners.toString()); + } + // TBD: filters + return rewritten; + } + + protected static void addContextParameter(Element root, String name, String value) { + // search the element where we have to put the new context parameter before! + // we know that either filters or listeners exist + Element searchElement = XMLUtils.getChildNode(root, "context-param"); + if ( searchElement == null ) { + searchElement = XMLUtils.getChildNode(root, "filter"); + if ( searchElement == null ) { + searchElement = XMLUtils.getChildNode(root, "filter-mapping"); + if ( searchElement == null ) { + searchElement = XMLUtils.getChildNode(root, "listener"); + } + } + } + final Element contextParamElement = root.getOwnerDocument().createElementNS(null, "context-param"); + final Element contextParamNameElement = root.getOwnerDocument().createElementNS(null, "param-name"); + final Element contextParamValueElement = root.getOwnerDocument().createElementNS(null, "param-value"); + contextParamElement.appendChild(contextParamNameElement); + contextParamElement.appendChild(contextParamValueElement); + XMLUtils.setValue(contextParamNameElement, name); + XMLUtils.setValue(contextParamValueElement, value); + root.insertBefore(contextParamElement, searchElement); + } +} \ No newline at end of file Propchange: cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/WebApplicationRewriter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/WebApplicationRewriter.java ------------------------------------------------------------------------------ svn:keywords = Id Added: cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/XMLUtils.java URL: http://svn.apache.org/viewvc/cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/XMLUtils.java?rev=417407&view=auto ============================================================================== --- cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/XMLUtils.java (added) +++ cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/XMLUtils.java Tue Jun 27 03:35:56 2006 @@ -0,0 +1,139 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.maven.deployer.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * @version $Id$ + */ +public class XMLUtils { + + public static Document parseXml(InputStream source) throws IOException, SAXException { + try { + DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); + documentFactory.setNamespaceAware(true); + documentFactory.setValidating(false); + DocumentBuilder docBuilder = documentFactory.newDocumentBuilder(); + // Parse using the local dtds instead of remote dtds. This + // allows to deploy the application offline + /* + docBuilder.setEntityResolver(new EntityResolver() { + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, + java.io.IOException { + if (systemId.equals("http://java.sun.com/dtd/web-app_2_3.dtd")) { + return new InputSource(getClass().getResourceAsStream("web-app_2_3.dtd")); + } + return null; + } + }); + */ + return docBuilder.parse(source); + } catch (ParserConfigurationException pce) { + throw new IOException("Creating document failed:" + pce.getMessage()); + } + } + + public static void write(Document node, OutputStream out) + throws Exception { + final Properties format = new Properties(); + format.put(OutputKeys.METHOD, "xml"); + format.put(OutputKeys.OMIT_XML_DECLARATION, "no"); + format.put(OutputKeys.INDENT, "yes"); + + Transformer transformer; + transformer = TransformerFactory.newInstance().newTransformer(); + transformer.setOutputProperties(format); + transformer.transform(new DOMSource(node), new StreamResult(out)); + } + + public static List getChildNodes(Element parent, String nodeName) { + final List nodes = new ArrayList(); + if ( parent != null && nodeName != null ) { + final NodeList children = parent.getChildNodes(); + if ( children != null ) { + for(int i=0; i 0 ) { + return (Element)children.get(0); + } + + return null; + } + + public static String getValue(Element node) { + if (node != null) { + if (node.getNodeType() == Node.ATTRIBUTE_NODE) { + return node.getNodeValue(); + } else { + node.normalize(); + NodeList childs = node.getChildNodes(); + int i = 0; + int length = childs.getLength(); + while (i < length) { + if (childs.item(i).getNodeType() == Node.TEXT_NODE) { + return childs.item(i).getNodeValue().trim(); + } else { + i++; + } + } + } + } + return null; + } + + public static void setValue(Element node, String value) { + if ( node != null ) { + // remove all children + while ( node.hasChildNodes() ) { + node.removeChild(node.getFirstChild()); + } + node.appendChild(node.getOwnerDocument().createTextNode(value)); + } + } +} \ No newline at end of file Propchange: cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/XMLUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cocoon/trunk/tools/cocoon-block-deployer/cocoon-deployer-plugin/src/main/java/org/apache/cocoon/maven/deployer/utils/XMLUtils.java ------------------------------------------------------------------------------ svn:keywords = Id