443652 - Remove dependency on java.lang.management classes

* Created safer Uptime singleton for use
* Removed direct references to java.lang.management from
   - Server.java
   - Log.java
   - AbstractLifeCycle.java
This commit is contained in:
Joakim Erdfelt 2014-09-10 10:04:23 -07:00 committed by Joakim Erdfelt
parent 40d84ff1e3
commit 3e4c9909e0
5 changed files with 164 additions and 8 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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()));
}
}

View File

@ -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());
}
}