/*
 * Copyright to the original author or authors.
 *
 * 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.rioproject.util;

import java.util.jar.*;
import java.io.*;
import java.net.*;

/**
 * Utility for getting the Rio Attributes from a Jar File.
 * <br>
 * The Rio attributes are:<br>
 * <br>
 * "Rio-Build:", for example:<br>
 * <br>
 *  Manifest-Version: 1.0<br>
 *  Rio-Build: 20070602<br>
 *  <br>
 *
 * @author Jim Clarke
 * @author Dennis Reedy
 */
public class RioManifest {
    public static final Attributes.Name RIO_BUILD =
        new Attributes.Name("Rio-Build");
    private JarInputStream jarIn = null;
    /** Holds value of property manifest. */
    private Manifest manifest;
    
    public RioManifest(URL url) throws IOException {
        if(!url.getProtocol().equals("jar")) {
            url = new URL("jar:" + url.toExternalForm() + "!/");
        }
        JarURLConnection uc = (JarURLConnection) url.openConnection();
        setManifest(uc.getManifest());
    }
    
    public RioManifest(InputStream in) throws IOException {
        jarIn = new JarInputStream(in);
        setManifest(jarIn.getManifest());
    }

    public RioManifest(Manifest manifest) {
        setManifest(manifest);
    }
    
    public void close() throws IOException {
        if(jarIn != null)
            jarIn.close();        
    }    

    /**
     * Get the Rio Build from a Jar file
     *
     * @return the Rio Build from a Jar file, or null if not defined.
     */
    public String getRioBuild() throws IOException {
        return getMainAttribute(RIO_BUILD);
    }
    
    /**
     * Get an Attribute from a Jar file
     *
     * @param name the name of the main attribute entry
     * @return the value of the main attribute from a Jar file, or null if not defined.
     */
    public String getMainAttribute(String name) throws IOException {
        return getMainAttribute(new Attributes.Name(name));
    }
    
    /**
     * Get an Attribute from a Jar file
     *
     * @param name the name of the main attribute entry
     * @return the value of the main attribute from a Jar file, or null if not defined.
     */
    public String getMainAttribute(Attributes.Name name) throws IOException {
        if(manifest==null)
            throw new NullPointerException("there is no manifest");
        Attributes attributes = manifest.getMainAttributes();
        if(attributes == null)
            return null;
        return (String)attributes.get(name);
    }
        
    /**
     * Get an Entry from a Jar file
     *
     * @param name the name of the entry
     * @return the attributes for the entry or null if not defined
     */
    public Attributes getEntry(String name) throws IOException {
        if(manifest==null)
            throw new NullPointerException("there is no manifest");
        return manifest.getAttributes(name);
    }

    public Attributes getMainAttributes() throws IOException {
        if(manifest==null)
            throw new NullPointerException("there is no manifest");
        return(manifest.getMainAttributes());
    }    

    /** 
     * Getter for property manifest.
     * 
     * @return Value of property manifest.
     */
    public Manifest getManifest() {
        return manifest;
    }
    
    /** 
     * Setter for property manifest.
     * 
     * @param manifest New value of property manifest.
     */
    public void setManifest(Manifest manifest) {
        this.manifest = manifest;
    }
    
}

@@ -1049,24 +1049,16 @@
 _BUILD =
-%0A       
  new Att
@@ -1080,24 +1080,24 @@
 io-Build%22);%0A
+
     private 
@@ -1506,243 +1506,8 @@
    %0A
-    public RioManifest(InputStream in) throws IOException %7B%0A        jarIn = new JarInputStream(in);%0A        setManifest(jarIn.getManifest());%0A    %7D%0A%0A    public RioManifest(Manifest manifest) %7B%0A        setManifest(manifest);%0A    %7D%0A    %0A
     
@@ -2072,355 +2072,8 @@
  */%0A
-    public String getMainAttribute(String name) throws IOException %7B%0A        return getMainAttribute(new Attributes.Name(name));%0A    %7D%0A    %0A    /**%0A     * Get an Attribute from a Jar file%0A     *%0A     * @param name the name of the main attribute entry%0A     * @return the value of the main attribute from a Jar file, or null if not defined.%0A     */%0A
     
@@ -2806,404 +2806,11 @@
     
-public Attributes getMainAttributes() throws IOException %7B%0A        if(manifest==null)%0A            throw new NullPointerException(%22there is no manifest%22);%0A        return(manifest.getMainAttributes());%0A    %7D    %0A%0A    /** %0A     * Getter for property manifest.%0A     * %0A     * @return Value of property manifest.%0A     */%0A    public Manifest getManifest() %7B%0A        return manifest;%0A    %7D%0A    %0A    /** 
+/**
 %0A   
@@ -2843,23 +2843,22 @@
 nifest.%0A
+
      *
- 
 %0A     * 

Remove dead code