1 22 package org.jboss.ha.singleton; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.security.InvalidParameterException ; 27 28 import javax.management.InstanceNotFoundException ; 29 import javax.management.MBeanException ; 30 import javax.management.ObjectName ; 31 import javax.management.ReflectionException ; 32 33 53 public class HASingletonController extends HASingletonSupport 54 implements HASingletonControllerMBean 55 { 56 58 private ObjectName mSingletonMBean; 59 private Object mSingleton; 60 private String mSingletonStartMethod = "startSingleton"; 61 private String mSingletonStopMethod = "stopSingleton"; 62 private String mSingletonStartMethodArgument; 63 private String mSingletonStopMethodArgument; 64 65 private static final Object [] NO_ARGS = new Object [0]; 66 private static final String [] NO_TYPE_NAMES = new String [0]; 67 private static final Class [] NO_TYPES = new Class [0]; 68 69 71 74 public HASingletonController() 75 { 76 } 78 79 81 public Object getTarget() 82 { 83 return mSingleton; 84 } 85 86 public void setTarget(Object target) 87 { 88 this.mSingleton = target; 89 } 90 91 public ObjectName getTargetName() 92 { 93 return mSingletonMBean; 94 } 95 96 public void setTargetName(ObjectName targetObjectName) 97 { 98 this.mSingletonMBean = targetObjectName; 99 } 100 101 public String getTargetStartMethod() 102 { 103 return mSingletonStartMethod; 104 } 105 106 public void setTargetStartMethod(String targetStartMethod) 107 throws InvalidParameterException 108 { 109 if (targetStartMethod != null) 110 mSingletonStartMethod = targetStartMethod; 111 } 112 113 114 public String getTargetStopMethod() 115 { 116 return mSingletonStopMethod; 117 } 118 119 public void setTargetStopMethod(String targetStopMethod) 120 throws InvalidParameterException 121 { 122 if (targetStopMethod != null) 123 mSingletonStopMethod = targetStopMethod; 124 } 125 126 public String getTargetStartMethodArgument() 127 { 128 return mSingletonStartMethodArgument ; 129 } 130 131 public void setTargetStartMethodArgument(String targetStartMethodArgument) 132 { 133 mSingletonStartMethodArgument = targetStartMethodArgument; 134 } 135 136 public String getTargetStopMethodArgument() 137 { 138 return mSingletonStopMethodArgument ; 139 } 140 141 public void setTargetStopMethodArgument(String targetStopMethodArgument) 142 { 143 mSingletonStopMethodArgument = targetStopMethodArgument; 144 } 145 146 148 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 e) 181 { 182 log.error("Controlled Singleton failed to become master", e); 183 } 184 } 185 186 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 e) 219 { 220 log.error("Controlled Singleton failed to resign from master position", e); 221 } 222 } 223 224 226 protected Object invokeSingletonMethod(Object target, 227 String operationName, Object param) 228 throws IllegalAccessException , InvocationTargetException , NoSuchMethodException 229 { 230 if (target != null && operationName != null) 231 { 232 Object [] params; 233 Class [] types; 234 235 if (param != null) 236 { 237 params = new Object [] { param }; 238 types = new Class [] { 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 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 invokeSingletonMBeanMethod(ObjectName target, 265 String operationName, Object param) 266 throws InstanceNotFoundException , MBeanException , ReflectionException 267 { 268 if (target != null && operationName != null) 269 { 270 Object [] params; 271 String [] signature; 272 273 if (param != null) 274 { 275 params = new Object [] { param }; 276 signature = new String [] { 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 getTargetMethod(Object target, String methodName, Class [] types) 301 throws NoSuchMethodException 302 { 303 Class clazz = target.getClass(); 304 NoSuchMethodException nsme = null; 305 while (clazz != null) 306 { 307 try 308 { 309 Method method = clazz.getDeclaredMethod(methodName, types); 310 return method; 311 } 312 catch (NoSuchMethodException e) 313 { 314 if (nsme == null) 316 { 317 nsme = e; 318 } 319 clazz = clazz.getSuperclass(); 321 } 322 } 323 throw nsme; 324 } 325 } 326 | Popular Tags |