1 17 package org.alfresco.repo.policy; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.List ; 22 import java.util.concurrent.locks.ReentrantReadWriteLock ; 23 24 import org.alfresco.service.cmr.repository.NodeRef; 25 import org.alfresco.service.namespace.QName; 26 27 28 36 class ClassBehaviourIndex<B extends ClassBehaviourBinding> implements BehaviourIndex<B> 37 { 38 private ReentrantReadWriteLock lock = new ReentrantReadWriteLock (); 40 41 private BehaviourMap<B> classMap = new BehaviourMap<B>(); 43 44 private BehaviourMap<ServiceBehaviourBinding> serviceMap = new BehaviourMap<ServiceBehaviourBinding>(); 46 47 private List <BehaviourChangeObserver<B>> observers = new ArrayList <BehaviourChangeObserver<B>>(); 49 50 private BehaviourFilter filter = null; 52 53 54 57 ClassBehaviourIndex(BehaviourFilter filter) 58 { 59 this.classMap.addChangeObserver(new BehaviourChangeObserver<B>() 61 { 62 public void addition(B binding, Behaviour behaviour) 63 { 64 for (BehaviourChangeObserver<B> listener : observers) 65 { 66 listener.addition(binding, behaviour); 67 } 68 } 69 }); 70 71 this.serviceMap.addChangeObserver(new BehaviourChangeObserver<ServiceBehaviourBinding>() 73 { 74 public void addition(ServiceBehaviourBinding binding, Behaviour behaviour) 75 { 76 for (BehaviourChangeObserver<B> listener : observers) 77 { 78 listener.addition(null, behaviour); 80 } 81 } 82 }); 83 84 this.filter = filter; 86 } 87 88 89 92 public Collection <BehaviourDefinition> getAll() 93 { 94 lock.readLock().lock(); 95 96 try 97 { 98 List <BehaviourDefinition> all = new ArrayList <BehaviourDefinition>(classMap.size() + serviceMap.size()); 99 all.addAll(classMap.getAll()); 100 all.addAll(serviceMap.getAll()); 101 return all; 102 } 103 finally 104 { 105 lock.readLock().unlock(); 106 } 107 } 108 109 110 113 @SuppressWarnings ("unchecked") 114 public Collection <BehaviourDefinition> find(B binding) 115 { 116 lock.readLock().lock(); 117 118 try 119 { 120 List <BehaviourDefinition> behaviours = new ArrayList <BehaviourDefinition>(); 121 122 boolean isEnabled = true; 124 if (filter != null) 125 { 126 NodeRef nodeRef = binding.getNodeRef(); 127 QName className = binding.getClassQName(); 128 isEnabled = (nodeRef == null) ? filter.isEnabled(className) : filter.isEnabled(nodeRef, className); 129 } 130 131 if (isEnabled) 132 { 133 BehaviourDefinition behaviour = null; 135 while(behaviour == null && binding != null) 136 { 137 behaviour = classMap.get(binding); 138 if (behaviour == null) 139 { 140 binding = (B)binding.generaliseBinding(); 141 } 142 } 143 if (behaviour != null) 144 { 145 behaviours.add(behaviour); 146 } 147 } 148 149 behaviours.addAll(serviceMap.getAll()); 151 152 return behaviours; 153 } 154 finally 155 { 156 lock.readLock().unlock(); 157 } 158 } 159 160 161 164 public void addChangeObserver(BehaviourChangeObserver<B> observer) 165 { 166 observers.add(observer); 167 } 168 169 170 173 public BehaviourFilter getFilter() 174 { 175 return filter; 176 } 177 178 179 184 public void putClassBehaviour(BehaviourDefinition<B> behaviour) 185 { 186 lock.writeLock().lock(); 187 try 188 { 189 classMap.put(behaviour); 190 } 191 finally 192 { 193 lock.writeLock().unlock(); 194 } 195 } 196 197 198 203 public void putServiceBehaviour(BehaviourDefinition<ServiceBehaviourBinding> behaviour) 204 { 205 lock.writeLock().lock(); 206 try 207 { 208 serviceMap.put(behaviour); 209 } 210 finally 211 { 212 lock.writeLock().unlock(); 213 } 214 } 215 216 } 217 | Popular Tags |