KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.rmi.AlreadyBoundException JavaDoc;
25 import java.rmi.NotBoundException JavaDoc;
26 import java.rmi.Remote JavaDoc;
27 import java.rmi.RemoteException JavaDoc;
28
29 import org.objectweb.carol.util.configuration.TraceCarol;
30
31 /**
32  * Client code for a cluster registry
33  */

34 public class ClusterRegistryClient implements ClusterRegistry {
35     private ClusterRegistryInternal cr;
36
37     // constructor
38
public ClusterRegistryClient(ClusterRegistryInternal cr)
39         throws RemoteException JavaDoc {
40         this.cr = cr;
41     }
42
43     public String JavaDoc[] list() throws RemoteException JavaDoc {
44         return cr.list();
45     }
46
47     public void test() throws RemoteException JavaDoc {
48         cr.test();
49     }
50
51     public Remote JavaDoc lookup(String JavaDoc name)
52         throws NotBoundException JavaDoc, RemoteException JavaDoc {
53         Object JavaDoc obj = cr.lookup(name);
54         if (obj instanceof Remote JavaDoc) {
55             return (Remote JavaDoc)obj;
56         }
57         byte[] buf = (byte[])obj;
58         ByteArrayInputStream JavaDoc inStream = new ByteArrayInputStream JavaDoc(buf);
59         int type = inStream.read();
60         try {
61             CmiInputStream in = new CmiInputStream(inStream);
62             if (type == ClusterRegistryInternal.CLUSTERED) {
63                 return ClusterStubData.read(in, null).getClusterStub();
64             } else {
65                 return (Remote JavaDoc) in.readObject();
66             }
67         } catch (IOException JavaDoc e) {
68             throw new RemoteException JavaDoc(e.toString());
69         } catch (ClassNotFoundException JavaDoc e) {
70             throw new RemoteException JavaDoc(e.toString());
71         }
72     }
73
74     private byte[] serialize(Remote JavaDoc obj) throws RemoteException JavaDoc {
75         ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
76         CmiOutputStream out;
77         try {
78             out = new CmiOutputStream(outStream);
79             out.writeObject(obj);
80             out.flush();
81             return outStream.toByteArray();
82         } catch (IOException JavaDoc e1) {
83             throw new RemoteException JavaDoc(e1.toString());
84         }
85     }
86
87     public void bind(String JavaDoc name, Remote JavaDoc obj)
88         throws AlreadyBoundException JavaDoc, RemoteException JavaDoc {
89             ClusterConfig cc = null;
90             try {
91                 cc = ClusterObject.getClusterConfig(obj);
92                 if (TraceCarol.isDebugCmiRegistry())
93                     TraceCarol.debugCmiRegistry("Global bind of " + name);
94             } catch (Exception JavaDoc e) {
95                 if (TraceCarol.isDebugCmiRegistry())
96                     TraceCarol.debugCmiRegistry("Local bind of " + name);
97             }
98             if (cc == null) {
99                 cr.bindSingle(name, obj);
100             } else {
101                 if (!cc.isGlobalAtBind()) {
102                     throw new RemoteException JavaDoc("not implemented");
103                 }
104                 obj = LowerOrb.toStub(obj);
105                 byte[] ser = serialize(obj);
106                 cr.bindCluster(name, ser);
107             }
108     }
109
110     public void rebind(String JavaDoc name, Remote JavaDoc obj) throws RemoteException JavaDoc {
111         ClusterConfig cc = null;
112         try {
113             cc = ClusterObject.getClusterConfig(obj);
114             if (TraceCarol.isDebugCmiRegistry())
115                 TraceCarol.debugCmiRegistry("Global bind of " + name);
116         } catch (Exception JavaDoc e) {
117             if (TraceCarol.isDebugCmiRegistry())
118                 TraceCarol.debugCmiRegistry("Local bind of " + name);
119         }
120         if (cc == null) {
121             cr.rebindSingle(name, obj);
122         } else {
123             if (!cc.isGlobalAtBind()) {
124                 throw new RemoteException JavaDoc("not implemented");
125             }
126             obj = LowerOrb.toStub(obj);
127             byte[] ser = serialize(obj);
128             cr.rebindCluster(name, ser);
129         }
130     }
131
132     public void unbind(String JavaDoc name) throws NotBoundException JavaDoc, RemoteException JavaDoc {
133         cr.unbind(name);
134     }
135 }
136
Popular Tags