KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > doctaskrunner > clientimpl > RemoteDocumentTaskManager


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.clientimpl;
17
18 import org.outerj.daisy.doctaskrunner.*;
19 import org.outerj.daisy.doctaskrunner.commonimpl.*;
20 import org.outerj.daisy.repository.VariantKey;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryImpl;
23 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
24 import org.apache.commons.httpclient.methods.PostMethod;
25 import org.apache.commons.httpclient.methods.GetMethod;
26 import org.apache.commons.httpclient.methods.DeleteMethod;
27 import org.apache.commons.httpclient.NameValuePair;
28 import org.outerx.daisy.x10Doctaskrunner.*;
29
30 import java.util.Date JavaDoc;
31
32 public class RemoteDocumentTaskManager implements DocumentTaskManager {
33     private RemoteRepositoryImpl repository;
34
35     public RemoteDocumentTaskManager(RemoteRepositoryImpl repository) {
36         this.repository = repository;
37     }
38
39     public long runTask(DocumentSelection documentSelection, TaskSpecification taskSpecification) throws TaskException, RepositoryException {
40         DaisyHttpClient httpClient = repository.getHttpClient();
41         PostMethod method = new PostMethod("/doctaskrunner/task");
42
43         TaskDescriptionDocument taskDescriptionDocument = TaskDescriptionDocument.Factory.newInstance();
44         TaskDescriptionDocument.TaskDescription taskDescription = taskDescriptionDocument.addNewTaskDescription();
45         TaskDescriptionDocument.TaskDescription.DocumentSelection documentSelectionXml = taskDescription.addNewDocumentSelection();
46
47         if (documentSelection instanceof EnumerationDocumentSelection) {
48             EnumerationDocumentSelection enumDocumentSelection = (EnumerationDocumentSelection)documentSelection;
49             VariantKey[] variantKeys = enumDocumentSelection.getKeys();
50
51             TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration enumerationXml = documentSelectionXml.addNewEnumeration();
52             TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration.Docvariant[] docvariants = new TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration.Docvariant[variantKeys.length];
53
54             for (int i = 0; i < variantKeys.length; i++) {
55                 docvariants[i] = TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration.Docvariant.Factory.newInstance();
56                 docvariants[i].setDocumentId(variantKeys[i].getDocumentId());
57                 docvariants[i].setBranchId(variantKeys[i].getBranchId());
58                 docvariants[i].setLanguageId(variantKeys[i].getLanguageId());
59             }
60
61             enumerationXml.setDocvariantArray(docvariants);
62         } else if (documentSelection instanceof QueryDocumentSelection) {
63             QueryDocumentSelection queryDocumentSelection = (QueryDocumentSelection)documentSelection;
64             String JavaDoc query = queryDocumentSelection.getQuery();
65             documentSelectionXml.setQuery(query);
66         } else {
67             throw new TaskException("Encountered unsupported DocumentSelection implementation: " + documentSelection.getClass().getName());
68         }
69
70         TaskDescriptionDocument.TaskDescription.Specification taskSpecificationXml = taskDescription.addNewSpecification();
71         taskSpecificationXml.setStopOnFirstError(taskSpecification.stopOnFirstError());
72         taskSpecificationXml.setDescription(taskSpecification.getDescription());
73         if (taskSpecification instanceof SimpleActionsTaskSpecificationImpl) {
74             TaskDescriptionDocument.TaskDescription.Specification.SimpleActions simpleActionsXml = taskSpecificationXml.addNewSimpleActions();
75             ((SimpleActionsTaskSpecificationImpl)taskSpecification).addXml(simpleActionsXml);
76         } else {
77             TaskDescriptionDocument.TaskDescription.Specification.Script scriptXml = taskSpecificationXml.addNewScript();
78             scriptXml.setLanguage(taskSpecification.getScriptLanguage());
79             scriptXml.setStringValue(taskSpecification.getScript());
80         }
81
82         method.setRequestBody(taskDescriptionDocument.newInputStream());
83
84         TaskCreatedDocument taskCreatedDocument = (TaskCreatedDocument)httpClient.executeMethod(method, TaskCreatedDocument.class, true);
85         return taskCreatedDocument.getTaskCreated().getTaskId();
86     }
87
88     public Task getTask(long id) throws TaskException, RepositoryException {
89         DaisyHttpClient httpClient = repository.getHttpClient();
90         GetMethod method = new GetMethod("/doctaskrunner/task/" + id);
91
92         TaskDocument taskDocument = (TaskDocument)httpClient.executeMethod(method, TaskDocument.class, true);
93         return instantiateTaskFromXml(taskDocument.getTask());
94     }
95
96     private TaskImpl instantiateTaskFromXml(TaskDocument.Task taskXml) {
97         Date JavaDoc finishedAt = taskXml.isSetFinishedAt() ? taskXml.getFinishedAt().getTime() : null;
98         return new TaskImpl(taskXml.getId(), taskXml.getDescription(), TaskState.fromString(taskXml.getState().toString()),
99                 taskXml.getOwnerId(), taskXml.getProgress(), taskXml.getDetails(), taskXml.getScript().getStringValue(),
100                 taskXml.getScript().getLanguage(), taskXml.getStartedAt().getTime(), finishedAt);
101     }
102
103     public Tasks getTasks() throws TaskException, RepositoryException {
104         DaisyHttpClient httpClient = repository.getHttpClient();
105         GetMethod method = new GetMethod("/doctaskrunner/task");
106
107         TasksDocument tasksDocument = (TasksDocument)httpClient.executeMethod(method, TasksDocument.class, true);
108         TaskDocument.Task[] tasksXml = tasksDocument.getTasks().getTaskArray();
109
110         TaskImpl[] tasks = new TaskImpl[tasksXml.length];
111         for (int i = 0; i < tasksXml.length; i++) {
112             tasks[i] = instantiateTaskFromXml(tasksXml[i]);
113         }
114
115         return new TasksImpl(tasks);
116     }
117
118     public void deleteTask(long id) throws TaskException, RepositoryException {
119         DaisyHttpClient httpClient = repository.getHttpClient();
120         DeleteMethod method = new DeleteMethod("/doctaskrunner/task/" + id);
121         httpClient.executeMethod(method, null, true);
122     }
123
124     public void interruptTask(long id) throws TaskException, RepositoryException {
125         DaisyHttpClient httpClient = repository.getHttpClient();
126         PostMethod method = new PostMethod("/doctaskrunner/task/" + id);
127
128         NameValuePair[] queryString = {new NameValuePair("action", "interrupt")};
129         method.setQueryString(queryString);
130         httpClient.executeMethod(method, null, true);
131     }
132
133     public TaskDocDetails getTaskDocDetails(long taskId) throws TaskException, RepositoryException {
134         DaisyHttpClient httpClient = repository.getHttpClient();
135         GetMethod method = new GetMethod("/doctaskrunner/task/" + taskId + "/docdetails");
136         TaskDocDetailsDocument taskDocDetailsDocument = (TaskDocDetailsDocument)httpClient.executeMethod(method, TaskDocDetailsDocument.class, true);
137         TaskDocDetailDocument.TaskDocDetail[] taskDocDetailsXml = taskDocDetailsDocument.getTaskDocDetails().getTaskDocDetailArray();
138
139         TaskDocDetailImpl[] taskDocDetails = new TaskDocDetailImpl[taskDocDetailsXml.length];
140         for (int i = 0; i < taskDocDetailsXml.length; i++) {
141             TaskDocDetailDocument.TaskDocDetail taskDocDetailXml = taskDocDetailsXml[i];
142             VariantKey variantKey = new VariantKey(taskDocDetailXml.getDocumentId(), taskDocDetailXml.getBranchId(), taskDocDetailXml.getLanguageId());
143             DocumentExecutionState state = DocumentExecutionState.fromString(taskDocDetailXml.getState().toString());
144             taskDocDetails[i] = new TaskDocDetailImpl(variantKey, state, taskDocDetailXml.getDetails());
145         }
146
147         return new TaskDocDetailsImpl(taskDocDetails);
148     }
149
150     public TaskSpecification createTaskSpecification(String JavaDoc description, String JavaDoc script, String JavaDoc scriptLanguage, boolean stopOnFirstError) {
151         return new TaskSpecificationImpl(description, script, scriptLanguage, stopOnFirstError);
152     }
153
154     public SimpleActionsTaskSpecification createSimpleActionsTaskSpecification(String JavaDoc description, boolean stopOnFirstError) {
155         return new SimpleActionsTaskSpecificationImpl(description, stopOnFirstError, repository);
156     }
157
158     public DocumentSelection createQueryDocumentSelection(String JavaDoc query) {
159         return new QueryDocumentSelection(query);
160     }
161
162     public DocumentSelection createEnumerationDocumentSelection(VariantKey[] variantKeys) {
163         return new EnumerationDocumentSelection(variantKeys);
164     }
165 }
166
Popular Tags