1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.portal.cocoon.workflow.jes;
12
13 import org.apache.log4j.Logger;
14
15
16 import org.astrogrid.portal.workflow.intf.*;
17 import org.astrogrid.community.beans.v1.Credentials;
18 import org.astrogrid.community.beans.v1.Account;
19 import org.astrogrid.community.beans.v1.Group;
20 import org.astrogrid.jes.delegate.JobSummary;
21 import org.astrogrid.workflow.beans.v1.Workflow;
22 import org.astrogrid.workflow.beans.v1.execution.JobURN;
23
24 import org.astrogrid.config.Config;
25 import org.astrogrid.config.SimpleConfig;
26
27 import org.astrogrid.community.client.policy.service.PolicyServiceDelegate;
28 import org.astrogrid.community.client.policy.service.PolicyServiceMockDelegate;
29 import org.astrogrid.community.client.policy.service.PolicyServiceSoapDelegate;
30
31 import org.astrogrid.community.common.policy.data.PolicyPermission ;
32 import org.astrogrid.community.common.policy.data.PolicyCredentials ;
33
34 import org.astrogrid.community.common.exception.CommunityIdentifierException;
35 import org.astrogrid.community.common.exception.CommunityServiceException;
36 import org.astrogrid.community.common.exception.CommunityPolicyException;
37
38 import org.astrogrid.community.User;
39 import org.astrogrid.store.Ivorn;
40
41 import java.util.Map;
42 import java.util.HashMap;
43 import java.net.MalformedURLException;
44
45 import org.apache.avalon.framework.parameters.Parameters;
46 import org.apache.cocoon.acting.AbstractAction;
47 import org.apache.cocoon.environment.Request;
48 import org.apache.cocoon.environment.Session;
49 import org.apache.cocoon.environment.Redirector;
50 import org.apache.cocoon.environment.SourceResolver;
51 import org.apache.cocoon.environment.ObjectModelHelper;
52
53 /***
54 * A Cocoon action to handle our workflow design commands.
55 *
56 */
57 public class JesAction extends AbstractAction {
58
59 /*** Compile-time switch used to turn tracing on/off.
60 * Set this to false to eliminate all trace statements within the byte code.
61 */
62 private static final boolean TRACE_ENABLED = true;
63
64 /*** Compile-time switch used to turn certain debugging statements on/off.
65 * Set this to false to eliminate these debugging statements
66 * within the byte code.
67 */
68 private static final boolean DEBUG_ENABLED = true;
69
70 private static Logger logger = Logger.getLogger( JesAction.class );
71
72 /***
73 * Name of JNDI property holding security delegate endpoint URL
74 */
75 public static final String ORG_ASTROGRID_PORTAL_COMMUNITY_URL = "org.astrogrid.portal.community.url";
76
77
78 private static final String
79 ASTROGRIDERROR_JOB_PERMISSION_DENIED = "AGWKFE00060",
80 ASTROGRIDERROR_PASSTHRU = "AGWKFE00100";
81
82 /***
83 * Cocoon param for the user param in the session.
84 *
85 */
86 public static final String USER_PARAM_NAME = "user-param";
87
88 /***
89 * Cocoon param for the user param in the session.
90 *
91 */
92 public static final String COMMUNITY_PARAM_TAG = "community";
93
94 /***
95 * Http request param for the action.
96 *
97 */
98 public static final String ACTION_PARAM_TAG = "action";
99
100 /***
101 * Http request param to confirm an action.
102 *
103 */
104 public static final String CONFIRM_PARAM_TAG = "confirm";
105
106 public static final String HTTP_JOBLIST_TAG = "job-list-tag" ,
107 JOBURN_PARAMETER = "jobURN" ,
108 HTTP_WORKFLOW_TAG = "workflow-tag",
109 HTTP_WORKFLOW_STATUS_TAG = "workflow-staus-tag",
110 COMMUNITY_ACCOUNT_TAG = "user" ,
111 COMMUNITY_NAME_TAG = "community_name" ,
112 CREDENTIAL_TAG = "credential" ,
113 COMMUNITY_TOKEN_TAG = "community-token",
114 USER_TAG = "user";
115
116 public static final String ERROR_MESSAGE_PARAMETER = "ErrorMessage";
117
118 public static final String ACTION_CANCEL_JOB = "cancel-job",
119 ACTION_DELETE_JOB = "delete-job",
120 ACTION_READ_JOB_LIST = "read-job-list",
121 ACTION_READ_JOB = "read-job";
122
123 public static final String AUTHORIZATION_RESOURCE_JOB = "job" ,
124 AUTHORIZATION_ACTION_EDIT = "edit";
125
126 public static final String USERID_COMMUNITY_SEPARATOR = "@";
127
128
129 /***
130 * Our action method.
131 *
132 */
133 public Map act ( Redirector redirector,
134 SourceResolver resolver,
135 Map objectModel,
136 String source,
137 Parameters params ) {
138 if( TRACE_ENABLED ) trace( "JesAction.act() entry" );
139
140 JesActionImpl myAction = null;
141 Map retMap = null;
142
143 try {
144
145 myAction = new JesActionImpl( redirector,
146 resolver,
147 objectModel,
148 source,
149 params );
150
151 retMap = myAction.act();
152
153 }
154 catch ( Exception ex ) {
155 ex.printStackTrace();
156 }
157 finally {
158 if( TRACE_ENABLED ) trace( "JesAction.act() exit" );
159 }
160
161 return retMap;
162
163 }
164
165
166 private class JesActionImpl {
167
168 private Redirector redirector;
169 private SourceResolver resolver;
170 private Map objectModel,
171 results;
172 private String source;
173 private Parameters params;
174 private Request request;
175 private Session session;
176 private WorkflowManager workflowManager;
177 private String userid,
178 community,
179 group,
180 token;
181 private String action;
182 private boolean bConfirm;
183 private String template;
184 private Credentials credentials;
185 private User user;
186
187 public JesActionImpl( Redirector redirector,
188 SourceResolver resolver,
189 Map objectModel,
190 String source,
191 Parameters params ) {
192 if( TRACE_ENABLED ) trace( "JesActionImpl() entry" );
193
194 try {
195
196 this.redirector = redirector;
197 this.resolver = resolver;
198 this.objectModel = objectModel;
199 this.source = source;
200 this.params = params;
201
202 this.results = new HashMap();
203
204
205 this.request = ObjectModelHelper.getRequest( objectModel );
206 this.session = request.getSession();
207
208 WorkflowManagerFactory wmFactory = new WorkflowManagerFactory();
209 this.workflowManager = wmFactory.getManager() ;
210
211
212 this.retrieveUserDetails();
213
214 this.action = request.getParameter( ACTION_PARAM_TAG );
215 this.bConfirm = new Boolean (
216 request.getParameter(CONFIRM_PARAM_TAG) ).booleanValue();
217
218 }
219 catch( WorkflowInterfaceException wix ) {
220 wix.printStackTrace();
221 }
222 finally {
223 if( TRACE_ENABLED ) trace( "JesActionImpl() exit" );
224 }
225
226 }
227
228
229 public Map act() {
230 if( TRACE_ENABLED ) trace( "JesActionImpl.act() entry" );
231
232 try {
233
234 debug( "action is: " + action );
235
236 this.consistencyCheck();
237
238 if( action == null ){
239 debug( "action is null");
240 }
241 else if( action.equals( ACTION_CANCEL_JOB ) ) {
242 this.cancelJob();
243 this.readJobList();
244 }
245 else if( action.equals( ACTION_DELETE_JOB ) ) {
246 this.deleteJob() ;
247 this.readJobList();
248 }
249 else if( action.equals( ACTION_READ_JOB_LIST ) ) {
250 this.readJobList();
251 }
252 else if( action.equals( ACTION_READ_JOB ) ) {
253 this.readJob();
254 }
255 else {
256 debug( "unsupported action");
257 }
258
259 }
260 catch( ConsistencyException cex ) {
261 debug( "ConsistencyException occurred");
262 }
263
264 catch( Exception ex) {
265 debug( "Exception: ex" );
266 ex.printStackTrace();
267 }
268
269 catch( Throwable th ) {
270 debug( "Throwable th" );
271 th.printStackTrace();
272 }
273 finally {
274 if( TRACE_ENABLED ) trace( "JesActionImpl.act() exit" );
275 }
276
277 return results;
278
279 }
280
281
282
283
284
285 private void retrieveUserDetails() {
286 if( TRACE_ENABLED ) trace( "JesActionImpl.retrieveUserDetails() entry" );
287
288 try {
289
290
291 String fullUserid = (String)session.getAttribute( USER_TAG );
292 this.userid = fullUserid.substring( fullUserid.lastIndexOf('/')+1 );
293 this.community = fullUserid.substring( fullUserid.indexOf('/')+2, fullUserid.lastIndexOf('/') );
294 this.group = this.community;
295
296 debug( "userid: " + this.userid );
297
298 debug( "community: " + this.community );
299
300 debug( "group: " + this.group );
301
302
303
304 debug( "token: " + this.token );
305
306 Account account = new Account();
307 account.setName( userid );
308 account.setCommunity( community );
309
310 Group group = new Group();
311 group.setName( this.userid );
312 group.setCommunity( community );
313
314 this.credentials = new Credentials();
315 credentials.setAccount( account );
316 credentials.setGroup( group );
317 credentials.setSecurityToken( "dummy" );
318
319
320 this.user = new User( this.userid, this.community, this.group, this.token );
321
322 }
323 finally {
324 if( TRACE_ENABLED ) trace( "JesActionImpl.retrieveUserDetails() exit" );
325 }
326
327 }
328
329 /***
330 * Get the policy delegate.
331 * Looks for the property org.astrogrid.portal.community.url
332 * if this property is not found (or is set to "dummy")
333 * then a mock delegate is returned.
334 * @return either a genuine or mock delegate
335 * @throws MalformedURLException if the url is malformed
336 */
337 private PolicyServiceDelegate getPolicyDelegate() throws MalformedURLException {
338 final Config config = SimpleConfig.getSingleton();
339 final String endpoint = config.getString(ORG_ASTROGRID_PORTAL_COMMUNITY_URL, "dummy");
340 if ("dummy".equals(endpoint)) {
341 debug("Using dummy delegate");
342 return new PolicyServiceMockDelegate();
343 } else {
344 debug("Using delegate at "+endpoint);
345 return new PolicyServiceSoapDelegate(endpoint);
346 }
347 }
348
349
350 private void consistencyCheck() throws ConsistencyException {
351 if( TRACE_ENABLED ) trace( "consistencyCheck() entry" );
352 debug( "userid: " + userid );
353 debug( "community: " + community );
354 debug( "name: " );
355 debug( "description: " );
356
357 if( userid == null ) {
358 ;
359 throw new ConsistencyException();
360 }
361 else if( action == null ) {
362
363 debug( "action is null" );
364
365
366 }
367 if( TRACE_ENABLED ) trace( "consistencyCheck()) exit" );
368 }
369
370
371 private void readJobList() {
372 if( TRACE_ENABLED ) trace( "JesActionImpl.readJobList() entry" );
373
374 try {
375
376
377
378
379
380 JobExecutionService jes = workflowManager.getJobExecutionService();
381 JobSummary[] jobSummaries = jes.readJobList( credentials.getAccount() ) ;
382 Workflow[] workflows = new Workflow[ jobSummaries.length ];
383
384 for( int i=0; i<workflows.length; i++ ) {
385 try
386 {
387 workflows[i] = jes.readJob( jobSummaries[i].getJobURN() );
388 }
389 catch(Exception e)
390 {
391
392
393 }
394 }
395
396 this.request.setAttribute( HTTP_JOBLIST_TAG, workflows );
397 }
398 catch( WorkflowInterfaceException wix ) {
399 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, wix.toString() ) ;
400 }
401 catch( Exception ex ) {
402 ex.printStackTrace() ;
403 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, "permission denied" );
404
405 }
406 finally {
407 if( TRACE_ENABLED )
408 trace( "JesActionImpl.readJobList() exit" );
409 }
410
411 }
412
413
414 private void cancelJob() {
415 if( TRACE_ENABLED ) trace( "JesActionImpl.cancelJob() entry" );
416
417 JobURN jobURN = new JobURN();
418
419 try
420 {
421 String jobURNString = request.getParameter( JOBURN_PARAMETER ) ;
422
423 if( jobURNString == null ) {
424 debug("jobURN string is null") ;
425 throw new ConsistencyException() ;
426 }
427 else{
428 jobURN.setContent( jobURNString );
429 JobExecutionService jes = workflowManager.getJobExecutionService();
430 jes.cancelJob( jobURN ) ;
431 }
432 }
433
434 catch( WorkflowInterfaceException wix ) {
435 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, wix.toString() ) ;
436 }
437 catch( Exception ex ) {
438 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, "permission denied" );
439 }
440 finally {
441 if( TRACE_ENABLED )
442 trace( "JesActionImpl.cancelJob() exit" );
443 }
444
445 }
446
447
448 private void deleteJob() {
449 if( TRACE_ENABLED ) trace( "JesActionImpl.deleteJob() entry" );
450
451 JobURN jobURN = new JobURN();
452
453 try {
454 String jobURNString = request.getParameter( JOBURN_PARAMETER ) ;
455
456 if( jobURNString == null ) {
457 debug("jobURN string is null") ;
458 throw new ConsistencyException() ;
459 }
460 else{
461 debug("jobURN: " + jobURNString ) ;
462 jobURN.setContent( jobURNString );
463 JobExecutionService jes = workflowManager.getJobExecutionService();
464 jes.deleteJob( jobURN ) ;
465 }
466 }
467
468 catch( WorkflowInterfaceException wix ) {
469 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, wix.toString() ) ;
470 }
471 catch( Exception ex ) {
472 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, "permission denied" );
473 }
474 finally {
475 if( TRACE_ENABLED )
476 trace( "JesActionImpl.deleteJob() exit" );
477 }
478
479 }
480
481
482 private void readJob() {
483 if( TRACE_ENABLED ) trace( "JesActionImpl.readJob() entry" );
484
485 Workflow workflow = null;
486 JobURN jobURN = new JobURN();
487
488 try {
489
490 String jobURNString = request.getParameter( JOBURN_PARAMETER ) ;
491
492 if( jobURNString == null ) {
493 debug("jobURN string is null") ;
494 throw new ConsistencyException() ;
495 }
496 else{
497 jobURN.setContent( jobURNString );
498 JobExecutionService jes = workflowManager.getJobExecutionService();
499 workflow = jes.readJob( jobURN ) ;
500 }
501
502 if (workflow != null ){
503 debug( "name: " + workflow.getName() ) ;
504 debug( "description: " + workflow.getDescription() ) ;
505
506
507 debug( "about to set workflow session attribute..." ) ;
508 session.setAttribute( HTTP_WORKFLOW_STATUS_TAG, workflow ) ;
509 }
510
511 }
512 catch( WorkflowInterfaceException wix ) {
513 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, wix.toString() ) ;
514 }
515 catch( Exception ex ) {
516 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, "permission denied" );
517 }
518 finally {
519 if( TRACE_ENABLED )
520 trace( "JesActionImpl.readJob() exit" );
521 }
522
523 }
524
525
526 private void checkPermissions ( String someResource, String anAction )
527 throws CommunityServiceException,
528 CommunityPolicyException,
529 CommunityIdentifierException {
530 if( TRACE_ENABLED ) trace( "JesActionImpl.checkPermission() entry" ) ;
531
532 PolicyServiceDelegate ps = null;
533 PolicyCredentials pCredentials = null;
534 PolicyPermission pPermission = null;
535 this.credentials = new Credentials();
536
537 try {
538 ps = getPolicyDelegate () ;
539 pCredentials = new PolicyCredentials();
540 pCredentials.setAccount( this.credentials.getAccount().getName() );
541 pCredentials.setGroup( this.credentials.getGroup().getName() );
542 pPermission = ps.checkPermissions( pCredentials, someResource, anAction ) ;
543
544 }
545 catch( MalformedURLException muex ) {
546 muex.printStackTrace();
547 }
548 finally {
549 if( TRACE_ENABLED ) trace( "JesActionImpl.checkPermission() exit" ) ;
550 }
551
552 }
553
554
555 }
556
557
558 private class ConsistencyException extends Exception {
559 }
560
561
562 private void trace( String traceString ) {
563
564 System.out.println( traceString );
565 }
566
567 private void debug( String logString ){
568
569 System.out.println( logString );
570 }
571
572
573 }