KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > ee > jmx > jboss > JBoss4RMIRemoteMBeanScheduler


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.ee.jmx.jboss;
17
18 import org.quartz.SchedulerException;
19 import org.quartz.impl.RemoteMBeanScheduler;
20
21 import javax.management.AttributeList JavaDoc;
22 import javax.management.MBeanServerConnection JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * <p>
31  * An implementation of the <code>Scheduler</code> interface that remotely
32  * proxies all method calls to the equivalent call on a given <code>QuartzScheduler</code>
33  * instance, via JBoss's JMX RMIAdaptor.
34  * </p>
35  *
36  * <p>
37  * Set the <b>providerURL</b> property to your MBean server URL.
38  * This defaults to: <i>jnp://localhost:1099</i>
39  * </p>
40  *
41  * @see org.quartz.Scheduler
42  * @see org.quartz.core.QuartzScheduler
43  * @see org.quartz.core.SchedulingContext
44  */

45 public class JBoss4RMIRemoteMBeanScheduler extends RemoteMBeanScheduler {
46
47     private static final String JavaDoc DEFAULT_PROVIDER_URL = "jnp://localhost:1099";
48     private static final String JavaDoc RMI_ADAPTOR_JNDI_NAME = "jmx/rmi/RMIAdaptor";
49     
50     private MBeanServerConnection JavaDoc server = null;
51     private String JavaDoc providerURL = DEFAULT_PROVIDER_URL;
52     
53     public JBoss4RMIRemoteMBeanScheduler() throws SchedulerException {
54     }
55     
56
57     /**
58      * Set the remote MBean server URL.
59      *
60      * Defaults to: <i>jnp://localhost:1099</i>
61      */

62     public void setProviderURL(String JavaDoc providerURL) {
63         this.providerURL = providerURL;
64     }
65
66     /**
67      * Initialize this remote MBean scheduler, getting the JBoss
68      * RMIAdaptor for communication.
69      */

70     public void initialize() throws SchedulerException {
71         InitialContext JavaDoc ctx = null;
72         try {
73             ctx = new InitialContext JavaDoc(getContextProperties());
74             server = (MBeanServerConnection JavaDoc)ctx.lookup(RMI_ADAPTOR_JNDI_NAME);
75         } catch (Exception JavaDoc e) {
76             throw new SchedulerException("Failed to lookup JBoss JMX RMI Adaptor.", e);
77         } finally {
78             if (ctx != null) {
79                 try {
80                     ctx.close();
81                 } catch (NamingException JavaDoc ignore) {
82                 }
83             }
84         }
85     }
86     
87     /**
88      * Get the properties to use when creating a JNDI InitialContext.
89      *
90      * <p>
91      * This method is broken out so it can be extended to pass credentials
92      * or other properties not currently supported.
93      * </p>
94      */

95     protected Properties JavaDoc getContextProperties() {
96         Properties JavaDoc props = new Properties JavaDoc();
97         props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
98         props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
99         props.put(Context.PROVIDER_URL, providerURL);
100         
101         return props;
102     }
103
104     protected Object JavaDoc getAttribute(String JavaDoc attribute) throws SchedulerException {
105         try {
106             return server.getAttribute(getSchedulerObjectName(), attribute);
107         } catch (Exception JavaDoc e) {
108             throw new SchedulerException("Failed to get Scheduler MBean attribute: " + attribute, e);
109         }
110     }
111
112     protected AttributeList JavaDoc getAttributes(String JavaDoc[] attributes) throws SchedulerException {
113         try {
114             return server.getAttributes(getSchedulerObjectName(), attributes);
115         } catch (Exception JavaDoc e) {
116             throw new SchedulerException("Failed to get Scheduler MBean attributes: " + Arrays.asList(attributes), e);
117         }
118     }
119
120     protected Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc[] params,
121             String JavaDoc[] signature) throws SchedulerException {
122         try {
123             return server.invoke(getSchedulerObjectName(), operationName, params, signature);
124         } catch (Exception JavaDoc e) {
125             throw new SchedulerException(
126                 "Failed to invoke Scheduler MBean operation: " + operationName, e);
127         }
128     }
129 }
130
Popular Tags