KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > server > interceptor > MBeanServerInterceptorConfigurator


1 /*
2  * Copyright (C) MX4J.
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.server.interceptor;
10
11 import java.util.ArrayList JavaDoc;
12
13 import javax.management.MBeanException JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 import mx4j.ImplementationException;
18
19 /**
20  * MBean that configures the MBeanServer --> MBean interceptor chain.
21  *
22  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
23  * @version $Revision: 1.7 $
24  */

25 public class MBeanServerInterceptorConfigurator implements MBeanServerInterceptorConfiguratorMBean
26 {
27     public static final String JavaDoc OBJECT_NAME = "JMImplementation:type=MBeanServerInterceptorConfigurator";
28
29     private final MBeanServer JavaDoc server;
30     private final ArrayList JavaDoc preInterceptors = new ArrayList JavaDoc();
31     private final ArrayList JavaDoc postInterceptors = new ArrayList JavaDoc();
32     private final ArrayList JavaDoc clientInterceptors = new ArrayList JavaDoc();
33     private volatile boolean running;
34    private boolean chainModified;
35    private MBeanServerInterceptor head;
36
37    /**
38      * Creates an instance of this configurator, for the given MBeanServer
39      */

40     public MBeanServerInterceptorConfigurator(MBeanServer JavaDoc server)
41     {
42         this.server = server;
43       chainModified = true;
44     }
45
46     /**
47      * Appends the given interceptor, provided by the client, to the existing interceptor chain.
48      * @see #registerInterceptor
49      */

50     public void addInterceptor(MBeanServerInterceptor interceptor)
51     {
52         synchronized (clientInterceptors)
53         {
54             clientInterceptors.add(interceptor);
55          chainModified = true;
56         }
57     }
58
59     /**
60      * Appends the given interceptor, provided by the client, to the existing interceptor chain and registers it as MBean.
61      * @see #addInterceptor
62      */

63     public void registerInterceptor(MBeanServerInterceptor interceptor, ObjectName JavaDoc name) throws MBeanException JavaDoc
64     {
65         // First, try register this interceptor. The call will use the old interceptor chain
66
try
67         {
68             server.registerMBean(interceptor, name);
69          addInterceptor(interceptor);
70         }
71         catch (Exception JavaDoc x)
72         {
73             throw new MBeanException JavaDoc(x, "Could not register interceptor with name " + name);
74         }
75     }
76
77     /**
78      * Removes all the interceptors added via {@link #addInterceptor(MBeanServerInterceptor interceptor)}.
79      * @see #addInterceptor
80      */

81     public void clearInterceptors()
82     {
83         synchronized (clientInterceptors)
84         {
85             clientInterceptors.clear();
86          chainModified = true;
87         }
88     }
89
90     /**
91      * Adds the given interceptor at the beginning of the interceptor chain, before the custom interceptors that may be added
92      * via {@link #addInterceptor}.
93      * This method is called by the MBeanServer during initialization, to configure the interceptors needed to work properly.
94      */

95     public void addPreInterceptor(MBeanServerInterceptor interceptor)
96     {
97         if (isRunning()) throw new ImplementationException();
98       preInterceptors.add(interceptor);
99     }
100
101     /**
102      * Adds the given interceptor at the end of the interceptor chain, after the custom interceptors that may be added
103      * via {@link #addInterceptor}.
104      * This method is called by the MBeanServer during initialization, to configure the interceptors needed to work properly.
105      */

106     public void addPostInterceptor(MBeanServerInterceptor interceptor)
107     {
108       if (isRunning()) throw new ImplementationException();
109       postInterceptors.add(interceptor);
110     }
111
112     /**
113      * Returns the head interceptor of the interceptor chain.
114      * The head interceptor is always present.
115      */

116     public MBeanServerInterceptor getHeadInterceptor()
117     {
118         if (!isRunning()) return null;
119
120       if (chainModified) setupChain();
121
122       return head;
123     }
124
125    private void setupChain()
126    {
127       chainModified = false;
128
129       int size = clientInterceptors.size();
130       ArrayList JavaDoc chain = new ArrayList JavaDoc(preInterceptors.size() + size + postInterceptors.size());
131       chain.addAll(preInterceptors);
132       if (size > 0)
133       {
134          synchronized (clientInterceptors)
135          {
136             chain.addAll(clientInterceptors);
137          }
138       }
139       chain.addAll(postInterceptors);
140
141       // Set the chain on the first interceptor
142
MBeanServerInterceptor first = (MBeanServerInterceptor)chain.get(0);
143       first.setChain(chain);
144
145       head = first;
146    }
147
148     /**
149      * Starts this configurator, so that the MBeanServer is now able to accept incoming calls.
150      * @see #stop
151      * @see #isRunning
152      */

153     public void start()
154     {
155         if (!isRunning())
156         {
157             running = true;
158         }
159     }
160
161     /**
162      * Stops this configurator, so that the MBeanServer is not able to accept incoming calls.
163      * @see #start
164      */

165     public void stop()
166     {
167         if (isRunning())
168         {
169             running = false;
170         }
171     }
172
173     /**
174      * Returns whether this configurator is running and thus if the MBeanServer can accept incoming calls
175      * @see #start
176      */

177     public boolean isRunning()
178     {
179         return running;
180     }
181 }
182
Popular Tags