3 Replies Latest reply on Dec 6, 2010 5:10 PM by Lukáš Fryč

    How to get manifest from war on JBoss AS

    Lukáš Fryč Master

      Hi guys,

      do anybody know how to get manifest from inside war deployed in JBoss AS (6 M5)?

      I have JSF libraries attached to project and want to detect their version:

       

       

       

      Class<?> applicationClass = facesContext.getApplication().getClass();
      CodeSource codeSource = applicationClass.getProtectionDomain().getCodeSource();
      URL url = codeSource.getLocation();
      JarInputStream jarInputStream = new JarInputStream(url.openStream());
      Manifest manifest = jarInputStream.getManifest();
      

       

      such code returns manifest == null.

       

      ...
      URL url = codeSource.getLocation();
      File file = new File(url.getFile());
      jarInputStream = new JarInputStream(new FileInputStream(file));
      Manifest manifest = jarInputStream.getManifest();
      

       

      the code above works well with application exploded to JBoss AS or Tomcat.
      However it doesn't work in zipped war due to VFS URL given.

        • 1. Re: How to get manifest from war on JBoss AS
          jaikiran pai Master

          Would this work?

           

          Package pack = applicationClass.getPackage();
          String version = null;
          version = pack.getImplementationVersion();
          if (version == null)
          {
           version = pack.getSpecificationVersion();
          } 
          
          
          • 2. Re: How to get manifest from war on JBoss AS
            Nick Belaevski Master

            Lukas,

             

            I have the following code in VersionBean:

             

                    private Manifest readManifest() {
                        ProtectionDomain domain = VersionBean.class.getProtectionDomain();
                        if (domain != null) {
                            CodeSource codeSource = domain.getCodeSource();
                            if (codeSource != null) {
                                URL url = codeSource.getLocation();
                                if (url != null) {
                                    InputStream manifestStream = null;
                                    try {
                                        manifestStream = URLToStreamHelper.urlToStream(new URL(url, JarFile.MANIFEST_NAME));
                                        return new Manifest(manifestStream);
                                    } catch (MalformedURLException e1) {
                                        //that's ok - just log in debug
                                        if (LOGGER.isDebugEnabled()) {
                                            LOGGER.debug(e1.getMessage(), e1);
                                        }
                                    } catch (IOException e) {
                                        //that's ok - just log in debug
                                        if (LOGGER.isDebugEnabled()) {
                                            LOGGER.debug(e.getMessage(), e);
                                        }
                                    } finally {
                                        if (manifestStream != null) {
                                            try {
                                                manifestStream.close();
                                            } catch (IOException e) {
                                                LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
                                            }
                                        }
                                    }
                                   
                                    JarInputStream jis = null;
                                    try {
                                        URLConnection urlConnection = url.openConnection();
                                        urlConnection.setUseCaches(false);
                                        if (urlConnection instanceof JarURLConnection) {
                                            JarURLConnection jarUrlConnection = (JarURLConnection) urlConnection;
                                            return jarUrlConnection.getManifest();
                                        } else {
                                            jis = new JarInputStream(urlConnection.getInputStream());
                                            return jis.getManifest();
                                        }
                                    } catch (IOException e) {
                                        LOGGER.error(MessageFormat.format("Error reading META-INF/MANIFEST.MF file: {0}", e.getMessage()), e);
                                    } finally {
                                        if (jis != null) {
                                            try {
                                                jis.close();
                                            } catch (IOException e) {
                                                LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                       
                        return null;
                    }
                    private Manifest readManifest() {
                        ProtectionDomain domain = VersionBean.class.getProtectionDomain();
                        if (domain != null) {
                            CodeSource codeSource = domain.getCodeSource();
                            if (codeSource != null) {
                                URL url = codeSource.getLocation();
                                if (url != null) {
                                    InputStream manifestStream = null;
                                    try {
                                        manifestStream = URLToStreamHelper.urlToStream(new URL(url, JarFile.MANIFEST_NAME));
                                        return new Manifest(manifestStream);
                                    } catch (MalformedURLException e1) {
                                        //that's ok - just log in debug
                                        if (LOGGER.isDebugEnabled()) {
                                            LOGGER.debug(e1.getMessage(), e1);
                                        }
                                    } catch (IOException e) {
                                        //that's ok - just log in debug
                                        if (LOGGER.isDebugEnabled()) {
                                            LOGGER.debug(e.getMessage(), e);
                                        }
                                    } finally {
                                        if (manifestStream != null) {
                                            try {
                                                manifestStream.close();
                                            } catch (IOException e) {
                                                LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
                                            }
                                        }
                                    }
            
                                    JarInputStream jis = null;
                                    try {
                                        URLConnection urlConnection = url.openConnection();
                                        urlConnection.setUseCaches(false);
            
                                        if (urlConnection instanceof JarURLConnection) {
                                            JarURLConnection jarUrlConnection = (JarURLConnection) urlConnection;
                                            return jarUrlConnection.getManifest();
                                        } else {
                                            jis = new JarInputStream(urlConnection.getInputStream());
                                            return jis.getManifest();
                                        }
                                    } catch (IOException e) {
                                        LOGGER.error(MessageFormat.format("Error reading META-INF/MANIFEST.MF file: {0}", e.getMessage()), e);
                                    } finally {
                                        if (jis != null) {
                                            try {
                                                jis.close();
                                            } catch (IOException e) {
                                                LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
                                            }
                                        }
                                    }
                                }
                            }
                        }
            
                        return null;
                    }