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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*******************************************************************************
* 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
* 20071113 209701 pmoogk@ca.ibm.com - Peter Moogk
* 20080111 214907 pmoogk@ca.ibm.com - Peter Moogk
* 20080214 218996 pmoogk@ca.ibm.com - Peter Moogk, Concurrent exception fix
* 20080325 222095 pmoogk@ca.ibm.com - Peter Moogk
*******************************************************************************/
package org.eclipse.wst.ws.service.internal.policy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.wst.ws.service.policy.IFilter;
import org.eclipse.wst.ws.service.policy.IServicePolicy;
import org.eclipse.wst.ws.service.policy.IStateEnumerationItem;
import org.eclipse.wst.ws.service.policy.ServicePolicyActivator;
import org.eclipse.wst.ws.service.policy.listeners.IPolicyChildChangeListener;
import org.eclipse.wst.ws.service.policy.listeners.IPolicyPlatformLoadListener;
import org.eclipse.wst.ws.service.policy.listeners.IPolicyPlatformProjectLoadListener;
import org.osgi.service.prefs.BackingStoreException;
public class ServicePolicyPlatformImpl
{
private List<IPolicyPlatformLoadListener> loadListeners;
private Map<String, ServicePolicyImpl> committedPolicyMap;
private Map<String, ServicePolicyImpl> policyMap;
private Map<String, List<IStateEnumerationItem>> enumList;
private Map<String, StateEnumerationItemImpl> enumItemList;
private Map<IProject, ProjectEntry> enabledProjectMap;
private List<Expression> enabledList;
private List<IPolicyChildChangeListener> childChangeListeners;
private List<IPolicyChildChangeListener> childChangeListenersOnCommit;
private List<IServicePolicy> commitChildChangePolicy;
private List<Boolean> commitChildChangeAdded;
private List<IServicePolicy> queuedChildChangePolicy;
private List<Boolean> queuedChildChangeAdded;
private List<IPolicyPlatformProjectLoadListener> projectPlatformListeners;
public ServicePolicyPlatformImpl()
{
}
public void load()
{
ServicePolicyRegistry registry = new ServicePolicyRegistry( this );
List<String> localIds = LocalUtils.getLocalPolicyIds();
loadListeners = new Vector<IPolicyPlatformLoadListener>();
policyMap = new HashMap<String, ServicePolicyImpl>();
enumList = new HashMap<String, List<IStateEnumerationItem>>();
enumItemList = new HashMap<String, StateEnumerationItemImpl>();
enabledProjectMap = new HashMap<IProject, ProjectEntry>();
enabledList = new Vector<Expression>();
childChangeListeners = new Vector<IPolicyChildChangeListener>();
childChangeListenersOnCommit = new Vector<IPolicyChildChangeListener>();
commitChildChangePolicy = new Vector<IServicePolicy>();
commitChildChangeAdded = new Vector<Boolean>();
projectPlatformListeners = new Vector<IPolicyPlatformProjectLoadListener>();
registry.load( loadListeners, policyMap, enumList, enumItemList );
// This first loop through the ids will only create service policy objects
// without populating them with there values.
for( String localPolicyId : localIds )
{
ServicePolicyImpl localPolicy = new ServicePolicyImpl( false, localPolicyId, this );
policyMap.put( localPolicyId, localPolicy );
}
// This second loop will populate the service policy values and
// crossreference parent and child service policies.
for( String localPolicyId : localIds )
{
LocalUtils.loadLocalPolicy( localPolicyId, this );
}
for( IPolicyPlatformLoadListener loadListener : loadListeners )
{
loadListener.load();
}
commitChanges( false );
// Commit any project level changes that have been made by load listeners.
for( IProject project : enabledProjectMap.keySet() )
{
commitChanges( project );
}
}
public void addEnabledExpression( Expression expression )
{
if( expression != null )
{
enabledList.add( expression );
}
}
public boolean isEnabled( Object object )
{
boolean result = false;
IEvaluationContext context = new EvaluationContext( null, object );
context.addVariable( "selection", object ); //$NON-NLS-1$
context.setAllowPluginActivation( true );
for( Expression enabledItem : enabledList )
{
try
{
EvaluationResult expResult = enabledItem.evaluate( context );
// If any expression returns TRUE or NOT_LOADED we will return true as
// the result.
if( expResult != EvaluationResult.FALSE )
{
result = true;
break;
}
}
catch( CoreException exc )
{
// Ignore the expression if an exception occurs.
ServicePolicyActivator.logError( "Error evaluating enablement expression.", exc ); //$NON-NLS-1$
}
}
return result;
}
public void commitChanges( boolean saveLocals )
{
List<String> localIds = new Vector<String>();
if( saveLocals ) LocalUtils.removeAllPreferencePolicies( null, null );
// Fire child change events for commit listeners
for( IPolicyChildChangeListener listener : childChangeListenersOnCommit )
{
listener.childChange( commitChildChangePolicy, commitChildChangeAdded );
}
removeDeletedPreferenceData();
commitChildChangePolicy = new Vector<IServicePolicy>();
commitChildChangeAdded = new Vector<Boolean>();
for( ServicePolicyImpl policy : policyMap.values() )
{
policy.commitChanges();
if( saveLocals && !policy.isPredefined() )
{
LocalUtils.saveLocalPolicy( policy );
localIds.add( policy.getId() );
}
}
if( saveLocals ) LocalUtils.saveLocalIds( localIds );
committedPolicyMap = new HashMap<String, ServicePolicyImpl>();
committedPolicyMap.putAll( policyMap );
ServicePolicyActivator.getDefault().savePluginPreferences();
}
private void removeDeletedPreferenceData()
{
for( int index = 0; index < commitChildChangePolicy.size(); index++ )
{
IServicePolicy policy = commitChildChangePolicy.get( index );
boolean added = commitChildChangeAdded.get( index );
if( !added )
{
// Add policy is being deleted. We need to remove any state data
// for this policy at the workspace level and the project level.
LocalUtils.removeAllPreferencePolicies( policy.getId(), null );
for( IProject proj : enabledProjectMap.keySet() )
{
if( proj.exists() && proj.isOpen() )
{
IEclipsePreferences projPrefs = new ProjectScope( proj ).getNode( ServicePolicyActivator.PLUGIN_ID );
LocalUtils.removeAllPreferencePolicies( policy.getId(), projPrefs );
try
{
projPrefs.flush();
}
catch( BackingStoreException exc )
{
ServicePolicyActivator.logError( "Error flushing project properties.", exc ); //$NON-NLS-1$
}
}
}
}
}
}
/**
* This method is called internally to remove a service policy.
*
* @param policy
*/
public void removePolicy( IServicePolicy policy )
{
policyMap.remove( policy.getId() );
fireChildChangeEvent( policy, false );
}
/**
* This method is only called from the platform API
*
* @param policy
*/
public void removePlatformPolicy( IServicePolicy policy )
{
if( policy.isPredefined() ) return;
IServicePolicy parent = policy.getParentPolicy();
if( parent == null )
{
// Remove any children first
List<IServicePolicy> children = new Vector<IServicePolicy>( policy.getChildren() );
for( IServicePolicy child : children )
{
policy.removeChild( child );
}
removePolicy( (ServicePolicyImpl)policy);
}
else
{
parent.removeChild( policy );
}
}
public void addChildChangeListener( IPolicyChildChangeListener listener, boolean onCommit )
{
if( onCommit )
{
childChangeListenersOnCommit.add( listener );
}
else
{
childChangeListeners.add( listener );
}
}
public void removeChildChangeListener( IPolicyChildChangeListener listener, boolean onCommit )
{
if( onCommit )
{
childChangeListenersOnCommit.remove( listener );
}
else
{
childChangeListeners.remove( listener );
}
}
public void queueChildChangeListeners( boolean queue )
{
if( queue && queuedChildChangeAdded == null )
{
queuedChildChangeAdded = new Vector<Boolean>();
queuedChildChangePolicy = new Vector<IServicePolicy>();
}
else if( !queue && queuedChildChangeAdded != null )
{
// Queuing has been turned off so we will fire all the queued events.
for( IPolicyChildChangeListener listener : childChangeListeners )
{
listener.childChange( queuedChildChangePolicy, queuedChildChangeAdded );
}
queuedChildChangeAdded = null;
queuedChildChangePolicy = null;
}
}
public void fireChildChangeEvent( IServicePolicy policy, boolean isAdd )
{
if( queuedChildChangeAdded == null )
{
List<IServicePolicy> policyList = new Vector<IServicePolicy>(1);
List<Boolean> addedList = new Vector<Boolean>(1);
policyList.add( policy );
addedList.add( isAdd );
for( IPolicyChildChangeListener listener : childChangeListeners )
{
listener.childChange( policyList, addedList );
}
}
else
{
queuedChildChangeAdded.add( isAdd );
queuedChildChangePolicy.add( policy );
}
commitChildChangeAdded.add( isAdd );
commitChildChangePolicy.add( policy );
}
public void commitChanges( IProject project )
{
for( ServicePolicyImpl policy : policyMap.values() )
{
((PolicyStateImpl)policy.getPolicyState( project )).commitChanges();
}
ProjectEntry entry = getProjectEntry( project );
entry.isEnabledCommitted = entry.isEnabled;
setProjectEnabled( project, entry.isEnabledCommitted );
try
{
IEclipsePreferences projectPrefs = new ProjectScope( project ).getNode( ServicePolicyActivator.PLUGIN_ID );
projectPrefs.flush();
}
catch( BackingStoreException exc )
{
ServicePolicyActivator.logError( "Error while committing project preferences.", exc ); //$NON-NLS-1$
}
}
public void discardChanges()
{
policyMap = new HashMap<String, ServicePolicyImpl>();
policyMap.putAll( committedPolicyMap );
commitChildChangePolicy = new Vector<IServicePolicy>();
commitChildChangeAdded = new Vector<Boolean>();
for( ServicePolicyImpl policy : committedPolicyMap.values() )
{
policy.discardChanges();
}
}
public void discardChanges( IProject project )
{
for( ServicePolicyImpl policy : policyMap.values() )
{
((PolicyStateImpl)policy.getPolicyState( project )).discardChanges();
}
ProjectEntry entry = getProjectEntry( project );
entry.isEnabled = entry.isEnabledCommitted;
}
public void restoreDefaults()
{
Vector<ServicePolicyImpl> tempPolicyValues
= new Vector<ServicePolicyImpl>( policyMap.values() );
for( ServicePolicyImpl policy : tempPolicyValues )
{
policy.restoreDefaults();
}
}
public void restoreDefaults( IProject project )
{
for( ServicePolicyImpl policy : policyMap.values() )
{
policy.restoreDefaults( project );
}
}
public Set<String> getAllPolicyIds()
{
return policyMap.keySet();
}
public List<IServicePolicy> getRootServicePolicies( IFilter filter )
{
List<IServicePolicy> rootPolicies = new Vector<IServicePolicy>();
for( String policyId : policyMap.keySet() )
{
ServicePolicyImpl policy = policyMap.get( policyId );
if( policy.getParentPolicy() == null )
{
if( filter == null || (filter != null && filter.accept( policy )) )
{
rootPolicies.add( policy );
}
}
}
return rootPolicies;
}
public ServicePolicyImpl createServicePolicy( IServicePolicy parent,
String id,
String enumListId,
String defaultEnumId )
{
if( id == null ) id = "org.eclipse.wst.ws.service.policy.id"; //$NON-NLS-1$
String uniqueId = makeUniqueId( id );
ServicePolicyImpl policy = new ServicePolicyImpl( false, uniqueId, this );
policy.setParent( (ServicePolicyImpl)parent );
policy.setEnumListId( enumListId );
policy.setDefaultEnumId( defaultEnumId );
policyMap.put( uniqueId, policy );
fireChildChangeEvent( policy, true );
return policy;
}
public ServicePolicyImpl getServicePolicy( String id )
{
return id == null ? null : policyMap.get( id );
}
public List<IStateEnumerationItem> getStateEnumeration( String enumId )
{
return enumList.get( enumId );
}
public IStateEnumerationItem getStateItemEnumeration( String stateItemId )
{
return enumItemList.get( stateItemId );
}
public boolean isProjectPreferencesEnabled( IProject project )
{
ProjectEntry entry = getProjectEntry( project );
return entry.isEnabled;
}
private boolean getProjectPreferenceEnabled( IProject project )
{
String pluginId = ServicePolicyActivator.PLUGIN_ID;
IEclipsePreferences projectPreference = new ProjectScope( project ).getNode( pluginId );
String key = pluginId + ".projectEnabled"; //$NON-NLS-1$
return projectPreference.getBoolean( key, false );
}
public void setProjectPreferencesEnabled( IProject project, boolean value )
{
ProjectEntry entry = getProjectEntry( project );
entry.isEnabled = value;
}
/**
* Add a project platform listener. When a particular project is referenced
* by in the service policy platform this listener will be called the first
* time the project is loaded into the system.
*
* @param listener the listener
*/
public void addProjectLoadListener( IPolicyPlatformProjectLoadListener listener )
{
projectPlatformListeners.add( listener );
}
/**
* Removes a project platform listener.
*
* @param listener the listener
*/
public void removeProjectLoadListener( IPolicyPlatformProjectLoadListener listener )
{
projectPlatformListeners.remove( listener );
}
private void setProjectEnabled( IProject project, boolean value )
{
String pluginId = ServicePolicyActivator.PLUGIN_ID;
IEclipsePreferences projectPreference = new ProjectScope( project ).getNode( pluginId );
String key = pluginId + ".projectEnabled"; //$NON-NLS-1$
projectPreference.putBoolean( key, value );
}
private ProjectEntry getProjectEntry( IProject project )
{
ProjectEntry entry = enabledProjectMap.get( project );
if( entry == null )
{
entry = new ProjectEntry();
enabledProjectMap.put( project, entry );
entry.isEnabledCommitted = getProjectPreferenceEnabled( project );
entry.isEnabled = entry.isEnabledCommitted;
fireProjectLoadListener( project );
}
return entry;
}
private void fireProjectLoadListener( IProject project )
{
for( IPolicyPlatformProjectLoadListener listener : projectPlatformListeners )
{
listener.load( project );
}
}
private String makeUniqueId( String id )
{
String result = id;
int idCount = 1;
Pattern pattern = Pattern.compile( "\\d*$" ); // Match any numerical digits at the end //$NON-NLS-1$
// of the string.
while( policyMap.containsKey( result ) )
{
Matcher matcher = pattern.matcher( result );
result = matcher.replaceFirst( "" ) + idCount; //$NON-NLS-1$
idCount++;
}
return result;
}
private class ProjectEntry
{
boolean isEnabledCommitted;
boolean isEnabled;
}
}
|