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