KickJava   Java API By Example, From Geeks To Geeks.

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


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.Remote JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26
27 /**
28  * @author nieuviar
29  *
30  */

31 public class StubData {
32     private ClusterId id;
33     private byte[] serializedStub;
34     private Object JavaDoc stub;
35     private int factor;
36     private double loadIncr; // Is lower or equal to 1.0
37

38     public StubData(ClusterId id, byte[] serStub, int factor) throws RemoteException JavaDoc {
39         if (factor < 1) {
40             throw new RemoteException JavaDoc("bad load factor : " + factor);
41         }
42         this.id = id;
43         this.serializedStub = serStub;
44         this.stub = null;
45         this.factor = factor;
46         this.loadIncr = 1.0 / (double)factor;
47     }
48
49     public StubData(ClusterId id, Remote JavaDoc stub, int factor) throws RemoteException JavaDoc {
50         if (factor < 1) {
51             throw new RemoteException JavaDoc("bad load factor : " + factor);
52         }
53         this.id = id;
54         this.serializedStub = null;
55         this.stub = stub;
56         this.factor = factor;
57         this.loadIncr = 1.0 / (double)factor;
58     }
59
60     StubData(Remote JavaDoc stub) {
61         this.id = null;
62         this.serializedStub = null;
63         this.stub = stub;
64         this.factor = Config.DEFAULT_RR_FACTOR;
65         this.loadIncr = 1.0 / (double)Config.DEFAULT_RR_FACTOR;
66     }
67
68     public ClusterId getId() {
69         return id;
70     }
71
72     public byte[] getSerializedStub() throws IOException JavaDoc {
73         if (serializedStub == null) {
74             ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
75             CmiOutputStream out = new CmiOutputStream(outStream);
76             out.writeObject(stub);
77             out.flush();
78             serializedStub = outStream.toByteArray();
79         }
80         return serializedStub;
81     }
82
83     public Remote JavaDoc getStub() throws RemoteException JavaDoc {
84         Object JavaDoc s = getStubOrException();
85         if (s instanceof Remote JavaDoc) {
86             return (Remote JavaDoc)s;
87         }
88         throw (RemoteException JavaDoc)s;
89     }
90
91     public Object JavaDoc getStubOrException() {
92         if (stub == null) {
93             ByteArrayInputStream JavaDoc inStream = new ByteArrayInputStream JavaDoc(serializedStub);
94             try {
95                 CmiInputStream in = new CmiInputStream(inStream);
96                 stub = (Remote JavaDoc)in.readObject();
97             } catch (IOException JavaDoc e) {
98                 stub = new RemoteException JavaDoc(e.toString());
99             } catch (ClassNotFoundException JavaDoc e) {
100                 stub = new RemoteException JavaDoc(e.toString());
101             }
102         }
103         return stub;
104     }
105
106     public double getLoadIncr() {
107         return loadIncr;
108     }
109
110     public int getFactor() {
111         return factor;
112     }
113
114     public String JavaDoc toString() {
115         String JavaDoc str = "[id:" + id + ",stub:";
116         Object JavaDoc o = getStubOrException();
117         if (o instanceof Remote JavaDoc) {
118             return str + o.toString() + "]";
119         } else {
120             return str + "serialized]";
121         }
122     }
123
124     public int hashCode() {
125         if (id != null) {
126             return id.hashCode();
127         }
128         return System.identityHashCode(this);
129     }
130
131     public boolean equals(Object JavaDoc obj) {
132         if (obj == this) return true;
133         if (id == null) return false;
134         if (!(obj instanceof StubData)) return false;
135         StubData sd = (StubData)obj;
136         return id.equals(sd.id);
137     }
138 }
139
Popular Tags