View Javadoc

1   /*
2    * Copyright © 2012 Eirik Bjornset.
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  
17  package net.grinderscript.dotnet.scriptengine;
18  
19  import grinderscript.net.core.IGrinderWorker;
20  import net.grinder.common.UncheckedGrinderException;
21  import net.grinder.scriptengine.ScriptEngineService.WorkerRunnable;
22  import net.grinder.scriptengine.ScriptExecutionException;
23  import net.grinder.util.Sleeper;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  public class DotNetWorkerRunnable implements WorkerRunnable {
29  
30  	private final Logger logger = LoggerFactory
31  			.getLogger(DotNetWorkerRunnable.class);
32  	
33  	private IGrinderWorker underlying;
34  
35  	public DotNetWorkerRunnable(IGrinderWorker underlying)
36  			throws DotNetScriptExecutionException {
37  		logger.trace("ctor: Enter, underlying = {}", underlying);
38  		this.underlying = underlying;
39  		logger.trace("ctor: Exit");
40  	}
41  
42  	@Override
43  	public void run() throws ScriptExecutionException {
44  		logger.trace("run: Enter");
45  		try {
46  			underlying.Run();
47  		} catch (UncheckedGrinderException e) {
48  			if ("Thread has been shut down".equals(e.getMessage())) {
49  				throw e;
50  			}
51  			logger.info("run: Got an UncheckedGrinderException", e);
52  		} catch (DotNetCheckedExceptionMarshall marshall) {
53  			if (marshall.getCause() instanceof Sleeper.ShutdownException) {
54  				throw new DotNetScriptExecutionException(
55  						"Shutdown during sleep",
56  						(Sleeper.ShutdownException) marshall.getCause());
57  			}
58  		}
59  		logger.trace("run: Exit");
60  	}
61  
62  	@Override
63  	public void shutdown() throws ScriptExecutionException {
64  		logger.trace("shutdown: Enter");
65  		try {
66  			underlying.Shutdown();
67  		} finally {
68  			underlying = null;
69  		}
70  		logger.trace("shutdown: Exit");
71  	}
72  
73  	IGrinderWorker getUnderlying() {
74  		return underlying;
75  	}
76  }