1 22 package org.jboss.test.jmx.ha; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.UndeclaredThrowableException ; 27 import java.security.Principal ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import org.jboss.ha.jmx.HAServiceMBeanSupport; 33 import org.jboss.invocation.Invocation; 34 import org.jboss.invocation.MarshalledInvocation; 35 import org.jboss.security.SecurityAssociation; 36 import org.jboss.system.Registry; 37 38 public class HAService 39 extends HAServiceMBeanSupport 40 implements HAServiceRemote, HAServiceMBean 41 { 42 private int count = 0; 43 44 private Map marshalledInvocationMapping; 45 46 public void startService() 47 throws Exception 48 { 49 super.startService(); 50 51 Method [] methods = HAServiceRemote.class.getMethods(); 53 HashMap tmpMap = new HashMap (methods.length); 54 for(int m = 0; m < methods.length; m ++) 55 { 56 Method method = methods[m]; 57 Long hash = new Long (MarshalledInvocation.calculateHash(method)); 58 tmpMap.put(hash, method); 59 } 60 marshalledInvocationMapping = Collections.unmodifiableMap(tmpMap); 61 62 Registry.bind(new Integer (serviceName.hashCode()), serviceName); 64 } 65 66 public void stopService() 67 throws Exception 68 { 69 super.stopService(); 70 71 Registry.unbind(new Integer (serviceName.hashCode())); 73 } 74 75 78 public Map getMethodMap() 79 { 80 return marshalledInvocationMapping; 81 } 82 83 86 public Object invoke(Invocation invocation) 87 throws Exception 88 { 89 if (invocation instanceof MarshalledInvocation) 91 { 92 MarshalledInvocation mi = (MarshalledInvocation) invocation; 93 mi.setMethodMap(marshalledInvocationMapping); 94 } 95 Method method = invocation.getMethod(); 96 Object [] args = invocation.getArguments(); 97 98 Principal principal = invocation.getPrincipal(); 100 Object credential = invocation.getCredential(); 101 SecurityAssociation.setPrincipal(principal); 102 SecurityAssociation.setCredential(credential); 103 104 try 106 { 107 return method.invoke(this, args); 108 } 109 catch(InvocationTargetException e) 110 { 111 Throwable t = e.getTargetException(); 112 if( t instanceof Exception ) 113 throw (Exception ) t; 114 else 115 throw new UndeclaredThrowableException (t, method.toString()); 116 } 117 finally 118 { 119 SecurityAssociation.clear(); 121 } 122 } 123 124 126 public String hello() 127 { 128 return "Hello"; 129 } 130 131 public String getClusterNode() 132 { 133 return getPartition().getNodeName(); 134 } 135 136 } 137 | Popular Tags |