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.IGrinderContext;
20  import grinderscript.net.core.IGrinderLogger;
21  import grinderscript.net.core.IGrinderTest;
22  
23  import java.io.IOException;
24  
25  import net.grinder.common.GrinderException;
26  import net.grinder.common.GrinderProperties;
27  import net.grinder.engine.common.ScriptLocation;
28  import net.grinder.script.Grinder.ScriptContext;
29  import net.grinder.script.NonInstrumentableTypeException;
30  import net.grinder.script.Test;
31  
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import system.Type;
36  
37  public class DotNetContextBridge implements IGrinderContext {
38  
39  	private final Logger logger = LoggerFactory
40  			.getLogger(DotNetContextBridge.class);
41  
42  	private final ScriptContext scriptContext;
43  	private final GrinderProperties properties;
44  	private final ScriptLocation script;
45  
46  	public DotNetContextBridge(ScriptContext scriptContext,
47  			GrinderProperties properties, ScriptLocation script) {
48  		logger.trace(
49  				"ctor: Enter, scriptContext = {}, properties = {}, script = {}",
50  				new Object[] { scriptContext, properties, script });
51  		this.scriptContext = scriptContext;
52  		this.properties = properties;
53  		this.script = script;
54  		logger.trace("ctor: Exit");
55  	}
56  
57  	@Override
58  	public void Sleep(long meanTime) {
59  		logger.trace("Sleep: Enter, meanTime = {}", meanTime);
60  		try {
61  			scriptContext.sleep(meanTime);
62  		} catch (GrinderException e) {
63  			throw new DotNetCheckedExceptionMarshall(e);
64  		}
65  		logger.trace("Sleep: Exit");
66  	}
67  
68  	@Override
69  	public void Sleep(long meanTime, long sigma) {
70  		logger.trace("Sleep: Enter, meanTime = {}, sigma = {}", meanTime, sigma);
71  		try {
72  			scriptContext.sleep(meanTime, sigma);
73  		} catch (GrinderException e) {
74  			throw new DotNetCheckedExceptionMarshall(e);
75  		}
76  		logger.trace("Sleep: Exit");
77  	}
78  
79  	@Override
80  	public IGrinderLogger GetLogger(String name) {
81  		return new DotNetLoggerBridge(name);
82  	}
83  
84  	@Override
85  	public IGrinderLogger GetLogger(Type type) {
86  		return new DotNetLoggerBridge(type);
87  	}
88  
89  	@Override
90  	public IGrinderTest CreateTest(int number, String description, IGrinderTest testAction) {
91  		logger.trace(
92  				"CreateTest: Enter, number = {}, description = {}, testAction = {}",
93  				new Object[] { number, description, testAction });
94  		Test test = new Test(number, description);
95  		DotNetTestBridge testBridge = new DotNetTestBridge(scriptContext, testAction);
96  		try {
97  			test.record(testBridge);
98  		} catch (NonInstrumentableTypeException e) {
99  			throw new DotNetCheckedExceptionMarshall(e);
100 		}
101 		logger.trace("CreateTest: Exit");
102 		return testBridge;
103 	}
104 
105 	@Override
106 	public boolean ContainsProperty(String key) {
107 		return properties.containsKey(key);
108 	}
109 
110 	@Override
111 	public String GetProperty(String key) {
112 		return properties.getProperty(key);
113 	}
114 
115 	@Override
116 	public String GetProperty(String key, String defaultValue) {
117 		return properties.getProperty(key, defaultValue);
118 	}
119 
120 	@Override
121 	public String getScriptFile() {
122 		try {
123 			return script.getFile().getCanonicalPath();
124 		} catch (IOException e) {
125 			throw new DotNetCheckedExceptionMarshall(e);
126 		}
127 	}
128 
129 	@Override
130 	public int getAgentNumber() {
131 		return scriptContext.getAgentNumber();
132 	}
133 
134 	@Override
135 	public int getProcessNumber() {
136 		return scriptContext.getProcessNumber();
137 	}
138 
139 	@Override
140 	public String getProcessName() {
141 		return scriptContext.getProcessName();
142 	}
143 
144 	@Override
145 	public int getFirstProcessNumber() {
146 		return scriptContext.getFirstProcessNumber();
147 	}
148 
149 	@Override
150 	public int getThreadNumber() {
151 		return scriptContext.getThreadNumber();
152 	}
153 
154 	@Override
155 	public int getRunNumber() {
156 		return scriptContext.getRunNumber();
157 	}
158 	
159 	
160 	ScriptContext getScriptContext() {
161 		return scriptContext;
162 	}
163 	
164 	GrinderProperties getProperties() {
165 		return properties;
166 	}
167 	
168 	ScriptLocation getScript() {
169 		return script;
170 	}
171 
172 }