KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > policy > ClassBehaviourIndex


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.policy;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.concurrent.locks.ReentrantReadWriteLock JavaDoc;
23
24 import org.alfresco.service.cmr.repository.NodeRef;
25 import org.alfresco.service.namespace.QName;
26
27
28 /**
29  * Class (Type/Aspect) oriented index of bound behaviours
30  *
31  * Note: Uses Class hierarchy to derive bindings.
32  *
33  * @author David Caruana
34  *
35  */

36 /*package*/ class ClassBehaviourIndex<B extends ClassBehaviourBinding> implements BehaviourIndex<B>
37 {
38     // Lock
39
private ReentrantReadWriteLock JavaDoc lock = new ReentrantReadWriteLock JavaDoc();
40     
41     // Map of class bindings
42
private BehaviourMap<B> classMap = new BehaviourMap<B>();
43     
44     // Map of service bindings
45
private BehaviourMap<ServiceBehaviourBinding> serviceMap = new BehaviourMap<ServiceBehaviourBinding>();
46     
47     // List of registered observers
48
private List JavaDoc<BehaviourChangeObserver<B>> observers = new ArrayList JavaDoc<BehaviourChangeObserver<B>>();
49
50     // Behaviour Filter
51
private BehaviourFilter filter = null;
52
53     
54     /**
55      * Construct.
56      */

57     /*package*/ ClassBehaviourIndex(BehaviourFilter filter)
58     {
59         // Observe class binding changes and propagate to our own observers
60
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         // Observe service binding changes and propagate to our own observers
72
this.serviceMap.addChangeObserver(new BehaviourChangeObserver<ServiceBehaviourBinding>()
73         {
74             public void addition(ServiceBehaviourBinding binding, Behaviour behaviour)
75             {
76                 for (BehaviourChangeObserver<B> listener : observers)
77                 {
78                     // Note: Don't specify class ref as service-level bindings affect all classes
79
listener.addition(null, behaviour);
80                 }
81             }
82         });
83
84         // Setup state
85
this.filter = filter;
86     }
87
88     
89     /* (non-Javadoc)
90      * @see org.alfresco.repo.policy.BehaviourIndex#getAll()
91      */

92     public Collection JavaDoc<BehaviourDefinition> getAll()
93     {
94         lock.readLock().lock();
95         
96         try
97         {
98             List JavaDoc<BehaviourDefinition> all = new ArrayList JavaDoc<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     /* (non-Javadoc)
111      * @see org.alfresco.repo.policy.BehaviourIndex#find()
112      */

113     @SuppressWarnings JavaDoc("unchecked")
114     public Collection JavaDoc<BehaviourDefinition> find(B binding)
115     {
116         lock.readLock().lock();
117         
118         try
119         {
120             List JavaDoc<BehaviourDefinition> behaviours = new ArrayList JavaDoc<BehaviourDefinition>();
121
122             // Determine if behaviour has been disabled
123
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                 // Find class behaviour by scanning up the class hierarchy
134
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             // Append all service-level behaviours
150
behaviours.addAll(serviceMap.getAll());
151
152             return behaviours;
153         }
154         finally
155         {
156             lock.readLock().unlock();
157         }
158     }
159
160
161     /* (non-Javadoc)
162      * @see org.alfresco.repo.policy.BehaviourIndex#find()
163      */

164     public void addChangeObserver(BehaviourChangeObserver<B> observer)
165     {
166         observers.add(observer);
167     }
168
169     
170     /* (non-Javadoc)
171      * @see org.alfresco.repo.policy.BehaviourIndex#getFilter()
172      */

173     public BehaviourFilter getFilter()
174     {
175         return filter;
176     }
177
178     
179     /**
180      * Binds a Class Behaviour into this index
181      *
182      * @param behaviour the class bound behaviour
183      */

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     /**
199      * Binds a Service Behaviour into this index
200      *
201      * @param behaviour the service bound behaviour
202      */

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