001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.console;
017:
018: import java.lang.reflect.Method;
019: import java.lang.reflect.Modifier;
020: import javax.portlet.GenericPortlet;
021: import javax.portlet.PortletRequest;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import org.apache.geronimo.console.util.PortletManager;
026: import org.apache.geronimo.management.geronimo.WebContainer;
027:
028: /**
029: * Superclass with some generic functionality for console portlets
030: *
031: * @version $Rev: 566146 $ $Date: 2007-08-15 06:43:23 -0700 (Wed, 15 Aug 2007) $
032: */
033: public class BasePortlet extends GenericPortlet {
034: private final static Log log = LogFactory.getLog(BasePortlet.class);
035: protected final static String WEB_SERVER_JETTY = "jetty";
036: protected final static String WEB_SERVER_TOMCAT = "tomcat";
037: protected final static String WEB_SERVER_GENERIC = "generic";
038:
039: protected final static String getWebServerType(Class cls) {
040: Class[] intfs = cls.getInterfaces();
041: for (int i = 0; i < intfs.length; i++) {
042: Class intf = intfs[i];
043: if (intf.getName().indexOf("Jetty") > -1) {
044: return WEB_SERVER_JETTY;
045: } else if (intf.getName().indexOf("Tomcat") > -1) {
046: return WEB_SERVER_TOMCAT;
047: }
048: }
049: return WEB_SERVER_GENERIC;
050: }
051:
052: public final static void setProperty(Object target, String name,
053: Object value) {
054: boolean found = false;
055: Class cls = target.getClass();
056: String setter = "set" + Character.toUpperCase(name.charAt(0))
057: + name.substring(1);
058: Method[] list = cls.getMethods();
059: for (int i = 0; i < list.length; i++) {
060: Method method = list[i];
061: if (method.getName().equals(setter)
062: && method.getParameterTypes().length == 1
063: && Modifier.isPublic(method.getModifiers())
064: && !Modifier.isStatic(method.getModifiers())) {
065: found = true;
066: try {
067: method.invoke(target, new Object[] { value });
068: } catch (Exception e) {
069: log.error("Unable to set property " + name + " on "
070: + target.getClass().getName());
071: }
072: break;
073: }
074: }
075: if (!found) {
076: throw new IllegalArgumentException("No such method found ("
077: + setter + " on " + target.getClass().getName()
078: + ")");
079: }
080: }
081:
082: public final static Object getProperty(Object target, String name) {
083: Class cls = target.getClass();
084: String getter = "get" + Character.toUpperCase(name.charAt(0))
085: + name.substring(1);
086: String booleanGetter = "is"
087: + Character.toUpperCase(name.charAt(0))
088: + name.substring(1);
089: Method[] list = cls.getMethods();
090: for (int i = 0; i < list.length; i++) {
091: Method method = list[i];
092: String methodName = method.getName();
093: if ((methodName.equals(getter) || methodName
094: .equals(booleanGetter))
095: && method.getParameterTypes().length == 0
096: && Modifier.isPublic(method.getModifiers())
097: && !Modifier.isStatic(method.getModifiers())) {
098: try {
099: return method.invoke(target, new Object[0]);
100: } catch (Exception e) {
101: log.error("Unable to get property " + name + " on "
102: + target.getClass().getName());
103: }
104: break;
105: }
106: }
107: throw new IllegalArgumentException("No such method found ("
108: + getter + " on " + target.getClass().getName() + ")");
109: }
110:
111: public final static Object callOperation(Object target,
112: String operation, Object[] args) {
113: Class cls = target.getClass();
114: Method[] list = cls.getMethods();
115: for (int i = 0; i < list.length; i++) {
116: Method method = list[i];
117: if (method.getName().equals(operation)
118: && ((args == null && method.getParameterTypes().length == 0) || (args != null && args.length == method
119: .getParameterTypes().length))
120: && Modifier.isPublic(method.getModifiers())
121: && !Modifier.isStatic(method.getModifiers())) {
122: try {
123: return method.invoke(target, args);
124: } catch (Exception e) {
125: log.error("Unable to invoke " + operation + " on "
126: + target.getClass().getName());
127: }
128: break;
129: }
130: }
131: throw new IllegalArgumentException("No such method found ("
132: + operation + " on " + target.getClass().getName()
133: + ")");
134: }
135: }
|