protected Manifest updateManifest()

in web/web-urlhandler/src/main/java/org/apache/aries/web/converter/impl/WarToWabConverterImpl.java [265:423]


  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
    if (manifest == null) {
      manifest = new Manifest();
      manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1");
    } else {
      // remove digest attributes if was is signed
      signed = removeDigestAttributes(manifest);
    }
    
    // Compare the manifest and the supplied properties
    
    //
    // Web-ContextPath
    //

    String webCPath = properties.get(WEB_CONTEXT_PATH);
    if (webCPath == null) {
        throw new IOException(WEB_CONTEXT_PATH + " parameter is missing.");
    }
    properties.put(WEB_CONTEXT_PATH, addSlash(webCPath));  

    //
    // Bundle-Version
    //

    if (manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION) == null
        && !properties.containsKey(Constants.BUNDLE_VERSION)) {
      properties.put(Constants.BUNDLE_VERSION, DEFAULT_BUNDLE_VERSION);
    }

    //
    // Bundle-ManifestVersion
    //

    String manifestVersion = properties.get(Constants.BUNDLE_MANIFESTVERSION);
    if (manifestVersion == null) {
        manifestVersion = manifest.getMainAttributes().getValue(Constants.BUNDLE_MANIFESTVERSION);
        if (manifestVersion == null) {
            manifestVersion = DEFAULT_BUNDLE_MANIFESTVERSION;
        }
    } else if (!manifestVersion.equals("2")) {
        throw new IOException("Unsupported bundle manifest version " + manifestVersion);
    }
    properties.put(Constants.BUNDLE_MANIFESTVERSION, manifestVersion);

    // 
    // Bundle-ClassPath
    //

    ArrayList<String> classpath = new ArrayList<String>();

    // Set initial entry into classpath
    classpath.add(INITIAL_CLASSPATH_ENTRY);

    // Add any files from the WEB-INF/lib directory + their dependencies
    classpath.addAll(classPath);
    
    // Get the list from the URL and add to classpath (removing duplicates)
    mergePathList(properties.get(Constants.BUNDLE_CLASSPATH), classpath, ",");

    // Get the existing list from the manifest file and add to classpath
    // (removing duplicates)
    mergePathList(manifest.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH), classpath, ",");

    // Construct the classpath string and set it into the properties
    StringBuffer classPathValue = new StringBuffer();
    for (String entry : classpath) {
      classPathValue.append(",");
      classPathValue.append(entry);
    }

    if (!classpath.isEmpty()) {
      properties.put(Constants.BUNDLE_CLASSPATH, classPathValue.toString().substring(1));
    }

    @SuppressWarnings("serial")
    ArrayList<String> packages = new ArrayList<String>() {
      @Override
      public boolean contains(Object elem) {
        // Check for exact match of export list
        if (super.contains(elem))
          return true;

        if (!!!(elem instanceof String))
          return false;

        String expPackageStmt = (String) elem;
        String expPackage = expPackageStmt.split("\\s*;\\s*")[0];
        
        Pattern p = Pattern.compile("^\\s*"+Pattern.quote(expPackage)+"((;|\\s).*)?\\s*$");
        for (String s : this) {
          Matcher m = p.matcher(s);
          if (m.matches()) {
            return true;
          }
        }

        return false;
      }

    };
    
    //
    // Import-Package
    //
    packages.clear();
    
    // Get the list from the URL and add to classpath (removing duplicates)
    mergePathList(properties.get(Constants.IMPORT_PACKAGE), packages, ",");

    // Get the existing list from the manifest file and add to classpath
    // (removing duplicates)
    mergePathList(manifest.getMainAttributes().getValue(Constants.IMPORT_PACKAGE), packages, ",");

    // Add the default set of packages
    mergePathList(DEFAULT_IMPORT_PACKAGE_LIST, packages, ",");

    // Analyse the bytecode of any .class files in the jar to find any other
    // required imports
    if (!!!importPackages.isEmpty()) {
      StringBuffer generatedImports = new StringBuffer();
      for (String entry : importPackages) {
        generatedImports.append(',');
        generatedImports.append(entry);
        generatedImports.append(";resolution:=optional");
      }      
      
      mergePathList(generatedImports.substring(1), packages, ",");
    }

    // Construct the string and set it into the properties
    StringBuffer importValues = new StringBuffer();
    for (String entry : packages) {
      importValues.append(",");
      importValues.append(entry);
    }
    if (!packages.isEmpty()) {
      properties.put(Constants.IMPORT_PACKAGE, importValues.toString().substring(1));
    }
     
    // Take the properties map and add them to the manifest file
    for (Map.Entry<String, String> entry : properties.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        manifest.getMainAttributes().putValue(key, value);
    }
    
    //
    // Bundle-SymbolicName
    //

    if (manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME) == null) {
        manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, warName + "_" + manifest.hashCode());
    }
    
    return manifest;
  }