KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > ClusterObject


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.rmi.Remote JavaDoc;
23
24 /**
25  * Tell us if an object can be clustered or not.
26  */

27 public final class ClusterObject {
28     /**
29      * @return The cluster stub class corresponding to the parameter class or
30      * <code>null</code> if this object has no cluster stub class.
31      */

32     public static Class JavaDoc getClusterStubClass(Class JavaDoc cl)
33         throws ClassNotFoundException JavaDoc {
34         String JavaDoc stub_name = cl.getName();
35         String JavaDoc cstub_name = stub_name;
36         if (stub_name.endsWith("_Stub"))
37             cstub_name = cstub_name.substring(0, cstub_name.length() - 5);
38         else if (stub_name.endsWith("_OWStub"))
39             cstub_name = cstub_name.substring(0, cstub_name.length() - 7);
40         cstub_name += "_Cluster";
41         ClassLoader JavaDoc loader = cl.getClassLoader();
42         Class JavaDoc cstub_class = loader.loadClass(cstub_name);
43         return cstub_class;
44     }
45
46     public static Class JavaDoc getClusterConfigClass(Remote JavaDoc obj)
47         throws ClassNotFoundException JavaDoc {
48         Class JavaDoc cl = obj.getClass();
49         String JavaDoc obj_name = obj.getClass().getName();
50         String JavaDoc config_name = obj_name;
51         if (obj_name.endsWith("_Stub"))
52             config_name = config_name.substring(0, config_name.length() - 5);
53         else if (obj_name.endsWith("_OWStub"))
54             config_name = config_name.substring(0, config_name.length() - 7);
55         config_name += "_ClusterConfig";
56         ClassLoader JavaDoc loader = cl.getClassLoader();
57         return loader.loadClass(config_name);
58     }
59
60     /**
61      * Call getClusterConfig() on a cluster stub instead of this one whenever
62      * possible.
63      * @return A ClusterConfig object reflecting the static cluster
64      * configuration of <code>stub</code>'s class.
65      */

66     public static ClusterConfig getClusterConfig(Remote JavaDoc obj)
67         throws
68             ClassNotFoundException JavaDoc,
69             NoSuchMethodException JavaDoc,
70             IllegalAccessException JavaDoc,
71             InvocationTargetException JavaDoc {
72         Class JavaDoc config_class = getClusterConfigClass(obj);
73         java.lang.reflect.Method JavaDoc mth =
74             config_class.getMethod("getClusterConfig", (Class JavaDoc[]) null);
75         return (ClusterConfig) mth.invoke(null, (Object JavaDoc[]) null);
76     }
77 }
78
Popular Tags