You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2001/12/09 08:12:55 UTC
cvs commit: jakarta-avalon-phoenix/console/src/java/org/apache/avalon/phoenix/console TimeTool.java MBeanAccessor.java MBeanTool.java
donaldp 01/12/08 23:12:55
Added: console/src/java/org/apache/avalon/phoenix/console
TimeTool.java MBeanAccessor.java MBeanTool.java
Log:
Add some tools that make it easier to display MBeans using velocity.
Revision Changes Path
1.1 jakarta-avalon-phoenix/console/src/java/org/apache/avalon/phoenix/console/TimeTool.java
Index: TimeTool.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.phoenix.console;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* Provides convenient time utilities for Velocity.
*
* @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
*/
public class TimeTool
{
private final static long SECOND = 1000;
private final static long MINUTE = SECOND * 60;
private final static long HOUR = MINUTE * 60;
private final static long DAY = HOUR * 24;
private final static long YEAR = DAY * 365;
/**
* This is probably going to be the most used method.
* As such, here's the some quick examples
*
* Examples: "E, MMMM d" will result in "Tue, July 7"
* "EEE, M/dd/yyyy (H:m)" will result in "Tuesday, 7/07/1996 (14:12)"
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat</a>
* @param format SimpleDateFormat (e.g. "MM/dd/yyyy") that you wish to use for the given date
* @param date that is a Date
* @return String representation of the given date/time in the given format
*/
public static String formatDate( final String format, final long date )
{
final SimpleDateFormat formatter = new SimpleDateFormat( format );
return formatter.format( new Date( date ) );
}
public static String formatDuration( final long duration )
{
return formatDuration( duration, Integer.MAX_VALUE );
}
public static String formatDuration( long duration, int accuracy )
{
final long years = duration / YEAR;
duration -= years * YEAR;
final long days = duration / DAY;
duration -= days * DAY;
final long hours = duration / HOUR;
duration -= hours * HOUR;
final long minutes = duration / MINUTE;
duration -= minutes * MINUTE;
final long seconds = duration / SECOND;
duration -= seconds * SECOND;
final StringBuffer sb = new StringBuffer();
if( 0 < years )
{
if( 0 != sb.length() ) sb.append( ", " );
sb.append( years );
sb.append( " year" );
if( 1 != years ) sb.append( "s" );
accuracy--;
if( 0 >= accuracy ) return sb.toString();
}
if( 0 < days )
{
if( 0 != sb.length() ) sb.append( ", " );
sb.append( days );
sb.append( " day" );
if( 1 != days ) sb.append( "s" );
accuracy--;
if( 0 >= accuracy ) return sb.toString();
}
if( 0 < hours )
{
if( 0 != sb.length() ) sb.append( ", " );
sb.append( hours );
sb.append( " hour" );
if( 1 != hours ) sb.append( "s" );
accuracy--;
if( 0 >= accuracy ) return sb.toString();
}
if( 0 < minutes )
{
if( 0 != sb.length() ) sb.append( ", " );
sb.append( minutes );
sb.append( " minute" );
if( 1 != minutes ) sb.append( "s" );
accuracy--;
if( 0 >= accuracy ) return sb.toString();
}
if( 0 < seconds )
{
if( 0 != sb.length() ) sb.append( ", " );
sb.append( seconds );
sb.append( " second" );
if( 1 != seconds ) sb.append( "s" );
accuracy--;
if( 0 >= accuracy ) return sb.toString();
}
return sb.toString();
}
}
1.1 jakarta-avalon-phoenix/console/src/java/org/apache/avalon/phoenix/console/MBeanAccessor.java
Index: MBeanAccessor.java
===================================================================
package org.apache.avalon.phoenix.console;
import java.util.ArrayList;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ObjectInstance;
import org.apache.jmx.adaptor.RMIAdaptor;
/**
* This is a small utility class to allow easy access to mbean attributes.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
*/
public class MBeanAccessor
{
private final RMIAdaptor m_mBeanServer;
private final ObjectName m_objectName;
private MBeanInfo m_mBeanInfo;
private ObjectInstance m_objectInstance;
/** Hold the attribute objects by attribute name */
private ArrayList m_attributes = new ArrayList();
/**
* Create an accessor that treats, mBean attributes as normal attributes.
* It also adds a special attribute "meta" via which you can get the
* MBeanInfo.
*
* @param mBeanInfo the MBeanInfo to wrap
*/
public MBeanAccessor( final ObjectName objectName, final RMIAdaptor mBeanServer )
{
m_objectName = objectName;
m_mBeanServer = mBeanServer;
}
public String className()
throws Exception
{
return getObjectInstance().getClassName();
}
public MBeanInfo info()
throws Exception
{
return getMBeanInfo();
}
public ObjectName name()
{
return m_objectName;
}
/**
* Accessor method to get the fields by name.
*
* @param fieldName Name of static field to retrieve
*
* @return The value of the given field.
*/
public Object get( String fieldName )
throws Exception
{
//We need to force load the MBeanInfo
//If it hasn't yet been loaded
getMBeanInfo();
if( m_attributes.contains( fieldName ) )
{
return m_mBeanServer.getAttribute( m_objectName, fieldName );
}
else
{
return null;
}
}
private MBeanInfo getMBeanInfo()
throws Exception
{
if( null == m_mBeanInfo )
{
m_mBeanInfo = m_mBeanServer.getMBeanInfo( m_objectName );
final MBeanAttributeInfo[] attributes = m_mBeanInfo.getAttributes();
if( null != attributes )
{
for( int i = 0; i < attributes.length; i++ )
{
final MBeanAttributeInfo attribute = attributes[ i ];
m_attributes.add( attribute.getName() );
}
}
}
return m_mBeanInfo;
}
private ObjectInstance getObjectInstance()
throws Exception
{
if( null == m_objectInstance )
{
m_objectInstance = m_mBeanServer.getObjectInstance( m_objectName );
}
return m_objectInstance;
}
}
1.1 jakarta-avalon-phoenix/console/src/java/org/apache/avalon/phoenix/console/MBeanTool.java
Index: MBeanTool.java
===================================================================
package org.apache.avalon.phoenix.console;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.InstanceNotFoundException;
import org.apache.jmx.adaptor.RMIAdaptor;
/**
* This is a small utility class to interact with an MBeanServer.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
*/
public class MBeanTool
{
private final RMIAdaptor m_mBeanServer;
public MBeanTool( final RMIAdaptor mBeanServer )
{
m_mBeanServer = mBeanServer;
}
public MBeanAccessor getObject( final String name )
throws Exception
{
return new MBeanAccessor( getObjectName( name ), m_mBeanServer );
}
private ObjectName getObjectName( final String name )
throws Exception
{
//Deal with domains et al here
return new ObjectName( name );
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>