View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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  import org.apache.commons.io.IOUtils;
38  import org.apache.commons.lang.StringUtils;
39  import org.jdom.Document;
40  import org.jdom.Element;
41  import org.jdom.input.SAXBuilder;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import java.io.InputStream;
46  import java.util.HashMap;
47  import java.util.Iterator;
48  import java.util.List;
49  import java.util.Map;
50  import javax.inject.Singleton;
51  
52  
53  /**
54   * Extracts license information from the info/magnolia/cms/license/license.xml file.
55   *
56   * @version $Id$
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         }
122         catch (Exception e) {
123             log.error("failed to load license information");
124             log.error(e.getMessage(), e);
125         }
126         finally {
127             IOUtils.closeQuietly(in);
128         }
129     }
130 
131     /**
132      * Returns the current magnolia version, read from the jar Manifest. This will return null if classes are not inside
133      * a jar.
134      * @return implementation version, read from the manifest file.
135      */
136     public String getVersionFromManifest() {
137         Package p = this.getClass().getPackage();
138         if (p != null) {
139             return StringUtils.defaultString(p.getImplementationVersion());
140         }
141         return StringUtils.EMPTY;
142     }
143 
144     /**
145      * Load meta element.
146      */
147     private void load(Document document) {
148         Element metaElement = document.getRootElement().getChild(ELEMENT_META);
149 
150         @SuppressWarnings("unchecked")
151         List<Element> elements = metaElement.getChildren();
152 
153         values = new HashMap<String,String>(10);
154         Iterator<Element> iterator = elements.iterator();
155         while (iterator.hasNext()) {
156             Element element = iterator.next();
157             values.put(element.getName(), element.getText());
158         }
159 
160         String osName = System.getProperty("os.name");
161         values.put(OS_NAME, osName.replaceAll(" ", "-"));
162 
163         values.put(IMPLEMENTATION_VERSION, getVersionFromManifest());
164 
165     }
166 
167     /**
168      * Print version info to System.out.
169      */
170     public void printVersionInfo() {
171         System.out.println("---------------------------------------------");
172         System.out.println("MAGNOLIA LICENSE");
173         System.out.println("---------------------------------------------");
174         System.out.println("Version number : " + get(LicenseFileExtractor.VERSION_NUMBER));
175         System.out.println("Build          : " + get(LicenseFileExtractor.BUILD_NUMBER));
176         System.out.println("Edition        : " + get(LicenseFileExtractor.EDITION));
177         System.out.println("Provider       : " + get(LicenseFileExtractor.PROVIDER) + " (" + get(LicenseFileExtractor.PROVIDER_EMAIL) + ")");
178     }
179 }