KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > management > ServerConnectionProxy


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

16 package org.jmanage.core.management;
17
18 import org.jmanage.core.util.Loggers;
19
20 import java.util.List JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27
28 /**
29  * ServerConnectionProxy updates the context classloader before calling
30  * any method on the wrapped ServerConnection object. It later sets the
31  * classloader back to the original classloader.
32  *
33  * date: Aug 19, 2004
34  * @author Rakesh Kalra
35  */

36 public class ServerConnectionProxy implements InvocationHandler JavaDoc {
37
38     private static final Logger JavaDoc logger =
39             Loggers.getLogger(ServerConnectionProxy.class);
40
41     private ServerConnection connection;
42     private ClassLoader JavaDoc classLoader;
43
44     public ServerConnectionProxy(ServerConnection connection,
45                                  ClassLoader JavaDoc classLoader){
46         this.connection = connection;
47         this.classLoader = classLoader;
48     }
49
50     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
51         throws Throwable JavaDoc {
52
53         final ClassLoader JavaDoc contextClassLoader =
54                         Thread.currentThread().getContextClassLoader();
55         try {
56
57             /* temporarily change the thread context classloader */
58             Thread.currentThread().setContextClassLoader(classLoader);
59             /* invoke the method on the wrapped ServerConnection */
60             if(method.getName().equals("getAttributes")){
61                 assert args.length == 2;
62                 return getAttributes((ObjectName)args[0], (String JavaDoc[])args[1]);
63             }else{
64                 return method.invoke(connection, args);
65             }
66         } catch (IllegalAccessException JavaDoc e) {
67             throw new RuntimeException JavaDoc(e);
68         } catch (IllegalArgumentException JavaDoc e) {
69             throw new RuntimeException JavaDoc(e);
70         } catch (InvocationTargetException JavaDoc e) {
71             throw e.getCause();
72         } finally {
73             /* change the thread context classloader back to the
74                     original classloader*/

75             Thread.currentThread().setContextClassLoader(contextClassLoader);
76         }
77     }
78
79     /**
80      * Returns a list of ObjectAttribute objects containing attribute names
81      * and values for the given attributeNames
82      *
83      * @param objectName
84      * @param attributeNames
85      * @return
86      */

87     private List JavaDoc getAttributes(ObjectName objectName, String JavaDoc[] attributeNames) {
88
89         /* some attribute values may not be serializable, hence may fail,
90             hence we need to get one attribute at a time */

91         List JavaDoc attributeList = new LinkedList JavaDoc();
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 JavaDoc attributeName){
100
101         try {
102             // TODO: It will be better to have a getAttribute method which
103
// just works on a single attribute
104
List JavaDoc attrList = connection.getAttributes(objectName,
105                     new String JavaDoc[]{attributeName});
106             if(attrList.size() > 0)
107                 return (ObjectAttribute)attrList.get(0);
108         } catch (Exception JavaDoc e) {
109             String JavaDoc 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