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