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 static java.util.Collections.emptyList;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import net.grinder.common.GrinderProperties;
28  import net.grinder.engine.common.EngineException;
29  import net.grinder.engine.common.ScriptLocation;
30  import net.grinder.scriptengine.Instrumenter;
31  import net.grinder.scriptengine.ScriptEngineService;
32  import net.grinder.util.FileExtensionMatcher;
33  
34  /**
35   * .Net script engine.
36   * 
37   * @author Eirik Bjornset
38   */
39  public class DotNetScriptEngineService implements ScriptEngineService {
40  
41  	private final Logger logger = LoggerFactory
42  			.getLogger(DotNetScriptEngineService.class);
43  	
44  	private final FileExtensionMatcher dllFileMatcher = new FileExtensionMatcher(".dll");
45  
46  	private final GrinderProperties properties;
47  	private final DotNetScriptEngineFactory factory;
48  
49  	DotNetScriptEngineService(GrinderProperties properties, DotNetScriptEngineFactory factory) {
50  		logger.trace("ctor: Enter, properties = {}, factory = {}", properties, factory);
51  		this.properties = properties;
52  		this.factory = factory;
53  		logger.trace("ctor: Exit");
54  	}
55  	
56  	public DotNetScriptEngineService(GrinderProperties properties) {
57  		this(properties, new DotNetScriptEngineFactory.Default());
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	@Override public List<? extends Instrumenter> createInstrumenters()
64  			throws EngineException {
65  		return emptyList();
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	@Override public ScriptEngine createScriptEngine(ScriptLocation script)
72  			throws EngineException {
73  		logger.trace("createScriptEngine: Enter, script = {}", script);
74  		ScriptEngine result = null;
75  		if (dllFileMatcher.accept(script.getFile())) {
76  			DotNetScriptEngine engine = factory.createScriptEngine(properties, script);
77  			try {
78  				engine.initialize();
79  			} catch (IOException e) {
80  				throw new EngineException("Could not initalize the CLR bridge",
81  						e);
82  			}
83  			result = engine;
84  		}
85  		logger.trace("createScriptEngine: Exit, result = {}", result);
86  		return result;
87  	}
88  
89  	GrinderProperties getProperties()
90  	{
91  		return properties;
92  	}
93  	
94  	DotNetScriptEngineFactory getFactory() {
95  		return factory;
96  	}
97  }