KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > BasePortlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.console;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
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 /**
30  * Superclass with some generic functionality for console portlets
31  *
32  * @version $Rev: 476321 $ $Date: 2006-11-17 16:18:49 -0500 (Fri, 17 Nov 2006) $
33  */

34 public class BasePortlet extends GenericPortlet {
35     private final static Log log = LogFactory.getLog(BasePortlet.class);
36     protected final static String JavaDoc WEB_SERVER_JETTY = "jetty";
37     protected final static String JavaDoc WEB_SERVER_TOMCAT = "tomcat";
38     protected final static String JavaDoc WEB_SERVER_GENERIC = "generic";
39
40     protected final static String JavaDoc getWebServerType(Class JavaDoc cls) {
41         Class JavaDoc[] intfs = cls.getInterfaces();
42         for (int i = 0; i < intfs.length; i++) {
43             Class JavaDoc 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 JavaDoc target, String JavaDoc name, Object JavaDoc value) {
54         boolean found = false;
55         Class JavaDoc cls = target.getClass();
56         String JavaDoc setter = "set"+Character.toUpperCase(name.charAt(0))+name.substring(1);
57         Method JavaDoc[] list = cls.getMethods();
58         for (int i = 0; i < list.length; i++) {
59             Method JavaDoc 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 JavaDoc[]{value});
65                 } catch (Exception JavaDoc 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 JavaDoc("No such method found ("+setter+" on "+target.getClass().getName()+")");
73         }
74     }
75
76     public final static Object JavaDoc getProperty(Object JavaDoc target, String JavaDoc name) {
77         Class JavaDoc cls = target.getClass();
78         String JavaDoc getter = "get"+Character.toUpperCase(name.charAt(0))+name.substring(1);
79         Method JavaDoc[] list = cls.getMethods();
80         for (int i = 0; i < list.length; i++) {
81             Method JavaDoc 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 JavaDoc[0]);
86                 } catch (Exception JavaDoc e) {
87                     log.error("Unable to get property "+name+" on "+target.getClass().getName());
88                 }
89                 break;
90             }
91         }
92         throw new IllegalArgumentException JavaDoc("No such method found ("+getter+" on "+target.getClass().getName()+")");
93     }
94
95     public final static Object JavaDoc callOperation(Object JavaDoc target, String JavaDoc operation, Object JavaDoc[] args) {
96         Class JavaDoc cls = target.getClass();
97         Method JavaDoc[] list = cls.getMethods();
98         for (int i = 0; i < list.length; i++) {
99             Method JavaDoc 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 JavaDoc e) {
105                     log.error("Unable to invoke "+operation+" on "+target.getClass().getName());
106                 }
107                 break;
108             }
109         }
110         throw new IllegalArgumentException JavaDoc("No such method found ("+operation+" on "+target.getClass().getName()+")");
111     }
112 }
113
Popular Tags