View Javadoc
1   package com.atlassian.plugin.util;
2   
3   import io.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      private static final Logger LOG = LoggerFactory.getLogger(PluginFrameworkUtils.class);
19      private static final String BUILD_PROPERTY_PATH = "META-INF/maven/com.atlassian.plugins/atlassian-plugins-core/pom.properties";
20  
21      /**
22       * Not for instantiation.
23       */
24      private PluginFrameworkUtils() {
25      }
26  
27      /**
28       * Get the current plugin framework version.
29       * This is not necessarily in OSGi format as it is generated by maven during build process.
30       *
31       * @return current plugin framework version.
32       */
33      public static String getPluginFrameworkVersion() {
34          return pluginFrameworkVersionRef.get();
35      }
36  
37      private static final LazyReference<String> pluginFrameworkVersionRef = new LazyReference<String>() {
38          @Override
39          protected String create() {
40              return getPluginFrameworkVersionInternal();
41          }
42      };
43  
44      private static String getPluginFrameworkVersionInternal() {
45          Properties props = new Properties();
46          InputStream in = null;
47  
48          try {
49              in = ClassLoaderUtils.getResourceAsStream(BUILD_PROPERTY_PATH, PluginFrameworkUtils.class);
50              if (in != null) {
51                  // this should automatically get rid of comment lines.
52                  props.load(in);
53                  return props.getProperty("version");
54              } else {
55                  // probably ran via IDEA where pom.properties won't exist
56                  return "2.7.0";
57              }
58  
59          } catch (IOException e) {
60              LOG.error("cannot determine the plugin framework version", e);
61              throw new IllegalStateException("cannot determine the plugin framework version", e);
62          } finally {
63              IOUtils.closeQuietly(in);
64          }
65      }
66  }