001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.description;
021:
022: import org.apache.axiom.om.util.UUIDGenerator;
023: import org.apache.axis2.util.AxisPolicyLocator;
024: import org.apache.neethi.Policy;
025: import org.apache.neethi.PolicyReference;
026: import org.apache.neethi.PolicyRegistry;
027: import org.apache.neethi.PolicyRegistryImpl;
028:
029: import java.util.ArrayList;
030: import java.util.Hashtable;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: public class PolicyInclude {
035:
036: public static final int ANON_POLICY = 100;
037:
038: public static final int AXIS_POLICY = 1;
039:
040: public static final int AXIS_MODULE_POLICY = 2;
041:
042: public static final int AXIS_MODULE_OPERATION_POLICY = 17;
043:
044: public static final int AXIS_SERVICE_POLICY = 3;
045:
046: public static final int AXIS_OPERATION_POLICY = 4;
047:
048: public static final int AXIS_MESSAGE_POLICY = 5;
049:
050: public static final int SERVICE_POLICY = 6;
051:
052: public static final int PORT_POLICY = 7;
053:
054: public static final int PORT_TYPE_POLICY = 8;
055:
056: public static final int BINDING_POLICY = 9;
057:
058: public static final int OPERATION_POLICY = 10;
059:
060: public static final int BINDING_OPERATION_POLICY = 11;
061:
062: public static final int INPUT_POLICY = 12;
063:
064: public static final int OUTPUT_POLICY = 13;
065:
066: public static final int BINDING_INPUT_POLICY = 14;
067:
068: public static final int BINDING_OUTPUT_POLICY = 15;
069:
070: public static final int MESSAGE_POLICY = 16;
071:
072: private Policy policy = null;
073:
074: private Policy effectivePolicy = null;
075:
076: private PolicyRegistry reg;
077:
078: private AxisDescription description;
079:
080: private Hashtable wrapperElements = new Hashtable();
081:
082: public PolicyInclude() {
083: reg = new PolicyRegistryImpl();
084: }
085:
086: public PolicyInclude(AxisDescription axisDescription) {
087:
088: if (axisDescription.getParent() != null) {
089: PolicyInclude parentPolicyInclude = axisDescription
090: .getParent().getPolicyInclude();
091: reg = new PolicyRegistryImpl(parentPolicyInclude
092: .getPolicyRegistry());
093: } else {
094: reg = new PolicyRegistryImpl();
095: }
096: setDescription(axisDescription);
097: }
098:
099: public void setPolicyRegistry(PolicyRegistry reg) {
100: this .reg = reg;
101: }
102:
103: public PolicyRegistry getPolicyRegistry() {
104: return reg;
105: }
106:
107: public void setPolicy(Policy policy) {
108: wrapperElements.clear();
109:
110: if (policy.getName() == null && policy.getId() == null) {
111: policy.setId(UUIDGenerator.getUUID());
112: }
113:
114: Wrapper wrapper = new Wrapper(PolicyInclude.ANON_POLICY, policy);
115: if (policy.getName() != null) {
116: wrapperElements.put(policy.getName(), wrapper);
117: } else {
118: wrapperElements.put(policy.getId(), wrapper);
119: }
120: }
121:
122: public void updatePolicy(Policy policy) {
123: String key;
124:
125: if ((key = policy.getName()) == null
126: && (key = policy.getId()) == null) {
127: // TODO throw more meaningful exception ..
128: throw new RuntimeException(
129: "policy doesn't have a name or an id ");
130: }
131:
132: Wrapper wrapper = (Wrapper) wrapperElements.get(key);
133: wrapper.value = policy;
134: }
135:
136: public void setEffectivePolicy(Policy effectivePolicy) {
137: this .effectivePolicy = effectivePolicy;
138: }
139:
140: public void setDescription(AxisDescription description) {
141: this .description = description;
142: }
143:
144: public AxisDescription getDescription() {
145: return description;
146: }
147:
148: private PolicyInclude getParent() {
149:
150: if (description != null && description.getParent() != null) {
151: return description.getParent().getPolicyInclude();
152: }
153: return null;
154: }
155:
156: private void calculatePolicy() {
157:
158: Policy result = null;
159: Iterator iterator = wrapperElements.values().iterator();
160:
161: while (iterator.hasNext()) {
162: Object policyElement = ((Wrapper) iterator.next())
163: .getValue();
164: Policy p;
165:
166: if (policyElement instanceof PolicyReference) {
167: AxisPolicyLocator locator = new AxisPolicyLocator(
168: description);
169: p = (Policy) ((PolicyReference) policyElement)
170: .normalize(locator, false);
171:
172: } else if (policyElement instanceof Policy) {
173: p = (Policy) policyElement;
174:
175: } else {
176: // TODO AxisFault?
177: throw new RuntimeException();
178: }
179:
180: result = (result == null) ? (Policy) p : (Policy) result
181: .merge(p);
182: }
183:
184: this .policy = result;
185: }
186:
187: private void calculateEffectivePolicy() {
188: Policy result;
189:
190: if (getParent() != null) {
191: Policy parentPolicy = getParent().getEffectivePolicy();
192:
193: if (parentPolicy == null) {
194: result = getPolicy();
195:
196: } else {
197:
198: if (getPolicy() != null) {
199: result = (Policy) parentPolicy.merge(getPolicy());
200:
201: } else {
202: result = parentPolicy;
203: }
204: }
205:
206: } else {
207: result = getPolicy();
208: }
209: setEffectivePolicy(result);
210: }
211:
212: public Policy getPolicy() {
213: calculatePolicy();
214: return policy;
215: }
216:
217: public Policy getEffectivePolicy() {
218: calculateEffectivePolicy();
219: return effectivePolicy;
220: }
221:
222: public ArrayList getPolicyElements() {
223: ArrayList policyElementsList = new ArrayList();
224: Iterator policyElementIterator = wrapperElements.values()
225: .iterator();
226:
227: while (policyElementIterator.hasNext()) {
228: policyElementsList.add(((Wrapper) policyElementIterator
229: .next()).getValue());
230: }
231: return policyElementsList;
232: }
233:
234: public ArrayList getPolicyElements(int type) {
235: ArrayList policyElementList = new ArrayList();
236: Iterator wrapperElementIterator = wrapperElements.values()
237: .iterator();
238: Wrapper wrapper;
239:
240: while (wrapperElementIterator.hasNext()) {
241: wrapper = (Wrapper) wrapperElementIterator.next();
242:
243: if (wrapper.getType() == type) {
244: policyElementList.add(wrapper.getValue());
245: }
246: }
247: return policyElementList;
248: }
249:
250: public void registerPolicy(String key, Policy policy) {
251: reg.register(key, policy);
252: }
253:
254: public Policy getPolicy(String key) {
255: return reg.lookup(key);
256: }
257:
258: public void addPolicyElement(int type, Policy policy) {
259:
260: String key;
261:
262: if ((key = policy.getName()) == null
263: && (key = policy.getId()) == null) {
264: policy.setId(UUIDGenerator.getUUID());
265: }
266:
267: key = (policy.getName() != null) ? policy.getName() : policy
268: .getId();
269:
270: Wrapper wrapper = new Wrapper(type, policy);
271: wrapperElements.put(key, wrapper);
272: reg.register(key, policy);
273: }
274:
275: public void addPolicyRefElement(int type,
276: PolicyReference policyReference) {
277: Wrapper wrapper = new Wrapper(type, policyReference);
278: wrapperElements.put(policyReference.getURI(), wrapper);
279: }
280:
281: class Wrapper {
282: private int type;
283: private Object value;
284:
285: Wrapper(int type, Object value) {
286: setType(type);
287: setValue(value);
288: }
289:
290: void setType(int type) {
291: this .type = type;
292: }
293:
294: int getType() {
295: return type;
296: }
297:
298: void setValue(Object value) {
299: this .value = value;
300: }
301:
302: Object getValue() {
303: return value;
304: }
305: }
306:
307: public void removePolicyElement(String policyURI) {
308: wrapperElements.remove(policyURI);
309: reg.remove(policyURI);
310: }
311:
312: public void removeAllPolicyElements() {
313: wrapperElements.clear();
314: }
315:
316: public List getAttachedPolicies() {
317:
318: ArrayList arrayList = new ArrayList();
319:
320: for (Iterator iterator = wrapperElements.values().iterator(); iterator
321: .hasNext();) {
322: arrayList.add(((Wrapper) iterator.next()).value);
323: }
324:
325: return arrayList;
326: }
327: }
|