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     */
017    package org.apache.geronimo.console;
018    
019    import java.lang.reflect.Method;
020    import java.lang.reflect.Modifier;
021    import javax.portlet.GenericPortlet;
022    import javax.portlet.PortletRequest;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    
026    import org.apache.geronimo.console.util.PortletManager;
027    import org.apache.geronimo.management.geronimo.WebContainer;
028    
029    /**
030     * Superclass with some generic functionality for console portlets
031     *
032     * @version $Rev: 562021 $ $Date: 2007-08-02 01:51:18 -0400 (Thu, 02 Aug 2007) $
033     */
034    public class BasePortlet extends GenericPortlet {
035        private final static Log log = LogFactory.getLog(BasePortlet.class);
036        protected final static String WEB_SERVER_JETTY = "jetty";
037        protected final static String WEB_SERVER_TOMCAT = "tomcat";
038        protected final static String WEB_SERVER_GENERIC = "generic";
039    
040        protected final static String getWebServerType(Class cls) {
041            Class[] intfs = cls.getInterfaces();
042            for (int i = 0; i < intfs.length; i++) {
043                Class intf = intfs[i];
044                if(intf.getName().indexOf("Jetty") > -1) {
045                    return WEB_SERVER_JETTY;
046                } else if(intf.getName().indexOf("Tomcat") > -1) {
047                    return WEB_SERVER_TOMCAT;
048                }
049            }
050            return WEB_SERVER_GENERIC;
051        }
052    
053        public final static void setProperty(Object target, String name, Object value) {
054            boolean found = false;
055            Class cls = target.getClass();
056            String setter = "set"+Character.toUpperCase(name.charAt(0))+name.substring(1);
057            Method[] list = cls.getMethods();
058            for (int i = 0; i < list.length; i++) {
059                Method method = list[i];
060                if(method.getName().equals(setter) && method.getParameterTypes().length == 1 && Modifier.isPublic(method.getModifiers()) &&
061                        !Modifier.isStatic(method.getModifiers())) {
062                    found = true;
063                    try {
064                        method.invoke(target, new Object[]{value});
065                    } catch (Exception e) {
066                        log.error("Unable to set property "+name+" on "+target.getClass().getName());
067                    }
068                    break;
069                }
070            }
071            if(!found) {
072                throw new IllegalArgumentException("No such method found ("+setter+" on "+target.getClass().getName()+")");
073            }
074        }
075    
076        public final static Object getProperty(Object target, String name) {
077            Class cls = target.getClass();
078            String getter = "get"+Character.toUpperCase(name.charAt(0))+name.substring(1);
079            String booleanGetter = "is"+Character.toUpperCase(name.charAt(0))+name.substring(1);
080            Method[] list = cls.getMethods();
081            for (int i = 0; i < list.length; i++) {
082                Method method = list[i];
083                String methodName = method.getName();
084                if( (methodName.equals(getter) || methodName.equals(booleanGetter))
085                    && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) &&
086                        !Modifier.isStatic(method.getModifiers())) {
087                    try {
088                        return method.invoke(target, new Object[0]);
089                    } catch (Exception e) {
090                        log.error("Unable to get property "+name+" on "+target.getClass().getName());
091                    }
092                    break;
093                }
094            }
095            throw new IllegalArgumentException("No such method found ("+getter+" on "+target.getClass().getName()+")");
096        }
097    
098        public final static Object callOperation(Object target, String operation, Object[] args) {
099            Class cls = target.getClass();
100            Method[] list = cls.getMethods();
101            for (int i = 0; i < list.length; i++) {
102                Method method = list[i];
103                if(method.getName().equals(operation) && ((args == null && method.getParameterTypes().length == 0) || (args != null && args.length == method.getParameterTypes().length))
104                        && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) {
105                    try {
106                        return method.invoke(target, args);
107                    } catch (Exception e) {
108                        log.error("Unable to invoke "+operation+" on "+target.getClass().getName());
109                    }
110                    break;
111                }
112            }
113            throw new IllegalArgumentException("No such method found ("+operation+" on "+target.getClass().getName()+")");
114        }
115    }