Author: lfryc(a)redhat.com
Date: 2010-09-29 07:01:54 -0400 (Wed, 29 Sep 2010)
New Revision: 19366
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/VersionBean.java
modules/tests/metamer/trunk/application/src/main/webapp/templates/footer.xhtml
Log:
added server detection (able to distinguish JBoss AS 6 and Tomcat 6) (RFPL-799)
Modified:
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/VersionBean.java
===================================================================
---
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/VersionBean.java 2010-09-29
10:51:48 UTC (rev 19365)
+++
modules/tests/metamer/trunk/application/src/main/java/org/richfaces/tests/metamer/bean/VersionBean.java 2010-09-29
11:01:54 UTC (rev 19366)
@@ -25,6 +25,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.security.CodeSource;
import java.util.Properties;
@@ -42,9 +43,11 @@
/**
* Vendor and version information for project Metamer.
- *
- * @author asmirnov(a)exadel.com, <a
href="mailto:ppitonak@redhat.com">Pavol Pitonak</a>
- * @version $Revision$
+ *
+ * @author asmirnov(a)exadel.com
+ * @author <a href="mailto:ppitonak@redhat.com">Pavol Pitonak</a>
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
*/
@ManagedBean(name = "metamer")
@ApplicationScoped
@@ -60,6 +63,7 @@
private String shortVersion;
private String richFacesVersion;
private String jsfVersion;
+ private String serverVersion;
/**
* Initializes the managed bean.
@@ -71,7 +75,10 @@
InputStream inStream =
getClass().getClassLoader().getResourceAsStream("version.properties");
properties.load(inStream);
} catch (Exception e) {
- LOGGER.warn("Unable to load version.properties using
PomVersion.class.getClassLoader().getResourceAsStream(...)", e);
+ LOGGER
+ .warn(
+ "Unable to load version.properties using
PomVersion.class.getClassLoader().getResourceAsStream(...)",
+ e);
}
implementationTitle = properties.getProperty("Implementation-Title");
@@ -79,6 +86,7 @@
implementationVersion =
properties.getProperty("Implementation-Version");
scmRevision = properties.getProperty("SCM-Revision");
scmTimestamp = properties.getProperty("SCM-Timestamp");
+ serverVersion = initializeServerVersion();
}
public String getVendor() {
@@ -111,10 +119,11 @@
return implementationVersion;
}
- fullVersion = implementationTitle + " by " + implementationVendor +
", version " + implementationVersion + " SVN r. " + scmRevision;
+ fullVersion = implementationTitle + " by " + implementationVendor +
", version " + implementationVersion
+ + " SVN r. " + scmRevision;
return fullVersion;
}
-
+
public String getShortVersion() {
if (shortVersion != null) {
return shortVersion;
@@ -125,15 +134,15 @@
return implementationVersion;
}
- shortVersion = "Metamer " + implementationVersion + " r. " +
scmRevision;
+ shortVersion = "Metamer " + implementationVersion + " r." +
scmRevision;
return shortVersion;
}
-
+
public String getRichFacesVersion() {
if (richFacesVersion != null) {
return richFacesVersion;
}
-
+
org.richfaces.VersionBean rfVersionBean = new org.richfaces.VersionBean();
StringBuilder result = new StringBuilder();
result.append("RichFaces ");
@@ -141,7 +150,7 @@
result.append(" r.");
result.append(rfVersionBean.getVersion().getScmRevision());
richFacesVersion = result.toString();
-
+
return richFacesVersion;
}
@@ -196,14 +205,135 @@
}
public String getBrowserVersion() {
- HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext().getRequest();
+ HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext()
+ .getRequest();
return request.getHeader("user-agent");
}
-
+
public ProjectStage getProjectStage() {
return FacesContext.getCurrentInstance().getApplication().getProjectStage();
}
+ public String getServerVersion() {
+ return serverVersion;
+ }
+
+ private static String initializeServerVersion() {
+ String result = null;
+
+ try {
+ result = getJBossASVersionInfo();
+ } catch (FailToRetrieveInfo e) {
+ result = e.getMessage();
+ }
+
+ if (result == null) {
+ try {
+ result = getTomcatVersionInfo();
+ } catch (FailToRetrieveInfo e) {
+ result = e.getMessage();
+ }
+ }
+
+ if (result == null) {
+ result = "Server unknown";
+ }
+
+ return result;
+ }
+
+ private static String getTomcatVersionInfo() {
+ String result = (String) new InfoObtainer() {
+
+ @Override
+ protected Object obtainInfo() throws ClassNotFoundException,
IllegalAccessException,
+ InstantiationException, SecurityException, NoSuchMethodException,
IllegalArgumentException,
+ InvocationTargetException {
+
+ Class<?> clazz =
Class.forName("org.apache.catalina.util.ServerInfo");
+ return clazz.getMethod("getServerInfo").invoke(null);
+ }
+ }.getInfo();
+
+ return result.replace("/", " ");
+ }
+
+ private static String getJBossASVersionInfo() {
+ String versionNumber = (String) new
JBossASVersionInfoObtainer("getVersionNumber").getInfo();
+ String buildNumber = (String) new
JBossASVersionInfoObtainer("getBuildID").getInfo();
+
+ if (versionNumber == null) {
+ return null;
+ }
+
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("JBoss AS ");
+ buffer.append(versionNumber);
+
+ if (versionNumber.endsWith("SNAPSHOT")) {
+ buffer.append(" ");
+ buffer.append(buildNumber.replaceFirst("r", "r."));
+ }
+
+ return buffer.toString();
+ }
+
+ private static class JBossASVersionInfoObtainer extends InfoObtainer {
+ private String methodName;
+
+ public JBossASVersionInfoObtainer(String methodName) {
+ super();
+ this.methodName = methodName;
+ }
+
+ @Override
+ protected Object obtainInfo() throws ClassNotFoundException,
IllegalAccessException, InstantiationException,
+ SecurityException, NoSuchMethodException, IllegalArgumentException,
InvocationTargetException {
+
+ Class<?> clazz =
Class.forName("org.jboss.bootstrap.impl.as.server.ASVersion");
+ Object classInstance =
clazz.getMethod("getInstance").invoke(null);
+ return clazz.getMethod(methodName).invoke(classInstance);
+ }
+ }
+
+ private abstract static class InfoObtainer {
+ protected abstract Object obtainInfo() throws ClassNotFoundException,
IllegalAccessException,
+ InstantiationException, SecurityException, NoSuchMethodException,
IllegalArgumentException,
+ InvocationTargetException;
+
+ public final Object getInfo() {
+ try {
+ return obtainInfo();
+ } catch (ClassNotFoundException e) {
+ return null;
+ } catch (IllegalAccessException e) {
+ throw new FailToRetrieveInfo(fail("failed to retrieve info - ",
e), e);
+ } catch (InstantiationException e) {
+ throw new FailToRetrieveInfo(fail("failed to retrieve info - ",
e), e);
+ } catch (SecurityException e) {
+ throw new FailToRetrieveInfo(fail("failed to access version info -
", e), e);
+ } catch (NoSuchMethodException e) {
+ throw new FailToRetrieveInfo(fail("failed to access version info -
", e), e);
+ } catch (IllegalArgumentException e) {
+ throw new FailToRetrieveInfo(fail("failed to access version info -
", e), e);
+ } catch (InvocationTargetException e) {
+ throw new FailToRetrieveInfo(fail("failed to access version info -
", e), e);
+ }
+ }
+ }
+
+ private static class FailToRetrieveInfo extends RuntimeException {
+
+ public FailToRetrieveInfo(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ }
+
+ private static String fail(String message, Exception e) {
+ return message + e.getClass().getSimpleName() + e.getMessage();
+ }
+
@Override
public String toString() {
return getFullVersion();
Modified: modules/tests/metamer/trunk/application/src/main/webapp/templates/footer.xhtml
===================================================================
---
modules/tests/metamer/trunk/application/src/main/webapp/templates/footer.xhtml 2010-09-29
10:51:48 UTC (rev 19365)
+++
modules/tests/metamer/trunk/application/src/main/webapp/templates/footer.xhtml 2010-09-29
11:01:54 UTC (rev 19366)
@@ -32,6 +32,7 @@
<li><h:outputText id="richfacesVersion"
value="#{metamer.richFacesVersion}" /></li>
<li><h:outputText id="metamerVersion"
value="#{metamer.shortVersion}" /></li>
<li><h:outputText id="jsfVersion"
value="#{metamer.jsfVersion}" /></li>
+ <li><h:outputText id="serverVersion"
value="#{metamer.serverVersion}" /></li>
<li><h:outputText id="javaVersion"
value="#{metamer.javaVersion}" /> @ <h:outputText
id="osVersion"
value="#{metamer.osVersion}" /></li>
<li><h:outputText id="browserVersion"
value="#{metamer.browserVersion}" /></li>