1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.util;
18
19 import net.sf.ehcache.CacheException;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Properties;
24
25 /***
26 * Build properties of the product
27 *
28 * @author hhuynh
29 *
30 */
31 public class ProductInfo {
32 private static final String VERSION_RESOURCE = "/ehcache-version.properties";
33 private static final String UNKNOWN = "UNKNOWN";
34 private Properties props = new Properties();
35
36 /***
37 * Construct a default product info
38 */
39 public ProductInfo() {
40 this(VERSION_RESOURCE);
41 }
42
43 /***
44 * Construct product info object from a resource name
45 *
46 * @param resource
47 */
48 public ProductInfo(String resource) {
49 parseProductInfo(resource);
50 }
51
52 /***
53 * Construct product info object from a resource input stream
54 *
55 * @param resource
56 * @throws java.io.IOException
57 */
58 public ProductInfo(InputStream resource) {
59 try {
60 props.load(resource);
61 } catch (IOException e) {
62 throw new RuntimeException(e);
63 }
64 }
65
66 private void parseProductInfo(String resource) {
67 InputStream in = ProductInfo.class.getResourceAsStream(resource);
68 if (in == null) {
69 throw new RuntimeException("Can't find resource: " + resource);
70 }
71
72 try {
73 props.load(in);
74 } catch (IOException e) {
75 throw new RuntimeException(e);
76 } finally {
77 try {
78 in.close();
79 } catch (IOException e2) {
80
81 }
82 }
83 }
84
85 /***
86 *
87 * @return product name
88 */
89 public String getName() {
90 return props.getProperty("product-name", UNKNOWN);
91 }
92
93 /***
94 *
95 * @return version
96 */
97 public String getVersion() {
98 return props.getProperty("version", UNKNOWN);
99 }
100
101 /***
102 *
103 * @return the person who built
104 */
105 public String getBuiltBy() {
106 return props.getProperty("built-by", UNKNOWN);
107 }
108
109 /***
110 *
111 * @return jdk that was used
112 */
113 public String getBuildJdk() {
114 return props.getProperty("build-jdk", UNKNOWN);
115 }
116
117 /***
118 *
119 * @return build timestamp
120 */
121 public String getBuildTime() {
122 return props.getProperty("build-time", UNKNOWN);
123 }
124
125 /***
126 *
127 * @return revision
128 */
129 public String getBuildRevision() {
130 return props.getProperty("build-revision", UNKNOWN);
131 }
132
133 /***
134 *
135 * @return patch number
136 */
137 public String getPatchLevel() {
138 return props.getProperty("patch-level", UNKNOWN);
139 }
140
141 /***
142 *
143 * @return required core version
144 */
145 public String getRequiredCoreVersion() {
146 return props.getProperty("required-core-version");
147 }
148
149 /***
150 *
151 * @return true if the current product is an enterprise one
152 */
153 public boolean isEnterprise() {
154 return Boolean.parseBoolean(props.getProperty("enterprise"));
155 }
156
157 /***
158 * Assert that the current product is compatible with the version of ehcache available on the classpath
159 */
160 public void assertRequiredCoreVersionPresent() {
161 boolean ignoreVersionCheck = Boolean.getBoolean("terracotta.ehcache.versioncheck.skip");
162 String requiredCoreVersion = getRequiredCoreVersion();
163 if (ignoreVersionCheck || requiredCoreVersion == null) {
164
165 return;
166 }
167
168 ProductInfo coreProductInfo = new ProductInfo();
169
170 String coreVersion = coreProductInfo.getVersion();
171 if (!coreVersion.equals(requiredCoreVersion)) {
172 String msg = getName() + " version [" + getVersion() + "] only works with ehcache-core version [" +
173 requiredCoreVersion + "] (found version [" + coreVersion + "] on the classpath). " +
174 " Please make sure both versions are compatible!";
175 throw new CacheException(msg);
176 }
177 }
178
179
180 /***
181 * returns long version of the build string
182 */
183 @Override
184 public String toString() {
185 String versionString = String
186 .format(
187 "%s version %s was built on %s, at revision %s, with jdk %s by %s",
188 getName(), getVersion(), getBuildTime(),
189 getBuildRevision(), getBuildJdk(), getBuiltBy());
190 if (!UNKNOWN.equals(getPatchLevel())) {
191 versionString = versionString + ". Patch level " + getPatchLevel();
192 }
193 return versionString;
194 }
195 }