1
2
3
4
5
6
7
8
9
10
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 String cachedValue = valueCache.get( id );
71 if( cachedValue != null )
72 {
73 return cachedValue;
74 }
75 else
76 {
77 SettingConfig setting = values.get( id );
78 if( setting != null )
79 {
80 String value = setting.getStringValue();
81 valueCache.put( id, value );
82 return value;
83 }
84 }
85
86 return parent == null ? defaultValue : parent.getString( id, defaultValue );
87 }
88
89 public void setString( String id, String value )
90 {
91 String oldValue = getString( id, null );
92
93 if( oldValue == null && value == null )
94 return;
95
96 if( value != null && value.equals( oldValue ) )
97 return;
98
99 if( value == null )
100 {
101 clearSetting( id );
102 }
103 else
104 {
105 if( !values.containsKey( id ) )
106 {
107 SettingConfig setting = config.addNewSetting();
108 setting.setId( id );
109 values.put( id, setting );
110 }
111
112 values.get( id ).setStringValue( value );
113 valueCache.put( id, value );
114 }
115
116 notifySettingChanged( id, value, oldValue );
117 }
118
119 private void notifySettingChanged( String id, String value, String oldValue )
120 {
121 SettingsListener[] l = listeners.toArray( new SettingsListener[listeners.size()] );
122 for( SettingsListener listener : l )
123 {
124 listener.settingChanged( id, value, oldValue );
125 }
126 }
127
128 @Override
129 public void reloadSettings()
130 {
131 notifySettingsReloaded();
132
133 }
134
135 private void notifySettingsReloaded()
136 {
137 SettingsListener[] l = listeners.toArray( new SettingsListener[listeners.size()] );
138 for( SettingsListener listener : l )
139 {
140 listener.settingsReloaded();
141 }
142 }
143
144 public boolean getBoolean( String id )
145 {
146 String value = getString( id, null );
147
148 if( value != null )
149 return Boolean.parseBoolean( value );
150
151 return parent == null ? false : parent.getBoolean( id );
152 }
153
154 public long getLong( String id, long defaultValue )
155 {
156 String value = getString( id, null );
157
158 if( value != null )
159 {
160 try
161 {
162 return Long.parseLong( value );
163 }
164 catch( NumberFormatException e )
165 {
166 }
167 }
168
169 return parent == null ? defaultValue : parent.getLong( id, defaultValue );
170 }
171
172 public void setBoolean( String id, boolean value )
173 {
174 if( !value )
175 setString( id, "false" );
176 else
177 setString( id, "true" );
178 }
179
180 public void addSettingsListener( SettingsListener listener )
181 {
182 listeners.add( listener );
183 }
184
185 public void removeSettingsListener( SettingsListener listener )
186 {
187 listeners.remove( listener );
188 }
189
190 public void clearSetting( String id )
191 {
192 if( values.containsKey( id ) )
193 {
194 int ix = config.getSettingList().indexOf( values.get( id ) );
195 config.removeSetting( ix );
196 values.remove( id );
197 valueCache.remove( id );
198 }
199 }
200
201 public ModelItem getModelItem()
202 {
203 return item;
204 }
205
206 public void release()
207 {
208 if( listeners != null )
209 listeners.clear();
210
211 if( parent != null )
212 parent.removeSettingsListener( settingsListener );
213 }
214
215 private final class InternalSettingsListener implements SettingsListener
216 {
217 public void settingChanged( String name, String newValue, String oldValue )
218 {
219 if( !values.containsKey( name ) )
220 {
221 notifySettingChanged( name, newValue, oldValue );
222 }
223 }
224
225 @Override
226 public void settingsReloaded()
227 {
228 notifySettingsReloaded();
229 }
230 }
231
232 public void setLong( String id, long value )
233 {
234 setString( id, Long.toString( value ) );
235 }
236
237 public void setConfig( SettingsConfig soapuiSettings )
238 {
239 StringToStringMap changed = new StringToStringMap();
240
241 for( SettingConfig config : soapuiSettings.getSettingList() )
242 {
243 if( !config.getStringValue().equals( getString( config.getId(), null ) ) )
244 changed.put( config.getId(), getString( config.getId(), null ) );
245 }
246
247 values.clear();
248
249 config.set( soapuiSettings );
250 List<SettingConfig> settingList = config.getSettingList();
251 for( SettingConfig setting : settingList )
252 {
253 values.put( setting.getId(), setting );
254 }
255
256 for( String key : changed.keySet() )
257 {
258 notifySettingChanged( key, getString( key, null ), changed.get( key ) );
259 }
260 notifySettingsReloaded();
261 }
262
263 }