View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 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.pddescriptor;
35  
36  import info.magnolia.module.ModuleRegistry;
37  import info.magnolia.module.model.ModuleDefinition;
38  import info.magnolia.objectfactory.Components;
39  
40  import java.io.InputStream;
41  import java.util.HashMap;
42  import java.util.Iterator;
43  import java.util.List;
44  import java.util.Map;
45  
46  import javax.inject.Singleton;
47  
48  import org.apache.commons.io.IOUtils;
49  import org.apache.commons.lang3.StringUtils;
50  import org.jdom.Document;
51  import org.jdom.Element;
52  import org.jdom.input.SAXBuilder;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  /**
57   * Extracts product information from the info/magnolia/cms/pddescriptor/pddescriptor.xml file.
58   */
59  @Singleton
60  public class ProductDescriptorExtractor {
61  
62      private static final Logger log = LoggerFactory.getLogger(ProductDescriptorExtractor.class);
63  
64      public static final String VERSION_NUMBER = "VersionNumber";
65  
66      /**
67       * Same as VERSION_NUMBER, but read from the manifest.
68       */
69      public static final String IMPLEMENTATION_VERSION = "ImplementationVersion";
70  
71      /**
72       * Core version number read from core module definition.
73       */
74      public static final String CORE_VERSION = "CoreVersion";
75  
76      public static final String BUILD_NUMBER = "BuildNumber";
77  
78      public static final String PROVIDER = "Provider";
79  
80      public static final String PROVIDER_ADDRESS = "ProviderAddress";
81  
82      public static final String PROVIDER_EMAIL = "ProviderEmail";
83  
84      public static final String EDITION = "Edition";
85  
86      public static final String PRODUCT_DOMAIN = "ProductDomain";
87  
88      private static final String LICENSE_FILE_PATH = "info/magnolia/cms/pddescriptor/pddescriptor.xml";
89  
90      private static final String ELEMENT_META = "Meta";
91  
92      private static final String NOT_DEFINED = "Not Defined";
93  
94      private static final String OS_NAME = "OSName";
95  
96      private Map<String, String> values = new HashMap<String, String>();
97  
98      public String get(String id) {
99          if (values.containsKey(id)) {
100             return values.get(id);
101         }
102         return NOT_DEFINED;
103     }
104 
105     public Map<String, String> getEntries() {
106         return values;
107     }
108 
109     public String getOSName() {
110         String osName = System.getProperty("os.name");
111         return osName.replaceAll(" ", "-");
112     }
113 
114     public void init() {
115         InputStream in = getClass().getClassLoader().getResourceAsStream(LICENSE_FILE_PATH);
116         this.init(in);
117     }
118 
119     public void init(InputStream in) {
120         try {
121             SAXBuilder builder = new SAXBuilder();
122             Document document = builder.build(in);
123             this.load(document);
124         } catch (Exception e) {
125             log.error("failed to load license information");
126             log.error(e.getMessage(), e);
127         } finally {
128             IOUtils.closeQuietly(in);
129         }
130     }
131 
132     /**
133      * Returns the current magnolia version, read from the jar Manifest. This will return null if classes are not inside
134      * a jar.
135      *
136      * @return implementation version, read from the manifest file.
137      */
138     public String getVersionFromManifest() {
139         Package p = this.getClass().getPackage();
140         if (p != null) {
141             return StringUtils.defaultString(p.getImplementationVersion());
142         }
143         return StringUtils.EMPTY;
144     }
145 
146     /**
147      * @return  core version, read from core module definition
148      */
149     public String getCoreVersion() {
150         try {
151             ModuleDefinition coreModuleDefinition = Components.getComponent(ModuleRegistry.class).getDefinition("core");
152             return coreModuleDefinition.getVersion().toString();
153         } catch (IllegalArgumentException e) {
154             return StringUtils.EMPTY;
155         }
156     }
157 
158     /**
159      * Load meta element.
160      */
161     private void load(Document document) {
162         Element metaElement = document.getRootElement().getChild(ELEMENT_META);
163 
164         @SuppressWarnings("unchecked")
165         List<Element> elements = metaElement.getChildren();
166 
167         values = new HashMap<String, String>(10);
168         Iterator<Element> iterator = elements.iterator();
169         while (iterator.hasNext()) {
170             Element element = iterator.next();
171             values.put(element.getName(), element.getText());
172         }
173 
174         String osName = System.getProperty("os.name");
175         values.put(OS_NAME, osName.replaceAll(" ", "-"));
176 
177         values.put(IMPLEMENTATION_VERSION, getVersionFromManifest());
178         values.put(CORE_VERSION, getCoreVersion());
179     }
180 
181     /**
182      * Print version info to System.out.
183      */
184     public void printVersionInfo() {
185         System.out.println("---------------------------------------------");
186         System.out.println("MAGNOLIA LICENSE");
187         System.out.println("---------------------------------------------");
188         System.out.println("Version number : " + get(ProductDescriptorExtractor.VERSION_NUMBER));
189         System.out.println("Core version   : " + get(ProductDescriptorExtractor.CORE_VERSION));
190         System.out.println("Build          : " + get(ProductDescriptorExtractor.BUILD_NUMBER));
191         System.out.println("Edition        : " + get(ProductDescriptorExtractor.EDITION));
192         System.out.println("Provider       : " + get(ProductDescriptorExtractor.PROVIDER) + " (" + get(ProductDescriptorExtractor.PROVIDER_EMAIL) + ")");
193     }
194 }