User: schaefera
Date: 01/12/05 16:31:22
Added: src/main/org/jboss/management/mejb
ClientNotificationListener.java
JMSClientNotificationListener.java
JMSNotificationListener.java
JMSNotificationListenerMBean.java
ListenerMBean.java ListenerRegistration.java
ManagementBean.java
PollingClientNotificationListener.java
PollingNotificationListener.java
PollingNotificationListenerMBean.java
RMIClientNotificationListener.java
RMIClientNotificationListenerInterface.java
RMINotificationListener.java
RMINotificationListenerMBean.java
SearchClientNotificationListener.java
Removed: src/main/org/jboss/management/mejb MEJBBean.java
Log:
Adjustments to the latest update of the JSR-77 spec. as well as the
implementation of remote listeners.
Revision Changes Path
1.1
jboss/src/main/org/jboss/management/mejb/ClientNotificationListener.java
Index: ClientNotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Random;
import javax.management.InstanceNotFoundException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.JMException;
import javax.management.MalformedObjectNameException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanException;
import javax.management.NotCompliantMBeanException;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import org.jboss.jmx.connector.RemoteMBeanServer;
/**
* Basic Local Listener to receive Notification from a remote JMX Agent
**/
public abstract class ClientNotificationListener {
private ObjectName mSender;
private ObjectName mRemoteListener;
protected NotificationListener mClientListener;
protected Object mHandback;
private Random mRandom = new Random();
public ClientNotificationListener(
ObjectName pSender,
NotificationListener pClientListener,
Object pHandback
) {
mSender = pSender;
mClientListener = pClientListener;
mHandback = pHandback;
}
public ObjectName createListener(
MEJB pConnector,
String mClass,
Object[] pParameters,
String[] pSignatures
) throws
MalformedObjectNameException,
ReflectionException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException
{
ObjectName lName = null;
while( lName == null ) {
try {
lName = new ObjectName( "JMX:type=listener,id=" + mRandom.nextLong() );
ObjectInstance lInstance = pConnector.createMBean(
mClass,
lName,
pParameters,
pSignatures
);
lName = lInstance.getObjectName();
}
catch( InstanceAlreadyExistsException iaee ) {
lName = null;
}
catch( RemoteException re ) {
lName = null;
}
}
mRemoteListener = lName;
return lName;
}
public void addNotificationListener(
MEJB pConnector,
NotificationFilter pFilter
) throws
InstanceNotFoundException,
RemoteException
{
pConnector.addNotificationListener(
mSender,
mRemoteListener,
pFilter,
null
);
}
public void removeNotificationListener(
MEJB pConnector
) throws
InstanceNotFoundException,
RemoteException
{
try {
pConnector.removeNotificationListener(
mSender,
mRemoteListener
);
}
catch( JMException jme ) {
}
try {
pConnector.unregisterMBean( mRemoteListener );
}
catch( JMException jme ) {
}
}
public ObjectName getSenderMBean() {
return mSender;
}
public ObjectName getRemoteListenerName() {
return mRemoteListener;
}
public boolean equals( Object pTest ) {
if( pTest instanceof ClientNotificationListener ) {
ClientNotificationListener lListener = (ClientNotificationListener) pTest;
return
mSender.equals( lListener.mSender ) &&
mClientListener.equals( lListener.mClientListener );
}
return false;
}
}
1.1
jboss/src/main/org/jboss/management/mejb/JMSClientNotificationListener.java
Index: JMSClientNotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.management.JMException;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.rmi.PortableRemoteObject;
import org.jboss.jmx.connector.RemoteMBeanServer;
/**
* Local JMS Listener to receive the message and send to the listener
**/
public class JMSClientNotificationListener
extends ClientNotificationListener
implements MessageListener
{
public JMSClientNotificationListener(
ObjectName pSender,
NotificationListener pClientListener,
Object pHandback,
NotificationFilter pFilter,
String pQueueJNDIName,
String pServerName,
MEJB pConnector
) throws
JMSException,
JMException,
NamingException,
RemoteException
{
super( pSender, pClientListener, pHandback );
// Get the JMS QueueConnectionFactory from the J2EE server
QueueConnection lConnection = getQueueConnection( pServerName, pQueueJNDIName
);
// Create JMS Session and create temporary Queue
QueueSession lSession = lConnection.createQueueSession( false,
Session.AUTO_ACKNOWLEDGE );
Queue lQueue = lSession.createTemporaryQueue();
// Register the listener as MBean on the remote JMX server
createListener(
pConnector,
"org.jboss.jmx.connector.notification.JMSNotificationListener",
new Object[] { pQueueJNDIName, lQueue },
new String[] { String.class.getName(), Queue.class.getName() }
);
// Create JMS message receiver, create local message listener and set it as
message
// listener to the receiver
QueueReceiver lReceiver = lSession.createReceiver( lQueue, null );
lReceiver.setMessageListener( this );
addNotificationListener( pConnector, pFilter );
}
public void onMessage( Message pMessage ) {
try {
// Unpack the Notification from the Message and hand it over to the clients
// Notification Listener
Notification lNotification = (Notification) ( (ObjectMessage) pMessage
).getObject();
mClientListener.handleNotification( lNotification, mHandback );
}
catch( JMSException je ) {
je.printStackTrace();
}
}
/**
* Creates a SurveyManagement bean.
*
* @return Returns a SurveyManagement bean for use by the Survey handler.
**/
private QueueConnection getQueueConnection( String pServerName, String
pQueueJNDIName )
throws NamingException, JMSException
{
Context lJNDIContext = null;
if( pServerName != null ) {
Hashtable lProperties = new Hashtable();
lProperties.put( Context.PROVIDER_URL, pServerName );
lJNDIContext = new InitialContext( lProperties );
} else {
lJNDIContext = new InitialContext();
}
Object aRef = lJNDIContext.lookup( pQueueJNDIName );
QueueConnectionFactory aFactory = (QueueConnectionFactory)
PortableRemoteObject.narrow( aRef, QueueConnectionFactory.class );
QueueConnection lConnection = aFactory.createQueueConnection();
lConnection.start();
return lConnection;
}
}
1.1
jboss/src/main/org/jboss/management/mejb/JMSNotificationListener.java
Index: JMSNotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
/**
* Remote Listener using JMS to send the event
**/
public class JMSNotificationListener
implements JMSNotificationListenerMBean
{
// JMS Queue Session and Sender must be created on the server-side
// therefore they are transient and created on the first notification
// call
private transient QueueSender mSender;
private transient QueueSession mSession;
private String mJNDIName;
private Queue mQueue;
public JMSNotificationListener(
String pJNDIName,
Queue pQueue
) throws JMSException
{
mJNDIName = pJNDIName;
mQueue = pQueue;
}
/**
* Handles the given notification by sending this to the remote
* client listener
*
* @param pNotification Notification to be send
* @param pHandback Handback object
*/
public void handleNotification(
Notification pNotification,
Object pHandback
) {
try {
if( mSender == null ) {
// Get QueueConnectionFactory, create Connection, Session and then Sender
QueueConnection lConnection = getQueueConnection( mJNDIName );
mSession = lConnection.createQueueSession( false,
Session.AUTO_ACKNOWLEDGE );
mSender = ( (QueueSession) mSession).createSender( mQueue );
}
// Create a message and send to the Queue
Message lMessage = mSession.createObjectMessage( pNotification );
mSender.send( lMessage );
}
catch( Exception e ) {
e.printStackTrace();
}
}
/**
* Test if this and the given Object are equal. This is true if the given
* object both refer to the same local listener
*
* @param pTest Other object to test
if equal
*
* @return True if both are of
same type and
* refer to the
same local listener
**/
public boolean equals( Object pTest ) {
if( pTest instanceof JMSNotificationListener ) {
try {
return mQueue.getQueueName().equals(
( (JMSNotificationListener) pTest).mQueue.getQueueName()
);
}
catch( JMSException je ) {
je.printStackTrace();
}
}
return false;
}
/**
* @return Hashcode of the local
listener
**/
public int hashCode() {
return mQueue.hashCode();
}
/**
* Creates a SurveyManagement bean.
*
* @return Returns a SurveyManagement bean for use by the Survey handler.
**/
private QueueConnection getQueueConnection( String pJNDIName )
throws NamingException, JMSException
{
Context aJNDIContext = new InitialContext();
Object aRef = aJNDIContext.lookup( pJNDIName );
QueueConnectionFactory aFactory = (QueueConnectionFactory)
PortableRemoteObject.narrow( aRef, QueueConnectionFactory.class );
QueueConnection lConnection = aFactory.createQueueConnection();
lConnection.start();
return lConnection;
}
}
1.1
jboss/src/main/org/jboss/management/mejb/JMSNotificationListenerMBean.java
Index: JMSNotificationListenerMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import javax.management.Notification;
import javax.management.NotificationListener;
/**
* MBean Interface of a Notification Listener MBean
* using JMS to send the notifications back to the client.
* <br>
* This interface is only necessary because of the naming
* conventions for standard MBeans.
*
* @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
**/
public interface JMSNotificationListenerMBean
extends ListenerMBean
{
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
// Public --------------------------------------------------------
}
1.1 jboss/src/main/org/jboss/management/mejb/ListenerMBean.java
Index: ListenerMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import javax.management.Notification;
import javax.management.NotificationListener;
/**
* MBean Interface of a Notification Listener MBean
*
* @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
**/
public interface ListenerMBean
extends NotificationListener
{
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
// Public --------------------------------------------------------
/**
* Handles the given notifcation event and passed it to the registered
* listener
*
* @param pNotification NotificationEvent
* @param pHandback Handback object
*
* @throws RemoteException If a Remote Exception occurred
*/
public void handleNotification(
Notification pNotification,
Object pHandback
);
}
1.1
jboss/src/main/org/jboss/management/mejb/ListenerRegistration.java
Index: ListenerRegistration.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.security.InvalidParameterException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.CreateException;
import javax.management.InstanceNotFoundException;
import javax.management.ListenerNotFoundException;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.j2ee.ManagementHome;
import org.jboss.management.mejb.MEJB;
/**
* Root class of the JBoss JSR-77 implementation of
* {@link javax.management.j2ee.ListenerRegistration ListenerRegistration}.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Andreas Schaefer</a>.
* @version $Revision: 1.1 $
*
* <p><b>Revisions:</b>
*
* <p><b>20011205 Andreas Schaefer:</b>
* <ul>
* <li> Creation
* </ul>
**/
public class ListenerRegistration
implements javax.management.j2ee.ListenerRegistration
{
// Constants -----------------------------------------------------
public static final int NOTIFICATION_TYPE_RMI = 0;
public static final int NOTIFICATION_TYPE_JMS = 1;
public static final int NOTIFICATION_TYPE_POLLING = 2;
// Attributes ----------------------------------------------------
private ManagementHome mHome;
private int mEventType = NOTIFICATION_TYPE_RMI;
private String[] mOptions;
private List mListeners = new ArrayList();
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
public ListenerRegistration( ManagementHome pHome, String[] pOptions ) {
if( pHome == null ) {
throw new InvalidParameterException( "Home Interface must be specified" );
}
mHome = pHome;
mOptions = pOptions;
}
// Public --------------------------------------------------------
// javax.management.j2ee.ListenerRegistration implementation -----
public void addNotificationListener(
ObjectName pName,
NotificationListener pListener,
NotificationFilter pFilter,
Object pHandback
)
throws
InstanceNotFoundException,
RemoteException
{
MEJB lManagement = null;
// Create the remote MBean and register it
try {
// Get EJB
lManagement = getMEJB();
ClientNotificationListener lListener = null;
switch( mEventType ) {
case NOTIFICATION_TYPE_RMI:
lListener = new RMIClientNotificationListener(
pName,
pListener,
pHandback,
pFilter,
lManagement
);
break;
case NOTIFICATION_TYPE_JMS:
lListener = new JMSClientNotificationListener(
pName,
pListener,
pHandback,
pFilter,
mOptions[ 0 ],
mOptions[ 1 ], // JNDI-Server name
lManagement
);
break;
case NOTIFICATION_TYPE_POLLING:
lListener = new PollingClientNotificationListener(
pName,
pListener,
pHandback,
pFilter,
5000, // Sleeping Period
2500, // Maximum Pooled List Size
lManagement
);
}
// Add this listener on the client to remove it when the client goes down
mListeners.add( lListener );
}
catch( Exception e ) {
if( e instanceof RuntimeException ) {
throw (RuntimeException) e;
}
if( e instanceof InstanceNotFoundException ) {
throw (InstanceNotFoundException) e;
}
throw new RuntimeException( "Remote access to perform this operation
failed: " + e.getMessage() );
}
finally {
if( lManagement != null ) {
try {
lManagement.remove();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
}
public void removeNotificationListener(
ObjectName pName,
NotificationListener pListener,
NotificationFilter pFilter,
Object pHandback
)
throws
InstanceNotFoundException,
ListenerNotFoundException,
RemoteException
{
MEJB lManagement = null;
try {
// Get EJB
lManagement = getMEJB();
ClientNotificationListener lCheck = new SearchClientNotificationListener(
pName, pListener );
int i = mListeners.indexOf( lCheck );
if( i >= 0 ) {
ClientNotificationListener lListener = (ClientNotificationListener)
mListeners.get( i );
lListener.removeNotificationListener( lManagement );
}
}
catch( Exception e ) {
if( e instanceof RuntimeException ) {
throw (RuntimeException) e;
}
if( e instanceof InstanceNotFoundException ) {
throw (InstanceNotFoundException) e;
}
throw new RuntimeException( "Remote access to perform this operation
failed: " + e.getMessage() );
}
finally {
if( lManagement != null ) {
try {
lManagement.remove();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
}
// Y overrides ---------------------------------------------------
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
private MEJB getMEJB()
throws
CreateException,
RemoteException
{
return (MEJB) mHome.create();
}
// Inner classes -------------------------------------------------
}
1.1 jboss/src/main/org/jboss/management/mejb/ManagementBean.java
Index: ManagementBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.InstanceNotFoundException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.IntrospectionException;
import javax.management.ListenerNotFoundException;
import javax.management.NotCompliantMBeanException;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.ObjectInstance;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import javax.management.j2ee.ManagementHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.jmx.connector.RemoteMBeanServer;
import org.jboss.management.j2ee.J2EEManagedObject;
/**
* Management Session Bean to enable the client to manage the
* server its is deployed on.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Andreas Schaefer</a>
* @version $Revision: 1.1 $
*
* @ejb:bean name="MEJB"
* display-name="JBoss Management EJB (MEJB)"
* type="Stateless"
* jndi-name="ejb/mgmt/J2EEManagement"
* @--ejb:interface generate="none"
* remote-class="javax.management.j2ee.Management"
* @ejb:interface extends="javax.management.j2ee.Management"
* @--ejb:home extends="javax.management.j2ee.ManagementHome"
* @ejb:home generate="none"
* remote-class="javax.management.j2ee.ManagementHome"
* @ejb:env-entry description="JNDI-Name of the MBeanServer to be used to look it up.
If 'null' the first of all listed local MBeanServer is taken"
* name="Server-Name"
* value="null"
*
**/
public class ManagementBean
implements SessionBean
{
// -------------------------------------------------------------------------
// Static
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// Members
// -------------------------------------------------------------------------
private SessionContext mContext;
/**
* Reference to the MBeanServer all the methods of this Connector are
* forwarded to
**/
private RemoteMBeanServer mConnector;
// -------------------------------------------------------------------------
// Methods
// -------------------------------------------------------------------------
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public Object getAttribute( ObjectName pName, String pAttribute )
throws
MBeanException,
AttributeNotFoundException,
InstanceNotFoundException,
ReflectionException,
RemoteException
{
return mConnector.getAttribute( pName, pAttribute );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public AttributeList getAttributes( ObjectName pName, String[] pAttributes )
throws
InstanceNotFoundException,
ReflectionException,
RemoteException
{
return mConnector.getAttributes( pName, pAttributes );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public String getDefaultDomain()
throws RemoteException
{
return J2EEManagedObject.getDomainName();
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public Integer getMBeanCount()
throws RemoteException
{
try {
return new Integer(
queryNames(
new ObjectName( getDefaultDomain() + ":*" )
).size()
);
}
catch( Exception e ) {
}
return new Integer( 0 );
}
public MBeanInfo getMBeanInfo( ObjectName pName )
throws
IntrospectionException,
InstanceNotFoundException,
ReflectionException,
RemoteException
{
return mConnector.getMBeanInfo( pName );
}
public ListenerRegistration getListenerRegistry()
throws RemoteException
{
return new ListenerRegistration(
(ManagementHome) mContext.getEJBObject().getEJBHome(),
new String[] {}
);
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public Object invoke( ObjectName pName, String pOperationName, Object[] pParams,
String[] pSignature )
throws
InstanceNotFoundException,
MBeanException,
ReflectionException,
RemoteException
{
return mConnector.invoke(
pName,
pOperationName,
pParams,
pSignature
);
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public boolean isRegistered( ObjectName pName )
throws RemoteException
{
return mConnector.isRegistered( pName );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public Set queryNames( ObjectName pName )
throws RemoteException
{
return mConnector.queryNames( pName, null );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public void setAttribute( ObjectName pName, Attribute pAttribute )
throws
AttributeNotFoundException,
InstanceNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException,
RemoteException
{
mConnector.setAttribute( pName, pAttribute );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public AttributeList setAttributes( ObjectName pName, AttributeList pAttributes )
throws
InstanceNotFoundException,
ReflectionException,
RemoteException
{
return mConnector.setAttributes( pName, pAttributes );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public ObjectInstance createMBean(
String pClass,
ObjectName pName,
Object[] pParameters,
String[] pSignature
)
throws
InstanceAlreadyExistsException,
MBeanException,
MBeanRegistrationException,
NotCompliantMBeanException,
ReflectionException,
RemoteException
{
return mConnector.createMBean( pClass, pName, pParameters, pSignature );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public void unregisterMBean(
ObjectName pName
)
throws
InstanceNotFoundException,
MBeanRegistrationException,
RemoteException
{
mConnector.unregisterMBean( pName );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public void addNotificationListener(
ObjectName pBroadcaster,
ObjectName pListener,
NotificationFilter pFilter,
Object pHandback
)
throws
InstanceNotFoundException,
RemoteException
{
mConnector.addNotificationListener( pBroadcaster, pListener, pFilter,
pHandback );
}
/**
* @throws RemoteException Necessary for a EJB
*
* @ejb:interface-method view-type="remote"
**/
public void removeNotificationListener(
ObjectName pBroadcaster,
ObjectName pListener
)
throws
InstanceNotFoundException,
ListenerNotFoundException,
RemoteException
{
mConnector.removeNotificationListener( pBroadcaster, pListener );
}
/**
* Create the Session Bean which takes the first available
* MBeanServer as target server
*
* @throws CreateException
*
* @ejb:create-method
**/
public void ejbCreate()
throws
CreateException
{
if( mConnector == null ) {
try {
Context aJNDIContext = new InitialContext();
String lServerName = ( (String) aJNDIContext.lookup(
"java:comp/env/Server-Name"
) ).trim();
if( lServerName == null || lServerName.length() == 0 ||
lServerName.equals( "null" ) ) {
ArrayList lServers = MBeanServerFactory.findMBeanServer( null );
if( lServers.size() > 0 ) {
mConnector = new LocalConnector( (MBeanServer) lServers.get( 0 ) );
} else {
throw new CreateException(
"No local JMX MBeanServer available"
);
}
} else {
Object lServer = aJNDIContext.lookup( lServerName );
if( lServer != null ) {
if( lServer instanceof MBeanServer ) {
mConnector = new LocalConnector( (MBeanServer) lServer );
} else
if( lServer instanceof RemoteMBeanServer ) {
mConnector = (RemoteMBeanServer) lServer;
} else {
throw new CreateException(
"Server: " + lServer + " reference by Server-Name: " +
lServerName +
" is not of type MBeanServer or RemoteMBeanServer: "
);
}
} else {
throw new CreateException(
"Server-Name " + lServerName + " does not reference an Object
in JNDI"
);
}
}
}
catch( NamingException ne ) {
throw new EJBException( ne );
}
}
}
/**
* Describes the instance and its content for debugging purpose
*
* @return Debugging information about the instance and its content
**/
public String toString()
{
return "Management [ " + " ]";
}
// -------------------------------------------------------------------------
// Framework Callbacks
// -------------------------------------------------------------------------
/**
* Set the associated session context. The container invokes this method on
* an instance after the instance has been created.
* <p>This method is called with no transaction context.
*
* @param aContext A SessionContext interface for the instance. The instance
* should store the reference to the context in an instance variable.
* @throws EJBException Should something go wrong while seting the context,
* an EJBException will be thrown.
**/
public void setSessionContext( SessionContext aContext )
throws
EJBException
{
mContext = aContext;
}
/**
* The activate method is called when the instance is activated from its
* "passive" state. The instance should acquire any resource that it has
* released earlier in the ejbPassivate() method.
* <p>This method is called with no transaction context.
*
* @throws EJBException Thrown by the method to indicate a failure caused
* by a system-level error
**/
public void ejbActivate()
throws
EJBException
{
}
/**
* The passivate method is called before the instance enters the "passive"
* state. The instance should release any resources that it can re-acquire
* later in the ejbActivate() method.
* <p>After the passivate method completes, the instance must be in a state
* that allows the container to use the Java Serialization protocol to
* externalize and store away the instance's state.
* <p>This method is called with no transaction context.
*
* @throws EJBException Thrown by the method to indicate a failure caused
* by a system-level error
**/
public void ejbPassivate()
throws
EJBException
{
}
/**
* A container invokes this method before it ends the life of the session
* object. This happens as a result of a client's invoking a remove
* operation, or when a container decides to terminate the session object
* after a timeout.
* <p>This method is called with no transaction context.
*
* @throws EJBException Thrown by the method to indicate a failure caused
* by a system-level error
**/
public void ejbRemove()
throws
EJBException
{
}
private class LocalConnector implements RemoteMBeanServer {
private MBeanServer mServer = null;
public LocalConnector( MBeanServer pServer ) {
mServer = pServer;
}
public ObjectInstance createMBean(
String pClassName,
ObjectName pName
) throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException
{
return mServer.createMBean( pClassName, pName );
}
public ObjectInstance createMBean(
String pClassName,
ObjectName pName,
ObjectName pLoaderName
) throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException,
InstanceNotFoundException
{
return mServer.createMBean( pClassName, pName, pLoaderName );
}
public ObjectInstance createMBean(
String pClassName,
ObjectName pName,
Object[] pParams,
String[] pSignature
) throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException
{
return mServer.createMBean( pClassName, pName, pParams, pSignature );
}
public ObjectInstance createMBean(
String pClassName,
ObjectName pName,
ObjectName pLoaderName,
Object[] pParams,
String[] pSignature
) throws
ReflectionException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MBeanException,
NotCompliantMBeanException,
InstanceNotFoundException
{
return mServer.createMBean( pClassName, pName, pLoaderName, pParams,
pSignature );
}
public void unregisterMBean(
ObjectName pName
) throws
InstanceNotFoundException,
MBeanRegistrationException
{
mServer.unregisterMBean( pName );
}
public ObjectInstance getObjectInstance(
ObjectName pName
) throws
InstanceNotFoundException
{
return mServer.getObjectInstance( pName );
}
public Set queryMBeans(
ObjectName pName,
QueryExp pQuery
) {
return mServer.queryMBeans( pName, pQuery );
}
public Set queryNames(
ObjectName pName,
QueryExp pQuery
) {
return mServer.queryNames( pName, pQuery );
}
public boolean isRegistered(
ObjectName pName
) {
return mServer.isRegistered( pName );
}
public boolean isInstanceOf(
ObjectName pName,
String pClassName
) throws
InstanceNotFoundException
{
return mServer.isInstanceOf( pName, pClassName );
}
public Integer getMBeanCount(
) {
return mServer.getMBeanCount();
}
public Object getAttribute(
ObjectName pName,
String pAttribute
) throws
MBeanException,
AttributeNotFoundException,
InstanceNotFoundException,
ReflectionException
{
return mServer.getAttribute( pName, pAttribute );
}
public AttributeList getAttributes(
ObjectName pName,
String[] pAttributes
) throws
InstanceNotFoundException,
ReflectionException
{
return mServer.getAttributes( pName, pAttributes );
}
public void setAttribute(
ObjectName pName,
Attribute pAttribute
) throws
InstanceNotFoundException,
AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException
{
mServer.setAttribute( pName, pAttribute );
}
public AttributeList setAttributes(
ObjectName pName,
AttributeList pAttributes
) throws
InstanceNotFoundException,
ReflectionException
{
return mServer.setAttributes( pName, pAttributes );
}
public Object invoke(
ObjectName pName,
String pActionName,
Object[] pParams,
String[] pSignature
) throws
InstanceNotFoundException,
MBeanException,
ReflectionException
{
return mServer.invoke( pName, pActionName, pParams, pSignature );
}
public String getDefaultDomain(
) {
return mServer.getDefaultDomain();
}
public MBeanInfo getMBeanInfo(
ObjectName pName
) throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException
{
return mServer.getMBeanInfo( pName );
}
public void addNotificationListener(
ObjectName pName,
NotificationListener pListener,
NotificationFilter pFilter,
Object pHandback
) throws
InstanceNotFoundException
{
mServer.addNotificationListener( pName, pListener, pFilter, pHandback );
}
public void removeNotificationListener(
ObjectName pName,
NotificationListener pListener
) throws
InstanceNotFoundException,
ListenerNotFoundException
{
mServer.removeNotificationListener( pName, pListener );
}
public void addNotificationListener(
ObjectName pName,
ObjectName pListener,
NotificationFilter pFilter,
Object pHandback
) throws
InstanceNotFoundException
{
mServer.addNotificationListener( pName, pListener, pFilter, pHandback );
}
public void removeNotificationListener(
ObjectName pName,
ObjectName pListener
) throws
InstanceNotFoundException,
ListenerNotFoundException,
UnsupportedOperationException
{
mServer.removeNotificationListener( pName, pListener );
}
}
}
1.1
jboss/src/main/org/jboss/management/mejb/PollingClientNotificationListener.java
Index: PollingClientNotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import javax.management.JMException;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import org.jboss.jmx.connector.RemoteMBeanServer;
/**
* Local Polling Listener to receive the message and send to the listener
**/
public class PollingClientNotificationListener
extends ClientNotificationListener
implements Runnable
{
private MEJB mConnector;
private int mSleepingPeriod = 2000;
public PollingClientNotificationListener(
ObjectName pSender,
NotificationListener pClientListener,
Object pHandback,
NotificationFilter pFilter,
int pSleepingPeriod,
int pMaximumListSize,
MEJB pConnector
) throws
JMException,
RemoteException
{
super( pSender, pClientListener, pHandback );
if( pSleepingPeriod > 0 ) {
mSleepingPeriod = pSleepingPeriod;
}
mConnector = pConnector;
// Register the listener as MBean on the remote JMX server
createListener(
pConnector,
"org.jboss.jmx.connector.notification.PollingNotificationListener",
new Object[] { new Integer( pMaximumListSize ), new Integer(
pMaximumListSize ) },
new String[] { Integer.TYPE.getName(), Integer.TYPE.getName() }
);
addNotificationListener( pConnector, pFilter );
new Thread( this ).start();
}
public void run() {
while( true ) {
try {
try {
List lNotifications = (List) mConnector.invoke(
getRemoteListenerName(),
"getNotifications",
new Object[] {},
new String[] {}
);
Iterator i = lNotifications.iterator();
while( i.hasNext() ) {
Notification lNotification = (Notification) i.next();
mClientListener.handleNotification(
lNotification,
mHandback
);
}
}
catch( Exception e ) {
System.out.println(
"PollingClientNotificationListener.getNotifications() got exception " + e );
}
Thread.sleep( mSleepingPeriod );
}
catch( InterruptedException ie ) {
}
}
}
}
1.1
jboss/src/main/org/jboss/management/mejb/PollingNotificationListener.java
Index: PollingNotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.management.Notification;
import javax.management.NotificationListener;
/**
* Remote Listener using Polling to send the event
**/
public class PollingNotificationListener
implements PollingNotificationListenerMBean
{
private List mList;
private int mMaximumSize = 1000;
public PollingNotificationListener(
int pListSize,
int pMaximumListSize
) {
if( pListSize <= 0 ) {
pListSize = 1000;
}
mList = new ArrayList( pListSize );
if( pMaximumListSize > 0 && pMaximumListSize > pListSize ) {
mMaximumSize = pMaximumListSize;
}
}
/**
* Handles the given notification by sending this to the remote
* client listener
*
* @param pNotification Notification to be send
* @param pHandback Handback object
*/
public void handleNotification(
Notification pNotification,
Object pHandback
) {
synchronized( mList ) {
if( mList.size() <= mMaximumSize ) {
mList.add( pNotification );
}
}
}
public List getNotifications() {
return getNotifications( mMaximumSize );
}
public List getNotifications( int pMaxiumSize ) {
List lReturn = null;
synchronized( mList ) {
pMaxiumSize = pMaxiumSize > mList.size() ? mList.size() : pMaxiumSize;
lReturn = new ArrayList( mList.subList( 0, pMaxiumSize ) );
mList.removeAll( lReturn );
}
return lReturn;
}
}
1.1
jboss/src/main/org/jboss/management/mejb/PollingNotificationListenerMBean.java
Index: PollingNotificationListenerMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.util.List;
import javax.management.Notification;
import javax.management.NotificationListener;
/**
* MBean Interface of a Notification Listener MBean
* using Polling to send the notifications back to the client.
* <br>
* This interface is only necessary because of the naming
* conventions for standard MBeans.
*
* @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
**/
public interface PollingNotificationListenerMBean
extends ListenerMBean
{
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
// Public --------------------------------------------------------
public List getNotifications();
public List getNotifications( int pMaxiumSize );
}
1.1
jboss/src/main/org/jboss/management/mejb/RMIClientNotificationListener.java
Index: RMIClientNotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.management.JMException;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import org.jboss.jmx.connector.RemoteMBeanServer;
/**
* Client-side RMI Listener to receive the message and send to the
* clients listener. Its stub is used on the server-side to hand
* the Notifications over to this class.
*
* @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
**/
public class RMIClientNotificationListener
extends ClientNotificationListener
implements RMIClientNotificationListenerInterface
{
public RMIClientNotificationListener(
ObjectName pSender,
NotificationListener pClientListener,
Object pHandback,
NotificationFilter pFilter,
MEJB pConnector
) throws
RemoteException,
JMException
{
super( pSender, pClientListener, pHandback );
// Export the RMI object to become a callback object
UnicastRemoteObject.exportObject( this );
// Register the listener as MBean on the remote JMX server
createListener(
pConnector,
"org.jboss.jmx.connector.notification.RMINotificationListener",
new Object[] { this },
new String[] { RMIClientNotificationListenerInterface.class.getName() }
);
addNotificationListener( pConnector, pFilter );
}
/**
* Handles the given notification by sending this to the remote
* client listener
*
* @param pNotification Notification to be send
* @param pHandback Handback object
**/
public void handleNotification(
Notification pNotification,
Object pHandback
) throws
RemoteException
{
mClientListener.handleNotification(
pNotification,
mHandback
);
}
}
1.1
jboss/src/main/org/jboss/management/mejb/RMIClientNotificationListenerInterface.java
Index: RMIClientNotificationListenerInterface.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import javax.management.Notification;
/**
* This Interface defines the methods in the RMI Stub
* transferred to the server-side.
*
* @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
**/
public interface RMIClientNotificationListenerInterface
extends Remote, Serializable
{
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
// Public --------------------------------------------------------
/**
* Handles the given notifcation event and passed it to the registered
* listener
*
* @param pNotification NotificationEvent
* @param pHandback Handback object
*
* @throws RemoteException If a Remote Exception occurred
*/
public void handleNotification(
Notification pNotification,
Object pHandback
) throws
RemoteException;
}
1.1
jboss/src/main/org/jboss/management/mejb/RMINotificationListener.java
Index: RMINotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import javax.management.Notification;
/**
* Notification Listener Implementation registered as
* MBean on the remote JMX Server and the added as
* Notification Listener on the remote JMX Server.
* Each notification received will be transfered to
* the remote client using RMI Callback Objects.
*
* @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
**/
public class RMINotificationListener
implements RMINotificationListenerMBean
{
// -------------------------------------------------------------------------
// Members
// -------------------------------------------------------------------------
private RMIClientNotificationListenerInterface mClientListener;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
/**
* Creates the RMI Notification Listener MBean implemenation which
* will be registered at the remote JMX Server as notificatin listener
* and then send the notification over the provided RMI Notification
* sender to the client
*
* @param pClientListener RMI-Stub used to transfer the Notification over
* the wire.
**/
public RMINotificationListener( RMIClientNotificationListenerInterface
pClientListener ) {
System.out.println( "RMINotificationListener(), client listener: " +
pClientListener );
mClientListener = pClientListener;
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Handles the given notifcation event and passed it to the registered
* RMI Notification Sender
*
* @param pNotification NotificationEvent
* @param pHandback Handback object
*/
public void handleNotification(
Notification pNotification,
Object pHandback
) {
try {
mClientListener.handleNotification( pNotification, pHandback );
}
catch( RemoteException re ) {
throw new RuntimeException( re.getMessage() );
}
}
/**
* Test if this and the given Object are equal. This is true if the given
* object both refer to the same local listener
*
* @param pTest Other object to test
if equal
*
* @return True if both are of
same type and
* refer to the
same local listener
**/
public boolean equals( Object pTest ) {
if( pTest instanceof RMINotificationListener ) {
return mClientListener.equals(
( (RMINotificationListener) pTest).mClientListener
);
}
return false;
}
/**
* @return Hashcode of the remote
listener
**/
public int hashCode() {
return mClientListener.hashCode();
}
}
1.1
jboss/src/main/org/jboss/management/mejb/RMINotificationListenerMBean.java
Index: RMINotificationListenerMBean.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import javax.management.Notification;
import javax.management.NotificationListener;
/**
* MBean Interface of a Notification Listener MBean
* using RMI Callback Objects to send the notifications
* back to the client.
* <br>
* This interface is only necessary because of the naming
* conventions for standard MBeans.
*
* @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
**/
public interface RMINotificationListenerMBean
extends ListenerMBean
{
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
// Public --------------------------------------------------------
}
1.1
jboss/src/main/org/jboss/management/mejb/SearchClientNotificationListener.java
Index: SearchClientNotificationListener.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.management.mejb;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import org.jboss.jmx.connector.RemoteMBeanServer;
/**
* Local Listener only to seach other Local Listeners
**/
public class SearchClientNotificationListener
extends ClientNotificationListener
{
public SearchClientNotificationListener(
ObjectName pSender,
NotificationListener pClientListener
) {
super( pSender, pClientListener, null );
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development