Link to home
Start Free TrialLog in
Avatar of kalyandm
kalyandm

asked on

read application.xml or manifest

Hi friends
I have an EAR file which has application.xml and manifest.mf files.Now I have a java class which is alos part of the same EAR and has a method that needs to read a property from manifest.mf  called "Implementation-Version".Please can anyone suggest how I can do this.

Thanks
Avatar of mbvvsatish
mbvvsatish
Flag of Canada image

you can use the Class java.util.jar.Manifest to read the manifest files

or here is an alternative approach
http://forum.java.sun.com/thread.jspa?threadID=642761&messageID=3887881

Avatar of kalyandm
kalyandm

ASKER

Thanks .but I want to know How I can read the manifest from within ear for eg. manifest is located in meta-inf/ within ear >how can I get refrence that and then get it's attributes.
Hi
Thanks.I have seen thsi example already and we have to pass the name of the jar as an argument so it works fine but in my case I already have an ear and my class should find the manifest file within this ear and then read the properties from the manifest file.if you can help on this please
i will try to find out, but its not that easy. it will be good if you increase the points.
points increased!!
following is the program that reads the manifest file in a ear file (Archive & Directory Format)

use like this
java EarVersion Your_EAR_Directory
java EarVersion YourEar.ear


import java.io.*;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.jar.Attributes;


public class EarVersion{

  private static final String ATTR_IMPL_VERSION="Implementation-Version";
  private static final String ATTR_IMPL_TITLE="Implementation-Title";
  private static final String ATTR_IMPL_VENDOR="Implementation-Vendor";


  private static void printUsage(){
    System.out.println("java -jar earversion.jar {EAR file|Directory}");
  }

  public void directoryVersionInfo(File directory)throws Exception{
    String [] archives = directory.list(new java.io.FilenameFilter(){
          public boolean accept(File dir, String name){

            boolean result = (name.endsWith(".jar")||name.endsWith(".war"))||new File(dir,name).isDirectory();

            return result;
          }
        });
    for(int i = 0;i<archives.length;i++){
      File file = new File(directory, archives[i]);
      if(file.isDirectory()){
        directoryVersionInfo(file);
            System.out.println(file.getAbsolutePath());
            java.util.jar.Manifest earManifest = new java.util.jar.Manifest(new FileInputStream(file.getAbsolutePath().toString()+"\\MANIFEST.MF"));
          Attributes attribMap = earManifest.getMainAttributes();
            displayVersionInfo(attribMap);
        }
    }
  }

  private static void displayVersionInfo(Attributes attribMap){
    String version = (String) attribMap.getValue(ATTR_IMPL_VERSION);
    if(version!=null){
      System.out.println("\tVersion: " + version);
    }
    String vendor = (String) attribMap.getValue(ATTR_IMPL_VENDOR);
    if(vendor!=null){
      System.out.println("\tVendor:  " + vendor);
    }
    String title = (String) attribMap.getValue(ATTR_IMPL_TITLE);
    if(title!=null){
      System.out.println("\tTitle:   " + title);
    }
    if(version==null&&title==null&vendor==null){
      System.out.println("\tNo version information available.");
    }
  }

  public void archiveVersionInfo(File archive)throws Exception{
    JarFile earFile = new JarFile(archive, true, JarFile.OPEN_READ);
    try{
      java.util.jar.Manifest earManifest = earFile.getManifest();
      if (earManifest == null) {
        throw new Exception("No Manifest file in " + archive.getName());
      }
      Attributes attribMap = earManifest.getMainAttributes();
      if (attribMap == null) {
        throw new Exception("Invalid Manifest file in " + archive.getName());
      }

      System.out.println("Hello/" +archive.getName());
      displayVersionInfo(attribMap);
    }finally{
      earFile.close();
    }
  }

  public static final void main(String [] args){
    if(args.length<1){
      printUsage();
      return ;
    }
    String earFileName = args[0];
    File earFile = new File(earFileName);
    EarVersion versionChecker = new EarVersion();
    try{
      if (earFile.isDirectory()) {
        versionChecker.directoryVersionInfo(earFile);
      }
      else {
        versionChecker.archiveVersionInfo(earFile);
      }
    }catch(Exception ex){
      System.err.println("Error obtaining version for Enterprise Application Repository: "+ex.toString());
      ex.printStackTrace();
    }
  }
}
Hi
Thanks for the post.As I mentioned in my previous post, I don't have an option to specify the file name (of the ear) anywhere in my class as my class is the part of ear itself and it needs to refer to manifest within the same ear.sing fileinputstream is  a good option but how can I navigate with in the ear to find manifest file is the question..
I'm trying to use
   InputStream is  = this.getClass().getResourceAsStream ("meta-inf/manifest.mf");

to resolve the path of manifest file within ear but it returns null.Any ideas please?
try giving
../meta-inf/manifest.mf
or
../../meta-inf/manifest.mf
Tried it doesn't work..sorry..
Basically the class in which check for manifest exists like this

x.ear--
         |__meta-inf/manifest.mf
         |__a.jar--
                       |_myclass

so myclass should access manifest file.



                   
Hi friends any idea please .do i need to modify the classpath or something in my manifest file under ear...
Hello Administrator,
Please can you close this question as I have found the soultion myself now.I just had to correct the path and it works.

Thanks
can you please explain how you have solved the problem?
I did not manage to access application.xml/manifest file as such but we created a new file which just has the version number of build in same jar and I'm reading that.tha solves the problem but I'm still looking at how I can access manifest under the ear file.

Hope this clarifies
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial