View Javadoc

1   /*
2    * $Id: MuleManifest.java 20088 2010-11-05 16:51:41Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.config;
11  
12  import org.mule.api.config.MuleConfiguration;
13  
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.net.URL;
17  import java.security.AccessController;
18  import java.security.PrivilegedAction;
19  import java.util.Enumeration;
20  import java.util.jar.Attributes;
21  import java.util.jar.Manifest;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * This is a static class that provides access to the Mule core manifest file. 
28   */
29  // TODO EE-572
30  public class MuleManifest
31  {
32      /**
33       * logger used by this class
34       */
35      protected static final Log logger = LogFactory.getLog(MuleManifest.class);
36  
37      private static Manifest manifest;
38  
39      public static String getProductVersion()
40      {
41          final String version = getManifestProperty("Implementation-Version");
42          return version == null ? "Unknown" : version;
43      }
44  
45      public static String getVendorName()
46      {
47          return getManifestProperty("Specification-Vendor");
48      }
49  
50      public static String getVendorUrl()
51      {
52          return getManifestProperty("Vendor-Url");
53      }
54  
55      public static String getProductUrl()
56      {
57          return getManifestProperty("Product-Url");
58      }
59  
60      public static String getProductName()
61      {
62          return getManifestProperty("Implementation-Title");
63      }
64  
65      public static String getProductMoreInfo()
66      {
67          return getManifestProperty("More-Info");
68      }
69  
70      public static String getProductSupport()
71      {
72          return getManifestProperty("Support");
73      }
74  
75      public static String getProductLicenseInfo()
76      {
77          return getManifestProperty("License");
78      }
79  
80      public static String getProductDescription()
81      {
82          return getManifestProperty("Description");
83      }
84  
85      public static String getBuildNumber()
86      {
87          return getManifestProperty("Build-Revision");
88      }
89      
90      public static String getBuildDate()
91      {
92          return getManifestProperty("Build-Date");
93      }
94  
95      public static String getDevListEmail()
96      {
97          return getManifestProperty("Dev-List-Email");
98      }
99  
100     public static Manifest getManifest()
101     {
102         if (manifest == null)
103         {
104             manifest = new Manifest();
105 
106             InputStream is = null;
107             try
108             {
109                 // We want to load the MANIFEST.MF from the mule-core jar. Sine we
110                 // don't know the version we're using we have to search for the jar on the classpath
111                 URL url = AccessController.doPrivileged(new PrivilegedAction<URL>()
112                 {
113                     public URL run()
114                     {
115                         try
116                         {
117                             Enumeration e = MuleConfiguration.class.getClassLoader().getResources(
118                                     ("META-INF/MANIFEST.MF"));
119                             while (e.hasMoreElements())
120                             {
121                                 URL url = (URL) e.nextElement();
122                                 if ((url.toExternalForm().indexOf("mule-core") > -1 && url.toExternalForm()
123                                     .indexOf("tests.jar") < 0)
124                                     || url.toExternalForm().matches(".*mule.*-.*-embedded.*\\.jar.*"))
125                                 {
126                                     return url;
127                                 }
128                             }
129                         }
130                         catch (IOException e1)
131                         {
132                             logger.warn("Failure reading manifest: " + e1.getMessage(), e1);
133                         }
134                         return null;
135                     }
136                 });
137 
138                 if (url != null)
139                 {
140                     is = url.openStream();
141                 }
142 
143                 if (is != null)
144                 {
145                     manifest.read(is);
146                 }
147             }
148             catch (IOException e)
149             {
150                 logger.warn("Failed to read manifest Info, Manifest information will not display correctly: "
151                         + e.getMessage());
152             }
153         }
154         return manifest;
155     }
156 
157     protected static String getManifestProperty(String name)
158     {
159         return getManifest().getMainAttributes().getValue(new Attributes.Name(name));
160     }
161 }