Skip to content

Commit

Permalink
log the startup version of hawtio so folks can easily spot which vers…
Browse files Browse the repository at this point in the history
…ion they're using
  • Loading branch information
jstrachan committed Jul 18, 2013
1 parent 412b036 commit a5454a4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 32 deletions.
33 changes: 2 additions & 31 deletions hawtio-core/src/main/java/io/hawt/config/ConfigFacade.java
@@ -1,14 +1,12 @@
package io.hawt.config;

import io.hawt.util.IOHelper;
import io.hawt.util.MBeanSupport;
import io.hawt.util.Objects;
import io.hawt.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

/**
* A facade for the hawtio configuration features.
Expand All @@ -32,7 +30,6 @@ public static ConfigFacade getSingleton() {
public void init() throws Exception {
ConfigFacade.singleton = this;
super.init();

}

@Override
Expand All @@ -43,33 +40,7 @@ protected String getDefaultObjectName() {
@Override
public String getVersion() {
if (version == null) {
// lets try find the maven property - as the Java API rarely works :)
InputStream is = null;
String fileName = "/META-INF/maven/io.hawt/hawtio-core/pom.properties";
// try to load from maven properties first
try {
Properties p = new Properties();
is = getClass().getResourceAsStream(fileName);
if (is != null) {
p.load(is);
version = p.getProperty("version", "");
}
} catch (Exception e) {
// ignore
} finally {
if (is != null) {
IOHelper.close(is, fileName, LOG);
}
}
}
if (version == null) {
Package aPackage = getClass().getPackage();
if (aPackage != null) {
version = aPackage.getImplementationVersion();
if (Strings.isBlank(version)) {
version = aPackage.getSpecificationVersion();
}
}
version = Objects.getVersion(getClass(),"io.hawt", "hawtio-core");
}
return version;
}
Expand Down
77 changes: 76 additions & 1 deletion hawtio-core/src/main/java/io/hawt/util/Objects.java
Expand Up @@ -17,11 +17,24 @@
*/
package io.hawt.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

/**
* Some helper methods - though could be replaced by JDK 1.7 code now in java.util.Objects
* whenever we are happy to ignore JDK 1.6
*/
public class Objects {
private static final transient Logger LOG = LoggerFactory.getLogger(Objects.class);

public static boolean equals(Object a, Object b) {
if (a == b) {
return true;
Expand All @@ -42,4 +55,66 @@ public static int compare(Comparable a, Comparable b) {
}
return a.compareTo(b);
}
}


/**
* Returns the version of the given class's package or the group and artifact of the jar
*/
public static String getVersion(Class<?> aClass, String groupId, String artifactId) {
String version = null;
// lets try find the maven property - as the Java API rarely works :)
InputStream is = null;
String fileName = "/META-INF/maven/" +
groupId + "/" + artifactId +
"/pom.properties";
// try to load from maven properties first
try {
Properties p = new Properties();
is = aClass.getResourceAsStream(fileName);
if (is != null) {
p.load(is);
version = p.getProperty("version", "");
}
} catch (Exception e) {
// ignore
} finally {
if (is != null) {
IOHelper.close(is, fileName, LOG);
}
}
if (version == null) {
Package aPackage = aClass.getPackage();
if (aPackage != null) {
version = aPackage.getImplementationVersion();
if (Strings.isBlank(version)) {
version = aPackage.getSpecificationVersion();
}
}
}
if (version == null) {
Enumeration<URL> resources = null;
try {
resources = aClass.getClassLoader().getResources("META-INF/MANIFEST.MF");
} catch (IOException e) {
// ignore
}
if (resources != null) {
String expectedBundleName = groupId + "." + artifactId;
while (resources.hasMoreElements()) {
try {
Manifest manifest = new Manifest(resources.nextElement().openStream());
Attributes attributes = manifest.getMainAttributes();
String bundleName = attributes.getValue("Bundle-SymbolicName");
if (Objects.equals(expectedBundleName, bundleName)) {
version = attributes.getValue("Implementation-Version");
if (Strings.isNotBlank(version)) break;
}
} catch (IOException e) {
// ignore
}
}
}
}
return version;
}
}
27 changes: 27 additions & 0 deletions hawtio-web/src/main/java/io/hawt/jmx/JmxTreeWatcher.java
@@ -1,18 +1,28 @@
package io.hawt.jmx;

import io.hawt.util.Objects;
import io.hawt.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
* A simple mbean to watch the JMX tree so its easy for clients to know when they should refresh their JMX trees (which typically isn't a cheap operation).
*/
public class JmxTreeWatcher implements JmxTreeWatcherMBean {
private static final transient Logger LOG = LoggerFactory.getLogger(JmxTreeWatcher.class);
private static AtomicBoolean logged = new AtomicBoolean();

private ObjectName objectName;
private MBeanServer mBeanServer;
private AtomicLong counter = new AtomicLong(0);
private NotificationListener listener;
private NotificationFilter filter;
private String version;

public void init() throws Exception {
if (objectName == null) {
Expand All @@ -39,6 +49,13 @@ public void init() throws Exception {

mBeanServer.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, listener, filter, handback);
}
if (logged.compareAndSet(false, true)) {
String text = getVersion();
if (Strings.isNotBlank(text)) {
text += " ";
}
LOG.info("Welcome to hawtio " + text + ": http://hawt.io/ : Don't cha wish your console was hawt like me? ;-)");
}
}

public void destroy() throws Exception {
Expand All @@ -52,6 +69,16 @@ public void destroy() throws Exception {
}
}

public String getVersion() {
if (version == null) {
version = Objects.getVersion(getClass(), "io.hawt", "hawtio-web");
if (version == null) {
version = "";
}
}
return version;
}

protected ObjectName getObjectName() throws Exception {
return new ObjectName("io.hawt.jmx:type=TreeWatcher");
}
Expand Down

0 comments on commit a5454a4

Please sign in to comment.