View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.impl.settings;
14  
15  import java.util.Collections;
16  import java.util.HashMap;
17  import java.util.HashSet;
18  import java.util.List;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import com.eviware.soapui.config.SettingConfig;
23  import com.eviware.soapui.config.SettingsConfig;
24  import com.eviware.soapui.model.ModelItem;
25  import com.eviware.soapui.model.settings.Settings;
26  import com.eviware.soapui.model.settings.SettingsListener;
27  import com.eviware.soapui.support.types.StringToStringMap;
28  
29  /***
30   * Settings implementation for XmlBeans generated SettingsConfig
31   * 
32   * @author Ole.Matzura
33   */
34  
35  public class XmlBeansSettingsImpl implements Settings
36  {
37  	private final Settings parent;
38  	private final SettingsConfig config;
39  	private final Map<String, SettingConfig> values = Collections.synchronizedMap( new HashMap<String, SettingConfig>());
40  	private final Map<String, String> valueCache = Collections.synchronizedMap( new StringToStringMap());
41  	private final Set<SettingsListener> listeners = new HashSet<SettingsListener>();
42  	private final ModelItem item;
43  	private final SettingsListener settingsListener = new WeakSettingsListener( new InternalSettingsListener() );
44  
45  	public XmlBeansSettingsImpl( ModelItem item, Settings parent, SettingsConfig config )
46  	{
47  		this.item = item;
48  		this.config = config;
49  		this.parent = parent;
50  
51  		List<SettingConfig> settingList = config.getSettingList();
52  		for( SettingConfig setting : settingList )
53  		{
54  			values.put( setting.getId(), setting );
55  		}
56  
57  		if( parent != null )
58  		{
59  			parent.addSettingsListener( settingsListener );
60  		}
61  	}
62  
63  	public boolean isSet( String id )
64  	{
65  		return values.containsKey( id );
66  	}
67  
68  	public String getString( String id, String defaultValue )
69  	{
70  		if( valueCache.containsKey( id ) )
71  		{
72  			return valueCache.get( id );
73  		}
74  		else if( values.containsKey( id ) )
75  		{
76  			valueCache.put( id, values.get( id ).getStringValue() );
77  			return valueCache.get( id );
78  		}
79  
80  		return parent == null ? defaultValue : parent.getString( id, defaultValue );
81  	}
82  
83  	public void setString( String id, String value )
84  	{
85  		String oldValue = getString( id, null );
86  
87  		if( oldValue == null && value == null )
88  			return;
89  
90  		if( value != null && value.equals( oldValue ) )
91  			return;
92  
93  		if( value == null )
94  		{
95  			clearSetting( id );
96  		}
97  		else
98  		{
99  			if( !values.containsKey( id ) )
100 			{
101 				SettingConfig setting = config.addNewSetting();
102 				setting.setId( id );
103 				values.put( id, setting );
104 			}
105 
106 			values.get( id ).setStringValue( value );
107 			valueCache.put( id, value );
108 		}
109 
110 		notifySettingChanged( id, value, oldValue );
111 	}
112 
113 	private void notifySettingChanged( String id, String value, String oldValue )
114 	{
115 		SettingsListener[] l = listeners.toArray( new SettingsListener[listeners.size()] );
116 		for( SettingsListener listener : l )
117 		{
118 			listener.settingChanged( id, value, oldValue );
119 		}
120 	}
121 
122 	public boolean getBoolean( String id )
123 	{
124 		String value = getString( id, null );
125 
126 		if( value != null )
127 			return Boolean.parseBoolean( value );
128 
129 		return parent == null ? false : parent.getBoolean( id );
130 	}
131 
132 	public long getLong( String id, long defaultValue )
133 	{
134 		String value = getString( id, null );
135 
136 		if( value != null )
137 		{
138 			try
139 			{
140 				return Long.parseLong( value );
141 			}
142 			catch( NumberFormatException e )
143 			{
144 			}
145 		}
146 
147 		return parent == null ? defaultValue : parent.getLong( id, defaultValue );
148 	}
149 
150 	public void setBoolean( String id, boolean value )
151 	{
152 		if( !value )
153 			setString( id, "false" );
154 		else
155 			setString( id, "true" );
156 	}
157 
158 	public void addSettingsListener( SettingsListener listener )
159 	{
160 		listeners.add( listener );
161 	}
162 
163 	public void removeSettingsListener( SettingsListener listener )
164 	{
165 		listeners.remove( listener );
166 	}
167 
168 	public void clearSetting( String id )
169 	{
170 		if( values.containsKey( id ) )
171 		{
172 			int ix = config.getSettingList().indexOf( values.get( id ) );
173 			config.removeSetting( ix );
174 			values.remove( id );
175 			valueCache.remove( id );
176 		}
177 	}
178 
179 	public ModelItem getModelItem()
180 	{
181 		return item;
182 	}
183 
184 	public void release()
185 	{
186 		if( listeners != null )
187 			listeners.clear();
188 
189 		if( parent != null )
190 			parent.removeSettingsListener( settingsListener );
191 	}
192 
193 	private final class InternalSettingsListener implements SettingsListener
194 	{
195 		public void settingChanged( String name, String newValue, String oldValue )
196 		{
197 			if( !values.containsKey( name ) )
198 			{
199 				notifySettingChanged( name, newValue, oldValue );
200 			}
201 		}
202 	}
203 
204 	public void setLong( String id, long value )
205 	{
206 		setString( id, Long.toString( value ) );
207 	}
208 
209 	public void setConfig( SettingsConfig soapuiSettings )
210 	{
211 		StringToStringMap changed = new StringToStringMap();
212 
213 		for( SettingConfig config : soapuiSettings.getSettingList() )
214 		{
215 			if( !config.getStringValue().equals( getString( config.getId(), null ) ) )
216 				changed.put( config.getId(), getString( config.getId(), null ) );
217 		}
218 
219 		values.clear();
220 
221 		config.set( soapuiSettings );
222 		List<SettingConfig> settingList = config.getSettingList();
223 		for( SettingConfig setting : settingList )
224 		{
225 			values.put( setting.getId(), setting );
226 		}
227 
228 		for( String key : changed.keySet() )
229 		{
230 			notifySettingChanged( key, getString( key, null ), changed.get( key ) );
231 		}
232 	}
233 }