KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > doctaskrunner > serverimpl > TaskRunner


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

16 package org.outerj.daisy.doctaskrunner.serverimpl;
17
18 import org.outerj.daisy.doctaskrunner.TaskSpecification;
19 import org.outerj.daisy.doctaskrunner.DocumentSelection;
20 import org.outerj.daisy.doctaskrunner.DocumentExecutionState;
21 import org.outerj.daisy.doctaskrunner.TaskState;
22 import org.outerj.daisy.repository.VariantKey;
23 import org.outerj.daisy.repository.Repository;
24 import org.mozilla.javascript.Context;
25 import org.mozilla.javascript.Scriptable;
26 import org.mozilla.javascript.Script;
27 import org.mozilla.javascript.ScriptableObject;
28
29 import java.io.StringWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.Arrays JavaDoc;
33
34 import EDU.oswego.cs.dl.util.concurrent.Sync;
35
36 public class TaskRunner implements Runnable JavaDoc {
37     private DocumentSelection documentSelection;
38     private TaskSpecification taskSpecification;
39     private TaskContext taskContext;
40     private Repository repository;
41
42     public TaskRunner(DocumentSelection documentSelection, TaskSpecification taskSpecification,
43                       TaskContext taskContext, Repository repository) {
44         this.documentSelection = documentSelection;
45         this.taskSpecification = taskSpecification;
46         this.taskContext = taskContext;
47         this.repository = repository;
48     }
49
50     public void run() {
51         try {
52             boolean hasErrors = false;
53             Context cx = Context.enter();
54             try {
55                 if (!"javascript".equalsIgnoreCase(taskSpecification.getScriptLanguage()))
56                     throw new Exception JavaDoc("Unsupported script language: \"" + taskSpecification.getScriptLanguage() + "\".");
57
58                 taskContext.setTaskState(TaskState.RUNNING, "Collecting documents to process", null);
59                 VariantKey[] variantKeys = documentSelection.getKeys(repository);
60                 Arrays.sort(variantKeys);
61                 taskContext.initDocumentResults(variantKeys);
62
63                 cx.setOptimizationLevel(-1);
64                 taskContext.setProgress("Compiling script");
65                 Script script;
66                 try {
67                     script = cx.compileString(taskSpecification.getScript(), "", 1, null);
68                 } catch (Exception JavaDoc e) {
69                     String JavaDoc details = createExceptionDescription(e);
70                     taskContext.setTaskState(TaskState.INTERRUPTED_BY_ERROR, "", details);
71                     return;
72                 }
73
74                 Sync executionLock = taskContext.getExecutionLock();
75                 int currentPercentage = 0;
76                 taskContext.setProgress(formatProgress(0));
77                 for (int i = 0; i < variantKeys.length; i++) {
78                     if (taskContext.isInterrupted()) {
79                         taskContext.setTaskState(TaskState.INTERRUPTED_BY_USER, formatProgress(currentPercentage), null);
80                         return;
81                     }
82
83                     // create a new scope
84
Scriptable scope = cx.initStandardObjects();
85
86                     // make some stuff available to the script
87
Object JavaDoc wrappedRepository = Context.javaToJS(repository, scope);
88                     ScriptableObject.putProperty(scope, "repository", wrappedRepository);
89                     Object JavaDoc wrappedKey = Context.javaToJS(variantKeys[i], scope);
90                     ScriptableObject.putProperty(scope, "variantKey", wrappedKey);
91
92                     // execute the script
93
Exception JavaDoc exception = null;
94                     executionLock.acquire();
95                     try {
96                         script.exec(cx, scope);
97                     } catch (Exception JavaDoc e) {
98                         exception = e;
99                     } finally {
100                         executionLock.release();
101                     }
102
103                     // update result state for this document variant
104
if (exception != null) {
105                         String JavaDoc details = createExceptionDescription(exception);
106                         taskContext.setDocumentResult(variantKeys[i], DocumentExecutionState.ERROR, details);
107                     } else {
108                         taskContext.setDocumentResult(variantKeys[i], DocumentExecutionState.DONE, null);
109                     }
110
111                     if (exception != null && taskSpecification.stopOnFirstError()) {
112                         taskContext.setTaskState(TaskState.INTERRUPTED_BY_ERROR, formatProgress(currentPercentage), "");
113                         return;
114                     } else if (exception != null) {
115                         hasErrors = true;
116                     }
117
118                     // calculate percentage and update progress indication
119
int percentage = (int)(((double)(i+1) / (double)variantKeys.length) * 100);
120                     if (percentage != currentPercentage) {
121                         currentPercentage = percentage;
122                         taskContext.setProgress(formatProgress(currentPercentage));
123                     }
124                 }
125             } catch (Throwable JavaDoc e) {
126                 String JavaDoc details = createExceptionDescription(e);
127                 taskContext.setTaskState(TaskState.INTERRUPTED_BY_ERROR, "", details);
128                 return;
129             } finally {
130                 Context.exit();
131             }
132
133             taskContext.setTaskState(hasErrors ? TaskState.FINISHED_WITH_ERRORS : TaskState.FINISHED, "", null);
134         } finally {
135             taskContext.cleanup();
136         }
137     }
138
139     private String JavaDoc formatProgress(int percentage) {
140         return percentage + "%";
141     }
142
143     private String JavaDoc createExceptionDescription(Throwable JavaDoc throwable) {
144         Writer JavaDoc writer = new StringWriter JavaDoc();
145         throwable.printStackTrace(new PrintWriter JavaDoc(writer));
146         return writer.toString();
147     }
148 }
149
Popular Tags