diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index b2ac61491fd..09e5a427b5d 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -19,7 +19,6 @@ package org.eclipse.jetty.server; import java.io.IOException; -import java.lang.management.ManagementFactory; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URI; @@ -53,6 +52,7 @@ import org.eclipse.jetty.util.AttributesMap; import org.eclipse.jetty.util.Jetty; import org.eclipse.jetty.util.MultiException; import org.eclipse.jetty.util.URIUtil; +import org.eclipse.jetty.util.Uptime; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.Name; @@ -376,7 +376,7 @@ public class Server extends HandlerWrapper implements Attributes mex.ifExceptionThrow(); - LOG.info(String.format("Started @%dms",ManagementFactory.getRuntimeMXBean().getUptime())); + LOG.info(String.format("Started @%dms",Uptime.getUptime())); } @Override diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/Uptime.java b/jetty-util/src/main/java/org/eclipse/jetty/util/Uptime.java new file mode 100644 index 00000000000..ce672a0680a --- /dev/null +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/Uptime.java @@ -0,0 +1,127 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Provide for a Uptime class that is compatible with Android and the new Java 8 compact1 and compact2 environments. + */ +public class Uptime +{ + public static final int NOIMPL = -1; + + public static interface Impl + { + public long getUptime(); + } + + public static class DefaultImpl implements Impl + { + public Object mxBean; + public Method uptimeMethod; + + public DefaultImpl() + { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + try + { + Class mgmtFactory = Class.forName("java.lang.management.ManagementFactory",true,cl); + Class runtimeClass = Class.forName("java.lang.management.RuntimeMXBean",true,cl); + Class noparams[] = new Class[0]; + Method mxBeanMethod = mgmtFactory.getMethod("getRuntimeMXBean",noparams); + if (mxBeanMethod == null) + { + throw new UnsupportedOperationException("method getRuntimeMXBean() not found"); + } + mxBean = mxBeanMethod.invoke(mgmtFactory); + if (mxBean == null) + { + throw new UnsupportedOperationException("getRuntimeMXBean() method returned null"); + } + uptimeMethod = runtimeClass.getMethod("getUptime",noparams); + if (mxBean == null) + { + throw new UnsupportedOperationException("method getUptime() not found"); + } + } + catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) + { + throw new UnsupportedOperationException("Implementation not available",e); + } + } + + @Override + public long getUptime() + { + try + { + return (long)uptimeMethod.invoke(mxBean); + } + catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) + { + return NOIMPL; + } + } + } + + private static final Uptime INSTANCE = new Uptime(); + + public static Uptime getInstance() + { + return INSTANCE; + } + + private Impl impl; + + private Uptime() + { + try + { + impl = new DefaultImpl(); + } + catch (UnsupportedOperationException e) + { + System.err.printf("Defaulting Uptime to NOIMPL due to (%s) %s%n",e.getClass().getName(),e.getMessage()); + impl = null; + } + } + + public Impl getImpl() + { + return impl; + } + + public void setImpl(Impl impl) + { + this.impl = impl; + } + + public static long getUptime() + { + Uptime u = getInstance(); + if (u == null || u.impl == null) + { + return NOIMPL; + } + return u.impl.getUptime(); + } +} diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java index 1b0d7366ebc..13a9eeffb05 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/AbstractLifeCycle.java @@ -18,9 +18,9 @@ package org.eclipse.jetty.util.component; -import java.lang.management.ManagementFactory; import java.util.concurrent.CopyOnWriteArrayList; +import org.eclipse.jetty.util.Uptime; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.log.Log; @@ -174,7 +174,7 @@ public abstract class AbstractLifeCycle implements LifeCycle { _state = __STARTED; if (LOG.isDebugEnabled()) - LOG.debug(STARTED+" @{}ms {}",ManagementFactory.getRuntimeMXBean().getUptime(),this); + LOG.debug(STARTED+" @{}ms {}",Uptime.getUptime(),this); for (Listener listener : _listeners) listener.lifeCycleStarted(this); } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java index 91b61f9a64f..f133dc0ed86 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java @@ -20,7 +20,6 @@ package org.eclipse.jetty.util.log; import java.io.IOException; import java.io.InputStream; -import java.lang.management.ManagementFactory; import java.lang.reflect.Method; import java.net.URL; import java.security.AccessController; @@ -34,6 +33,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.eclipse.jetty.util.Loader; +import org.eclipse.jetty.util.Uptime; import org.eclipse.jetty.util.annotation.ManagedAttribute; /** @@ -167,8 +167,6 @@ public class Log return; __initialized = true; - final long uptime=ManagementFactory.getRuntimeMXBean().getUptime(); - try { Class log_class = Loader.loadClass(Log.class, __logClass); @@ -185,7 +183,7 @@ public class Log } if (LOG!=null) - LOG.info(String.format("Logging initialized @%dms",uptime)); + LOG.info(String.format("Logging initialized @%dms",Uptime.getUptime())); } } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/UptimeTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/UptimeTest.java new file mode 100644 index 00000000000..cf9b4c74420 --- /dev/null +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/UptimeTest.java @@ -0,0 +1,31 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.util; + +import org.junit.Test; + +public class UptimeTest +{ + @Test + public void testUptime() + { + // should not throw an exception (if it does, the exception flows out and fails the testcase) + System.err.printf("Uptime = %,d%n",Uptime.getUptime()); + } +}