KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > remote > rmi > RMIRemoteNotificationClientHandler


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.remote.rmi;
10
11 import java.io.IOException JavaDoc;
12 import java.security.AccessController JavaDoc;
13 import java.security.PrivilegedAction JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.management.remote.NotificationResult JavaDoc;
17 import javax.management.remote.rmi.RMIConnection JavaDoc;
18
19 import mx4j.remote.AbstractRemoteNotificationClientHandler;
20 import mx4j.remote.ConnectionNotificationEmitter;
21 import mx4j.remote.HeartBeat;
22
23 /**
24  * RMI-specific RemoteNotificationClientHandler.
25  *
26  * @version $Revision: 1.4 $
27  */

28 public class RMIRemoteNotificationClientHandler extends AbstractRemoteNotificationClientHandler
29 {
30    private final RMIConnection JavaDoc connection;
31    private final ClassLoader JavaDoc defaultLoader;
32
33    public RMIRemoteNotificationClientHandler(RMIConnection JavaDoc connection, ClassLoader JavaDoc defaultLoader, ConnectionNotificationEmitter emitter, HeartBeat heartbeat, Map JavaDoc environment)
34    {
35       super(emitter, heartbeat, environment);
36       this.connection = connection;
37       this.defaultLoader = defaultLoader;
38    }
39
40    protected NotificationResult JavaDoc fetchNotifications(long sequence, int maxNumber, long timeout) throws IOException JavaDoc
41    {
42       ClassLoader JavaDoc currentLoader = Thread.currentThread().getContextClassLoader();
43       if (defaultLoader == null || defaultLoader.equals(currentLoader))
44       {
45          return invokeFetchNotifications(sequence, maxNumber, timeout);
46       }
47       else
48       {
49          try
50          {
51             setContextClassLoader(defaultLoader);
52             return invokeFetchNotifications(sequence, maxNumber, timeout);
53          }
54          finally
55          {
56             setContextClassLoader(currentLoader);
57          }
58       }
59    }
60
61    private NotificationResult JavaDoc invokeFetchNotifications(long sequence, int maxNumber, long timeout) throws IOException JavaDoc
62    {
63       return connection.fetchNotifications(sequence, maxNumber, timeout);
64    }
65
66    private void setContextClassLoader(final ClassLoader JavaDoc loader)
67    {
68       AccessController.doPrivileged(new PrivilegedAction JavaDoc()
69       {
70          public Object JavaDoc run()
71          {
72             Thread.currentThread().setContextClassLoader(loader);
73             return null;
74          }
75       });
76    }
77 }
78
Popular Tags