Skip to main content
summaryrefslogtreecommitdiffstats
blob: d206b9b801b79e791cbfe14c21e7483b1b6e5d43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20071024   196997 pmoogk@ca.ibm.com - Peter Moogk
 * 20071221   213492 pmoogk@ca.ibm.com - Peter Moogk
 * 20080109   214818 pmoogk@ca.ibm.com - Peter Moogk
 * 20080325   222095 pmoogk@ca.ibm.com - Peter Moogk
 * 20080430   221578 pmoogk@ca.ibm.com - Peter Moogk, Fixed commit problem.
 *******************************************************************************/
package org.eclipse.wst.ws.service.internal.policy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.wst.ws.service.policy.IPolicyState;
import org.eclipse.wst.ws.service.policy.IServicePolicy;
import org.eclipse.wst.ws.service.policy.ServicePolicyActivator;
import org.eclipse.wst.ws.service.policy.ServicePolicyPlatform;
import org.eclipse.wst.ws.service.policy.listeners.IPolicyStateChangeListener;

public class PolicyStateImpl implements IPolicyState
{
  private List<IPolicyStateChangeListener> stateChangeListeners;
  private List<IPolicyStateChangeListener> stateChangeListenersOnlyOnCommit;
  private IPreferencesService              service;
  private IProject                         project;
  private boolean                          mutable;
  private IServicePolicy                   policy;
  private Map<String,TableEntry>           table;
  private ServicePolicyPlatform            platform;

  public PolicyStateImpl( IServicePolicy policy, IProject project)
  {
    this.service              = Platform.getPreferencesService();
    this.platform             = ServicePolicyPlatform.getInstance();
    this.project              = project;
    this.policy               = policy;
    this.mutable              = policy.isPredefined() ? false : true;
    this.table                = new HashMap<String, TableEntry>();
  }
  
  public void commitChanges()
  {
    // We don't do anything if this policy is not mutable.
    if( !mutable ) return;
    
    IEclipsePreferences preferences = null;
    
    if( project == null )
    {
      preferences = new InstanceScope().getNode( ServicePolicyActivator.PLUGIN_ID );
    }
    else
    {
      preferences = new ProjectScope( project ).getNode( ServicePolicyActivator.PLUGIN_ID );
    }

    for( Map.Entry<String, TableEntry> entry : table.entrySet() )
    {
      String     key        = entry.getKey();
      String     storeKey   = makeStoreKey( key );
      TableEntry tableEntry = entry.getValue();
      String     value      = tableEntry.value;
      
      if( value != null )
      {
        String oldValue = tableEntry.lastCommittedValue;
       
        // Always need to put the value in the backing store even if it is the same value
        // since we delete all preferences before committing them.
        preferences.put( storeKey, value );
        tableEntry.lastCommittedValue = value;
        
        if( oldValue == null )
        {
          // Temporarily remove the tableEntry value so that getValue will get the old value.
          tableEntry.value = null;
          oldValue = getValue( key );
        
          // Restore the tableEntry value;
          tableEntry.value = value;
        }
        
        if( !value.equals( oldValue ) ) 
        {          
          firePolicyStateChange( stateChangeListenersOnlyOnCommit, key, oldValue, value );
        }
      }
    }
  }
  
  private IEclipsePreferences[] getNodes()
  {
    IEclipsePreferences[] result = null;
    
    if( project != null && platform.isProjectPreferencesEnabled(project) )
    { 
      result    = new IEclipsePreferences[2];
      result[0] = new ProjectScope( project ).getNode( ServicePolicyActivator.PLUGIN_ID );
      result[1] = new InstanceScope().getNode( ServicePolicyActivator.PLUGIN_ID );
    }
    else
    {
      result = new IEclipsePreferences[1];
      result[0] = new InstanceScope().getNode( ServicePolicyActivator.PLUGIN_ID );
    }
    
    return result;
  }
  
  public void discardChanges()
  {
    if( !mutable ) return;
    
    for( TableEntry entry : table.values() )
    {
      entry.value = null;
    }
  }
  
  public void restoreDefaults()
  {
    // We want to restore the setting to there default state, but we don't
    // want change the backend eclipse preferences, since the user might not
    // commit these changes.  Therefore, we will set the values for all the
    // table entries to their default value.  
    if( project == null )
    {
      for( TableEntry entry : table.values() )
      {
         entry.value = entry.defaultValue;
      }
    }
    else
    {
      // Note: since we are restoring the state for a project we need to ensure
      //       that only the instance scope values are used.  (ie. we want to
      //       ignore project scope values.)
      IEclipsePreferences[] nodes = new IEclipsePreferences[]{new InstanceScope().getNode( ServicePolicyActivator.PLUGIN_ID )};
      
      for( Map.Entry<String,TableEntry> entry : table.entrySet() )
      {
        String     storeKey     = makeStoreKey( entry.getKey() );
        TableEntry tableEntry   = entry.getValue();
        String     defaultValue = "";
        
        if( tableEntry.defaultValue != null )
        {
          defaultValue = tableEntry.defaultValue; 
        }
        
        tableEntry.value = service.get( storeKey, defaultValue, nodes );        
      }
    }
    
  }
  
  /** 
   * The method gets the value from the following places in the given order.
   * 
   * 1) From the local table.
   * 2) From the preference store nodes.  Note: this could include searching
   *         the project preference store and the plugin preference store.
   * 3) From the default in the local table.
   */
  public String getValue(String key)
  {
    String     result = null;
    TableEntry entry  = table.get( key );
    
    if( entry == null )
    {
      entry = new TableEntry();
      table.put( key, entry );
    }
    
    if( project == null ||
        ( project != null && platform.isProjectPreferencesEnabled( project ) ) )
    { 
        result = entry.value;
    }
    
    if( result == null )
    {
      // We don't have a local value for this key so we will look in the
      // preference store.
      String defaultValue = ""; //$NON-NLS-1$
      String storeKey     = makeStoreKey( key );
      
      if( entry.defaultValue != null )
      {
        defaultValue = entry.defaultValue;
      }
      
      result = service.get( storeKey, defaultValue, getNodes() );
    }
    
    return result;
  }

  private String makeStoreKey( String key )
  {
    return policy.getId() + "." + key;    //$NON-NLS-1$
  }
  
  public boolean isMutable()
  {
    return mutable;
  }

  public void putDefaultValue( String key, String defaultValue, boolean overrideExisting )
  {
    if( !mutable ) return;
    
    TableEntry entry = table.get( key );
    
    if( entry == null )
    {
      entry = new TableEntry();
      table.put( key, entry );
    }
    
    if( entry.defaultValue == null || overrideExisting )
    {
      entry.defaultValue = defaultValue;
    }
    
  }

  public void putValue(String key, String value)
  {
    if( !mutable ) return;
    
    TableEntry entry = table.get( key );
    
    if( entry == null )
    {
      entry = new TableEntry();
      table.put( key, entry );
    }

    String oldValue = getValue( key );
    
    entry.value = value;
    firePolicyStateChange( stateChangeListeners, key, oldValue, value );
  }

  public void internalSetMutable( boolean mutable )
  {
    this.mutable = mutable;  
  }
  
  public void setMutable(boolean mutable)
  {
    if( policy.isPredefined() )
    {
      ServicePolicyActivator.logError( "Attempt to set mutability on a predefined service policy.", null ); //$NON-NLS-1$
    }
    else
    {
      this.mutable = mutable;
    }
  }  
  
  public void addPolicyStateChangeListener(IPolicyStateChangeListener listener, boolean notifyOnCommitOnly )
  {
    if( notifyOnCommitOnly )
    {
      if( stateChangeListenersOnlyOnCommit == null )
      {
        stateChangeListenersOnlyOnCommit = new Vector<IPolicyStateChangeListener>();
      }
      
      stateChangeListenersOnlyOnCommit.add( listener );
    }
    else
    {
      if( stateChangeListeners == null )
      {
        stateChangeListeners = new Vector<IPolicyStateChangeListener>();
      }
      
      stateChangeListeners.add( listener );
    }
  }
  
  public void removePolicyStateChangeListener(IPolicyStateChangeListener listener)
  {
    if( stateChangeListeners != null )
    {
      stateChangeListeners.remove( listener );
    }
    
    if( stateChangeListenersOnlyOnCommit != null )
    {
      stateChangeListenersOnlyOnCommit.remove( listener );
    }
  }
  
  private void firePolicyStateChange( List<IPolicyStateChangeListener> listeners,
                                      String                           key, 
                                      String                           oldValue, 
                                      String                           newValue )
  {
    if( listeners != null )
    {
      for( IPolicyStateChangeListener listener : listeners )  
      {
        listener.policyStateChange( policy, key, oldValue, newValue );
      }
    }
  }
  
  private class TableEntry
  {
    String value;
    String lastCommittedValue;
    String defaultValue;
  }
}

Back to the top