1 16 package org.jmanage.core.management; 17 18 import org.jmanage.core.util.Loggers; 19 20 import java.util.List ; 21 import java.util.LinkedList ; 22 import java.util.logging.Logger ; 23 import java.util.logging.Level ; 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.InvocationTargetException ; 27 28 36 public class ServerConnectionProxy implements InvocationHandler { 37 38 private static final Logger logger = 39 Loggers.getLogger(ServerConnectionProxy.class); 40 41 private ServerConnection connection; 42 private ClassLoader classLoader; 43 44 public ServerConnectionProxy(ServerConnection connection, 45 ClassLoader classLoader){ 46 this.connection = connection; 47 this.classLoader = classLoader; 48 } 49 50 public Object invoke(Object proxy, Method method, Object [] args) 51 throws Throwable { 52 53 final ClassLoader contextClassLoader = 54 Thread.currentThread().getContextClassLoader(); 55 try { 56 57 58 Thread.currentThread().setContextClassLoader(classLoader); 59 60 if(method.getName().equals("getAttributes")){ 61 assert args.length == 2; 62 return getAttributes((ObjectName)args[0], (String [])args[1]); 63 }else{ 64 return method.invoke(connection, args); 65 } 66 } catch (IllegalAccessException e) { 67 throw new RuntimeException (e); 68 } catch (IllegalArgumentException e) { 69 throw new RuntimeException (e); 70 } catch (InvocationTargetException e) { 71 throw e.getCause(); 72 } finally { 73 75 Thread.currentThread().setContextClassLoader(contextClassLoader); 76 } 77 } 78 79 87 private List getAttributes(ObjectName objectName, String [] attributeNames) { 88 89 91 List attributeList = new LinkedList (); 92 for(int i=0; i<attributeNames.length; i++){ 93 attributeList.add(getAttribute(objectName, attributeNames[i])); 94 } 95 return attributeList; 96 } 97 98 private ObjectAttribute getAttribute(ObjectName objectName, 99 String attributeName){ 100 101 try { 102 List attrList = connection.getAttributes(objectName, 105 new String []{attributeName}); 106 if(attrList.size() > 0) 107 return (ObjectAttribute)attrList.get(0); 108 } catch (Exception e) { 109 String msg = "Error retriving attribute=" + 110 attributeName + ", objectName=" + objectName; 111 logger.log(Level.WARNING, msg); 112 logger.log(Level.FINE, msg, e); 113 return new ObjectAttribute(attributeName, 114 ObjectAttribute.STATUS_ERROR, e.getMessage()); 115 } 116 return new ObjectAttribute(attributeName, 117 ObjectAttribute.STATUS_NOT_FOUND, null); 118 } 119 } 120 | Popular Tags |