KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > singleton > HASingletonController


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.ha.singleton;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.security.InvalidParameterException JavaDoc;
27
28 import javax.management.InstanceNotFoundException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.ReflectionException JavaDoc;
32
33 /**
34  * A clustered singleton service that calls a configurable
35  * method on a target mbean, whenever the current node becomes
36  * the master. Correspondigly, it calls a configurable method
37  * on the target mbean, whenever the current node resigns from
38  * being the master.
39  *
40  * Optional string arguments may be passed to those methods.
41  *
42  * The prevailing usage of this MBean is to deploy on the
43  * master node the content of the deploy-hasingleton directory.
44  *
45  * @author <a HREF="mailto:ivelin@apache.org">Ivelin Ivanov</a>
46  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
47  * @author <a HREF="mailto:mr@gedoplan.de">Marcus Redeker</a>
48  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
49  * @author Brian Stansberry
50  *
51  * @version $Revision: 58563 $
52  */

53 public class HASingletonController extends HASingletonSupport
54    implements HASingletonControllerMBean
55 {
56    // Private Data --------------------------------------------------
57

58    private ObjectName JavaDoc mSingletonMBean;
59    private Object JavaDoc mSingleton;
60    private String JavaDoc mSingletonStartMethod = "startSingleton";
61    private String JavaDoc mSingletonStopMethod = "stopSingleton";
62    private String JavaDoc mSingletonStartMethodArgument;
63    private String JavaDoc mSingletonStopMethodArgument;
64
65    private static final Object JavaDoc[] NO_ARGS = new Object JavaDoc[0];
66    private static final String JavaDoc[] NO_TYPE_NAMES = new String JavaDoc[0];
67    private static final Class JavaDoc[] NO_TYPES = new Class JavaDoc[0];
68
69    // Constructors --------------------------------------------------
70

71    /**
72     * Default CTOR
73     */

74    public HASingletonController()
75    {
76       // empty
77
}
78    
79    // Attributes ----------------------------------------------------
80

81    public Object JavaDoc getTarget()
82    {
83       return mSingleton;
84    }
85    
86    public void setTarget(Object JavaDoc target)
87    {
88       this.mSingleton = target;
89    }
90    
91    public ObjectName JavaDoc getTargetName()
92    {
93       return mSingletonMBean;
94    }
95
96    public void setTargetName(ObjectName JavaDoc targetObjectName)
97    {
98       this.mSingletonMBean = targetObjectName;
99    }
100
101    public String JavaDoc getTargetStartMethod()
102    {
103       return mSingletonStartMethod;
104    }
105
106    public void setTargetStartMethod(String JavaDoc targetStartMethod)
107       throws InvalidParameterException JavaDoc
108    {
109       if (targetStartMethod != null)
110          mSingletonStartMethod = targetStartMethod;
111    }
112
113
114    public String JavaDoc getTargetStopMethod()
115    {
116       return mSingletonStopMethod;
117    }
118
119    public void setTargetStopMethod(String JavaDoc targetStopMethod)
120       throws InvalidParameterException JavaDoc
121    {
122       if (targetStopMethod != null)
123          mSingletonStopMethod = targetStopMethod;
124    }
125
126    public String JavaDoc getTargetStartMethodArgument()
127    {
128       return mSingletonStartMethodArgument ;
129    }
130
131    public void setTargetStartMethodArgument(String JavaDoc targetStartMethodArgument)
132    {
133       mSingletonStartMethodArgument = targetStartMethodArgument;
134    }
135
136    public String JavaDoc getTargetStopMethodArgument()
137    {
138       return mSingletonStopMethodArgument ;
139    }
140
141    public void setTargetStopMethodArgument(String JavaDoc targetStopMethodArgument)
142    {
143       mSingletonStopMethodArgument = targetStopMethodArgument;
144    }
145   
146    // HASingleton implementation ------------------------------------
147

148    /**
149     * Call the target start method
150     *
151     * @see org.jboss.ha.singleton.HASingletonSupport#startSingleton()
152     */

153    public void startSingleton()
154    {
155       super.startSingleton();
156
157       try
158       {
159          if (mSingleton != null)
160          {
161             invokeSingletonMethod(
162                   mSingleton,
163                   mSingletonStartMethod,
164                   mSingletonStartMethodArgument
165                   );
166          }
167          else if (mSingletonMBean != null)
168          {
169             invokeSingletonMBeanMethod(
170                mSingletonMBean,
171                mSingletonStartMethod,
172                mSingletonStartMethodArgument
173                );
174          }
175          else
176          {
177             log.warn("No singleton configured; cannot start");
178          }
179       }
180       catch (Exception JavaDoc e)
181       {
182          log.error("Controlled Singleton failed to become master", e);
183       }
184    }
185
186    /**
187     * Call the target stop method
188     *
189     * @see org.jboss.ha.singleton.HASingletonSupport#stopSingleton()
190     */

191    public void stopSingleton()
192    {
193       super.stopSingleton();
194
195       try
196       {
197          if (mSingleton != null)
198          {
199             invokeSingletonMethod(
200                   mSingleton,
201                   mSingletonStopMethod,
202                   mSingletonStopMethodArgument
203                   );
204          }
205          else if (mSingletonMBean != null)
206          {
207             invokeSingletonMBeanMethod(
208                mSingletonMBean,
209                mSingletonStopMethod,
210                mSingletonStopMethodArgument
211                );
212          }
213          else
214          {
215             log.warn("No singleton configured; cannot start");
216          }
217       }
218       catch (Exception JavaDoc e)
219       {
220          log.error("Controlled Singleton failed to resign from master position", e);
221       }
222    }
223
224    // Protected -----------------------------------------------------
225

226    protected Object JavaDoc invokeSingletonMethod(Object JavaDoc target,
227       String JavaDoc operationName, Object JavaDoc param)
228       throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc
229    {
230       if (target != null && operationName != null)
231       {
232          Object JavaDoc[] params;
233          Class JavaDoc[] types;
234          
235          if (param != null)
236          {
237             params = new Object JavaDoc[] { param };
238             types = new Class JavaDoc[] { param.getClass() };
239             
240             log.debug("Calling operation: " + operationName +
241                   "(" + param + "), on target: '" + target + "'");
242          }
243          else
244          {
245             params = NO_ARGS;
246             types = NO_TYPES;
247             
248             log.debug("Calling operation: " + operationName +
249                   "(), on target: '" + target + "'");
250          }
251          
252          Method JavaDoc method = getTargetMethod(target, operationName, types);
253          
254          return method.invoke(target, params);
255       }
256       else
257       {
258          log.debug("No configured target mbean or operation to call");
259          
260          return null;
261       }
262    }
263    
264    protected Object JavaDoc invokeSingletonMBeanMethod(ObjectName JavaDoc target,
265       String JavaDoc operationName, Object JavaDoc param)
266       throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
267    {
268       if (target != null && operationName != null)
269       {
270          Object JavaDoc[] params;
271          String JavaDoc[] signature;
272          
273          if (param != null)
274          {
275             params = new Object JavaDoc[] { param };
276             signature = new String JavaDoc[] { param.getClass().getName() };
277             
278             log.debug("Calling operation: " + operationName +
279                   "(" + param + "), on target: '" + target + "'");
280          }
281          else
282          {
283             params = NO_ARGS;
284             signature = NO_TYPE_NAMES;
285             
286             log.debug("Calling operation: " + operationName +
287                   "(), on target: '" + target + "'");
288          }
289
290          return server.invoke(target, operationName, params, signature);
291       }
292       else
293       {
294          log.debug("No configured target mbean or operation to call");
295          
296          return null;
297       }
298    }
299    
300    public static Method JavaDoc getTargetMethod(Object JavaDoc target, String JavaDoc methodName, Class JavaDoc[] types)
301          throws NoSuchMethodException JavaDoc
302    {
303       Class JavaDoc clazz = target.getClass();
304       NoSuchMethodException JavaDoc nsme = null;
305       while (clazz != null)
306       {
307          try
308          {
309             Method JavaDoc method = clazz.getDeclaredMethod(methodName, types);
310             return method;
311          }
312          catch (NoSuchMethodException JavaDoc e)
313          {
314             // Cache the one from the top level class
315
if (nsme == null)
316             {
317                nsme = e;
318             }
319            // Keep searching
320
clazz = clazz.getSuperclass();
321          }
322       }
323       throw nsme;
324    }
325 }
326
Popular Tags