001: /*
002: * $Id: BackgroundProcess.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.interceptor;
022:
023: import java.io.Serializable;
024:
025: import com.opensymphony.xwork2.ActionInvocation;
026:
027: /**
028: * Background thread to be executed by the ExecuteAndWaitInterceptor.
029: *
030: */
031: public class BackgroundProcess implements Serializable {
032:
033: private static final long serialVersionUID = 3884464776311686443L;
034:
035: protected Object action;
036: protected ActionInvocation invocation;
037: protected String result;
038: protected Exception exception;
039: protected boolean done;
040:
041: /**
042: * Constructs a background process
043: *
044: * @param threadName The thread name
045: * @param invocation The action invocation
046: * @param threadPriority The thread priority
047: */
048: public BackgroundProcess(String threadName,
049: final ActionInvocation invocation, int threadPriority) {
050: this .invocation = invocation;
051: this .action = invocation.getAction();
052: try {
053: final Thread t = new Thread(new Runnable() {
054: public void run() {
055: try {
056: beforeInvocation();
057: result = invocation.invokeActionOnly();
058: afterInvocation();
059: } catch (Exception e) {
060: exception = e;
061: }
062:
063: done = true;
064: }
065: });
066: t.setName(threadName);
067: t.setPriority(threadPriority);
068: t.start();
069: } catch (Exception e) {
070: exception = e;
071: }
072: }
073:
074: /**
075: * Called before the background thread determines the result code
076: * from the ActionInvocation.
077: *
078: * @throws Exception any exception thrown will be thrown, in turn, by the ExecuteAndWaitInterceptor
079: */
080: protected void beforeInvocation() throws Exception {
081: }
082:
083: /**
084: * Called after the background thread determines the result code
085: * from the ActionInvocation, but before the background thread is
086: * marked as done.
087: *
088: * @throws Exception any exception thrown will be thrown, in turn, by the ExecuteAndWaitInterceptor
089: */
090: protected void afterInvocation() throws Exception {
091: }
092:
093: /**
094: * Retrieves the action.
095: *
096: * @return the action.
097: */
098: public Object getAction() {
099: return action;
100: }
101:
102: /**
103: * Retrieves the action invocation.
104: *
105: * @return the action invocation
106: */
107: public ActionInvocation getInvocation() {
108: return invocation;
109: }
110:
111: /**
112: * Gets the result of the background process.
113: *
114: * @return the result; <tt>null</tt> if not done.
115: */
116: public String getResult() {
117: return result;
118: }
119:
120: /**
121: * Gets the exception if any was thrown during the execution of the background process.
122: *
123: * @return the exception or <tt>null</tt> if no exception was thrown.
124: */
125: public Exception getException() {
126: return exception;
127: }
128:
129: /**
130: * Returns the status of the background process.
131: *
132: * @return <tt>true</tt> if finished, <tt>false</tt> otherwise
133: */
134: public boolean isDone() {
135: return done;
136: }
137: }
|