View Javadoc

1   package com.atlassian.plugin.util;
2   
3   import com.atlassian.util.concurrent.LazyReference;
4   import org.apache.commons.io.IOUtils;
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.util.Properties;
11  
12  /**
13   * General utility functions for plugin framework.
14   *
15   * @since 2.7.0
16   */
17  public final class PluginFrameworkUtils
18  {
19      private static final Logger LOG = LoggerFactory.getLogger(PluginFrameworkUtils.class);
20      private static final String BUILD_PROPERTY_PATH = "META-INF/maven/com.atlassian.plugins/atlassian-plugins-core/pom.properties";
21  
22      /**
23       * Not for instantiation.
24       */
25      private PluginFrameworkUtils()
26      {}
27  
28      /**
29       * Get the current plugin framework version.
30       * This is not necessarily in OSGi format as it is generated by maven during build process.
31       *
32       * @return current plugin framework version.
33       */
34      public static String getPluginFrameworkVersion()
35      {
36          return pluginFrameworkVersionRef.get();
37      }
38  
39      private static final LazyReference<String> pluginFrameworkVersionRef = new LazyReference<String>()
40      {
41          @Override
42          protected String create() throws Exception
43          {
44              return getPluginFrameworkVersionInternal();
45          }
46      };
47  
48      private static String getPluginFrameworkVersionInternal()
49      {
50          Properties props = new Properties();
51          InputStream in = null;
52  
53          try
54          {
55              in = ClassLoaderUtils.getResourceAsStream(BUILD_PROPERTY_PATH, PluginFrameworkUtils.class);
56              if (in != null)
57              {
58                  // this should automatically get rid of comment lines.
59                  props.load(in);
60                  return props.getProperty("version");
61              }
62              else
63              {
64                  // probably ran via IDEA where pom.properties won't exist
65                  return "2.7.0";
66              }
67  
68          }
69          catch (IOException e)
70          {
71              LOG.error("cannot determine the plugin framework version", e);
72              throw new IllegalStateException("cannot determine the plugin framework version", e);
73          }
74          finally
75          {
76              IOUtils.closeQuietly(in);
77          }
78      }
79  }