View Javadoc

1   package org.xvsm.internal.tasks;
2   
3   import org.xvsm.core.VoidEntry;
4   import org.xvsm.core.aspect.AspectContext;
5   import org.xvsm.core.aspect.GlobalIPoint;
6   import org.xvsm.internal.EventProcessingPool;
7   import org.xvsm.internal.GlobalAspectManager;
8   import org.xvsm.internal.ReplySenderPool;
9   import org.xvsm.internal.TimeoutSchedulerPool;
10  import org.xvsm.internal.exceptions.AspectNotOkException;
11  import org.xvsm.internal.exceptions.AspectRescheduleException;
12  import org.xvsm.internal.exceptions.AspectSkipException;
13  import org.xvsm.internal.exceptions.FatalException;
14  import org.xvsm.internal.exceptions.XCoreRemoteException;
15  import org.xvsm.remote.TransportHandler;
16  import org.xvsm.remote.interfaces.ITransportSender;
17  
18  /***
19   * 
20   * @author Christian Schreiber, Michael Proestler
21   * 
22   */
23  public class ShutdownTask extends Task {
24  
25  	/***
26  	 * Auto generated serial version uid.
27  	 */
28  	private static final long serialVersionUID = 8527533614953903594L;
29  
30  	/***
31  	 * Default constructor.
32  	 * 
33  	 */
34  	public ShutdownTask() {
35  		super();
36  	}
37  
38  	/***
39  	 * {@inheritDoc}
40  	 */
41  	public void run() {
42  		Thread.currentThread().setName(this.getClass().getName());
43  		new ShutdownThread(this).start();
44  	}
45  }
46  
47  class ShutdownThread extends Thread {
48  	private ShutdownTask task;
49  
50  	public ShutdownThread(ShutdownTask task) {
51  		this.task = task;
52  	}
53  
54  	public void run() {
55  		Thread.currentThread().setName(this.getClass().getName());
56  		try {
57  			Thread.sleep(2000);
58  		} catch (InterruptedException e1) {
59  			throw new FatalException(e1);
60  		}
61  		boolean skip = false;
62  		try {
63  			AspectContext acontext = new AspectContext(null, null, null, null,
64  					null, null, 0, this.task.getAspectContext());
65  			acontext.setGlobalIPoint(GlobalIPoint.PreCoreShutdown);
66  			GlobalAspectManager.execute(acontext);
67  		} catch (AspectNotOkException e) {
68  			return;
69  		} catch (AspectRescheduleException e) {
70  			EventProcessingPool.getInstance().execute(this.task);
71  			return;
72  		} catch (AspectSkipException e) {
73  			skip = true;
74  		}
75  		if (!skip) {
76  			EventProcessingPool.getInstance().shutdown();
77  			TimeoutSchedulerPool.shutdown();
78  			ReplySenderPool.shutdown();
79  			TransportHandler.getInstance().shutdown();
80  
81  			if (this.task.getAnswerToContainer() != null) {
82  				// all thread pools are shut down so we have to do this manually
83  				@SuppressWarnings("unchecked")
84  				ITransportSender s = TransportHandler.getInstance().getSender(
85  						this.task.getAnswerToContainer());
86  				try {
87  					s.sendResponse(this.task.getAnswerToContainer(),
88  							new ReplyTask(new VoidEntry(), this.task
89  									.getAnswerToContainer(), this.task.getTaskID()));
90  				} catch (XCoreRemoteException e) {
91  					throw new FatalException(e);
92  				}
93  			} else {
94  				this.task.setResult(new VoidEntry());
95  			}
96  
97  			try {
98  				Thread.sleep(1000);
99  			} catch (InterruptedException e) {
100 				throw new FatalException(e);
101 			} finally {
102 				// System.exit(0);
103 			}
104 		}
105 	}
106 }