View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.model.propertyexpansion.resolvers;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  import java.util.Properties;
18  import java.util.Set;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.apache.xmlbeans.XmlString;
23  
24  import com.eviware.soapui.model.ModelItem;
25  import com.eviware.soapui.model.TestPropertyHolder;
26  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
27  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
29  import com.eviware.soapui.model.testsuite.TestProperty;
30  import com.eviware.soapui.model.testsuite.TestPropertyListener;
31  import com.eviware.soapui.support.types.StringList;
32  
33  public class GlobalPropertyResolver implements PropertyResolver
34  {
35  	public class EnvironmentPropertyHolder implements TestPropertyHolder
36  	{
37  		public void addTestPropertyListener(TestPropertyListener listener)
38  		{
39  		}
40  
41  		public ModelItem getModelItem()
42  		{
43  			return null;
44  		}
45  
46  		public Map<String, TestProperty> getProperties()
47  		{
48  			Map<String, String> properties = System.getenv();
49  			Map<String,TestProperty> result = new HashMap<String, TestProperty>();
50  			
51  			for( Object key : properties.keySet() )
52  			{
53  				result.put(key.toString(), new EnviromentTestProperty( key ));
54  			}
55  			
56  			return result;
57  		}
58  
59  		public String getPropertiesLabel()
60  		{
61  			return "Environment Properties";
62  		}
63  
64  		public TestProperty getProperty(String name)
65  		{
66  			Map<String, String> properties = System.getenv();
67  			return properties.containsKey(name ) ? new EnviromentTestProperty( name ) : null;
68  		}
69  
70  		public TestProperty getPropertyAt(int index)
71  		{
72  			return getProperty(getPropertyNames()[index]);
73  		}
74  
75  		public int getPropertyCount()
76  		{
77  			return System.getenv().size();
78  		}
79  
80  		public String[] getPropertyNames()
81  		{
82  			Set<String> keys = System.getenv().keySet();
83  			StringList result = new StringList();
84  			for( Object key : keys )
85  				result.add( key.toString() );
86  			return result.toStringArray();
87  		}
88  
89  		public String getPropertyValue(String name)
90  		{
91  			TestProperty property = getProperty(name);
92  			return property == null ? null : property.getValue();
93  		}
94  
95  		public boolean hasProperty(String name)
96  		{
97  			return System.getenv().containsKey(name);
98  		}
99  
100 		public void removeTestPropertyListener(TestPropertyListener listener)
101 		{
102 		}
103 
104 		public void setPropertyValue(String name, String value)
105 		{
106 		}
107 		
108 		private class EnviromentTestProperty implements TestProperty
109 		{
110 			private final Object key;
111 
112 			public EnviromentTestProperty(Object key)
113 			{
114 				this.key = key;
115 			}
116 
117 			public String getDefaultValue()
118 			{
119 				return null;
120 			}
121 
122 			public String getDescription()
123 			{
124 				return null;
125 			}
126 
127 			public ModelItem getModelItem()
128 			{
129 				return null;
130 			}
131 
132 			public String getName()
133 			{
134 				return key.toString();
135 			}
136 
137 			public QName getType()
138 			{
139 				return XmlString.type.getName();
140 			}
141 
142 			public String getValue()
143 			{
144 				return System.getenv(key.toString());
145 			}
146 
147 			public boolean isReadOnly()
148 			{
149 				return true;
150 			}
151 
152 			public void setValue(String value)
153 			{
154 			}
155 		}
156 	}
157 
158 	public class SystemPropertyHolder implements TestPropertyHolder
159 	{
160 		public void addTestPropertyListener(TestPropertyListener listener)
161 		{
162 		}
163 
164 		public ModelItem getModelItem()
165 		{
166 			return null;
167 		}
168 
169 		public Map<String, TestProperty> getProperties()
170 		{
171 			Properties properties = System.getProperties();
172 			Map<String,TestProperty> result = new HashMap<String, TestProperty>();
173 			
174 			for( Object key : properties.keySet() )
175 			{
176 				result.put(key.toString(), new SystemTestProperty( key ));
177 			}
178 			
179 			return result;
180 		}
181 
182 		public String getPropertiesLabel()
183 		{
184 			return "System Properties";
185 		}
186 
187 		public TestProperty getProperty(String name)
188 		{
189 			Properties properties = System.getProperties();
190 			return properties.containsKey(name ) ? new SystemTestProperty( name ) : null;
191 		}
192 
193 		public TestProperty getPropertyAt(int index)
194 		{
195 			return getProperty(getPropertyNames()[index]);
196 		}
197 
198 		public int getPropertyCount()
199 		{
200 			return System.getProperties().size();
201 		}
202 
203 		public String[] getPropertyNames()
204 		{
205 			Set<Object> keys = System.getProperties().keySet();
206 			StringList result = new StringList();
207 			for( Object key : keys )
208 				result.add( key.toString() );
209 			return result.toStringArray();
210 		}
211 
212 		public String getPropertyValue(String name)
213 		{
214 			TestProperty property = getProperty(name);
215 			return property == null ? null : property.getValue();
216 		}
217 
218 		public boolean hasProperty(String name)
219 		{
220 			return System.getProperties().containsKey(name);
221 		}
222 
223 		public void removeTestPropertyListener(TestPropertyListener listener)
224 		{
225 		}
226 
227 		public void setPropertyValue(String name, String value)
228 		{
229 			System.setProperty(name, value);
230 		}
231 		
232 		private class SystemTestProperty implements TestProperty
233 		{
234 			private final Object key;
235 
236 			public SystemTestProperty(Object key)
237 			{
238 				this.key = key;
239 			}
240 
241 			public String getDefaultValue()
242 			{
243 				return null;
244 			}
245 
246 			public String getDescription()
247 			{
248 				return null;
249 			}
250 
251 			public ModelItem getModelItem()
252 			{
253 				return null;
254 			}
255 
256 			public String getName()
257 			{
258 				return key.toString();
259 			}
260 
261 			public QName getType()
262 			{
263 				return XmlString.type.getName();
264 			}
265 
266 			public String getValue()
267 			{
268 				return System.getProperty(key.toString());
269 			}
270 
271 			public boolean isReadOnly()
272 			{
273 				return false;
274 			}
275 
276 			public void setValue(String value)
277 			{
278 				System.setProperty(key.toString(), value);
279 			}
280 		}
281 	}
282 
283 	private SystemPropertyHolder systemPropertyHolder;
284 	private EnvironmentPropertyHolder environmentPropertyHolder;
285 
286 	public GlobalPropertyResolver()
287 	{
288 		systemPropertyHolder = new SystemPropertyHolder();
289 		environmentPropertyHolder = new EnvironmentPropertyHolder();
290 	}
291 	
292 	public String resolveProperty( PropertyExpansionContext context, String name, boolean globalOverride )
293 	{
294 		String result = ResolverUtils.checkForExplicitReference( name, PropertyExpansion.SYSTEM_REFERENCE, systemPropertyHolder, context, globalOverride );
295 		if( result != null )
296 			return result;
297 		
298 		result = ResolverUtils.checkForExplicitReference( name, PropertyExpansion.ENV_REFERENCE, environmentPropertyHolder, context, globalOverride );
299 		if( result != null )
300 			return result;
301 		
302 		//	if not, check for explicit global property (stupid 1.7.6 syntax that should be removed..)
303 		if(  name.length() > 2 && name.charAt( 0 ) == PropertyExpansion.PROPERTY_SEPARATOR 
304 					&& name.charAt( 1 ) == PropertyExpansion.PROPERTY_SEPARATOR )
305 			return PropertyExpansionUtils.getGlobalProperty( name.substring( 2 ) );
306 		else
307 			return PropertyExpansionUtils.getGlobalProperty( name );
308 		
309 		
310 	}
311 }