KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > BackgroundTaskExecutor


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.books.publisher.impl;
17
18 import org.apache.cocoon.Constants;
19 import org.apache.cocoon.Processor;
20 import org.apache.cocoon.components.CocoonComponentManager;
21 import org.apache.avalon.framework.context.Context;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.framework.logger.Logger;
24 import org.apache.avalon.framework.component.WrapperComponentManager;
25
26 import java.util.Date JavaDoc;
27
28 public class BackgroundTaskExecutor implements Runnable JavaDoc {
29     private final ServiceManager serviceManager;
30     private final Context context;
31     private final Logger logger;
32     private final BookPublishTask bookPublishTask;
33     private String JavaDoc taskId;
34     private BookPublisherImpl owner;
35     private Date JavaDoc started = new Date JavaDoc();
36
37     public BackgroundTaskExecutor(BookPublishTask bookPublishTask, BookPublisherImpl owner, Logger logger, Context context, ServiceManager serviceManager) {
38         this.bookPublishTask = bookPublishTask;
39         this.logger = logger;
40         this.context = context;
41         this.serviceManager = serviceManager;
42         this.owner = owner;
43     }
44
45     public void setTaskId(String JavaDoc taskId) {
46         this.taskId = taskId;
47     }
48
49     public String JavaDoc getTaskId() {
50         return taskId;
51     }
52
53     public BookPublishTask getBookPublishTask() {
54         return bookPublishTask;
55     }
56
57     public Date JavaDoc getStarted() {
58         return started;
59     }
60
61     /**
62      * Note: setTaskId must be called before run!
63      */

64     public void run() {
65         try {
66             started = new Date JavaDoc();
67             org.apache.cocoon.environment.Context envContext =
68                     (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
69
70             BackgroundEnvironment env = new BackgroundEnvironment(logger, envContext);
71
72             Processor processor = (Processor) serviceManager.lookup(Processor.ROLE);
73
74             Object JavaDoc key = CocoonComponentManager.startProcessing(env);
75             CocoonComponentManager.enterEnvironment(env, new WrapperComponentManager(serviceManager), processor);
76
77             try {
78                 bookPublishTask.run();
79             } finally {
80                 CocoonComponentManager.leaveEnvironment();
81                 CocoonComponentManager.endProcessing(env, key);
82                 serviceManager.release(processor);
83             }
84         } catch (Throwable JavaDoc e) {
85             logger.error("Unexpected error in book publication background thread.", e);
86         } finally {
87             try {
88                 bookPublishTask.getBookInstance().unlock();
89             } catch (Throwable JavaDoc e) {
90                 logger.error("Error unlocking book instance " + bookPublishTask.getBookInstance().getName(), e);
91             }
92             owner.taskEnded(taskId);
93         }
94     }
95 }
96
Popular Tags