KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > agent > EventListenerProxy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.management.agent;
25
26 import java.rmi.Naming JavaDoc;
27 import java.rmi.RemoteException JavaDoc;
28 import java.rmi.NotBoundException JavaDoc;
29 import java.rmi.registry.Registry JavaDoc;
30 import java.rmi.registry.LocateRegistry JavaDoc;
31 import java.rmi.server.UnicastRemoteObject JavaDoc;
32
33 import java.util.Hashtable JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import javax.management.*;
38
39 /**
40  * The EventListenerProxy recieves notifications from RemoteListenerConnectors
41  * registered on the server managed objects and forwards them to the corresponding
42  * local listeners
43  *
44  * @author Hans Hrasna
45  */

46 public class EventListenerProxy extends UnicastRemoteObject JavaDoc implements RemoteEventListener {
47     private Hashtable JavaDoc listenerTable = new Hashtable JavaDoc();
48     private Hashtable JavaDoc handbackTable = new Hashtable JavaDoc();
49     private static String JavaDoc proxyAddress;
50     private static EventListenerProxy eventProxy = null;
51     private static int portnum=1100;
52     private static String JavaDoc 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 JavaDoc re) {
63                 if(debug) System.out.println("Naming.rebind("+ proxyAddress +", eventProxy): " + re);
64                 try {
65                     eventProxy = new EventListenerProxy();
66                     Registry JavaDoc 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 JavaDoc e) {
70                     eventProxy = null;
71                     if(debug) e.printStackTrace();
72                 }
73             } catch (Exception JavaDoc e) {
74                 if(debug) e.printStackTrace();
75             }
76         }
77         return eventProxy;
78     }
79
80     public EventListenerProxy() throws java.rmi.RemoteException JavaDoc {
81         String JavaDoc hostName;
82         rmiName = "RemoteEventListener" + hashCode() + System.currentTimeMillis();
83         try {
84             hostName = java.net.InetAddress.getLocalHost().getHostAddress();
85         } catch (java.net.UnknownHostException JavaDoc e) {
86             hostName = "localhost";
87             System.out.println(e);
88         }
89         proxyAddress = "//"+ hostName + ":" + portnum + "/" + rmiName;
90     }
91
92     public void handleNotification(Notification n, Object JavaDoc h) throws RemoteException JavaDoc {
93         if (debug) System.out.println("EventListenerProxy:handleNotification(" + n + ")");
94         NotificationListener listener = (NotificationListener)listenerTable.get((String JavaDoc)h);
95         if (listener != null) {
96             Object JavaDoc handback = handbackTable.get((String JavaDoc)h);
97             listener.handleNotification(n,handback);
98         } else {
99             System.out.println("EventListenerProxy: listener id " + h + " not found");
100         }
101     }
102
103     public String JavaDoc getProxyAddress() {
104         return proxyAddress;
105     }
106
107     public void addListener(String JavaDoc id, NotificationListener l, Object JavaDoc 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 JavaDoc 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