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