From dev-return-91459-apmail-geronimo-dev-archive=geronimo.apache.org@geronimo.apache.org Mon Jul 4 07:24:20 2011 Return-Path: X-Original-To: apmail-geronimo-dev-archive@www.apache.org Delivered-To: apmail-geronimo-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2BA316EB9 for ; Mon, 4 Jul 2011 07:24:20 +0000 (UTC) Received: (qmail 96857 invoked by uid 500); 4 Jul 2011 07:24:20 -0000 Delivered-To: apmail-geronimo-dev-archive@geronimo.apache.org Received: (qmail 96443 invoked by uid 500); 4 Jul 2011 07:24:11 -0000 Mailing-List: contact dev-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list dev@geronimo.apache.org Received: (qmail 96429 invoked by uid 99); 4 Jul 2011 07:24:06 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jul 2011 07:24:06 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of rwonly@gmail.com designates 209.85.161.182 as permitted sender) Received: from [209.85.161.182] (HELO mail-gx0-f182.google.com) (209.85.161.182) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jul 2011 07:24:00 +0000 Received: by gxk28 with SMTP id 28so2393627gxk.13 for ; Mon, 04 Jul 2011 00:23:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=+S88JoiGgLO4NAtkj/siJ8VXTqa9mLlFz2v4pU6HZQg=; b=N8VgcUYggDAISAmjfp4+6WGAJOY6edKaR2p3stp05jNGHGUaa5HOD+XMcLthWw7KYF VRFpFdaHIF4SRja/pJpW/eAmO7u+iOU3EtVjBoOfzlSeCL61QUXhBZrORDYNBXB4W5qJ blbJ8dOBKDUI5OPLi40hS6/HLIajt81jrRtfk= Received: by 10.236.189.103 with SMTP id b67mr6905824yhn.369.1309764219152; Mon, 04 Jul 2011 00:23:39 -0700 (PDT) MIME-Version: 1.0 Received: by 10.147.38.4 with HTTP; Mon, 4 Jul 2011 00:23:19 -0700 (PDT) In-Reply-To: References: <20110703061803.16DA3238885D@eris.apache.org> From: Rex Wang Date: Mon, 4 Jul 2011 15:23:19 +0800 Message-ID: Subject: Re: svn commit: r1142379 - /geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java To: dev@geronimo.apache.org Content-Type: multipart/alternative; boundary=20cf303f69b205aa3b04a7393fb5 --20cf303f69b205aa3b04a7393fb5 Content-Type: text/plain; charset=ISO-8859-1 Thanks Ivan! I use the identifySpecDDSchemaVersion to replace the new added one in TomcatModuleBuilder. commit at rev 1142573 2011/7/3 Ivan > Hi, Rex, my two cents :-) > a. Is it possible to move those common codes to the > AbstractWebModuleBuilder, so that both Tomcat and Jetty could use it ? > b. We have some util methods for IO operation, it might be used for close > the stream, e.g. IOUtils.close(in); > > 2011/7/3 > > Author: rwonly >> Date: Sun Jul 3 06:18:02 2011 >> New Revision: 1142379 >> >> URL: http://svn.apache.org/viewvc?rev=1142379&view=rev >> Log: >> GERONIMO-6024 support dd schema validation when deploy web app >> >> Modified: >> >> geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java >> >> Modified: >> geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java >> URL: >> http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java?rev=1142379&r1=1142378&r2=1142379&view=diff >> >> ============================================================================== >> --- >> geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java >> (original) >> +++ >> geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java >> Sun Jul 3 06:18:02 2011 >> @@ -30,6 +30,8 @@ import java.util.HashSet; >> import java.util.Map; >> import java.util.Set; >> import java.util.jar.JarFile; >> +import java.util.regex.Matcher; >> +import java.util.regex.Pattern; >> >> import javax.servlet.Servlet; >> import javax.xml.bind.JAXBException; >> @@ -99,6 +101,7 @@ import org.apache.geronimo.xbeans.geroni >> import >> org.apache.geronimo.xbeans.geronimo.web.tomcat.TomcatWebAppDocument; >> import org.apache.geronimo.xbeans.geronimo.web.tomcat.TomcatWebAppType; >> import >> org.apache.geronimo.xbeans.geronimo.web.tomcat.config.GerTomcatDocument; >> +import org.apache.openejb.jee.JavaeeSchema; >> import org.apache.openejb.jee.JaxbJavaee; >> import org.apache.openejb.jee.WebApp; >> import org.apache.xbean.osgi.bundle.util.BundleUtils; >> @@ -269,6 +272,18 @@ public class TomcatModuleBuilder extends >> return module; >> } >> >> + private boolean isSchemaDefined(String xmlFile, String definedTag){ >> + // we need remove the comments first >> + Pattern commentsPattern = Pattern.compile("", >> Pattern.DOTALL); >> + Matcher commentsMatcher = commentsPattern.matcher(xmlFile); >> + >> + Pattern schemaPattern = Pattern.compile("<(\\w*:)?" + definedTag >> + "(.*)schemaLocation(.*)>", Pattern.DOTALL); >> + Matcher schemaMatcher = >> schemaPattern.matcher(commentsMatcher.replaceAll("")); >> + >> + return schemaMatcher.find(); >> + >> + } >> + >> protected Module createModule(Object plan, JarFile moduleFile, String >> targetPath, URL specDDUrl, Environment earEnvironment, String contextRoot, >> Module parentModule, Naming naming, ModuleIDBuilder idBuilder) throws >> DeploymentException { >> assert moduleFile != null : "moduleFile is null"; >> assert targetPath != null : "targetPath is null"; >> @@ -285,21 +300,41 @@ public class TomcatModuleBuilder extends >> // read in the entire specDD as a string, we need this for >> getDeploymentDescriptor >> // on the J2ee management object >> specDD = JarUtils.readAll(specDDUrl); >> + >> + InputStream in = null; >> + >> + // firstly validate the DD xml file, if it is defined by a >> schema. >> + if (isSchemaDefined(specDD, "web-app")){ >> + in = specDDUrl.openStream(); >> + try { >> + JaxbJavaee.validateJavaee(JavaeeSchema.WEB_APP_3_0, >> in); >> + } catch (Exception e) { >> + throw new DeploymentException("Error validate web.xml >> for " + targetPath, e); >> + } finally { >> + if (in != null) >> + in.close(); >> + } >> + } >> >> // we found web.xml, if it won't parse that's an error. >> - InputStream in = specDDUrl.openStream(); >> + in = specDDUrl.openStream(); >> try { >> webApp = (WebApp) JaxbJavaee.unmarshalJavaee(WebApp.class, >> in); >> + } catch (Exception e) { >> + // Output the target path in the error to make it clearer >> to the user which webapp >> + // has the problem. The targetPath is used, as moduleFile >> may have an unhelpful >> + // value such as >> C:\geronimo-1.1\var\temp\geronimo-deploymentUtil22826.tmpdir >> + throw new DeploymentException("Error unmarshal web.xml >> for " + targetPath, e); >> } finally { >> - in.close(); >> + if (in != null) >> + in.close(); >> } >> >> - } catch (JAXBException e) { >> - // Output the target path in the error to make it clearer to >> the user which webapp >> - // has the problem. The targetPath is used, as moduleFile >> may have an unhelpful >> - // value such as >> C:\geronimo-1.1\var\temp\geronimo-deploymentUtil22826.tmpdir >> - throw new DeploymentException("Error parsing web.xml for " + >> targetPath, e); >> } catch (Exception e) { >> + if (e instanceof DeploymentException) { >> + throw new DeploymentException(e); >> + } >> + >> if (!moduleFile.getName().endsWith(".war")) { >> //not for us >> return null; >> >> >> > > > -- > Ivan > -- Lei Wang (Rex) rwonly AT apache.org --20cf303f69b205aa3b04a7393fb5 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks Ivan!

I use the identifySpecDDSchemaVersion to replace the ne= w added one in TomcatModuleBuilder. commit at rev 1142573

2011/7/3 Ivan <xhhsld@gmail.com>
Hi, Rex, =A0my tw= o cents :-)
a. Is it possible to move those common codes to the Abstrac= tWebModuleBuilder, so that both Tomcat and Jetty could use it ?=A0
b. We have some util methods for IO operation, it might be used for cl= ose the stream, e.g. IOUtils.close(in);

2011/7/3 <rwonly@apache.org>=

Author: rwonly
Date: Sun Jul =A03 06:18:02 2011
New Revision: 1142379

URL: http://svn.apache.org/viewvc?rev=3D1142379&view=3Drev=
Log:
GERONIMO-6024 support dd schema validation when deploy web app

Modified:
=A0 =A0geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/m= ain/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src= /main/java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java URL: http://svn.apache.org/viewvc/gero= nimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/java/org= /apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java?rev=3D1142379&a= mp;r1=3D1142378&r2=3D1142379&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/= java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java (origin= al)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7-builder/src/main/= java/org/apache/geronimo/tomcat/deployment/TomcatModuleBuilder.java Sun Jul= =A03 06:18:02 2011
@@ -30,6 +30,8 @@ import java.util.HashSet;
=A0import java.util.Map;
=A0import java.util.Set;
=A0import java.util.jar.JarFile;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;

=A0import javax.servlet.Servlet;
=A0import javax.xml.bind.JAXBException;
@@ -99,6 +101,7 @@ import org.apache.geronimo.xbeans.geroni
=A0import org.apache.geronimo.xbeans.geronimo.web.tomcat.TomcatWebAppDocume= nt;
=A0import org.apache.geronimo.xbeans.geronimo.web.tomcat.TomcatWebAppType;<= br> =A0import org.apache.geronimo.xbeans.geronimo.web.tomcat.config.GerTomcatDo= cument;
+import org.apache.openejb.jee.JavaeeSchema;
=A0import org.apache.openejb.jee.JaxbJavaee;
=A0import org.apache.openejb.jee.WebApp;
=A0import org.apache.xbean.osgi.bundle.util.BundleUtils;
@@ -269,6 +272,18 @@ public class TomcatModuleBuilder extends
=A0 =A0 =A0 =A0 return module;
=A0 =A0 }

+ =A0 =A0private boolean isSchemaDefined(String xmlFile, String definedTag)= {
+ =A0 =A0 =A0 =A0// we need remove the comments first
+ =A0 =A0 =A0 =A0Pattern commentsPattern =3D Pattern.compile("<!--(= .*)-->", Pattern.DOTALL);
+ =A0 =A0 =A0 =A0Matcher commentsMatcher =3D commentsPattern.matcher(xmlFil= e);
+
+ =A0 =A0 =A0 =A0Pattern schemaPattern =3D Pattern.compile("<(\\w*:= )?" + definedTag + "(.*)schemaLocation(.*)>", Pattern.DOT= ALL);
+ =A0 =A0 =A0 =A0Matcher schemaMatcher =3D schemaPattern.matcher(commentsMa= tcher.replaceAll(""));
+
+ =A0 =A0 =A0 =A0return schemaMatcher.find();
+
+ =A0 =A0}
+
=A0 =A0 protected Module createModule(Object plan, JarFile moduleFile, Str= ing targetPath, URL specDDUrl, Environment earEnvironment, String contextRo= ot, Module parentModule, Naming naming, ModuleIDBuilder idBuilder) throws D= eploymentException {
=A0 =A0 =A0 =A0 assert moduleFile !=3D null : "moduleFile is null&quo= t;;
=A0 =A0 =A0 =A0 assert targetPath !=3D null : "targetPath is null&quo= t;;
@@ -285,21 +300,41 @@ public class TomcatModuleBuilder extends
=A0 =A0 =A0 =A0 =A0 =A0 // read in the entire specDD as a string, we need = this for getDeploymentDescriptor
=A0 =A0 =A0 =A0 =A0 =A0 // on the J2ee management object
=A0 =A0 =A0 =A0 =A0 =A0 specDD =3D JarUtils.readAll(specDDUrl);
+
+ =A0 =A0 =A0 =A0 =A0 =A0InputStream in =3D null;
+
+ =A0 =A0 =A0 =A0 =A0 =A0// firstly validate the DD xml file, if it is defi= ned by a schema.
+ =A0 =A0 =A0 =A0 =A0 =A0if (isSchemaDefined(specDD, "web-app")){=
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0in =3D specDDUrl.openStream();
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0try {
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0JaxbJavaee.validateJavaee(JavaeeSc= hema.WEB_APP_3_0, in);
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} catch (Exception e) {
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0throw new DeploymentException(&quo= t;Error validate web.xml for " + targetPath, e);
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} finally {
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (in !=3D null)
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0in.close();
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
+ =A0 =A0 =A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0 =A0 =A0 // we found web.xml, if it won't parse that= 9;s an error.
- =A0 =A0 =A0 =A0 =A0 =A0InputStream in =3D specDDUrl.openStream();
+ =A0 =A0 =A0 =A0 =A0 =A0in =3D specDDUrl.openStream();
=A0 =A0 =A0 =A0 =A0 =A0 try {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 webApp =3D (WebApp) JaxbJavaee.unmarshalJa= vaee(WebApp.class, in);
+ =A0 =A0 =A0 =A0 =A0 =A0} catch (Exception e) {
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// Output the target path in the error to = make it clearer to the user which webapp
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// has the problem. The targetPath is used= , as moduleFile may have an unhelpful
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// value such as C:\geronimo-1.1\var\temp\= geronimo-deploymentUtil22826.tmpdir
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0throw new DeploymentException("Error = unmarshal web.xml for " + targetPath, e);
=A0 =A0 =A0 =A0 =A0 =A0 } finally {
- =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0in.close();
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (in !=3D null)
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0in.close();
=A0 =A0 =A0 =A0 =A0 =A0 }

- =A0 =A0 =A0 =A0} catch (JAXBException e) {
- =A0 =A0 =A0 =A0 =A0 =A0// Output the target path in the error to make it = clearer to the user which webapp
- =A0 =A0 =A0 =A0 =A0 =A0// has the problem. =A0The targetPath is used, as = moduleFile may have an unhelpful
- =A0 =A0 =A0 =A0 =A0 =A0// value such as C:\geronimo-1.1\var\temp\geronimo= -deploymentUtil22826.tmpdir
- =A0 =A0 =A0 =A0 =A0 =A0throw new DeploymentException("Error parsing = web.xml for " + targetPath, e);
=A0 =A0 =A0 =A0 } catch (Exception e) {
+ =A0 =A0 =A0 =A0 =A0 =A0if (e instanceof DeploymentException) {
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0throw new DeploymentException(e);
+ =A0 =A0 =A0 =A0 =A0 =A0}
+
=A0 =A0 =A0 =A0 =A0 =A0 if (!moduleFile.getName().endsWith(".war"= ;)) {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //not for us
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return null;





--
Ivan



--
Lei Wang (Rex)
rwonl= y AT apache.org
--20cf303f69b205aa3b04a7393fb5--