KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > interceptor > AbstractSharedInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.interceptor;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.management.InstanceAlreadyExistsException JavaDoc;
28 import javax.management.MBeanOperationInfo JavaDoc;
29 import javax.management.MBeanParameterInfo JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.modelmbean.ModelMBean JavaDoc;
34 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
35 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
36 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
37
38 import org.jboss.logging.Logger;
39 import org.jboss.mx.modelmbean.ModelMBeanConstants;
40 import org.jboss.mx.modelmbean.RequiredModelMBeanInstantiator;
41 import org.jboss.mx.server.Invocation;
42 import org.jboss.mx.service.ServiceConstants;
43 import org.jboss.mx.util.AgentID;
44
45
46 /**
47  * Base class for shared interceptors. This class provides some default method
48  * implementations for shared interceptors.
49  *
50  * @see org.jboss.mx.interceptor.SharedInterceptor
51  * @see org.jboss.mx.server.MBeanInvoker
52  *
53  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
54  * @version $Revision: 37459 $
55  */

56 public abstract class AbstractSharedInterceptor
57    extends AbstractInterceptor
58    implements SharedInterceptor
59 {
60
61    /**
62     * MBean server reference for shared interceptors.
63     */

64    protected MBeanServer JavaDoc server = null;
65    
66    /**
67     * Object name of this interceptor. Shared interceptors must always contain
68     * a valid object name.
69     */

70    protected ObjectName JavaDoc objectName = null;
71    
72    
73    // Constructors --------------------------------------------------
74

75    /**
76     * Constructs a new shared interceptor instance. The interceptor is not
77     * automatically registered to the MBean server. Notice that the interceptor
78     * name must be set before the call to {@link #register} method. Shared
79     * interceptor names must be unique within the MBean server.
80     */

81    public AbstractSharedInterceptor() {}
82    
83    /**
84     * Constructs a new shared interceptor instance with a given name. The interceptor
85     * is not automatically registered to the MBean server. Notice that the
86     * shared interceptor name must be unique name among all shared interceptors
87     * within the MBean server.
88     *
89     * @param name name of this interceptor
90     *
91     * @throws IllegalArgumentException if name contains <tt>null</tt> reference
92     */

93    public AbstractSharedInterceptor(String JavaDoc name)
94    {
95       super(name);
96    }
97    
98    
99    // SharedInterceptor implementation ------------------------------
100

101    public ObjectName JavaDoc getObjectName()
102    {
103       return objectName;
104    }
105    
106    public MBeanServer JavaDoc getMBeanServer()
107    {
108       return server;
109    }
110    
111    /**
112     * Registers the interceptor to the MBean server. <p>
113     *
114     * The interceptor is registered under the
115     * {@link org.jboss.mx.service.ServiceConstants#JBOSSMX_DOMAIN JBOSSMX_DOMAIN}
116     * name. An interceptor's object name contains a <tt>type</tt> property and
117     * a <tt>name</tt> property. Property <tt>type</tt> always contains string
118     * <tt>'Interceptor'</tt> as its value. Interceptor's name is used as a value
119     * for the <tt>name</tt> property. Therefore, an interceptor created with
120     * name <tt>'Bart'</tt> can be found from the MBean server under object name: <br><pre>
121     *
122     * {@link org.jboss.mx.service.ServiceConstants#JBOSSMX_DOMAIN JBOSSMX_DOMAIN}:type=Interceptor,name=Bart,*
123     *
124     * </pre>
125     *
126     * If the log reference has not been set for this interceptor when it is
127     * registered, this implementation will register a log MBean via the system
128     * log manager under {@link org.jboss.mx.service.ServiceConstants#JBOSSMX_DOMAIN JBOSSMX}
129     * domain (see {@link org.jboss.mx.logging.SystemLogManager SystemLogManager}
130     * for details). The log instance's name will match the pattern: <br><pre>
131     *
132     * "JBossMX.Interceptor.&lt;interceptor name&gt;"
133     *
134     * </pre>
135     *
136     * @param server MBean server where this shared interceptor is registered
137     */

138    public synchronized ObjectName JavaDoc register(MBeanServer JavaDoc server)
139          throws InterceptorNameConflictException
140    {
141       
142       // store MBean server reference
143
this.server = server;
144       
145       // check if log instance has been set
146
if (log == null)
147          log = Logger.getLogger("JBossMX.Interceptor." + name);
148          
149       try
150       {
151          // store the object name for later use
152
objectName = createObjectName();
153       
154          // query the server for this name
155
Set JavaDoc names = server.queryNames(objectName, null /* NO QUERY EXPR. */);
156          
157          // if the query returns a non empty set, throw an exception
158
if (names.size() > 0)
159             throw new InterceptorNameConflictException(
160                   "A shared interceptor named '" + name + "' already registered " +
161                   "to this MBean server (" + AgentID.get(server) + ")"
162             );
163             
164          // register the interceptor to server
165
ModelMBean JavaDoc rmm = RequiredModelMBeanInstantiator.instantiate();
166          rmm.setManagedResource(this, ModelMBeanConstants.OBJECT_REF);
167          rmm.setModelMBeanInfo(getManagementInterface());
168          server.registerMBean(rmm, objectName);
169          
170          // mark the interceptor as shared
171
isShared = true;
172       }
173       
174       catch (InstanceAlreadyExistsException JavaDoc e)
175       {
176          // we already checked that the instance doesn't exist with a query,
177
// however it is possible it was created by another thread before we
178
// actually had a chance to register
179
throw new InterceptorNameConflictException(
180                "A shared interceptor named '" + name + "' already registered " +
181                "to this MBean server (" + AgentID.get(server) + ")"
182          );
183       }
184       
185       catch (Exception JavaDoc e)
186       {
187          // anything else indicates there's something much more wrong if
188
// we can't register a simple MBean, so just log an error
189
if (log != null)
190             log.error(e.toString(), e);
191       }
192       
193       return objectName;
194    }
195
196    /**
197     * This method is part of the interceptor MBean's registration lifecycle.
198     * It is called before the MBean is registered to the server. Concrete
199     * interceptor implementations can override this method to provide
200     * initialization code that should be executed before the interceptor
201     * is registered. <p>
202     *
203     * Any exception that is propagated from this method to its caller will
204     * cancel the interceptor registration.
205     *
206     * @throws Exception if you want to cancel the interceptor registration
207     */

208    public void init() throws Exception JavaDoc {}
209
210    /**
211     * This method is part of the interceptor MBean's registration lifecycle.
212     * It is called after the MBean is registered to the server. Concrete
213     * interceptor implementations can override this method to provide
214     * initialization code that should be executed once the MBean server and
215     * object name references for this interceptor have been resolved.
216     */

217    public void start() {}
218
219    /**
220     * This method is part of the interceptor MBean's registration lifecycle.
221     * It is called before the MBean is unregistered from the server. Concrete
222     * interceptor implementations can override this method to provide
223     * cleanup code that should be executed before the interceptor is
224     * unregistered. <p>
225     *
226     * Any exception that is propagated from this method to its caller will
227     * cancel the interceptor unregistration.
228     *
229     * @throws Exception if you want to cancel the interceptor unregistration
230     */

231    public void stop() throws Exception JavaDoc {}
232
233    /**
234     * This method is part of the interceptor MBean's registration lifecycle.
235     * It is called after the MBean has been unregistered from the server. Concrete
236     * interceptor implementations can override this method to provide
237     * cleanup code that should be executed once the interceptor is no longer
238     * registered to the MBean server.
239     */

240    public void destroy() {}
241
242    
243    // MBeanRegistration implementation ------------------------------
244

245    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc oname) throws Exception JavaDoc
246    {
247       this.server = server;
248       
249       if (oname == null)
250          this.objectName = createObjectName();
251       else
252          this.objectName = oname;
253      
254       init();
255       
256       return objectName;
257    }
258    
259    public void postRegister(Boolean JavaDoc registrationSuccesful) {
260       isShared = true;
261       
262       start();
263    }
264    
265    public void preDeregister() throws Exception JavaDoc {
266       stop();
267
268       isShared = false;
269       objectName = null;
270    }
271    
272    public void postDeregister() {
273       destroy();
274    }
275    
276    
277    // AbstractInterceptor overrides ---------------------------------
278

279    /**
280     * Shared interceptors allows their name to be set only before they have
281     * been registered to the MBean server. After that the name is fixed and
282     * any attempt to invoke this method to change the name will yield a
283     * IllegalArgumentException.
284     *
285     * @param name name of this shared interceptor
286     *
287     * @throws IllegalArgumentException if there was an attempt to change the
288     * name after the interceptor had been registered to the server
289     */

290    public synchronized void setName(String JavaDoc name)
291    {
292       if (isShared())
293          throw new IllegalArgumentException JavaDoc("Cannot change the interceptor name. Already registered.");
294          
295       this.name = name;
296    }
297    
298    
299    // Object overrides ----------------------------------------------
300

301    /**
302     * Returns a string representation of this interceptor instance.
303     *
304     * @return string representation
305     */

306    public String JavaDoc toString()
307    {
308       String JavaDoc className = getClass().getName();
309       int index = className.lastIndexOf('.');
310       
311       return className.substring((index < 0) ? 0 : index) + "[" +
312              "name=" + name + "SHARED " + objectName + "]";
313    }
314    
315    
316    // Protected -----------------------------------------------------
317

318    /**
319     * Creates an object name for this interceptor. The object name contains a
320     * <tt>type</tt> property and a <tt>name</tt> property. Property <tt>type</tt>
321     * always contains string <tt>'Interceptor'</tt> as its value. Interceptor's
322     * name is used as a value for the <tt>name</tt> property. Therefore, an
323     * interceptor created with name <tt>'Bart'</tt> will generate an object name
324     * matching to pattern: <br><pre>
325     *
326     * {@link org.jboss.mx.service.ServiceConstants#JBOSSMX_DOMAIN JBOSSMX_DOMAIN}:type=Interceptor,name=Bart,*
327     *
328     * </pre>
329     *
330     * @return generated object name for this interceptor
331     *
332     * @throws MalformedObjectNameException if the object name could not be
333     * created
334     */

335    protected ObjectName JavaDoc createObjectName() throws MalformedObjectNameException JavaDoc
336    {
337       // create the object name for this shared interceptor
338
Hashtable JavaDoc props = new Hashtable JavaDoc(2);
339       props.put("type", "Interceptor");
340       props.put("name", name);
341       props.put("ID", "0");
342       
343       return new ObjectName JavaDoc(ServiceConstants.JBOSSMX_DOMAIN, props);
344    }
345
346    
347    // Private -------------------------------------------------------
348

349    private ModelMBeanInfo JavaDoc getManagementInterface()
350    {
351       return new ModelMBeanInfoSupport JavaDoc(
352          this.getClass().getName(), // resource object class name
353
"Interceptor invocation interface", // description of the MBean
354

355          null, // attributes
356

357          null, // constructors
358

359          new ModelMBeanOperationInfo JavaDoc[] // operations
360
{
361             new ModelMBeanOperationInfo JavaDoc(
362                   "invoke", // name
363
"Shared interceptor invoke operation.", // description
364
new MBeanParameterInfo JavaDoc[] // arguments
365
{
366                      new MBeanParameterInfo JavaDoc(
367                            "invocation", // name
368
Invocation.class.getName(), // type
369
"The invocation object." // description
370
)
371                   },
372                   Object JavaDoc.class.getName(), // return type
373
MBeanOperationInfo.ACTION_INFO // impact
374
)
375          },
376          
377          null // notifications
378
);
379    }
380    
381 }
382
383
384
Popular Tags