/
Context.java
620 lines (566 loc) · 27 KB
/
Context.java
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package i5.las2peer.api;
import java.io.Serializable;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
import i5.las2peer.api.execution.InternalServiceException;
import i5.las2peer.api.execution.ServiceAccessDeniedException;
import i5.las2peer.api.execution.ServiceInvocationFailedException;
import i5.las2peer.api.execution.ServiceMethodNotFoundException;
import i5.las2peer.api.execution.ServiceNotAuthorizedException;
import i5.las2peer.api.execution.ServiceNotAvailableException;
import i5.las2peer.api.execution.ServiceNotFoundException;
import i5.las2peer.api.logging.MonitoringEvent;
import i5.las2peer.api.p2p.ServiceNameVersion;
import i5.las2peer.api.persistency.Envelope;
import i5.las2peer.api.persistency.EnvelopeAccessDeniedException;
import i5.las2peer.api.persistency.EnvelopeCollisionHandler;
import i5.las2peer.api.persistency.EnvelopeNotFoundException;
import i5.las2peer.api.persistency.EnvelopeOperationFailedException;
import i5.las2peer.api.security.Agent;
import i5.las2peer.api.security.AgentAccessDeniedException;
import i5.las2peer.api.security.AgentAlreadyExistsException;
import i5.las2peer.api.security.AgentException;
import i5.las2peer.api.security.AgentLockedException;
import i5.las2peer.api.security.AgentNotFoundException;
import i5.las2peer.api.security.AgentOperationFailedException;
import i5.las2peer.api.security.GroupAgent;
import i5.las2peer.api.security.ServiceAgent;
import i5.las2peer.api.security.UserAgent;
import i5.las2peer.execution.ServiceThread;
import i5.las2peer.p2p.AgentAlreadyRegisteredException;
import i5.las2peer.security.MessageReceiver;
/**
* Provides access to the context of the current call.
*
*/
public interface Context {
// get Context
/**
* Gets the current las2peer context.
*
* @throws IllegalStateException called not in a las2peer execution thread
* @return the current context
*/
public static Context getCurrent() {
return ServiceThread.getCurrentContext();
}
/**
* Gets the current las2peer context.
*
* @throws IllegalStateException called not in a las2peer execution thread
* @return the current context
*/
public static Context get() {
return getCurrent();
}
// get Service
/**
* Get the current service.
*
* @return the current service
*/
public Service getService();
/**
* Get the current service, avoiding casting.
*
* @param <T> type of the service
* @param serviceType service class
* @return the current service
*/
public <T extends Service> T getService(Class<T> serviceType);
// Agents
/**
* Get the main agent of this context. In most cases, this is the user.
*
* @return the calling agent.
*/
public Agent getMainAgent();
/**
* Get the current service agent responsible for executing the service.
*
* @return the executing service agent.
*/
public ServiceAgent getServiceAgent();
/**
* Creates a new UserAgent.
*
* @param passphrase the passphrase to protect the newly generated agent
* @return A new unlocked UserAgent which is not stored to the network yet.
* @throws AgentOperationFailedException if an error occurred on the node.
*/
public UserAgent createUserAgent(String passphrase) throws AgentOperationFailedException;
/**
* Creates a new GroupAgent.
*
* @param members Initial member list
* @return A new unlocked GroupAgent which is not stored to the network yet.
* @throws AgentOperationFailedException If an error occurred on the node.
*/
public GroupAgent createGroupAgent(Agent[] members) throws AgentOperationFailedException;
/**
* Creates a new GroupAgent with groupName.
*
* @param members Initial member list
* @return A new unlocked GroupAgent which is not stored to the network yet.
* @throws AgentOperationFailedException If an error occurred on the node.
*/
public GroupAgent createGroupAgent(Agent[] members, String groupName) throws AgentOperationFailedException;
/**
* Fetches an agent from the network and trys to unlock it using the specified
* agent.
*
* @param agentId The id of the agent to fetch.
* @param using The agent used to unlock the fetched agent.
* @return An unlocked instance of the requested agent.
* @throws AgentAccessDeniedException If the given agent cannot access the
* fetched agent.
* @throws AgentNotFoundException If the specified agent cannot be found.
* @throws AgentOperationFailedException If an error occurred on the node.
*/
public Agent requestAgent(String agentId, Agent using)
throws AgentAccessDeniedException, AgentNotFoundException, AgentOperationFailedException;
/**
* Requests an agent from the network using the calling (main) agent.
*
* @param agentId The id of the agent to fetch.
* @return An unlocked instance of the requested agent.
* @throws AgentAccessDeniedException If the main agent cannot access the
* fetched agent.
* @throws AgentNotFoundException If the specified agent cannot be found.
* @throws AgentOperationFailedException If an error occurred on the node.
*/
public Agent requestAgent(String agentId)
throws AgentAccessDeniedException, AgentNotFoundException, AgentOperationFailedException;
/**
* Fetches an agent from the network.
*
* @param agentId The id of the agent to fetch.
* @return A probably locked instance of the specified agent.
* @throws AgentNotFoundException If the specified agent cannot be found.
* @throws AgentOperationFailedException If an error occurred on the node.
*/
public Agent fetchAgent(String agentId) throws AgentNotFoundException, AgentOperationFailedException;
/**
* Stores and/or updates an agent to the network.
*
* The given agent must be unlocked.
*
* @param agent The unlocked agent to store.
* @throws AgentAccessDeniedException If the agent cannot be overridden due
* to access restrictions. Or it is the
* AnonymousAgent.
* @throws AgentAlreadyExistsException If another agent already exists
* (regarding some agent specific
* properties).
* @throws AgentOperationFailedException If an error occurred on the node.
* @throws AgentLockedException If the agent is locked.
*/
public void storeAgent(Agent agent) throws AgentAccessDeniedException, AgentAlreadyExistsException,
AgentOperationFailedException, AgentLockedException;
/**
* Checks if the agent specified by using is able to unlock the agent agentId.
* This also includes recursive unlocking.
*
* @param agentId The agent to be checked.
* @param using The agent to unlock.
* @return true If using is able to unlock agentId.
* @throws AgentNotFoundException If the agent specified by agentId does
* not exist.
* @throws AgentOperationFailedException If an error occurred on the node.
*/
public boolean hasAccess(String agentId, Agent using) throws AgentNotFoundException, AgentOperationFailedException;
/**
* Checks if the main agent is able to unlock the agent agentId. This also
* includes recursive unlocking.
*
* @param agentId The agent to be checked.
* @return true If the main agent is able to unlock the given agent.
* @throws AgentNotFoundException If the agent specified by agentId does
* not exist.
* @throws AgentOperationFailedException If an error occurred on the node.
*/
public boolean hasAccess(String agentId) throws AgentNotFoundException, AgentOperationFailedException;
// UserAgents
/**
* Gets a {@link UserAgent}'s identifier by its login name.
*
* @param loginName The user's login name
* @return The identifier of the user agents.
* @throws AgentNotFoundException If there is no agent with the given
* login name.
* @throws AgentOperationFailedException On node errors.
*/
public String getUserAgentIdentifierByLoginName(String loginName)
throws AgentNotFoundException, AgentOperationFailedException;
/**
* Gets a {@link UserAgent}'s identifier by its email address.
*
* @param emailAddress The user's email address.
* @return The identifier of the user agents.
* @throws AgentNotFoundException If there is no agent with the given
* email address.
* @throws AgentOperationFailedException On node errors.
*/
public String getUserAgentIdentifierByEmail(String emailAddress)
throws AgentNotFoundException, AgentOperationFailedException;
// TODO remove this after monitoring concept is fixed
@Deprecated // edge case for monitoring, violates service replication concept
public void registerReceiver(MessageReceiver receiver) throws AgentAlreadyRegisteredException, AgentException;
// Envelopes
/**
* Requests an envelope from the network. This means fetching and decrypting it
* using the specified agent.
*
* @param identifier Identifier of the envelope.
* @param using Agentu sing to open the envelope.
* @return An opened envelope.
* @throws EnvelopeAccessDeniedException If the given agent is not able to
* access the envelope.
* @throws EnvelopeNotFoundException If the envelope doesn not exist.
* @throws EnvelopeOperationFailedException If an error occurred in the node or
* network.
*/
public Envelope requestEnvelope(String identifier, Agent using)
throws EnvelopeAccessDeniedException, EnvelopeNotFoundException, EnvelopeOperationFailedException;
/**
* Requests an envelope from the network. This means fetching and decrypting it
* using the current main agent.
*
* @param identifier Identifier of the envelope.
* @return An opened envelope.
* @throws EnvelopeAccessDeniedException If the given agent is not able to
* access the envelope.
* @throws EnvelopeNotFoundException If the envelope doesn not exist.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
*/
public Envelope requestEnvelope(String identifier)
throws EnvelopeAccessDeniedException, EnvelopeNotFoundException, EnvelopeOperationFailedException;
/**
* Stores the envelope to the network and signs it with the specified agent.
*
* @param env The envelope to store.
* @param using The agent to be used to sign the envelope.
* @throws EnvelopeAccessDeniedException If the specified agent is not
* allowed to write to the envelope.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
*/
public void storeEnvelope(Envelope env, Agent using)
throws EnvelopeAccessDeniedException, EnvelopeOperationFailedException;
/**
* Stores the envelope to the network and signs it with the current main agent.
*
* @param env The envelope to store.
* @throws EnvelopeAccessDeniedException If the specified agent is not
* allowed to write to the envelope.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
*/
public void storeEnvelope(Envelope env) throws EnvelopeAccessDeniedException, EnvelopeOperationFailedException;
/**
* Stores the envelope to the network and signs it with the specified agent.
*
* @param env The envelope to store.
* @param handler An handler to resolve storage conflict (e.g. the envelope has
* been updated in the meantime).
* @param using The agent to be used to sign the envelope (and must have
* signed the envelope or must have access to the signing agent
* if there are previous versions).
* @throws EnvelopeAccessDeniedException If the specified agent is not
* allowed to write to the envelope.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
*/
public void storeEnvelope(Envelope env, EnvelopeCollisionHandler handler, Agent using)
throws EnvelopeAccessDeniedException, EnvelopeOperationFailedException;
/**
* Stores the envelope to the network and signs it with the current main agent.
*
* @param env The envelope to store.
* @param handler An handler to resolve storage conflict (e.g. the envelope has
* been updated in the meantime).
* @throws EnvelopeAccessDeniedException If the specified agent is not
* allowed to write to the envelope.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
*/
public void storeEnvelope(Envelope env, EnvelopeCollisionHandler handler)
throws EnvelopeAccessDeniedException, EnvelopeOperationFailedException;
/**
* Reclaims the envelope using the specified agent.
*
* A reclaim operation marks the envelope as deleted and indicates that the
* envelope is no longer needed anymore (e.g. can be deleted by other nodes).
* However, it is not guaranteed that the envelope will be deleted since the
* nature of a p2p network.
*
* @param identifier The identifier of the envelope.
* @param using The agent that has signed the envelope or an agent that has
* access to the signing agent.
* @throws EnvelopeAccessDeniedException If the agent has not signed the
* envelope.
* @throws EnvelopeNotFoundException If the envelope does not exist.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
*/
public void reclaimEnvelope(String identifier, Agent using)
throws EnvelopeAccessDeniedException, EnvelopeNotFoundException, EnvelopeOperationFailedException;
/**
* Reclaims the envelope using the current main agent agent.
*
* A reclaim operation marks the envelope as deleted and indicates that the
* envelope is no longer needed anymore (e.g. can be deleted by other nodes).
* However, it is not guaranteed that the envelope will be deleted since the
* nature of a p2p network.
*
* @param identifier The identifier of the envelope.
* @throws EnvelopeAccessDeniedException If the agent has not signed the
* envelope.
* @throws EnvelopeNotFoundException If the envelope does not exist.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
*/
public void reclaimEnvelope(String identifier)
throws EnvelopeAccessDeniedException, EnvelopeNotFoundException, EnvelopeOperationFailedException;
/**
* Creates a new envelope with the given agent as signing agent and first
* reader.
*
* @param identifier Identifier of the envelope.
* @param using Signing agent (owner) of the envelope.
* @return An envelope that is not stored to the network yet.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
* @throws EnvelopeAccessDeniedException If the agent is not allowed to
* create envelopes (e.g. the
* AnonymousAgent).
*/
public Envelope createEnvelope(String identifier, Agent using)
throws EnvelopeOperationFailedException, EnvelopeAccessDeniedException;
/**
* Creates a new envelope with the current main agent as signing agent and first
* reader.
*
* @param identifier Identifier of the envelope.
* @return An envelope that is not stored to the network yet.
* @throws EnvelopeOperationFailedException If an error occurred at the node or
* in the network.
* @throws EnvelopeAccessDeniedException If the agent is not allowed to
* create envelopes (e.g. the
* AnonymousAgent).
*/
public Envelope createEnvelope(String identifier)
throws EnvelopeOperationFailedException, EnvelopeAccessDeniedException;
// RMI
/**
* Invokes the method of any other service on behalf of the main agent, thus
* sending the main agent as calling agent.
*
* @param service The service class. A version may be specified (for example
* package.serviceClass@1.0.0-1 or package.serviceClass@1.0).
* The core tries to find an appropriate version (version
* 1.0.5 matches 1.0). If no version is specified, the newest
* version is picked.
* @param method The service method.
* @param parameters The parameters list.
* @return The invocation result.
* @throws ServiceNotFoundException If the service is not known to the
* network.
* @throws ServiceNotAvailableException If the service is temporarily not
* available.
* @throws InternalServiceException If the remote service throws an
* exception.
* @throws ServiceMethodNotFoundException If the service method does not
* exist.
* @throws ServiceInvocationFailedException If the service invocation failed.
* @throws ServiceAccessDeniedException If the access to the service has
* been denied.
* @throws ServiceNotAuthorizedException If access to the service method
* requires a logged in user.
*/
public Serializable invoke(String service, String method, Serializable... parameters)
throws ServiceNotFoundException, ServiceNotAvailableException, InternalServiceException,
ServiceMethodNotFoundException, ServiceInvocationFailedException, ServiceAccessDeniedException,
ServiceNotAuthorizedException;
/**
* Invokes the method of any other service on behalf of the main agent, thus
* sending the main agent as calling agent.
*
* @param service The service class. A version may be specified (for example
* package.serviceClass@1.0.0-1 or package.serviceClass@1.0).
* The core tries to find an appropriate version (version
* 1.0.5 matches 1.0). If no version is specified, the newest
* version is picked.
* @param method The service method.
* @param parameters The parameters list.
* @return The invocation result.
* @throws ServiceNotFoundException If the service is not known to the
* network.
* @throws ServiceNotAvailableException If the service is temporarily not
* available.
* @throws InternalServiceException If the remote service throws an
* exception.
* @throws ServiceMethodNotFoundException If the service method does not
* exist.
* @throws ServiceInvocationFailedException If the service invocation failed.
* @throws ServiceAccessDeniedException If the access to the service has
* been denied.
* @throws ServiceNotAuthorizedException If access to the service method
* requires a logged in user.
*/
public Serializable invoke(ServiceNameVersion service, String method, Serializable... parameters)
throws ServiceNotFoundException, ServiceNotAvailableException, InternalServiceException,
ServiceMethodNotFoundException, ServiceInvocationFailedException, ServiceAccessDeniedException,
ServiceNotAuthorizedException;
/**
* Invokes a service method using the agent of this service as calling agent.
*
* @param service The service class. A version may be specified (for example
* package.serviceClass@1.0.0-1 or package.serviceClass@1.0).
* The core tries to find an appropriate version (version
* 1.0.5 matches 1.0). If no version is specified, the newest
* version is picked.
* @param method The service method.
* @param parameters The parameters list.
* @return The invocation result.
* @throws ServiceNotFoundException If the service is not known to the
* network.
* @throws ServiceNotAvailableException If the service is temporarily not
* available.
* @throws InternalServiceException If the remote service throws an
* exception.
* @throws ServiceMethodNotFoundException If the service method does not
* exist.
* @throws ServiceInvocationFailedException If the service invocation failed.
* @throws ServiceAccessDeniedException If the access to the service has
* been denied.
* @throws ServiceNotAuthorizedException If access to the service method
* requires a logged in user.
*/
public Serializable invokeInternally(String service, String method, Serializable... parameters)
throws ServiceNotFoundException, ServiceNotAvailableException, InternalServiceException,
ServiceMethodNotFoundException, ServiceInvocationFailedException, ServiceAccessDeniedException,
ServiceNotAuthorizedException;
/**
* Invokes a service method using the agent of this service as calling agent.
*
* @param service The service class. A version may be specified (for example
* package.serviceClass@1.0.0-1 or package.serviceClass@1.0).
* The core tries to find an appropriate version (version
* 1.0.5 matches 1.0). If no version is specified, the newest
* version is picked.
* @param method The service method.
* @param parameters The parameters list.
* @return The invocation result.
* @throws ServiceNotFoundException If the service is not known to the
* network.
* @throws ServiceNotAvailableException If the service is temporarily not
* available.
* @throws InternalServiceException If the remote service throws an
* exception.
* @throws ServiceMethodNotFoundException If the service method does not
* exist.
* @throws ServiceInvocationFailedException If the service invocation failed.
* @throws ServiceAccessDeniedException If the access to the service has
* been denied.
* @throws ServiceNotAuthorizedException If access to the service method
* requires a logged in user.
*/
public Serializable invokeInternally(ServiceNameVersion service, String method, Serializable... parameters)
throws ServiceNotFoundException, ServiceNotAvailableException, InternalServiceException,
ServiceMethodNotFoundException, ServiceInvocationFailedException, ServiceAccessDeniedException,
ServiceNotAuthorizedException;
// Execution
/**
* Gets the executor for this service.
*
* Async tasks should be handeld using this executor. They can access the
* current context.
*
* @return The executor responsible for the current service call.
*/
public ExecutorService getExecutor();
// Logging
/**
* Gets the logger for the given class.
*
* @param cls A class.
*
* @return The logging instance for the current service.
*/
public Logger getLogger(Class<?> cls);
// Monitoring
/**
* Writes a log message to the l2p system using node observers. Also makes data
* available to MobSOS.
*
* Does not include the current acting main agent. The MonitoringEvent will
* default to MonitoringEvent.SERVICE_MESSAGE. Use this method for trivial log
* messages.
*
* @param message A message.
*/
public void monitorEvent(String message);
/**
* Writes a log message to the l2p system using node observers. Also makes data
* available to MobSOS.
*
* Does not include the current acting main agent.
*
* @param event Differentiates between different log messages. Use
* MonitoringEvent.SERVICE_CUSTOM_MESSAGE_XXX as parameter.
* @param message A message.
*/
public void monitorEvent(MonitoringEvent event, String message);
/**
* Writes a log message to the l2p system using node observers. Also makes data
* available to MobSOS.
*
* Does not include the current acting main agent.
*
* @param from Specifies from which class the message is sent from. Usually
* "this" is passed as parameter.
* @param event Differentiates between different log messages. Use
* MonitoringEvent.SERVICE_CUSTOM_MESSAGE_XXX as parameter.
* @param message A message.
*/
public void monitorEvent(Object from, MonitoringEvent event, String message);
/**
* Writes a log message to the l2p system using node observers. Also makes data
* available to MobSOS.
*
* @param from Specifies from which class the message is sent from.
* Usually "this" is passed as parameter.
* @param event Differentiates between different log messages. Use
* MonitoringEvent.SERVICE_CUSTOM_MESSAGE_XXX as
* parameter.
* @param message A message.
* @param includeActingUser If set to true, the current main agent will be
* included.
*/
public void monitorEvent(Object from, MonitoringEvent event, String message, boolean includeActingUser);
/**
* Writes an XES event message to the l2p system using node observers. Also
* makes data
* available to MobSOS.
*
* @param event Differentiates between different log messages. Use
* MonitoringEvent.SERVICE_CUSTOM_MESSAGE_XXX as parameter.
* @param message A message.
* @param caseId The case id of the XES event.
* @param activityName The activity name of the XES event.
* @param resourceId The resource id of the XES event.
* @param resourceType The resource type of the XES event.
* @param lifecyclePhase The lifecycle of the XES event.
*/
public void monitorXESEvent(MonitoringEvent event, String message, String caseId, String activityName,
String resourceId, String resourceType, String lifecyclePhase, Long timeOfEvent);
// Class loading
/**
* Gets the class loader responsible for loading the current service.
*
* @return The current service class loader.
*/
public ClassLoader getServiceClassLoader();
}