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