KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.alfresco.service.cmr.dictionary.ClassDefinition;
24 import org.alfresco.service.cmr.dictionary.DictionaryService;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.namespace.QName;
27
28 /**
29  * Delegate for a Class-level Policy. Provides access to Policy Interface
30  * implementations which invoke the appropriate bound behaviours.
31  *
32  * @author David Caruana
33  *
34  * @param <P> the policy interface
35  */

36 public class ClassPolicyDelegate<P extends ClassPolicy>
37 {
38     private DictionaryService dictionary;
39     private CachedPolicyFactory<ClassBehaviourBinding, P> factory;
40
41
42     /**
43      * Construct.
44      *
45      * @param dictionary the dictionary service
46      * @param policyClass the policy interface class
47      * @param index the behaviour index to query against
48      */

49     @SuppressWarnings JavaDoc("unchecked")
50     /*package*/ ClassPolicyDelegate(DictionaryService dictionary, Class JavaDoc<P> policyClass, BehaviourIndex<ClassBehaviourBinding> index)
51     {
52         // Get list of all pre-registered behaviours for the policy and
53
// ensure they are valid.
54
Collection JavaDoc<BehaviourDefinition> definitions = index.getAll();
55         for (BehaviourDefinition definition : definitions)
56         {
57             definition.getBehaviour().getInterface(policyClass);
58         }
59
60         // Rely on cached implementation of policy factory
61
// Note: Could also use PolicyFactory (without caching)
62
this.factory = new CachedPolicyFactory<ClassBehaviourBinding, P>(policyClass, index);
63         this.dictionary = dictionary;
64     }
65     
66
67     /**
68      * Gets the Policy implementation for the specified Class
69      *
70      * When multiple behaviours are bound to the policy for the class, an
71      * aggregate policy implementation is returned which invokes each policy
72      * in turn.
73      *
74      * @param classQName the class qualified name
75      * @return the policy
76      */

77     public P get(QName classQName)
78     {
79         return get(null, classQName);
80     }
81
82     /**
83      * Gets the Policy implementation for the specified Class
84      *
85      * @param nodeRef the node reference
86      * @param classQName the class name
87      * @return the policy
88      */

89     public P get(NodeRef nodeRef, QName classQName)
90     {
91         ClassDefinition classDefinition = dictionary.getClass(classQName);
92         if (classDefinition == null)
93         {
94             throw new IllegalArgumentException JavaDoc("Class " + classQName + " has not been defined in the data dictionary");
95         }
96         return factory.create(new ClassBehaviourBinding(dictionary, nodeRef, classQName));
97     }
98     
99     /**
100      * Gets the collection of Policy implementations for the specified Class
101      *
102      * @param classQName the class qualified name
103      * @return the collection of policies
104      */

105     public Collection JavaDoc<P> getList(QName classQName)
106     {
107         return getList(null, classQName);
108     }
109     
110     /**
111      * Gets the collection of Policy implementations for the specified Class
112      *
113      * @param nodeRef the node reference
114      * @param classQName the class qualified name
115      * @return the collection of policies
116      */

117     public Collection JavaDoc<P> getList(NodeRef nodeRef, QName classQName)
118     {
119         ClassDefinition classDefinition = dictionary.getClass(classQName);
120         if (classDefinition == null)
121         {
122             throw new IllegalArgumentException JavaDoc("Class " + classQName + " has not been defined in the data dictionary");
123         }
124         return factory.createList(new ClassBehaviourBinding(dictionary, nodeRef, classQName));
125     }
126
127     /**
128      * Gets the policy implementation for the given classes. The single <tt>Policy</tt>
129      * will be a wrapper of multiple appropriate policies.
130      *
131      * @param classQNames the class qualified names
132      * @return Returns the policy
133      */

134     public P get(Set JavaDoc<QName> classQNames)
135     {
136         return get(null, classQNames);
137     }
138
139     /**
140      * Gets the policy implementation for the given classes. The single <tt>Policy</tt>
141      * will be a wrapper of multiple appropriate policies.
142      *
143      * @param nodeRef the node reference
144      * @param classQNames the class qualified names
145      * @return Returns the policy
146      */

147     public P get(NodeRef nodeRef, Set JavaDoc<QName> classQNames)
148     {
149         return factory.toPolicy(getList(nodeRef, classQNames));
150     }
151
152     /**
153      * Gets the collection of <tt>Policy</tt> implementations for the given classes
154      *
155      * @param classQNames the class qualified names
156      * @return Returns the collection of policies
157      */

158     public Collection JavaDoc<P> getList(Set JavaDoc<QName> classQNames)
159     {
160         return getList(null, classQNames);
161     }
162
163     /**
164      * Gets the collection of <tt>Policy</tt> implementations for the given classes
165      *
166      * @param classQNames the class qualified names
167      * @return Returns the collection of policies
168      */

169     public Collection JavaDoc<P> getList(NodeRef nodeRef, Set JavaDoc<QName> classQNames)
170     {
171         Collection JavaDoc<P> policies = new HashSet JavaDoc<P>();
172         for (QName classQName : classQNames)
173         {
174             P policy = factory.create(new ClassBehaviourBinding(dictionary, nodeRef, classQName));
175             if (policy instanceof PolicyList)
176             {
177                 policies.addAll(((PolicyList<P>)policy).getPolicies());
178             }
179             else
180             {
181                 policies.add(policy);
182             }
183         }
184         return policies;
185     }
186 }
187
Popular Tags