View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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  
51  
52  /**
53   * User: sameercharles Date: Nov 5, 2003 Time: 4:18:59 PM
54   * @author Sameer Charles
55   * @version 1.1
56   */
57  public class LicenseFileExtractor {
58      private static final Logger log = LoggerFactory.getLogger(LicenseFileExtractor.class);
59  
60      public static final String VERSION_NUMBER = "VersionNumber"; //$NON-NLS-1$
61  
62      /**
63       * Same as VERSION_NUMBER, but read from the manifest.
64       */
65      public static final String IMPLEMENTATION_VERSION = "ImplementationVersion"; //$NON-NLS-1$
66  
67      public static final String BUILD_NUMBER = "BuildNumber"; //$NON-NLS-1$
68  
69      public static final String PROVIDER = "Provider"; //$NON-NLS-1$
70  
71      public static final String PROVIDER_ADDRESS = "ProviderAddress"; //$NON-NLS-1$
72  
73      public static final String PROVIDER_EMAIL = "ProviderEmail"; //$NON-NLS-1$
74  
75      public static final String EDITION = "Edition"; //$NON-NLS-1$
76  
77      public static final String PRODUCT_DOMAIN = "ProductDomain"; //$NON-NLS-1$
78  
79      private static final String LICENSE_FILE_PATH = "info/magnolia/cms/license/license.xml"; //$NON-NLS-1$
80  
81      private static final String ELEMENT_META = "Meta"; //$NON-NLS-1$
82  
83      private static final String NOT_DEFINED = "Not Defined"; //$NON-NLS-1$
84  
85      private static final String OS_NAME = "OSName"; //$NON-NLS-1$
86  
87      private Map values;
88  
89      public static LicenseFileExtractor getInstance() {
90          return Components.getSingleton(LicenseFileExtractor.class);
91      }
92  
93      public String get(String id) {
94          if (values.containsKey(id)) {
95              return (String) values.get(id);
96          }
97          return NOT_DEFINED;
98      }
99  
100     public Map getEntries() {
101         return values;
102     }
103 
104     public String getOSName() {
105         String osName = System.getProperty("os.name"); //$NON-NLS-1$
106         return osName.replaceAll(" ", "-"); //$NON-NLS-1$ //$NON-NLS-2$
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"); //$NON-NLS-1$
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         List elements = metaElement.getChildren();
149 
150         values = new HashMap(10);
151         Iterator iterator = elements.iterator();
152         while (iterator.hasNext()) {
153             Element element = (Element) iterator.next();
154             values.put(element.getName(), element.getText());
155         }
156 
157         String osName = System.getProperty("os.name"); //$NON-NLS-1$
158         values.put(OS_NAME, osName.replaceAll(" ", "-")); //$NON-NLS-1$ //$NON-NLS-2$
159 
160         values.put(IMPLEMENTATION_VERSION, getVersionFromManifest());
161 
162     }
163 
164     /**
165      * Print version info to System.out
166      */
167     public void printVersionInfo() {
168         System.out.println("---------------------------------------------"); //$NON-NLS-1$
169         System.out.println("MAGNOLIA LICENSE"); //$NON-NLS-1$
170         System.out.println("---------------------------------------------"); //$NON-NLS-1$
171         System.out.println("Version number : " + get(LicenseFileExtractor.VERSION_NUMBER)); //$NON-NLS-1$
172         System.out.println("Build          : " + get(LicenseFileExtractor.BUILD_NUMBER)); //$NON-NLS-1$
173         System.out.println("Edition        : " + get(LicenseFileExtractor.EDITION)); //$NON-NLS-1$
174         System.out.println("Provider       : " + get(LicenseFileExtractor.PROVIDER) + " (" + get(LicenseFileExtractor.PROVIDER_EMAIL) + ")"); //$NON-NLS-1$ $NON-NLS-3$ $NON-NLS-5$
175     }
176 }