KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > core > Thread


1 /*
2  *
3  * ====================================================================
4  *
5  * Copyright 2004 The Apache Software Foundation
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */

20 package org.apache.slide.projector.processor.core;
21
22 import java.util.Map JavaDoc;
23
24 import org.apache.slide.projector.Context;
25 import org.apache.slide.projector.Processor;
26 import org.apache.slide.projector.Result;
27 import org.apache.slide.projector.URI;
28 import org.apache.slide.projector.descriptor.BooleanValueDescriptor;
29 import org.apache.slide.projector.descriptor.MapValueDescriptor;
30 import org.apache.slide.projector.descriptor.ParameterDescriptor;
31 import org.apache.slide.projector.descriptor.ResultDescriptor;
32 import org.apache.slide.projector.descriptor.StateDescriptor;
33 import org.apache.slide.projector.descriptor.URIValueDescriptor;
34 import org.apache.slide.projector.descriptor.XMLValueDescriptor;
35 import org.apache.slide.projector.engine.Job;
36 import org.apache.slide.projector.engine.ProcessorManager;
37 import org.apache.slide.projector.engine.Scheduler;
38 import org.apache.slide.projector.expression.Expression;
39 import org.apache.slide.projector.expression.ExpressionFactory;
40 import org.apache.slide.projector.i18n.ParameterMessage;
41 import org.apache.slide.projector.value.BooleanValue;
42 import org.apache.slide.projector.value.MapValue;
43 import org.apache.slide.projector.value.NullValue;
44 import org.apache.slide.projector.value.URIValue;
45 import org.apache.slide.projector.value.Value;
46 import org.apache.slide.projector.value.XMLValue;
47
48 /**
49 */

50 public class Thread implements Processor {
51     // parameters
52
public final static String JavaDoc PROCESSOR = "processor";
53     public final static String JavaDoc REPEAT = "repeat";
54     public final static String JavaDoc CONDITION = "condition";
55     public final static String JavaDoc REMAINING_CONDITION = "remainingCondition";
56     public final static String JavaDoc PARAMETERS = "parameters";
57     public final static String JavaDoc PERSISTENT = "persistent";
58     
59     private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[] {
60             new ParameterDescriptor(PROCESSOR, new ParameterMessage("job/parameter/processor"), new URIValueDescriptor()),
61             new ParameterDescriptor(REPEAT, new ParameterMessage("job/parameter/repeat"), new BooleanValueDescriptor(), BooleanValue.FALSE),
62             new ParameterDescriptor(PERSISTENT, new ParameterMessage("job/parameter/persistent"), new BooleanValueDescriptor(), BooleanValue.TRUE),
63             new ParameterDescriptor(CONDITION, new ParameterMessage("job/parameter/condition"), new XMLValueDescriptor()),
64             new ParameterDescriptor(REMAINING_CONDITION, new ParameterMessage("job/parameter/remainingCondition"), new XMLValueDescriptor(), NullValue.NULL),
65             new ParameterDescriptor(PARAMETERS, new ParameterMessage("job/parameter/parameters"), MapValueDescriptor.ANY)
66     };
67     
68     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(new StateDescriptor[] {
69         StateDescriptor.OK_DESCRIPTOR
70     });
71     
72     public Result process(Map JavaDoc parameters, Context context) throws Exception JavaDoc {
73             Map JavaDoc jobParameters = ((MapValue)parameters.get(PARAMETERS)).getMap();
74             URI jobUri = (URIValue)parameters.get(PROCESSOR);
75             XMLValue initialCondition = (XMLValue)parameters.get(CONDITION);
76             boolean repeatJob = ((BooleanValue)parameters.get(REPEAT)).booleanValue();
77             Value remainingConditionValue = (Value)parameters.get(REMAINING_CONDITION);
78             XMLValue remainingCondition = null;
79             if ( remainingConditionValue == NullValue.NULL ) {
80                 remainingCondition = initialCondition;
81             } else {
82                 remainingCondition = (XMLValue)remainingConditionValue;
83             }
84             boolean persistentJob = ((BooleanValue)parameters.get(PERSISTENT)).booleanValue();
85             Expression remainingExpression = ExpressionFactory.create(remainingCondition.getRootElement());
86             Expression initialExpression = ExpressionFactory.create(initialCondition.getRootElement());
87             Processor jobProcessor = ProcessorManager.getInstance().getProcessor(jobUri);
88             ProcessorManager.prepareValues(jobProcessor.getParameterDescriptors(), jobParameters, context);
89             String JavaDoc processorId = context.getProcessId();
90             Job job;
91             if ( processorId == null ) {
92                 job = new Job(context.getStep(), jobUri, initialExpression, remainingExpression, jobParameters, repeatJob, persistentJob);
93             } else {
94                 job = new Job(context.getProcessId()+":"+context.getStep(), jobUri, initialExpression, remainingExpression, jobParameters, repeatJob, persistentJob);
95             }
96             Scheduler.getInstance().registerJob(job);
97             Scheduler.getInstance().saveJobs();
98             return Result.OK;
99     }
100     
101     public ParameterDescriptor[] getParameterDescriptors() {
102         return parameterDescriptors;
103     }
104     
105     public ResultDescriptor getResultDescriptor() {
106         return resultDescriptor;
107     }
108 }
Popular Tags