KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > services > binding > ServiceBindingManager


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.services.binding;
23
24 import java.net.URL JavaDoc;
25
26 import javax.management.Attribute JavaDoc;
27 import javax.management.MBeanRegistration JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.jboss.logging.Logger;
32 import org.jboss.mx.util.JMXExceptionDecoder;
33 import org.jboss.mx.util.MBeanProxyExt;
34 import org.jboss.system.ServiceControllerMBean;
35 import org.jboss.system.server.ServerConfigLocator;
36
37 /** The services configuration binding manager mbean implementation.
38  *
39  * <p>The ServiceBindingManager enables the centralized management
40  * of ports, by service. The port configuration store is abstracted out
41  * using the ServicesStore and ServicesStoreFactory interfaces. Note that
42  * this class does not implement the JBoss services lifecycle methods
43  * and hook its behavior off of those because this service is used to
44  * configure other services before any of the lifecycle methods are invoked.
45  *
46  * @version $Revision: 37459 $
47  * @author <a HREF="mailto:bitpushr@rochester.rr.com">Mike Finn</a>
48  * @author Scott.Stark@jboss.org
49  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
50  *
51  * @jmx:mbean
52  */

53 public class ServiceBindingManager
54    implements MBeanRegistration JavaDoc, ServiceBindingManagerMBean
55 {
56    private static Logger log = Logger.getLogger(ServiceBindingManager.class);
57
58    /** The MBeanServer we are registered
59     */

60    private MBeanServer JavaDoc server;
61    
62    /** The ObjectName of this MBean
63     */

64    private ObjectName JavaDoc serviceName;
65    
66    /** The name of the server this manager is associated with. This is a
67     logical name used to lookup ServiceConfigs from the ServicesStore.
68     */

69    private String JavaDoc serverName;
70    
71    /** The name of the class implementation for the ServicesStoreFactory. The
72     default value is org.jboss.services.binding.XMLServicesStoreFactory
73     */

74    private String JavaDoc storeFactoryClassName = "org.jboss.services.binding.XMLServicesStoreFactory";
75    
76    /** The ObjectName of the ServiceController we'll be registering at
77     */

78    private ObjectName JavaDoc serviceController;
79    
80    /** The ServiceConfig store instance
81     */

82    private ServicesStore store;
83    
84    /** The URL of the configuration store
85     */

86    private URL JavaDoc storeURL;
87
88    // Attributes ----------------------------------------------------
89

90    /**
91     * @jmx:managed-attribute
92     */

93    public String JavaDoc getServerName()
94    {
95       return this.serverName;
96    }
97
98    /**
99     * @jmx:managed-attribute
100     */

101    public void setServerName(String JavaDoc serverName)
102    {
103       this.serverName = serverName;
104    }
105
106    /**
107     * @jmx:managed-attribute
108     */

109    public String JavaDoc getStoreFactoryClassName()
110    {
111       return this.storeFactoryClassName;
112    }
113
114    /**
115     * @jmx:managed-attribute
116     */

117    public void setStoreFactoryClassName(String JavaDoc storeFactoryClassName)
118    {
119       this.storeFactoryClassName = storeFactoryClassName;
120    }
121
122    /**
123     * @jmx:managed-attribute
124     */

125    public URL JavaDoc getStoreURL()
126    {
127       return storeURL;
128    }
129
130    /** Set the string representation of the store URL
131     * @jmx:managed-attribute
132     */

133    public void setStoreURL(URL JavaDoc storeURL)
134    {
135       this.storeURL = storeURL;
136    }
137
138    /**
139     * Get the ObjectName of the ServiceController
140     * @jmx:managed-attribute
141     */

142    public ObjectName JavaDoc getServiceController()
143    {
144       return serviceController;
145    }
146    
147    /**
148     * Set the ObjectName of the ServiceController.
149     *
150     * @jmx:managed-attribute
151     *
152     * @param serviceController the name of the controller to register to
153     * @throws Exception if there is a problem registering with the controller
154     */

155    public void setServiceController(ObjectName JavaDoc serviceController) throws Exception JavaDoc
156    {
157       this.serviceController = serviceController;
158       
159       // try to register a proxy with the controller
160
registerProxy();
161    }
162    
163    // MBeanRegistration overrides -----------------------------------
164

165    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
166       throws Exception JavaDoc
167    {
168       this.server = server;
169       this.serviceName = name;
170       
171       return name;
172    }
173    
174    public void postRegister(Boolean JavaDoc registrationDone)
175    {
176       if (registrationDone.booleanValue())
177       {
178          // Register with the default ServiceController
179
try
180          {
181             setServiceController(ServiceControllerMBean.OBJECT_NAME);
182          }
183          catch (Exception JavaDoc ignore)
184          {
185             // Ignore any exception at this point; the user
186
// might expicitly set the controller objectname
187
}
188       }
189    }
190    
191    public void preDeregister()
192       throws Exception JavaDoc
193    {
194       // unregister with ServiceController
195
this.unregisterProxy();
196       
197       // shutdown store
198
if (store != null)
199       {
200          store.store(storeURL);
201       }
202    }
203    
204    public void postDeregister()
205    {
206       // empty
207
}
208
209    // Operations ----------------------------------------------------
210

211    /**
212     * Looks up the service config for the given service using the
213     * server name bound to this mbean.
214     *
215     * @jmx:managed-operation
216     *
217     * @param serviceName the JMX ObjectName of the service
218     * @return ServiceConfig instance if found, null otherwise
219     */

220    public ServiceConfig getServiceConfig(ObjectName JavaDoc serviceName)
221       throws Exception JavaDoc
222    {
223       log.debug("getServiceConfig, server:" + serverName + ", serviceName:" + serviceName);
224       
225       if (store == null)
226       {
227          initStore();
228       }
229
230       // Look up the service config
231
ServiceConfig svcConfig = store.getService(serverName, serviceName);
232       ServiceConfig configCopy = null;
233       if (svcConfig != null)
234       {
235          configCopy = (ServiceConfig) svcConfig.clone();
236       }
237       return configCopy;
238    }
239
240    /**
241     * Looks up the service config for the requested service using the
242     * server name bound to this mbean and invokes the configuration delegate
243     * to apply the bindings to the service. If no config if found then this
244     * method is a noop.
245     *
246     * @jmx:managed-operation
247     *
248     * @param serviceName the JMX ObjectName of the service
249     * @exception Exception, thrown on failure to apply an existing configuration
250     */

251    public void applyServiceConfig(ObjectName JavaDoc serviceName)
252       throws Exception JavaDoc
253    {
254       if (store == null)
255       {
256          initStore();
257       }
258       // Look up the service config
259
ServiceConfig svcConfig = store.getService(serverName, serviceName);
260       if (svcConfig != null)
261       {
262          log.debug("applyServiceConfig, server:" + serverName + ", serviceName:" + serviceName + ", config=" + svcConfig);
263
264          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
265          String JavaDoc delegateName = svcConfig.getServiceConfigDelegateClassName();
266          if (delegateName != null)
267          {
268             Class JavaDoc delegateClass = loader.loadClass(delegateName);
269             ServicesConfigDelegate delegate = (ServicesConfigDelegate) delegateClass.newInstance();
270             delegate.applyConfig(svcConfig, server);
271          }
272       }
273    }
274
275    // Private -------------------------------------------------------
276

277    /**
278     * Register a dynamic proxy of myself with the ServiceController
279     */

280    private void registerProxy() throws Exception JavaDoc
281    {
282       if (serviceController != null)
283       {
284          log.debug("Registering with ServiceController: " + serviceController);
285          
286          // Create a dynamic proxy pointing to me, over the MBeanServer
287
org.jboss.system.ServiceBinding proxy = (org.jboss.system.ServiceBinding)
288             MBeanProxyExt.create(org.jboss.system.ServiceBinding.class, serviceName, server);
289          
290          try
291          {
292             server.setAttribute(serviceController, new Attribute JavaDoc("ServiceBinding", proxy));
293          }
294          catch (Exception JavaDoc e)
295          {
296             // Log a debug message indicating we couldn't register and rethrow
297
Throwable JavaDoc t = JMXExceptionDecoder.decode(e);
298             log.debug("Failed to register with ServiceController: " + serviceController + ", reason: " + t.getMessage());
299             throw (Exception JavaDoc)t;
300          }
301       }
302    }
303    
304    /**
305     * Unregister with ServiceController
306     * by setting ServiceBinding to null
307     */

308    private void unregisterProxy()
309    {
310       if (serviceController != null)
311       {
312          log.debug("Unregistering with ServiceController: " + serviceController);
313          try
314          {
315             server.setAttribute(serviceController, new Attribute JavaDoc("ServiceBinding", null));
316          }
317          catch (Exception JavaDoc e)
318          {
319             // Log a debug message indicating we couldn't unregister
320
Throwable JavaDoc t = JMXExceptionDecoder.decode(e);
321             log.debug("Failed to unregister with ServiceController: " + serviceController + ", reason: " + t.getMessage());
322          }
323       }
324    }
325    
326    /**
327     * Create and load the services store
328     */

329    private void initStore() throws Exception JavaDoc
330    {
331       log.info("Initializing store");
332       
333       // If no store url identified, use the ServerConfigURL
334
if (this.storeURL == null)
335       {
336          this.storeURL = ServerConfigLocator.locate().getServerConfigURL();
337       }
338       log.info("Using StoreURL: "+storeURL);
339
340       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
341       Class JavaDoc factoryClass = loader.loadClass(storeFactoryClassName);
342       ServicesStoreFactory storeFactory = (ServicesStoreFactory) factoryClass.newInstance();
343
344       // Create and load the store
345
store = storeFactory.newInstance();
346       store.load(storeURL);
347    }
348 }
349
Popular Tags