You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ga...@apache.org on 2010/02/02 20:07:17 UTC

svn commit: r905748 - in /incubator/aries/trunk/web: web-itests/src/test/java/org/apache/aries/web/converter/itest/ web-urlhandler/src/main/java/org/apache/aries/web/converter/impl/ web-urlhandler/src/test/java/org/apache/aries/web/converter/impl/

Author: gawor
Date: Tue Feb  2 19:07:12 2010
New Revision: 905748

URL: http://svn.apache.org/viewvc?rev=905748&view=rev
Log:
ARIES-139: Follow different rules when coverting a bundle vs. a war file. Also Web-ContextPath parameter is required for war conversion

Modified:
    incubator/aries/trunk/web/web-itests/src/test/java/org/apache/aries/web/converter/itest/WabConverterITest.java
    incubator/aries/trunk/web/web-urlhandler/src/main/java/org/apache/aries/web/converter/impl/WarToWabConverterImpl.java
    incubator/aries/trunk/web/web-urlhandler/src/test/java/org/apache/aries/web/converter/impl/WabConverterTest.java

Modified: incubator/aries/trunk/web/web-itests/src/test/java/org/apache/aries/web/converter/itest/WabConverterITest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/web/web-itests/src/test/java/org/apache/aries/web/converter/itest/WabConverterITest.java?rev=905748&r1=905747&r2=905748&view=diff
==============================================================================
--- incubator/aries/trunk/web/web-itests/src/test/java/org/apache/aries/web/converter/itest/WabConverterITest.java (original)
+++ incubator/aries/trunk/web/web-itests/src/test/java/org/apache/aries/web/converter/itest/WabConverterITest.java Tue Feb  2 19:07:12 2010
@@ -93,12 +93,13 @@
     
     File testWar = new File("test.war");
     Bundle converted = bundleContext.installBundle("webbundle:"+testWar.toURL().toExternalForm() 
-        + "?Bundle-SymbolicName=test.war.bundle");
+        + "?Bundle-SymbolicName=test.war.bundle&Web-ContextPath=foo");
     
     assertNotNull(converted);
     Dictionary<String,String> man = converted.getHeaders();
     
-    assertEquals("test.war.bundle", man.get(Constants.BUNDLE_SYMBOLICNAME));   
+    assertEquals("test.war.bundle", man.get(Constants.BUNDLE_SYMBOLICNAME)); 
+    assertEquals("/foo", man.get("Web-ContextPath"));
     assertTrue(man.get(Constants.IMPORT_PACKAGE).contains("javax.naming"));
   }
 

Modified: incubator/aries/trunk/web/web-urlhandler/src/main/java/org/apache/aries/web/converter/impl/WarToWabConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/web/web-urlhandler/src/main/java/org/apache/aries/web/converter/impl/WarToWabConverterImpl.java?rev=905748&r1=905747&r2=905748&view=diff
==============================================================================
--- incubator/aries/trunk/web/web-urlhandler/src/main/java/org/apache/aries/web/converter/impl/WarToWabConverterImpl.java (original)
+++ incubator/aries/trunk/web/web-urlhandler/src/main/java/org/apache/aries/web/converter/impl/WarToWabConverterImpl.java Tue Feb  2 19:07:12 2010
@@ -32,7 +32,6 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
-import java.util.StringTokenizer;
 import java.util.jar.Attributes;
 import java.util.jar.JarInputStream;
 import java.util.jar.JarOutputStream;
@@ -57,9 +56,6 @@
       + "javax.servlet.jsp.el;version=2.1,"
       + "javax.servlet.jsp.tagext;version=2.1";
 
-  private static final String DEFAULT_WEB_CONTEXT_PATH = "/";
-  private static final String DEFAULT_WEB_JSP_EXTRACT_LOCATION = "/";
-
   private Properties properties;
 
   // InputStream for the new WAB file
@@ -92,11 +88,14 @@
 
     try {
       jarInput = new JarInputStream(input.getInputStream());
-      scanForDependencies(jarInput);
-
-      // Add the new properties to the manifest byte stream
       Manifest manifest = jarInput.getManifest();
-      wabManifest = updateManifest(manifest);
+      if (isBundle(manifest)) {
+          wabManifest = updateBundleManifest(manifest);
+      } else {
+          scanForDependencies(jarInput);
+          // Add the new properties to the manifest byte stream
+          wabManifest = updateManifest(manifest);
+      }
     } 
     finally {
       try { if (jarInput != null) jarInput.close(); } catch (IOException e) { e.printStackTrace(); }
@@ -129,6 +128,18 @@
     wabFile = output.toByteArray();
   }
 
+  private boolean isBundle(Manifest manifest)  {
+      if (manifest == null) {
+          return false;          
+      }
+      Attributes attributes = manifest.getMainAttributes();
+      // TODO: need to check for other headers too
+      if (attributes.getValue(Constants.BUNDLE_SYMBOLICNAME) != null) {
+          return true;
+      }
+      return false;
+  }
+  
   private void scanRecursive(final JarInputStream jarInput, boolean topLevel) throws IOException 
   {
     ZipEntry entry;
@@ -190,6 +201,36 @@
         importPackages.remove(s);
   }
 
+  protected Manifest updateBundleManifest(Manifest manifest) throws IOException {
+      String webCPath = properties.getProperty(WEB_CONTEXT_PATH);
+      if (webCPath == null) {
+          webCPath = manifest.getMainAttributes().getValue(WEB_CONTEXT_PATH);
+      }
+      if (webCPath == null) {
+          throw new IOException("Must specify " + WEB_CONTEXT_PATH + " parameter. The " + 
+                                WEB_CONTEXT_PATH + " header is not defined in the source bundle.");
+      } else {
+          webCPath = addSlash(webCPath);
+          manifest.getMainAttributes().put(new Attributes.Name(WEB_CONTEXT_PATH), webCPath);
+      }
+      
+      // converter is not allowed to specify and override the following properties
+      // when source is already a bundle
+      checkParameter(Constants.BUNDLE_VERSION);
+      checkParameter(Constants.BUNDLE_MANIFESTVERSION);
+      checkParameter(Constants.BUNDLE_SYMBOLICNAME);
+      checkParameter(Constants.IMPORT_PACKAGE);
+      checkParameter(Constants.BUNDLE_CLASSPATH);
+              
+      return manifest;
+  }
+  
+  private void checkParameter(String parameter) throws IOException {
+      if (properties.containsKey(parameter)) {
+          throw new IOException("Cannot override " + parameter + " parameter when converting a bundle");
+      }
+  }
+  
   protected Manifest updateManifest(Manifest manifest) throws IOException
   {
     // If for some reason no manifest was generated, we start our own so that we don't null pointer later on
@@ -199,6 +240,16 @@
     }
     
     // Compare the manifest and the supplied properties
+    
+    //
+    // Web-ContextPath
+    //
+
+    String webCPath = properties.getProperty(WEB_CONTEXT_PATH);
+    if (webCPath == null) {
+        throw new IOException(WEB_CONTEXT_PATH + " parameter is required.");
+    }
+    properties.put(WEB_CONTEXT_PATH, addSlash(webCPath));  
 
     //
     // Bundle-Version
@@ -215,8 +266,7 @@
 
     if (manifest.getMainAttributes().getValue(Constants.BUNDLE_MANIFESTVERSION) == null
         && !properties.containsKey(Constants.BUNDLE_MANIFESTVERSION)) {
-      properties.put(Constants.BUNDLE_MANIFESTVERSION,
-          DEFAULT_BUNDLE_MANIFESTVERSION);
+      properties.put(Constants.BUNDLE_MANIFESTVERSION, DEFAULT_BUNDLE_MANIFESTVERSION);
     }
 
     //
@@ -225,8 +275,7 @@
 
     if (manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME) == null
         && !properties.containsKey(Constants.BUNDLE_SYMBOLICNAME)) {
-      properties.put(Constants.BUNDLE_SYMBOLICNAME, warName + "_"
-          + manifest.hashCode());
+      properties.put(Constants.BUNDLE_SYMBOLICNAME, warName + "_" + manifest.hashCode());
     }
 
     // 
@@ -322,32 +371,24 @@
     if (!packages.isEmpty()) {
       properties.put(Constants.IMPORT_PACKAGE, importValues.toString().substring(1));
     }
-    
-    //
-    // Web-ContextPath
-    //
-
-    String webCPath = properties.getProperty(WEB_CONTEXT_PATH);
-    if (webCPath == null) {
-        webCPath = manifest.getMainAttributes().getValue(WEB_CONTEXT_PATH);
-    }
-    if (webCPath == null) {
-        properties.put(WEB_CONTEXT_PATH, DEFAULT_WEB_CONTEXT_PATH);
-    } else {
-        // always ensure context path starts with slash
-        if (!webCPath.startsWith("/")) {
-            webCPath = "/" + webCPath;
-        }
-        properties.put(WEB_CONTEXT_PATH, webCPath);
-    }
-
+     
     // Take the properties map and add them to the manifest file
-    for (Object s : properties.keySet())
-      manifest.getMainAttributes().put(new Attributes.Name((String) s), properties.get(s));
+    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
+        String key = entry.getKey().toString();
+        String value = entry.getValue().toString();
+        manifest.getMainAttributes().put(new Attributes.Name(key), value);
+    }
     
     return manifest;
   }
 
+  private static String addSlash(String contextPath) {
+      if (!contextPath.startsWith("/")) {
+          contextPath = "/" + contextPath;
+      }
+      return contextPath;
+  }
+  
   // pathlist = A "delim" delimitted list of path entries
   private static void mergePathList(String pathlist, ArrayList<String> paths, String delim) {
       if (pathlist != null) {

Modified: incubator/aries/trunk/web/web-urlhandler/src/test/java/org/apache/aries/web/converter/impl/WabConverterTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/web/web-urlhandler/src/test/java/org/apache/aries/web/converter/impl/WabConverterTest.java?rev=905748&r1=905747&r2=905748&view=diff
==============================================================================
--- incubator/aries/trunk/web/web-urlhandler/src/test/java/org/apache/aries/web/converter/impl/WabConverterTest.java (original)
+++ incubator/aries/trunk/web/web-urlhandler/src/test/java/org/apache/aries/web/converter/impl/WabConverterTest.java Tue Feb  2 19:07:12 2010
@@ -20,6 +20,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -52,7 +53,9 @@
   @Test
   public void testNullManifest() throws Exception
   {
-    WarToWabConverterImpl sut = new WarToWabConverterImpl(makeTestFile(new byte[0]), WAR_FILE_NAME, new Properties());
+    Properties properties = new Properties();
+    properties.put(WarToWabConverter.WEB_CONTEXT_PATH, "/test");
+    WarToWabConverterImpl sut = new WarToWabConverterImpl(makeTestFile(new byte[0]), WAR_FILE_NAME, properties);
     
     Manifest res = sut.updateManifest(null);
     Attributes attrs = res.getMainAttributes();
@@ -63,7 +66,9 @@
   @Test
   public void testImportPackageMerge() throws Exception
   {
-    WarToWabConverterImpl sut = new WarToWabConverterImpl(makeTestFile(new byte[0]), WAR_FILE_NAME, new Properties());
+    Properties properties = new Properties();
+    properties.put(WarToWabConverter.WEB_CONTEXT_PATH, "/test");
+    WarToWabConverterImpl sut = new WarToWabConverterImpl(makeTestFile(new byte[0]), WAR_FILE_NAME, properties);
     
     Manifest input = new Manifest();
     input.getMainAttributes().putValue("Import-Package", "com.ibm.test,javax.servlet.http");
@@ -86,6 +91,7 @@
   public void testImportPackageWithAttributesMerge() throws Exception
   {
       Attributes attrs = convertWithProperties(
+                WarToWabConverter.WEB_CONTEXT_PATH, "/test",
                 Constants.IMPORT_PACKAGE, "javax.servlet.jsp; version=\"[2.0,2.1]\",javax.servlet.jsp.tagext; version=\"[2.0,2.1]\"");
       
       String actual = attrs.getValue("Import-Package");
@@ -112,6 +118,7 @@
     InputStreamProvider input = makeTestFile(bout.toByteArray());
     
     Properties props = new Properties();
+    props.put(WarToWabConverter.WEB_CONTEXT_PATH, "/test");
     props.put(Constants.BUNDLE_SYMBOLICNAME, "test.bundle");
     WarToWabConverterImpl sut = new WarToWabConverterImpl(input, WAR_FILE_NAME, props);
     
@@ -121,7 +128,8 @@
   
   @Test 
   public void testDefaultProperties() throws Exception {
-    Attributes attrs = convertWithProperties();
+    Attributes attrs = convertWithProperties(
+            WarToWabConverter.WEB_CONTEXT_PATH, "/test");
     
     assertTrue(attrs.getValue(Constants.BUNDLE_SYMBOLICNAME).startsWith(WAR_FILE_NAME_WO_SUFFIX));
     assertEquals("1.0", attrs.getValue(Constants.BUNDLE_VERSION));
@@ -149,7 +157,7 @@
   }
   
   @Test
-  public void testManifestAndPropertyOverwrites() throws Exception {
+  public void testBundleContextPathOverride() throws Exception {
     Manifest m = new Manifest();
     Attributes attrs = m.getMainAttributes();
     attrs.putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.test");
@@ -157,16 +165,56 @@
     attrs.putValue(Constants.IMPORT_PACKAGE, "org.apache.util,org.apache.test;version=1.0");
     attrs.putValue(Constants.BUNDLE_CLASSPATH, "jsp/classes");
     
-    attrs = convertWithProperties(m, Constants.BUNDLE_VERSION, "2.0",
-        Constants.IMPORT_PACKAGE, "org.apache.wab,org.apache.test;version=2.0",
-        Constants.BUNDLE_CLASSPATH, "aries/generated");
+    attrs = convertWithProperties(m, 
+        WarToWabConverter.WEB_CONTEXT_PATH, "WebFiles");
+       
+    assertEquals("org.apache.test", attrs.getValue(Constants.BUNDLE_SYMBOLICNAME));
+    assertEquals("1.0", attrs.getValue(Constants.BUNDLE_VERSION));
+    assertTrue(attrs.getValue(Constants.IMPORT_PACKAGE).contains("org.apache.util"));
+    assertTrue(attrs.getValue(Constants.IMPORT_PACKAGE).contains("org.apache.test;version=1.0"));    
+    assertEquals("jsp/classes", attrs.getValue(Constants.BUNDLE_CLASSPATH));
+    assertEquals("/WebFiles", attrs.getValue(WarToWabConverter.WEB_CONTEXT_PATH));
+  }
+  
+  @Test
+  public void testBundleContextPathManifestOverride() throws Exception {
+    Manifest m = new Manifest();
+    Attributes attrs = m.getMainAttributes();
+    attrs.putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.test");
+    attrs.putValue(WarToWabConverter.WEB_CONTEXT_PATH, "test");
+    attrs.putValue(Constants.BUNDLE_VERSION, "1.0");
+    attrs.putValue(Constants.IMPORT_PACKAGE, "org.apache.util,org.apache.test;version=1.0");
+    attrs.putValue(Constants.BUNDLE_CLASSPATH, "jsp/classes");
     
+    attrs = convertWithProperties(m, 
+        WarToWabConverter.WEB_CONTEXT_PATH, "WebFiles");
+       
     assertEquals("org.apache.test", attrs.getValue(Constants.BUNDLE_SYMBOLICNAME));
-    assertEquals("2.0", attrs.getValue(Constants.BUNDLE_VERSION));
+    assertEquals("1.0", attrs.getValue(Constants.BUNDLE_VERSION));
     assertTrue(attrs.getValue(Constants.IMPORT_PACKAGE).contains("org.apache.util"));
-    assertTrue(attrs.getValue(Constants.IMPORT_PACKAGE).contains("org.apache.test;version=2.0"));    
-    assertTrue(attrs.getValue(Constants.IMPORT_PACKAGE).contains("org.apache.wab"));
-    assertEquals("WEB-INF/classes,aries/generated,jsp/classes", attrs.getValue(Constants.BUNDLE_CLASSPATH));
+    assertTrue(attrs.getValue(Constants.IMPORT_PACKAGE).contains("org.apache.test;version=1.0"));    
+    assertEquals("jsp/classes", attrs.getValue(Constants.BUNDLE_CLASSPATH));
+    assertEquals("/WebFiles", attrs.getValue(WarToWabConverter.WEB_CONTEXT_PATH));
+  }
+  
+  @Test
+  public void testBundleManifestOverride() throws Exception {
+    Manifest m = new Manifest();
+    Attributes attrs = m.getMainAttributes();
+    attrs.putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.test");
+    attrs.putValue(WarToWabConverter.WEB_CONTEXT_PATH, "test");
+    attrs.putValue(Constants.BUNDLE_VERSION, "1.0");
+    attrs.putValue(Constants.IMPORT_PACKAGE, "org.apache.util,org.apache.test;version=1.0");
+    attrs.putValue(Constants.BUNDLE_CLASSPATH, "jsp/classes");
+    
+    try {
+        convertWithProperties(m, 
+                WarToWabConverter.WEB_CONTEXT_PATH, "WebFiles",
+                Constants.BUNDLE_SYMBOLICNAME, "foobar");
+        fail("Conversion did not fail as expected");
+    } catch (IOException e) {
+        // that's expected
+    }
   }
   
   private Attributes convertWithProperties(Manifest m, String ... props) throws Exception {