1 23 24 package com.sun.enterprise.management.agent; 25 26 import java.rmi.Naming ; 27 import java.rmi.RemoteException ; 28 import java.rmi.NotBoundException ; 29 import java.rmi.registry.Registry ; 30 import java.rmi.registry.LocateRegistry ; 31 import java.rmi.server.UnicastRemoteObject ; 32 33 import java.util.Hashtable ; 34 import java.util.Vector ; 35 import java.util.Iterator ; 36 37 import javax.management.*; 38 39 46 public class EventListenerProxy extends UnicastRemoteObject implements RemoteEventListener { 47 private Hashtable listenerTable = new Hashtable (); 48 private Hashtable handbackTable = new Hashtable (); 49 private static String proxyAddress; 50 private static EventListenerProxy eventProxy = null; 51 private static int portnum=1100; 52 private static String rmiName; 53 private static boolean debug = false; 54 55 public static EventListenerProxy getEventListenerProxy() { 56 if(eventProxy == null) { 57 try { 58 eventProxy = new EventListenerProxy(); 59 Naming.rebind(proxyAddress, eventProxy); 60 if(debug) System.out.println(rmiName + " bound to existing registry at port " + portnum ); 61 62 } catch (RemoteException re) { 63 if(debug) System.out.println("Naming.rebind("+ proxyAddress +", eventProxy): " + re); 64 try { 65 eventProxy = new EventListenerProxy(); 66 Registry r = LocateRegistry.createRegistry(portnum); 67 r.bind(rmiName, eventProxy); 68 if(debug) System.out.println(rmiName + " bound to newly created registry at port " + portnum ); 69 } catch(Exception e) { 70 eventProxy = null; 71 if(debug) e.printStackTrace(); 72 } 73 } catch (Exception e) { 74 if(debug) e.printStackTrace(); 75 } 76 } 77 return eventProxy; 78 } 79 80 public EventListenerProxy() throws java.rmi.RemoteException { 81 String hostName; 82 rmiName = "RemoteEventListener" + hashCode() + System.currentTimeMillis(); 83 try { 84 hostName = java.net.InetAddress.getLocalHost().getHostAddress(); 85 } catch (java.net.UnknownHostException e) { 86 hostName = "localhost"; 87 System.out.println(e); 88 } 89 proxyAddress = "//"+ hostName + ":" + portnum + "/" + rmiName; 90 } 91 92 public void handleNotification(Notification n, Object h) throws RemoteException { 93 if (debug) System.out.println("EventListenerProxy:handleNotification(" + n + ")"); 94 NotificationListener listener = (NotificationListener)listenerTable.get((String )h); 95 if (listener != null) { 96 Object handback = handbackTable.get((String )h); 97 listener.handleNotification(n,handback); 98 } else { 99 System.out.println("EventListenerProxy: listener id " + h + " not found"); 100 } 101 } 102 103 public String getProxyAddress() { 104 return proxyAddress; 105 } 106 107 public void addListener(String id, NotificationListener l, Object handback) { 108 if (debug) System.out.println("EventListenerProxy.addListener()"); 109 listenerTable.put(id, l); 110 handbackTable.put(id, handback); 111 } 112 113 public void removeListener(String id) throws ListenerNotFoundException { 114 if(listenerTable.remove(id) == null) { 115 throw new ListenerNotFoundException(); 116 } else { 117 handbackTable.remove(id); 118 } 119 } 120 } 121 | Popular Tags |