|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| EarParser.java | 83.3% | 87.5% | 100% | 87.2% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus.integration.ant.container;
|
|
| 58 |
|
|
| 59 |
import java.io.File;
|
|
| 60 |
import java.io.IOException;
|
|
| 61 |
import java.util.Iterator;
|
|
| 62 |
|
|
| 63 |
import javax.xml.parsers.ParserConfigurationException;
|
|
| 64 |
|
|
| 65 |
import org.apache.cactus.integration.ant.deployment.ApplicationXml;
|
|
| 66 |
import org.apache.cactus.integration.ant.deployment.DefaultEarArchive;
|
|
| 67 |
import org.apache.cactus.integration.ant.deployment.EarArchive;
|
|
| 68 |
import org.apache.cactus.integration.ant.deployment.WarArchive;
|
|
| 69 |
import org.apache.tools.ant.BuildException;
|
|
| 70 |
import org.xml.sax.SAXException;
|
|
| 71 |
|
|
| 72 |
/**
|
|
| 73 |
* Parse an EAR descriptor to extract meaninful information for Cactus,
|
|
| 74 |
* the results being stored in a {@link EarDeployableFile} object.
|
|
| 75 |
*
|
|
| 76 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 77 |
*
|
|
| 78 |
* @since Cactus 1.5
|
|
| 79 |
* @version $Id: EarParser.java,v 1.1.2.2 2003/10/25 21:05:48 vmassol Exp $
|
|
| 80 |
*/
|
|
| 81 |
public class EarParser |
|
| 82 |
{
|
|
| 83 |
/**
|
|
| 84 |
* Parse an EAR descriptor to extract meaninful information for Cactus.
|
|
| 85 |
*
|
|
| 86 |
* @param theDeployableFile the file to parse and deploy
|
|
| 87 |
* @return the parse results as a {@link EarDeployableFile} object
|
|
| 88 |
*/
|
|
| 89 | 4 |
public static final EarDeployableFile parse(File theDeployableFile) |
| 90 |
{
|
|
| 91 | 4 |
EarDeployableFile deployable = new EarDeployableFile();
|
| 92 |
|
|
| 93 | 4 |
try
|
| 94 |
{
|
|
| 95 | 4 |
deployable.setFile(theDeployableFile); |
| 96 |
|
|
| 97 | 4 |
EarArchive earArchive = new DefaultEarArchive(theDeployableFile);
|
| 98 | 4 |
String webUri = getUriOfCactifiedWebModule(earArchive); |
| 99 | 4 |
if (webUri == null) |
| 100 |
{
|
|
| 101 | 2 |
throw new BuildException("Could not find cactified web " |
| 102 |
+ "module in the [" + theDeployableFile + "] EAR."); |
|
| 103 |
} |
|
| 104 |
|
|
| 105 | 2 |
WarArchive warArchive = earArchive.getWebModule(webUri); |
| 106 | 2 |
if (warArchive == null) |
| 107 |
{
|
|
| 108 | 0 |
throw new BuildException("Could not find the WAR [" + webUri |
| 109 |
+ "] in the [" + theDeployableFile + "] EAR."); |
|
| 110 |
} |
|
| 111 |
|
|
| 112 | 2 |
deployable.setWarArchive(warArchive); |
| 113 | 2 |
deployable.setTestContext(parseTestContext(earArchive, webUri)); |
| 114 | 2 |
deployable.setServletRedirectorMapping( |
| 115 |
WarParser.parseServletRedirectorMapping( |
|
| 116 |
deployable.getWarArchive())); |
|
| 117 | 2 |
deployable.setFilterRedirectorMapping( |
| 118 |
WarParser.parseFilterRedirectorMapping( |
|
| 119 |
deployable.getWarArchive())); |
|
| 120 | 2 |
deployable.setJspRedirectorMapping( |
| 121 |
WarParser.parseJspRedirectorMapping( |
|
| 122 |
deployable.getWarArchive())); |
|
| 123 |
} |
|
| 124 |
catch (IOException e)
|
|
| 125 |
{
|
|
| 126 | 0 |
throw new BuildException("Failed to parse deployment descriptor " |
| 127 |
+ "for EAR file [" + theDeployableFile + "].", e); |
|
| 128 |
} |
|
| 129 |
catch (ParserConfigurationException e)
|
|
| 130 |
{
|
|
| 131 | 0 |
throw new BuildException("Failed to parse deployment descriptor " |
| 132 |
+ "for EAR file [" + theDeployableFile + "].", e); |
|
| 133 |
} |
|
| 134 |
catch (SAXException e)
|
|
| 135 |
{
|
|
| 136 | 0 |
throw new BuildException("Failed to parse deployment descriptor " |
| 137 |
+ "for EAR file [" + theDeployableFile + "].", e); |
|
| 138 |
} |
|
| 139 |
|
|
| 140 | 2 |
return deployable;
|
| 141 |
} |
|
| 142 |
|
|
| 143 |
/**
|
|
| 144 |
* Find the test context from the EAR archive.
|
|
| 145 |
*
|
|
| 146 |
* @return the test context
|
|
| 147 |
* @param theEar the EAR archive from which to extract the test context
|
|
| 148 |
* @param theWebUri the WAR URI of the WAR file in the EAR from which to
|
|
| 149 |
* extract the test context
|
|
| 150 |
* @throws IOException If there was a problem reading the deployment
|
|
| 151 |
* descriptor in the WAR
|
|
| 152 |
* @throws SAXException If the deployment descriptor of the WAR could not
|
|
| 153 |
* be parsed
|
|
| 154 |
* @throws ParserConfigurationException If there is an XML parser
|
|
| 155 |
* configration problem
|
|
| 156 |
*/
|
|
| 157 | 4 |
protected static final String parseTestContext(EarArchive theEar, |
| 158 |
String theWebUri) |
|
| 159 |
throws ParserConfigurationException, IOException, SAXException
|
|
| 160 |
{
|
|
| 161 | 4 |
String context = theEar.getApplicationXml() |
| 162 |
.getWebModuleContextRoot(theWebUri); |
|
| 163 | 4 |
if (context == null) |
| 164 |
{
|
|
| 165 |
// The application.xml does not define a <context-root> element.
|
|
| 166 |
// This is wrong!
|
|
| 167 | 1 |
throw new BuildException("Your application.xml must define a " |
| 168 |
+ "<context-root> element in the <web> module definition.");
|
|
| 169 |
} |
|
| 170 |
|
|
| 171 |
// Remove leading "/" if there is one.
|
|
| 172 | 3 |
if (context.startsWith("/")) |
| 173 |
{
|
|
| 174 | 3 |
context = context.substring(1); |
| 175 |
} |
|
| 176 |
|
|
| 177 | 3 |
return context;
|
| 178 |
} |
|
| 179 |
|
|
| 180 |
/**
|
|
| 181 |
* Finds the web module in the EAR that contains the servlet test
|
|
| 182 |
* redirector, and returns the web-uri of the module found.
|
|
| 183 |
*
|
|
| 184 |
* <em>A web-app is considered cactified when it contains at least a
|
|
| 185 |
* mapping for the Cactus servlet test redirector</em>
|
|
| 186 |
*
|
|
| 187 |
* @return The URI of the cactified web-module, or <code>null</code> if no
|
|
| 188 |
* cactified web-app could be found
|
|
| 189 |
* @param theEar the EAR archive from which to extract the web URI
|
|
| 190 |
* @throws IOException If there was a problem reading the deployment
|
|
| 191 |
* descriptor in the WAR
|
|
| 192 |
* @throws SAXException If the deployment descriptor of the WAR could not
|
|
| 193 |
* be parsed
|
|
| 194 |
* @throws ParserConfigurationException If there is an XML parser
|
|
| 195 |
* configration problem
|
|
| 196 |
*/
|
|
| 197 | 4 |
protected static final String getUriOfCactifiedWebModule(EarArchive theEar) |
| 198 |
throws SAXException, IOException, ParserConfigurationException
|
|
| 199 |
{
|
|
| 200 | 4 |
ApplicationXml applicationXml = theEar.getApplicationXml(); |
| 201 | 4 |
for (Iterator i = applicationXml.getWebModuleUris(); i.hasNext();)
|
| 202 |
{
|
|
| 203 | 3 |
String webUri = (String) i.next(); |
| 204 | 3 |
WarArchive war = theEar.getWebModule(webUri); |
| 205 | 3 |
if ((war != null) |
| 206 |
&& (WarParser.parseServletRedirectorMapping(war) != null))
|
|
| 207 |
{
|
|
| 208 | 2 |
return webUri;
|
| 209 |
} |
|
| 210 |
} |
|
| 211 | 2 |
return null; |
| 212 |
} |
|
| 213 |
} |
|
| 214 |
|
|
||||||||||