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
|
/**************************************************************************
* BasicAgent.java
*
* $Revision: 1.39 $
* $Date: 2005/04/26 06:48:12 $
*
* Copyright (C) 2001-2003 CERN - European Organization for Nuclear Research
* All rights reserved.
**************************************************************************/
package com.c2kernel.entity.agent;
import java.util.Iterator;
import com.c2kernel.common.AccessRightsException;
import com.c2kernel.common.CannotManageException;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.common.PersistencyException;
import com.c2kernel.entity.AgentPOA;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.InvalidEntityPathException;
import com.c2kernel.lookup.RolePath;
import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.persistency.TransactionManager;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.Property;
import com.c2kernel.property.PropertyArrayList;
import com.c2kernel.utils.Logger;
/**************************************************************************
* ActiveEntity
*
* @author $Author: abranson $ $Date: 2005/04/26 06:48:12 $
* @version $Revision: 1.39 $
**************************************************************************/
public class ActiveEntity extends AgentPOA
{
/**************************************************************************
* The CORBA Portable Object Adapter which holds the Agent
**************************************************************************/
private org.omg.PortableServer.POA mPOA = null;
/**************************************************************************
* The C2Kernel system key of the Agent
**************************************************************************/
private int mSystemKey = -1;
/**************************************************************************
* Connection to the persistency backeng
**************************************************************************/
private TransactionManager mDatabase = null;
/**************************************************************************
* The agent's joblist
**************************************************************************/
private JobList currentJobs;
/**
*
* @param key
* @param poa
*/
public ActiveEntity( int key,
org.omg.PortableServer.POA poa )
{
Logger.msg(5, "ActiveEntity::constructor() - SystemKey:" + key );
mSystemKey = key;
mPOA = poa;
mDatabase = Gateway.getStorage();
Logger.msg(5, "ActiveEntity::constructor - completed.");
}
/**
* initialise cristal2 properties & collector
*/
@Override
public void initialise( String agentProps )
throws AccessRightsException,
InvalidDataException,
PersistencyException
{
PropertyArrayList props = null;
Logger.msg(1, "ActiveEntity::initialise("+mSystemKey+")");
//initialise cristal2 properties & collector
try
{
props = initProps( agentProps );
mDatabase.commit( props );
}
catch( ClusterStorageException ex )
{
Logger.error("ActiveEntity::init() - Failed to init props/collector, aborting!");
Logger.error(ex);
mDatabase.abort( props );
throw new PersistencyException("Failed to init props => transaction aborted!");
}
Logger.msg(5, "ActiveEntity::init() - completed.");
}
/**
*
* @param propsString
* @return Properties
* @throws InvalidDataException Properties cannot be unmarshalled
* @throws ClusterStorageException
*/
private PropertyArrayList initProps( String propsString )
throws InvalidDataException,
ClusterStorageException
{
PropertyArrayList props = null;
// create properties
if( propsString != null && !propsString.equals("") )
{
try
{
props = (PropertyArrayList)Gateway.getMarshaller().unmarshall(propsString);
}
catch( Exception ex )
{
//any exception during unmarshall indicates that data was
//incorrect or the castor mapping was not set up
Logger.error(ex);
throw new InvalidDataException(ex.toString(), null);
}
Iterator<Property> iter = props.list.iterator();
while( iter.hasNext() )
mDatabase.put( mSystemKey, iter.next(), props );
}
else
{
Logger.warning("ActiveEntity::initProps() - NO Properties!");
}
return props;
}
/**************************************************************************
*
*
**************************************************************************/
@Override
public org.omg.PortableServer.POA _default_POA()
{
if(mPOA != null)
return mPOA;
else
return super._default_POA();
}
/**************************************************************************
*
*
**************************************************************************/
@Override
public int getSystemKey()
{
return mSystemKey;
}
/**************************************************************************
*
*
**************************************************************************/
@Override
public String queryData(String xpath)
throws AccessRightsException,
ObjectNotFoundException,
PersistencyException
{
String result = "";
int allPos = -1;
Logger.msg(1, "ActiveEntity::queryData("+mSystemKey+") - " + xpath );
try
{
if( (allPos=xpath.indexOf("all")) != -1 )
{
String query = xpath.substring(0,allPos);
String[] ids = mDatabase.getClusterContents( mSystemKey, query );
for( int i=0; i<ids.length; i++ )
{
result += ids[i];
if( i != ids.length-1 )
result += ",";
}
}
//****************************************************************
else
{
C2KLocalObject obj = mDatabase.get( mSystemKey, xpath, null );
result = Gateway.getMarshaller().marshall(obj);
}
}
catch (ObjectNotFoundException ex) {
throw ex;
}
catch(Throwable ex)
{
Logger.error("ActiveEntity::queryData("+mSystemKey+") - " +
xpath + " FAILED");
Logger.error(ex);
result = "<ERROR/>";
}
Logger.msg(7, "ActiveEntity::queryData("+mSystemKey+") - result:" + result );
return result;
}
/**
* Called by an activity when it reckons we need to update our joblist for it
*/
@Override
public synchronized void refreshJobList(int sysKey, String stepPath, String newJobs) {
try {
JobArrayList newJobList = (JobArrayList)Gateway.getMarshaller().unmarshall(newJobs);
// get our joblist
if (currentJobs == null)
currentJobs = new JobList( mSystemKey, null);
// remove old jobs for this item
currentJobs.removeJobsForStep( sysKey, stepPath );
// merge new jobs in
for (Object name : newJobList.list) {
Job newJob = (Job)name;
Logger.msg(6, "Adding job for "+newJob.getItemSysKey()+"/"+newJob.getStepPath()+":"+newJob.getPossibleTransition());
currentJobs.addJob(newJob);
}
} catch (Throwable ex) {
Logger.error("Could not refresh job list.");
Logger.error(ex);
}
}
@Override
public void addRole(String roleName) throws CannotManageException, ObjectNotFoundException {
RolePath newRole = Gateway.getLookup().getRoleManager().getRolePath(roleName);
try {
newRole.addAgent(new AgentPath(mSystemKey));
} catch (InvalidEntityPathException ex) {
throw new CannotManageException("Invalid syskey for agent: "+mSystemKey, "");
} catch (ObjectCannotBeUpdated ex) {
throw new CannotManageException("Could not update role");
}
}
@Override
public void removeRole(String roleName) throws CannotManageException, ObjectNotFoundException {
RolePath rolePath = Gateway.getLookup().getRoleManager().getRolePath(roleName);
try {
rolePath.removeAgent(new AgentPath(mSystemKey));
} catch (InvalidEntityPathException e) {
throw new CannotManageException("Invalid syskey for agent: "+mSystemKey, "");
} catch (ObjectCannotBeUpdated ex) {
throw new CannotManageException("Could not update role");
}
}
/**
*
*/
@Override
protected void finalize() throws Throwable {
Logger.msg(7, "Agent "+mSystemKey+" reaped");
Gateway.getStorage().clearCache(mSystemKey, null);
super.finalize();
}
}
|