View Javadoc

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