1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/**************************************************************************
* EntityProxy.java
*
* $Revision: 1.35 $
* $Date: 2005/05/10 11:40:09 $
*
* Copyright (C) 2001 CERN - European Organization for Nuclear Research
* All rights reserved.
**************************************************************************/
package com.c2kernel.entity.proxy;
import java.util.*;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.*;
import com.c2kernel.persistency.*;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.Property;
import com.c2kernel.utils.*;
/******************************************************************************
* It is a wrapper for the connection and communication with Entities.
* It can cache data loaded from the Entity to reduce communication with it.
* This cache is syncronised with corresponding Entity through an event mechanism.
*
* @version $Revision: 1.35 $ $Date: 2005/05/10 11:40:09 $
* @author $Author: abranson $
******************************************************************************/
abstract public class EntityProxy implements ManageableEntity
{
protected ManageableEntity mEntity = null;
protected org.omg.CORBA.Object mIOR;
protected int mSystemKey;
private HashMap mSubscriptions;
/**************************************************************************
*
**************************************************************************/
protected EntityProxy( org.omg.CORBA.Object ior,
int systemKey)
throws ObjectNotFoundException
{
Logger.msg(8,"EntityProxy::EntityProxy() - Initialising '" +systemKey+ "' entity");
initialise( ior, systemKey);
}
/**************************************************************************
*
**************************************************************************/
private void initialise( org.omg.CORBA.Object ior,
int systemKey)
throws ObjectNotFoundException
{
Logger.msg(8, "EntityProxy::initialise() - Initialising '" +systemKey+ "' entity");
mIOR = ior;
mSystemKey = systemKey;
mSubscriptions = new HashMap();
}
/**************************************************************************
*
**************************************************************************/
public ManageableEntity getEntity() throws ObjectNotFoundException
{
if (mEntity == null) {
mEntity = narrow();
}
return mEntity;
}
abstract public ManageableEntity narrow() throws ObjectNotFoundException;
/**************************************************************************
*
**************************************************************************/
//check who is using.. and if toString() is sufficient
public int getSystemKey()
{
return mSystemKey;
}
/**************************************************************************
*
**************************************************************************/
public String queryData( String path )
throws ObjectNotFoundException
{
try {
Logger.msg(7, "EntityProxy.queryData() - "+mSystemKey+"/"+path);
if (path.endsWith("all")) {
Logger.msg(7, "EntityProxy.queryData() - listing contents");
String[] result = Gateway.getStorage().getClusterContents(mSystemKey, path.substring(0, path.length()-3));
StringBuffer retString = new StringBuffer();
for (int i = 0; i < result.length; i++) {
retString.append(result[i]);
if (i<result.length-1) retString.append(",");
}
Logger.msg(7, "EntityProxy.queryData() - "+retString.toString());
return retString.toString();
}
C2KLocalObject target = Gateway.getStorage().get(mSystemKey, path, null);
return CastorXMLUtility.marshall(target);
} catch (ObjectNotFoundException e) {
throw e;
} catch (Exception e) {
Logger.error(e);
return "<ERROR>"+e.getMessage()+"</ERROR>";
}
}
public String[] getContents( String path ) throws ObjectNotFoundException {
try {
return Gateway.getStorage().getClusterContents(mSystemKey, path.substring(0, path.length()));
} catch (ClusterStorageException e) {
throw new ObjectNotFoundException(e.toString());
}
}
/**************************************************************************
*
**************************************************************************/
public C2KLocalObject getObject( String xpath )
throws ObjectNotFoundException
{
// load from storage, falling back to proxy loader if not found in others
try
{
return Gateway.getStorage().get( mSystemKey, xpath , this);
}
catch( ClusterStorageException ex )
{
Logger.msg(4, "Exception loading object :"+mSystemKey+"/"+xpath);
throw new ObjectNotFoundException( ex.toString() );
}
}
public String getProperty( String name )
throws ObjectNotFoundException
{
Logger.msg(5, "Get property "+name+" from syskey/"+mSystemKey);
Property prop = (Property)getObject("Property/"+name);
try
{
return prop.getValue();
}
catch (NullPointerException ex)
{
throw new ObjectNotFoundException();
}
}
public String getName()
{
try {
return getProperty("Name");
} catch (ObjectNotFoundException ex) {
return null;
}
}
/**************************************************************************
* Subscription methods
**************************************************************************/
public void subscribe (EntityProxyObserver observer,
String interest,
boolean preload)
{
MemberSubscription newSub = new MemberSubscription(this, interest, observer, preload);
synchronized (this){
mSubscriptions.put( newSub, observer );
}
new Thread(newSub).start();
Logger.msg(7, "Subscribed "+observer.getClass().getName()+" for "+interest);
}
public void unsubscribe(EntityProxyObserver observer)
{
synchronized (this){
for (Iterator e = mSubscriptions.keySet().iterator(); e.hasNext();) {
MemberSubscription thisSub = (MemberSubscription)e.next();
if (mSubscriptions.get( thisSub ) == observer) {
e.remove();
Logger.msg(7, "Unsubscribed "+observer.getClass().getName());
}
}
}
}
public void dumpSubscriptions(int logLevel) {
if (mSubscriptions.size() == 0) return;
Logger.msg(logLevel, "Subscriptions to proxy "+mSystemKey+":");
synchronized(this) {
for (Iterator iter = mSubscriptions.keySet().iterator(); iter.hasNext();) {
MemberSubscription element = (MemberSubscription)iter.next();
EntityProxyObserver obs = element.getObserver();
if (obs != null)
Logger.msg(logLevel, " "+element.getObserver().getClass().getName()+" subscribed to "+element.interest);
else
Logger.msg(logLevel, " Phantom subscription to "+element.interest);
}
}
}
public void notify(ProxyMessage message) {
Logger.msg(4, "EntityProxy.notify() - Received change notification for "+message.getPath()+" on "+mSystemKey);
synchronized (this){
if (!message.getServer().equals(EntityProxyManager.serverName))
Gateway.getStorage().clearCache(mSystemKey, message.getPath());
for (Iterator e = mSubscriptions.keySet().iterator(); e.hasNext();) {
MemberSubscription newSub = (MemberSubscription)e.next();
if (newSub.getObserver() == null) { // phantom
Logger.msg(4, "Removing phantom subscription to "+newSub.interest);
e.remove();
}
else
newSub.update(message.getPath(), message.getState());
}
}
}
/**
* If this is reaped, clear out the cache for it too.
*/
protected void finalize() throws Throwable {
Logger.msg(7, "Proxy "+mSystemKey+" reaped");
Gateway.getStorage().clearCache(mSystemKey, null);
Gateway.getProxyManager().removeProxy(mSystemKey);
super.finalize();
}
}
|