KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > license > LicenseFileExtractor


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.license;
14
15 import info.magnolia.cms.util.FactoryUtil;
16
17 import java.io.InputStream JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.jdom.Document;
26 import org.jdom.Element;
27 import org.jdom.input.SAXBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32 /**
33  * User: sameercharles Date: Nov 5, 2003 Time: 4:18:59 PM
34  * @author Sameer Charles
35  * @version 1.1
36  */

37 public class LicenseFileExtractor {
38
39     public static final String JavaDoc VERSION_NUMBER = "VersionNumber"; //$NON-NLS-1$
40

41     /**
42      * Same as VERSION_NUMBER, but read from the manifest.
43      */

44     public static final String JavaDoc IMPLEMENTATION_VERSION = "ImplementationVersion"; //$NON-NLS-1$
45

46     public static final String JavaDoc BUILD_NUMBER = "BuildNumber"; //$NON-NLS-1$
47

48     public static final String JavaDoc PROVIDER = "Provider"; //$NON-NLS-1$
49

50     public static final String JavaDoc PROVIDER_ADDRESS = "ProviderAddress"; //$NON-NLS-1$
51

52     public static final String JavaDoc PRIVIDER_EMAIL = "ProviderEmail"; //$NON-NLS-1$
53

54     public static final String JavaDoc EDITION = "Edition"; //$NON-NLS-1$
55

56     public static final String JavaDoc PRODUCT_DOMAIN = "ProductDomain"; //$NON-NLS-1$
57

58     public static final String JavaDoc VERSION_PAGE_HANDLE = "VersionPageHandle"; //$NON-NLS-1$
59

60     private static final String JavaDoc LICENSE_FILE_PATH = "info/magnolia/cms/license/license.xml"; //$NON-NLS-1$
61

62     private static final String JavaDoc ELEMENT_META = "Meta"; //$NON-NLS-1$
63

64     private static final String JavaDoc NOT_DEFINED = "Not Defined"; //$NON-NLS-1$
65

66     private static final String JavaDoc OS_NAME = "OSName"; //$NON-NLS-1$
67

68     /**
69      * Logger.
70      */

71     private static Logger log = LoggerFactory.getLogger(LicenseFileExtractor.class);
72
73     private Map JavaDoc values;
74
75     public static LicenseFileExtractor getInstance() {
76         return (LicenseFileExtractor) FactoryUtil.getSingleton(LicenseFileExtractor.class);
77     }
78
79     public String JavaDoc get(String JavaDoc id) {
80         if (values.containsKey(id)) {
81             return (String JavaDoc) values.get(id);
82         }
83         return NOT_DEFINED;
84     }
85
86     public Map JavaDoc getEntries() {
87         return values;
88     }
89
90     public String JavaDoc getOSName() {
91         String JavaDoc osName = System.getProperty("os.name"); //$NON-NLS-1$
92
return osName.replaceAll(" ", "-"); //$NON-NLS-1$ //$NON-NLS-2$
93
}
94
95     public void init() {
96         InputStream JavaDoc in = getClass().getClassLoader().getResourceAsStream(LICENSE_FILE_PATH);
97         this.init(in);
98     }
99
100     public void init(InputStream JavaDoc in) {
101         try {
102             SAXBuilder builder = new SAXBuilder();
103             Document document = builder.build(in);
104             this.load(document);
105         }
106         catch (Exception JavaDoc e) {
107             log.error("failed to load license information"); //$NON-NLS-1$
108
log.error(e.getMessage(), e);
109         }
110         finally {
111             IOUtils.closeQuietly(in);
112         }
113     }
114
115     /**
116      * Returns the current magnolia version, read from the jar Manifest. This will return null if classes are not inside
117      * a jar.
118      * @return implementation version, read from the manifest file.
119      */

120     public String JavaDoc getVersionFromManifest() {
121         Package JavaDoc p = this.getClass().getPackage();
122         if (p != null) {
123             return StringUtils.defaultString(p.getImplementationVersion());
124         }
125         return StringUtils.EMPTY;
126     }
127
128     /**
129      * load meta element
130      */

131     private void load(Document document) {
132         Element metaElement = document.getRootElement().getChild(ELEMENT_META);
133
134         List JavaDoc elements = metaElement.getChildren();
135
136         values = new HashMap JavaDoc(10);
137         Iterator JavaDoc iterator = elements.iterator();
138         while (iterator.hasNext()) {
139             Element element = (Element) iterator.next();
140             values.put(element.getName(), element.getText());
141         }
142
143         String JavaDoc osName = System.getProperty("os.name"); //$NON-NLS-1$
144
values.put(OS_NAME, osName.replaceAll(" ", "-")); //$NON-NLS-1$ //$NON-NLS-2$
145

146         values.put(IMPLEMENTATION_VERSION, getVersionFromManifest());
147
148     }
149 }
150
Popular Tags