PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/eclipse-wtp-webservices-R3.4.0/org.eclipse.wst.ws.service.policy/src/org/eclipse/wst/ws/service/internal/policy/ServicePolicyImpl.java

#
Java | 430 lines | 331 code | 74 blank | 25 comment | 35 complexity | 23f1a31241d019b43185aa1e57647214 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2007, 2008 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. * yyyymmdd bug Email and other contact information
  11. * -------- -------- -----------------------------------------------------------
  12. * 20071024 196997 pmoogk@ca.ibm.com - Peter Moogk
  13. * 20080214 218996 pmoogk@ca.ibm.com - Peter Moogk, Concurrent exception fix
  14. * 20080625 238482 pmoogk@ca.ibm.com - Peter Moogk, Adding thread safety to the service platform api.
  15. *******************************************************************************/
  16. package org.eclipse.wst.ws.service.internal.policy;
  17. import java.util.HashMap;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.Vector;
  23. import org.eclipse.core.resources.IProject;
  24. import org.eclipse.core.runtime.IStatus;
  25. import org.eclipse.core.runtime.Status;
  26. import org.eclipse.wst.ws.service.policy.IDescriptor;
  27. import org.eclipse.wst.ws.service.policy.IPolicyEnumerationList;
  28. import org.eclipse.wst.ws.service.policy.IPolicyRelationship;
  29. import org.eclipse.wst.ws.service.policy.IPolicyState;
  30. import org.eclipse.wst.ws.service.policy.IPolicyStateEnum;
  31. import org.eclipse.wst.ws.service.policy.IServicePolicy;
  32. import org.eclipse.wst.ws.service.policy.IStateEnumerationItem;
  33. import org.eclipse.wst.ws.service.policy.PolicyEnumerationListImpl;
  34. import org.eclipse.wst.ws.service.policy.PolicyRelationshipImpl;
  35. import org.eclipse.wst.ws.service.policy.ServicePolicyActivator;
  36. import org.eclipse.wst.ws.service.policy.listeners.IPolicyChildChangeListener;
  37. import org.eclipse.wst.ws.service.policy.listeners.IStatusChangeListener;
  38. public class ServicePolicyImpl implements IServicePolicy
  39. {
  40. private boolean predefined;
  41. private String id;
  42. private ServicePolicyImpl parent;
  43. private DescriptorImpl descriptor;
  44. private List<IServicePolicy> committedChildren;
  45. private List<IServicePolicy> children;
  46. private PolicyStateImpl policyState;
  47. private Map<IProject, PolicyStateImpl> projectPolicyStates;
  48. private List<IPolicyRelationship> relationshipList;
  49. private List<IPolicyChildChangeListener> childChangeListeners;
  50. private ServicePolicyPlatformImpl platform;
  51. private String enumListId;
  52. private String defaultEnumId;
  53. private List<IStatusChangeListener> statusChangeListeners;
  54. private IStatus status;
  55. private String unresolvedParent;
  56. private List<UnresolvedRelationship> unresolvedRelationshipList;
  57. public ServicePolicyImpl( boolean isPredefined, String id, ServicePolicyPlatformImpl platform )
  58. {
  59. this.predefined = isPredefined;
  60. this.id = id;
  61. this.children = new Vector<IServicePolicy>();
  62. this.relationshipList = new Vector<IPolicyRelationship>();
  63. this.unresolvedRelationshipList = new Vector<UnresolvedRelationship>();
  64. this.policyState = new PolicyStateImpl( platform.getApiPlatform(), this, null );
  65. this.platform = platform;
  66. this.childChangeListeners = new Vector<IPolicyChildChangeListener>();
  67. this.projectPolicyStates = new HashMap<IProject, PolicyStateImpl>();
  68. this.status = Status.OK_STATUS;
  69. }
  70. public boolean isPredefined()
  71. {
  72. return predefined;
  73. }
  74. public String getId()
  75. {
  76. return id;
  77. }
  78. public void setParent( ServicePolicyImpl parent )
  79. {
  80. this.parent = parent;
  81. if( parent != null )
  82. {
  83. parent.addChild( this, false );
  84. }
  85. }
  86. public void commitChanges()
  87. {
  88. policyState.commitChanges();
  89. // Make a copy of the children in the committedChildren object.
  90. committedChildren = new Vector<IServicePolicy>( children );
  91. }
  92. public void discardChanges()
  93. {
  94. policyState.discardChanges();
  95. fireChildChangesDuetoDiscard();
  96. //Restore children.
  97. children = new Vector<IServicePolicy>( committedChildren );
  98. }
  99. public List<IServicePolicy> getChildren()
  100. {
  101. return children;
  102. }
  103. private void addChild( ServicePolicyImpl child, boolean fireEvent )
  104. {
  105. children.add( child );
  106. if( fireEvent )
  107. {
  108. fireChildChangeEvent( child, true );
  109. }
  110. }
  111. public void removeChild(IServicePolicy policyToRemove )
  112. {
  113. if( !policyToRemove.isPredefined() )
  114. {
  115. // Remove all the children of this policy first.
  116. List<IServicePolicy> childPolicies = new Vector<IServicePolicy>( policyToRemove .getChildren() );
  117. for( IServicePolicy childPolicy : childPolicies )
  118. {
  119. policyToRemove.removeChild( childPolicy );
  120. }
  121. boolean removed = children.remove( policyToRemove );
  122. if( removed )
  123. {
  124. platform.removePolicy( policyToRemove );
  125. fireChildChangeEvent( policyToRemove , false );
  126. }
  127. }
  128. }
  129. public IDescriptor getDescriptor()
  130. {
  131. if( descriptor == null )
  132. {
  133. descriptor = new DescriptorImpl();
  134. }
  135. return descriptor;
  136. }
  137. public void setDescriptor( DescriptorImpl descriptor )
  138. {
  139. this.descriptor = descriptor;
  140. }
  141. public IServicePolicy getParentPolicy()
  142. {
  143. return parent;
  144. }
  145. public IPolicyState getPolicyState()
  146. {
  147. return policyState;
  148. }
  149. public IPolicyState getPolicyState( IProject project )
  150. {
  151. PolicyStateImpl projectPolicyState = projectPolicyStates.get( project );
  152. if( projectPolicyState == null )
  153. {
  154. projectPolicyState = new PolicyStateImpl( platform.getApiPlatform(), this, project );
  155. projectPolicyState.internalSetMutable( policyState.isMutable() );
  156. projectPolicyStates.put( project, projectPolicyState );
  157. }
  158. return projectPolicyState;
  159. }
  160. public IPolicyStateEnum getPolicyStateEnum()
  161. {
  162. EnumerationStateImpl newEnum = null;
  163. if( enumListId != null )
  164. {
  165. newEnum = new EnumerationStateImpl( platform.getApiPlatform(), enumListId, defaultEnumId, policyState );
  166. }
  167. return newEnum;
  168. }
  169. public IPolicyStateEnum getPolicyStateEnum( IProject project )
  170. {
  171. EnumerationStateImpl newEnum = null;
  172. if( enumListId != null )
  173. {
  174. newEnum = new EnumerationStateImpl( platform.getApiPlatform(), enumListId, defaultEnumId, getPolicyState( project ) );
  175. }
  176. return newEnum;
  177. }
  178. public void setPolicyState( PolicyStateImpl policyState )
  179. {
  180. this.policyState = policyState;
  181. }
  182. public void restoreDefaults()
  183. {
  184. // Remove all local children from the tree
  185. // Java will not let you remove an item from a collection
  186. // that is being iterated over, so we need to create a temporary
  187. // copy of the children collection to iterator over.
  188. Vector<IServicePolicy> tempChildren = new Vector<IServicePolicy>( children );
  189. for( IServicePolicy child : tempChildren )
  190. {
  191. if( !child.isPredefined() )
  192. {
  193. removeChild( child );
  194. }
  195. }
  196. policyState.restoreDefaults();
  197. }
  198. public void restoreDefaults( IProject project )
  199. {
  200. PolicyStateImpl stateImpl = projectPolicyStates.get( project );
  201. if( stateImpl != null )
  202. {
  203. stateImpl.restoreDefaults();
  204. }
  205. }
  206. public List<IPolicyRelationship> getRelationships()
  207. {
  208. return relationshipList;
  209. }
  210. public void setRelationships(List<IPolicyRelationship> relationships)
  211. {
  212. if( !predefined )
  213. {
  214. this.relationshipList = relationships;
  215. }
  216. }
  217. public String getEnumListId()
  218. {
  219. return enumListId;
  220. }
  221. public void setEnumListId(String enumListId)
  222. {
  223. this.enumListId = enumListId;
  224. }
  225. public String getDefaultEnumId()
  226. {
  227. return defaultEnumId;
  228. }
  229. public void setDefaultEnumId(String defaultEnumValue)
  230. {
  231. this.defaultEnumId = defaultEnumValue;
  232. }
  233. public void setUnresolvedParent( String parentId )
  234. {
  235. unresolvedParent = parentId;
  236. }
  237. public void setUnresolvedRelationships( List<UnresolvedRelationship> relationships )
  238. {
  239. unresolvedRelationshipList = relationships;
  240. }
  241. public void resolve()
  242. {
  243. setParent( platform.getServicePolicy( unresolvedParent ) );
  244. for( UnresolvedRelationship relationship : unresolvedRelationshipList )
  245. {
  246. List<String> sourceEnumIdList = relationship.getSourceEnumerationList();
  247. List<UnresolvedPolicyRelationship> targetEnumIdList = relationship.getTargetEnumerationList();
  248. List<IStateEnumerationItem> sourceEnumList = getResolvedEnumList( sourceEnumIdList );
  249. IPolicyEnumerationList sourcePolicyList = new PolicyEnumerationListImpl( sourceEnumList, this );
  250. List<IPolicyEnumerationList> targetPolicyList = new Vector<IPolicyEnumerationList>();
  251. for( UnresolvedPolicyRelationship targetEnum : targetEnumIdList )
  252. {
  253. IServicePolicy targetPolicy = platform.getServicePolicy( targetEnum.getPolicyId() );
  254. List<IStateEnumerationItem> targetList = getResolvedEnumList( targetEnum.getEnumList());
  255. IPolicyEnumerationList targetEnumPolicyList = new PolicyEnumerationListImpl( targetList, targetPolicy );
  256. targetPolicyList.add( targetEnumPolicyList );
  257. if( targetPolicy == null )
  258. {
  259. ServicePolicyActivator.logError( "Policy id, " + targetEnum.getPolicyId() + " not found.", null ); //$NON-NLS-1$ //$NON-NLS-2$
  260. }
  261. }
  262. IPolicyRelationship policyRelationship = new PolicyRelationshipImpl( sourcePolicyList, targetPolicyList );
  263. relationshipList.add( policyRelationship );
  264. }
  265. unresolvedParent = null;
  266. unresolvedRelationshipList = null;
  267. }
  268. private List<IStateEnumerationItem> getResolvedEnumList( List<String> enumIdList )
  269. {
  270. List<IStateEnumerationItem> enumList = new Vector<IStateEnumerationItem>();
  271. for( String enumId : enumIdList )
  272. {
  273. enumList.add( platform.getStateItemEnumeration( enumId ) );
  274. }
  275. return enumList;
  276. }
  277. public void addPolicyChildChangeListener(IPolicyChildChangeListener listener)
  278. {
  279. childChangeListeners.add( listener );
  280. }
  281. public void removePolicyChildChangeListener(IPolicyChildChangeListener listener)
  282. {
  283. childChangeListeners.remove( listener );
  284. }
  285. public void addStatusChangeListener( IStatusChangeListener listener )
  286. {
  287. if( statusChangeListeners == null )
  288. {
  289. statusChangeListeners = new Vector<IStatusChangeListener>();
  290. }
  291. statusChangeListeners.add( listener );
  292. }
  293. public void removeStatusChangeListener( IStatusChangeListener listener )
  294. {
  295. if( statusChangeListeners != null )
  296. {
  297. statusChangeListeners.remove( listener );
  298. }
  299. }
  300. public IStatus getStatus()
  301. {
  302. return status;
  303. }
  304. public void setStatus( IStatus status )
  305. {
  306. IStatus oldStatus = this.status;
  307. this.status = status;
  308. fireStatusChangeEvent( oldStatus, status );
  309. }
  310. private void fireStatusChangeEvent( IStatus oldStatus, IStatus newStatus )
  311. {
  312. if( statusChangeListeners != null )
  313. {
  314. for( IStatusChangeListener listener : statusChangeListeners )
  315. {
  316. listener.statusChange( this, oldStatus, newStatus);
  317. }
  318. }
  319. }
  320. private void fireChildChangesDuetoDiscard()
  321. {
  322. Set<IServicePolicy> childSet = new HashSet<IServicePolicy>( children );
  323. Set<IServicePolicy> committedChildSet = new HashSet<IServicePolicy>( committedChildren );
  324. for( IServicePolicy child : childSet )
  325. {
  326. if( committedChildSet.contains( child ) )
  327. {
  328. committedChildSet.remove( child );
  329. }
  330. else
  331. {
  332. // A child was added and is now being deleted by the discard.
  333. fireChildChangeEvent( child, false );
  334. platform.fireChildChangeEvent( child, false );
  335. }
  336. }
  337. // Any children left in the committed set must have been deleted and are now
  338. // being added back due to the discard.
  339. for( IServicePolicy child : committedChildSet )
  340. {
  341. fireChildChangeEvent( child, true );
  342. platform.fireChildChangeEvent( child, true );
  343. }
  344. }
  345. private void fireChildChangeEvent( IServicePolicy policy, boolean isAdd )
  346. {
  347. for( IPolicyChildChangeListener listener : childChangeListeners )
  348. {
  349. List<IServicePolicy> policyList = new Vector<IServicePolicy>(1);
  350. List<Boolean> addedList = new Vector<Boolean>(1);
  351. policyList.add( policy );
  352. addedList.add( isAdd );
  353. listener.childChange( policyList, addedList );
  354. }
  355. }
  356. }