1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */1718packageorg.apache.jetspeed.aggregator.impl;
1920import java.security.AccessControlContext;
21import java.security.PrivilegedAction;
2223import javax.security.auth.Subject;
2425import org.apache.commons.logging.Log;
26import org.apache.commons.logging.LogFactory;
27import org.apache.jetspeed.aggregator.RenderingJob;
28import org.apache.jetspeed.aggregator.Worker;
29import org.apache.jetspeed.aggregator.WorkerMonitor;
30import org.apache.jetspeed.security.JSSubject;
3132/***33 * Worker thread processes jobs and notify its WorkerMonitor when completed.34 * When no work is available, the worker simply sets itself in a waiting mode35 * pending reactivation by the WorkerMonitor36 *37 * @author <a href="mailto:raphael@apache.org">Raphael Luta</a>38 * @author <a>Woonsan Ko</a>39 * @version $Id: WorkerImpl.java 587064 2007-10-22 11:54:11Z woonsan $40 */41publicclassWorkerImplextends Thread implements Worker
42 {
43/*** Commons logging */44protectedfinalstatic Log log = LogFactory.getLog(WorkerImpl.class);
4546/*** Running status of this worker */47privateboolean running = true;
4849/*** Counter of consecutive jobs that can be processed before the50 worker being actually put back on the idle queue */51privateint jobCount = 0;
5253/*** Job to process */54private Runnable job = null;
5556/*** Context to process job within */57private AccessControlContext context = null;
5859/*** Monitor for this Worker */60private WorkerMonitor monitor = null;
6162publicWorkerImpl(WorkerMonitor monitor)
63 {
64super();
65this.setMonitor(monitor);
66this.setDaemon(true);
67 }
6869publicWorkerImpl(WorkerMonitor monitor, ThreadGroup tg, String name)
70 {
71super(tg, name);
72this.setMonitor(monitor);
73this.setDaemon(true);
74 }
7576/***77 * Return the number of jobs processed by this worker since the last time it78 * has been on the idle queue79 */80publicint getJobCount()
81 {
82returnthis.jobCount;
83 }
8485/***86 * Reset the processed job counter87 */88publicvoid resetJobCount()
89 {
90this.jobCount=0;
91 }
9293/***94 * Sets the running status of this Worker. If set to false, the Worker will95 * stop after processing its current job.96 */97publicvoid setRunning(boolean status)
98 {
99this.running = status;
100 }
101102/***103 * Sets the moitor of this worker104 */105publicvoid setMonitor(WorkerMonitor monitor)
106 {
107this.monitor = monitor;
108 }
109110/***111 * Sets the job to execute in security context112 */113publicvoid setJob(Runnable job, AccessControlContext context)
114 {
115this.job = job;
116this.context = context;
117 }
118119/***120 * Sets the job to execute121 */122publicvoid setJob(Runnable job)
123 {
124this.job = job;
125this.context = null;
126 }
127128/***129 * Retrieves the job to execute130 */131public Runnable getJob()
132 {
133returnthis.job;
134 }
135136/***137 * Process the job assigned, then notify Monitor. If no job available,138 * go into sleep mode139 */140publicvoid run()
141 {
142while (running)
143 {
144// wait for a job to come145synchronized (this)
146 {
147if (this.job == null)
148 {
149try150 {
151this.wait();
152 }
153catch (InterruptedException e)
154 {
155// nothing done156 }
157 }
158 }
159160// process it161if (this.job != null)
162 {
163 log.debug("Processing job for window :" + ((RenderingJob)job).getWindow().getId());
164 Subject subject = null;
165if (this.context != null)
166 {
167 subject = JSSubject.getSubject(this.context);
168 }
169if (subject != null)
170 {
171 JSSubject.doAsPrivileged(subject, new PrivilegedAction()
172 {
173public Object run()
174 {
175try176 {
177 WorkerImpl.this.job.run();
178 }
179catch (Throwable t)
180 {
181 log.error("Thread error", t);
182 }
183returnnull;
184 }
185 }, this.context);
186 }
187else188 {
189try190 {
191this.job.run();
192 }
193catch (Throwable t)
194 {
195 log.error("Thread error", t);
196 }
197 }
198 }
199200this.jobCount++;
201202// release the worker203 ((WorkerMonitorImpl) monitor).release(this);
204 }
205 }
206207 }