This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
/
VersionBean.java
231 lines (203 loc) · 8.5 KB
/
VersionBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* License Agreement.
*
* Rich Faces - Natural Ajax for Java Server Faces (JSF)
*
* Copyright (C) 2007 Exadel, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.richfaces;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.text.MessageFormat;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import org.ajax4jsf.resource.util.URLToStreamHelper;
import org.richfaces.log.Logger;
import org.richfaces.log.RichfacesLogger;
/**
* Vendor and version information for A4J project
*
* @author asmirnov@exadel.com (latest modification by $Author$)
* @version $Revision$ $Date$
*/
public final class VersionBean {
public static final Version VERSION = new Version();
/**
* Class for incapsulate version info.
*
* @author asmirnov@exadel.com (latest modification by $Author$)
* @version $Revision$ $Date$
*/
public static class Version {
private static final Logger LOGGER = RichfacesLogger.APPLICATION.getLogger();
private static final String UNKNOWN = "";
private String implementationVendor = UNKNOWN;
// TODO nick - default value for manifest file absense - review
private String implementationVersion = "4.3.0.20120730-M1";
private String implementationTitle = UNKNOWN;
private String scmTimestamp = UNKNOWN;
private String fullVersionString = UNKNOWN;
private boolean containsDataFromManifest = false;
public Version() {
initialize();
}
private String getAttributeValueOrDefault(Attributes attributes, String name) {
String value = attributes.getValue(name);
if (value == null) {
value = UNKNOWN;
}
return value;
}
private void initialize() {
Manifest manifest = null;
try {
manifest = readManifest();
} catch (Exception e) {
LOGGER.error(MessageFormat.format("Error reading project metadata: {0}", e.getMessage()), e);
}
if (manifest != null) {
initializePropertiesFromManifest(manifest);
initializeDerivativeProperties();
}
}
private void initializePropertiesFromManifest(Manifest manifest) {
containsDataFromManifest = true;
Attributes attributes = manifest.getMainAttributes();
implementationVendor = getAttributeValueOrDefault(attributes, "Implementation-Vendor");
implementationVersion = getAttributeValueOrDefault(attributes, "Implementation-Version");
implementationTitle = getAttributeValueOrDefault(attributes, "Implementation-Title");
scmTimestamp = getAttributeValueOrDefault(attributes, "SCM-Timestamp");
}
private void initializeDerivativeProperties() {
fullVersionString = MessageFormat.format("{0}", implementationVersion);
}
private Manifest readManifest() {
ProtectionDomain domain = VersionBean.class.getProtectionDomain();
if (domain != null) {
CodeSource codeSource = domain.getCodeSource();
if (codeSource != null) {
URL url = codeSource.getLocation();
if (url != null) {
InputStream manifestStream = null;
try {
URL manifestFileUrl;
if ("vfs".equals(url.getProtocol())) {
String manifestFile = String.format("%s/%s", url.toExternalForm(), JarFile.MANIFEST_NAME);
manifestFileUrl = new URL(manifestFile);
} else {
manifestFileUrl = new URL(url, JarFile.MANIFEST_NAME);
}
manifestStream = URLToStreamHelper.urlToStream(manifestFileUrl);
return new Manifest(manifestStream);
} catch (MalformedURLException e1) {
// that's ok - just log in debug
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(e1.getMessage(), e1);
}
} catch (IOException e) {
// that's ok - just log in debug
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(e.getMessage(), e);
}
} finally {
if (manifestStream != null) {
try {
manifestStream.close();
} catch (IOException e) {
LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
}
}
}
JarInputStream jis = null;
try {
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
if (urlConnection instanceof JarURLConnection) {
JarURLConnection jarUrlConnection = (JarURLConnection) urlConnection;
return jarUrlConnection.getManifest();
} else {
jis = new JarInputStream(urlConnection.getInputStream());
return jis.getManifest();
}
} catch (IOException e) {
LOGGER.error(MessageFormat.format("Error reading META-INF/MANIFEST.MF file: {0}", e.getMessage()),
e);
} finally {
if (jis != null) {
try {
jis.close();
} catch (IOException e) {
LOGGER.error(MessageFormat.format("Error closing stream: {0}", e.getMessage()), e);
}
}
}
}
}
}
return null;
}
boolean containsDataFromManifest() {
return containsDataFromManifest;
}
public String getVersion() {
return fullVersionString;
}
public String getImplementationTitle() {
return implementationTitle;
}
public String getImplementationVendor() {
return implementationVendor;
}
public String getImplementationVersion() {
return implementationVersion;
}
public String getScmTimestamp() {
return scmTimestamp;
}
@Override
public String toString() {
if (this.containsDataFromManifest()) {
return getImplementationTitle() + " by " + getImplementationVendor() + ", version " + getVersion();
} else {
return getVersion();
}
}
}
public String getVendor() {
return VERSION.getImplementationVendor();
}
public String getTitle() {
return VERSION.getImplementationTitle();
}
public String getTimestamp() {
return VERSION.getScmTimestamp();
}
public Version getVersion() {
return VERSION;
}
@Override
public String toString() {
return VERSION.toString();
}
}