View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.license;
35  
36  import info.magnolia.objectfactory.Components;
37  
38  import java.io.InputStream;
39  import java.util.HashMap;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Map;
43  
44  import javax.inject.Singleton;
45  
46  import org.apache.commons.io.IOUtils;
47  import org.apache.commons.lang3.StringUtils;
48  import org.jdom.Document;
49  import org.jdom.Element;
50  import org.jdom.input.SAXBuilder;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  
55  /**
56   * Extracts license information from the info/magnolia/cms/license/license.xml file.
57   */
58  @Singleton
59  public class LicenseFileExtractor {
60      private static final Logger log = LoggerFactory.getLogger(LicenseFileExtractor.class);
61  
62      public static final String VERSION_NUMBER = "VersionNumber";
63  
64      /**
65       * Same as VERSION_NUMBER, but read from the manifest.
66       */
67      public static final String IMPLEMENTATION_VERSION = "ImplementationVersion";
68  
69      public static final String BUILD_NUMBER = "BuildNumber";
70  
71      public static final String PROVIDER = "Provider";
72  
73      public static final String PROVIDER_ADDRESS = "ProviderAddress";
74  
75      public static final String PROVIDER_EMAIL = "ProviderEmail";
76  
77      public static final String EDITION = "Edition";
78  
79      public static final String PRODUCT_DOMAIN = "ProductDomain";
80  
81      private static final String LICENSE_FILE_PATH = "info/magnolia/cms/license/license.xml";
82  
83      private static final String ELEMENT_META = "Meta";
84  
85      private static final String NOT_DEFINED = "Not Defined";
86  
87      private static final String OS_NAME = "OSName";
88  
89      private Map<String, String> values = new HashMap<String, String>();
90  
91      public static LicenseFileExtractor getInstance() {
92          return Components.getComponent(LicenseFileExtractor.class);
93      }
94  
95      public String get(String id) {
96          if (values.containsKey(id)) {
97              return values.get(id);
98          }
99          return NOT_DEFINED;
100     }
101 
102     public Map<String, String> getEntries() {
103         return values;
104     }
105 
106     public String getOSName() {
107         String osName = System.getProperty("os.name");
108         return osName.replaceAll(" ", "-");
109     }
110 
111     public void init() {
112         InputStream in = getClass().getClassLoader().getResourceAsStream(LICENSE_FILE_PATH);
113         this.init(in);
114     }
115 
116     public void init(InputStream in) {
117         try {
118             SAXBuilder builder = new SAXBuilder();
119             Document document = builder.build(in);
120             this.load(document);
121         } catch (Exception e) {
122             log.error("failed to load license information");
123             log.error(e.getMessage(), e);
124         } finally {
125             IOUtils.closeQuietly(in);
126         }
127     }
128 
129     /**
130      * Returns the current magnolia version, read from the jar Manifest. This will return null if classes are not inside
131      * a jar.
132      *
133      * @return implementation version, read from the manifest file.
134      */
135     public String getVersionFromManifest() {
136         Package p = this.getClass().getPackage();
137         if (p != null) {
138             return StringUtils.defaultString(p.getImplementationVersion());
139         }
140         return StringUtils.EMPTY;
141     }
142 
143     /**
144      * Load meta element.
145      */
146     private void load(Document document) {
147         Element metaElement = document.getRootElement().getChild(ELEMENT_META);
148 
149         @SuppressWarnings("unchecked")
150         List<Element> elements = metaElement.getChildren();
151 
152         values = new HashMap<String, String>(10);
153         Iterator<Element> iterator = elements.iterator();
154         while (iterator.hasNext()) {
155             Element element = iterator.next();
156             values.put(element.getName(), element.getText());
157         }
158 
159         String osName = System.getProperty("os.name");
160         values.put(OS_NAME, osName.replaceAll(" ", "-"));
161 
162         values.put(IMPLEMENTATION_VERSION, getVersionFromManifest());
163 
164     }
165 
166     /**
167      * Print version info to System.out.
168      */
169     public void printVersionInfo() {
170         System.out.println("---------------------------------------------");
171         System.out.println("MAGNOLIA LICENSE");
172         System.out.println("---------------------------------------------");
173         System.out.println("Version number : " + get(LicenseFileExtractor.VERSION_NUMBER));
174         System.out.println("Build          : " + get(LicenseFileExtractor.BUILD_NUMBER));
175         System.out.println("Edition        : " + get(LicenseFileExtractor.EDITION));
176         System.out.println("Provider       : " + get(LicenseFileExtractor.PROVIDER) + " (" + get(LicenseFileExtractor.PROVIDER_EMAIL) + ")");
177     }
178 }