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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/**************************************************************************
* ItemProxy.java
*
* $Revision: 1.25 $
* $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.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import com.c2kernel.collection.Collection;
import com.c2kernel.collection.CollectionArrayList;
import com.c2kernel.common.AccessRightsException;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.InvalidTransitionException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.common.PersistencyException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.Item;
import com.c2kernel.entity.ItemHelper;
import com.c2kernel.entity.agent.Job;
import com.c2kernel.entity.agent.JobArrayList;
import com.c2kernel.lifecycle.instance.CompositeActivity;
import com.c2kernel.lifecycle.instance.Workflow;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.persistency.outcome.Viewpoint;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.Property;
import com.c2kernel.property.PropertyArrayList;
import com.c2kernel.utils.CastorXMLUtility;
import com.c2kernel.utils.Logger;
/******************************************************************************
* It is a wrapper for the connection and communication with Item
* It caches data loaded from the Item to reduce communication
*
* @version $Revision: 1.25 $ $Date: 2005/05/10 11:40:09 $
* @author $Author: abranson $
******************************************************************************/
public class ItemProxy
{
protected Item mItem = null;
protected ItemPath mItemPath;
protected org.omg.CORBA.Object mIOR;
private final HashMap<MemberSubscription<?>, ProxyObserver<?>>
mSubscriptions;
/**************************************************************************
*
**************************************************************************/
protected ItemProxy( org.omg.CORBA.Object ior,
ItemPath itemPath)
{
Logger.msg(8, "ItemProxy::initialise() - Initialising item proxy " +itemPath);
mIOR = ior;
mItemPath = itemPath;
mSubscriptions = new HashMap<MemberSubscription<?>, ProxyObserver<?>>();
}
public ItemPath getPath() {
return mItemPath;
}
protected Item getItem() throws ObjectNotFoundException {
if (mItem == null)
mItem = narrow();
return mItem;
}
public Item narrow() throws ObjectNotFoundException
{
try {
return ItemHelper.narrow(mIOR);
} catch (org.omg.CORBA.BAD_PARAM ex) { }
throw new ObjectNotFoundException("CORBA Object was not an Item, or the server is down.");
}
public void initialise( AgentPath agentId,
PropertyArrayList itemProps,
CompositeActivity workflow,
CollectionArrayList colls
)
throws AccessRightsException, InvalidDataException, PersistencyException, ObjectNotFoundException, MarshalException, ValidationException, IOException, MappingException
{
Logger.msg(7, "ItemProxy::initialise - started");
CastorXMLUtility xml = Gateway.getMarshaller();
if (itemProps == null) throw new InvalidDataException("No initial properties supplied");
String propString = xml.marshall(itemProps);
String wfString = "";
if (workflow != null) wfString = xml.marshall(workflow);
String collString = "";
if (colls != null) collString = xml.marshall(colls);
getItem().initialise( agentId.getSystemKey(), propString, wfString, collString);
}
public void setProperty(AgentProxy agent, String name, String value)
throws AccessRightsException,
PersistencyException, InvalidDataException
{
String[] params = new String[2];
params[0] = name;
params[1] = value;
try {
agent.execute(this, "WriteProperty", params);
} catch (AccessRightsException e) {
throw (e);
} catch (PersistencyException e) {
throw (e);
} catch (InvalidDataException e) {
throw (e);
} catch (Exception e) {
Logger.error(e);
throw new PersistencyException("Could not store property", "");
}
}
/**************************************************************************
*
**************************************************************************/
public String requestAction( Job thisJob )
throws AccessRightsException,
InvalidTransitionException,
ObjectNotFoundException,
InvalidDataException,
PersistencyException,
ObjectAlreadyExistsException
{
String outcome = thisJob.getOutcomeString();
// check fields that should have been filled in
if (outcome==null)
if (thisJob.isOutcomeRequired())
throw new InvalidDataException("Outcome is required.", "");
else
outcome="";
if (thisJob.getAgentPath() == null)
throw new InvalidDataException("No Agent specified.", "");
Logger.msg(7, "ItemProxy - executing "+thisJob.getStepPath()+" for "+thisJob.getAgentName());
return getItem().requestAction (thisJob.getAgentPath().getSystemKey(), thisJob.getStepPath(),
thisJob.getTransition().getId(), outcome);
}
/**************************************************************************
*
**************************************************************************/
private ArrayList<Job> getJobList(AgentPath agentPath, boolean filter)
throws AccessRightsException,
ObjectNotFoundException,
PersistencyException
{
JobArrayList thisJobList;
try {
String jobs = getItem().queryLifeCycle(agentPath.getSystemKey(), filter);
thisJobList = (JobArrayList)Gateway.getMarshaller().unmarshall(jobs);
}
catch (Exception e) {
Logger.error(e);
throw new PersistencyException("Exception::ItemProxy::getJobList() - Cannot unmarshall the jobs", null);
}
return thisJobList.list;
}
public ArrayList<Job> getJobList(AgentProxy agent)
throws AccessRightsException,
ObjectNotFoundException,
PersistencyException
{
return getJobList(agent.getPath(), true);
}
private Job getJobByName(String actName, AgentPath agent)
throws AccessRightsException,
ObjectNotFoundException,
PersistencyException {
ArrayList<Job> jobList = getJobList(agent, true);
for (Job job : jobList) {
if (job.getStepName().equals(actName) && job.hasOutcome())
return job;
}
return null;
}
public Collection<?> getCollection(String collName) throws ObjectNotFoundException {
return (Collection<?>)getObject(ClusterStorage.COLLECTION+"/"+collName+"/last");
}
public Workflow getWorkflow() throws ObjectNotFoundException {
return (Workflow)getObject(ClusterStorage.LIFECYCLE+"/workflow");
}
public Viewpoint getViewpoint(String schemaName, String viewName) throws ObjectNotFoundException {
return (Viewpoint)getObject(ClusterStorage.VIEWPOINT+"/"+schemaName+"/"+viewName);
}
public Job getJobByName(String actName, AgentProxy agent)
throws AccessRightsException,
ObjectNotFoundException,
PersistencyException {
return getJobByName(actName, agent.getPath());
}
/**
* If this is reaped, clear out the cache for it too.
*/
@Override
protected void finalize() throws Throwable {
Logger.msg(7, "Proxy "+mItemPath+" reaped");
Gateway.getStorage().clearCache(mItemPath, null);
Gateway.getProxyManager().removeProxy(mItemPath);
super.finalize();
}
/**************************************************************************
*
**************************************************************************/
public String queryData( String path )
throws ObjectNotFoundException
{
try {
Logger.msg(7, "EntityProxy.queryData() - "+mItemPath+"/"+path);
if (path.endsWith("all")) {
Logger.msg(7, "EntityProxy.queryData() - listing contents");
String[] result = Gateway.getStorage().getClusterContents(mItemPath, 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(mItemPath, path, null);
return Gateway.getMarshaller().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(mItemPath, 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( mItemPath, xpath , null);
}
catch( ClusterStorageException ex )
{
Logger.msg(4, "Exception loading object :"+mItemPath+"/"+xpath);
throw new ObjectNotFoundException( ex.toString() );
}
}
public String getProperty( String name )
throws ObjectNotFoundException
{
Logger.msg(5, "Get property "+name+" from item "+mItemPath);
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(MemberSubscription<?> newSub) {
newSub.setSubject(this);
synchronized (this){
mSubscriptions.put( newSub, newSub.getObserver() );
}
new Thread(newSub).start();
Logger.msg(7, "Subscribed "+newSub.getObserver().getClass().getName()+" for "+newSub.interest);
}
public void unsubscribe(ProxyObserver<?> observer)
{
synchronized (this){
for (Iterator<MemberSubscription<?>> e = mSubscriptions.keySet().iterator(); e.hasNext();) {
MemberSubscription<?> thisSub = 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 "+mItemPath+":");
synchronized(this) {
for (MemberSubscription<?> element : mSubscriptions.keySet()) {
ProxyObserver<?> 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 "+mItemPath);
synchronized (this){
if (Gateway.getProxyServer()== null || !message.getServer().equals(Gateway.getProxyServer().getServerName()))
Gateway.getStorage().clearCache(mItemPath, message.getPath());
for (Iterator<MemberSubscription<?>> e = mSubscriptions.keySet().iterator(); e.hasNext();) {
MemberSubscription<?> newSub = 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());
}
}
}
}
|