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.lang3.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  @Singleton
57  public class LicenseFileExtractor {
58      private static final Logger log = LoggerFactory.getLogger(LicenseFileExtractor.class);
59  
60      public static final String VERSION_NUMBER = "VersionNumber";
61  
62      /**
63       * Same as VERSION_NUMBER, but read from the manifest.
64       */
65      public static final String IMPLEMENTATION_VERSION = "ImplementationVersion";
66  
67      public static final String BUILD_NUMBER = "BuildNumber";
68  
69      public static final String PROVIDER = "Provider";
70  
71      public static final String PROVIDER_ADDRESS = "ProviderAddress";
72  
73      public static final String PROVIDER_EMAIL = "ProviderEmail";
74  
75      public static final String EDITION = "Edition";
76  
77      public static final String PRODUCT_DOMAIN = "ProductDomain";
78  
79      private static final String LICENSE_FILE_PATH = "info/magnolia/cms/license/license.xml";
80  
81      private static final String ELEMENT_META = "Meta";
82  
83      private static final String NOT_DEFINED = "Not Defined";
84  
85      private static final String OS_NAME = "OSName";
86  
87      private Map<String, String> values = new HashMap<String, String> ();
88  
89      public static LicenseFileExtractor getInstance() {
90          return Components.getComponent(LicenseFileExtractor.class);
91      }
92  
93      public String get(String id) {
94          if (values.containsKey(id)) {
95              return values.get(id);
96          }
97          return NOT_DEFINED;
98      }
99  
100     public Map<String, String> getEntries() {
101         return values;
102     }
103 
104     public String getOSName() {
105         String osName = System.getProperty("os.name");
106         return osName.replaceAll(" ", "-");
107     }
108 
109     public void init() {
110         InputStream in = getClass().getClassLoader().getResourceAsStream(LICENSE_FILE_PATH);
111         this.init(in);
112     }
113 
114     public void init(InputStream in) {
115         try {
116             SAXBuilder builder = new SAXBuilder();
117             Document document = builder.build(in);
118             this.load(document);
119         }
120         catch (Exception e) {
121             log.error("failed to load license information");
122             log.error(e.getMessage(), e);
123         }
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      * @return implementation version, read from the manifest file.
133      */
134     public String getVersionFromManifest() {
135         Package p = this.getClass().getPackage();
136         if (p != null) {
137             return StringUtils.defaultString(p.getImplementationVersion());
138         }
139         return StringUtils.EMPTY;
140     }
141 
142     /**
143      * Load meta element.
144      */
145     private void load(Document document) {
146         Element metaElement = document.getRootElement().getChild(ELEMENT_META);
147 
148         @SuppressWarnings("unchecked")
149         List<Element> elements = metaElement.getChildren();
150 
151         values = new HashMap<String,String>(10);
152         Iterator<Element> iterator = elements.iterator();
153         while (iterator.hasNext()) {
154             Element element = iterator.next();
155             values.put(element.getName(), element.getText());
156         }
157 
158         String osName = System.getProperty("os.name");
159         values.put(OS_NAME, osName.replaceAll(" ", "-"));
160 
161         values.put(IMPLEMENTATION_VERSION, getVersionFromManifest());
162 
163     }
164 
165     /**
166      * Print version info to System.out.
167      */
168     public void printVersionInfo() {
169         System.out.println("---------------------------------------------");
170         System.out.println("MAGNOLIA LICENSE");
171         System.out.println("---------------------------------------------");
172         System.out.println("Version number : " + get(LicenseFileExtractor.VERSION_NUMBER));
173         System.out.println("Build          : " + get(LicenseFileExtractor.BUILD_NUMBER));
174         System.out.println("Edition        : " + get(LicenseFileExtractor.EDITION));
175         System.out.println("Provider       : " + get(LicenseFileExtractor.PROVIDER) + " (" + get(LicenseFileExtractor.PROVIDER_EMAIL) + ")");
176     }
177 }