1 17 package org.apache.geronimo.console; 18 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Modifier ; 21 import javax.portlet.GenericPortlet; 22 import javax.portlet.PortletRequest; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.apache.geronimo.console.util.PortletManager; 27 import org.apache.geronimo.management.geronimo.WebContainer; 28 29 34 public class BasePortlet extends GenericPortlet { 35 private final static Log log = LogFactory.getLog(BasePortlet.class); 36 protected final static String WEB_SERVER_JETTY = "jetty"; 37 protected final static String WEB_SERVER_TOMCAT = "tomcat"; 38 protected final static String WEB_SERVER_GENERIC = "generic"; 39 40 protected final static String getWebServerType(Class cls) { 41 Class [] intfs = cls.getInterfaces(); 42 for (int i = 0; i < intfs.length; i++) { 43 Class intf = intfs[i]; 44 if(intf.getName().indexOf("Jetty") > -1) { 45 return WEB_SERVER_JETTY; 46 } else if(intf.getName().indexOf("Tomcat") > -1) { 47 return WEB_SERVER_TOMCAT; 48 } 49 } 50 return WEB_SERVER_GENERIC; 51 } 52 53 public final static void setProperty(Object target, String name, Object value) { 54 boolean found = false; 55 Class cls = target.getClass(); 56 String setter = "set"+Character.toUpperCase(name.charAt(0))+name.substring(1); 57 Method [] list = cls.getMethods(); 58 for (int i = 0; i < list.length; i++) { 59 Method method = list[i]; 60 if(method.getName().equals(setter) && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) && 61 !Modifier.isStatic(method.getModifiers())) { 62 found = true; 63 try { 64 method.invoke(target, new Object []{value}); 65 } catch (Exception e) { 66 log.error("Unable to set property "+name+" on "+target.getClass().getName()); 67 } 68 break; 69 } 70 } 71 if(!found) { 72 throw new IllegalArgumentException ("No such method found ("+setter+" on "+target.getClass().getName()+")"); 73 } 74 } 75 76 public final static Object getProperty(Object target, String name) { 77 Class cls = target.getClass(); 78 String getter = "get"+Character.toUpperCase(name.charAt(0))+name.substring(1); 79 Method [] list = cls.getMethods(); 80 for (int i = 0; i < list.length; i++) { 81 Method method = list[i]; 82 if(method.getName().equals(getter) && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) && 83 !Modifier.isStatic(method.getModifiers())) { 84 try { 85 return method.invoke(target, new Object [0]); 86 } catch (Exception e) { 87 log.error("Unable to get property "+name+" on "+target.getClass().getName()); 88 } 89 break; 90 } 91 } 92 throw new IllegalArgumentException ("No such method found ("+getter+" on "+target.getClass().getName()+")"); 93 } 94 95 public final static Object callOperation(Object target, String operation, Object [] args) { 96 Class cls = target.getClass(); 97 Method [] list = cls.getMethods(); 98 for (int i = 0; i < list.length; i++) { 99 Method method = list[i]; 100 if(method.getName().equals(operation) && ((args == null && method.getParameterTypes().length == 0) || (args != null && args.length == method.getParameterTypes().length)) 101 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { 102 try { 103 return method.invoke(target, args); 104 } catch (Exception e) { 105 log.error("Unable to invoke "+operation+" on "+target.getClass().getName()); 106 } 107 break; 108 } 109 } 110 throw new IllegalArgumentException ("No such method found ("+operation+" on "+target.getClass().getName()+")"); 111 } 112 } 113 | Popular Tags |