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: */package org.apache.cxf.service.model;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.concurrent.ConcurrentHashMap;
025: import java.util.concurrent.atomic.AtomicReference;
026:
027: import javax.xml.namespace.QName;
028:
029: public abstract class AbstractPropertiesHolder implements Extensible {
030: private AtomicReference<Map<String, Object>> propertyMap = new AtomicReference<Map<String, Object>>();
031: private AtomicReference<Object[]> extensors = new AtomicReference<Object[]>();
032: private Map<QName, Object> extensionAttributes;
033:
034: public Object getProperty(String name) {
035: if (null == propertyMap.get()) {
036: return null;
037: }
038: return propertyMap.get().get(name);
039: }
040:
041: public <T> T getProperty(String name, Class<T> cls) {
042: return cls.cast(getProperty(name));
043: }
044:
045: public void setProperty(String name, Object v) {
046: if (null == propertyMap.get()) {
047: propertyMap.compareAndSet(null,
048: new ConcurrentHashMap<String, Object>(4));
049: }
050: if (v == null) {
051: propertyMap.get().remove(name);
052: } else {
053: propertyMap.get().put(name, v);
054: }
055: }
056:
057: public void addExtensor(Object el) {
058: Object exts[] = extensors.get();
059: Object exts2[];
060: if (exts == null) {
061: exts2 = new Object[1];
062: } else {
063: exts2 = new Object[exts.length + 1];
064: for (int i = 0; i < exts.length; i++) {
065: exts2[i] = exts[i];
066: }
067: }
068: exts2[exts2.length - 1] = el;
069: if (!extensors.compareAndSet(exts, exts2)) {
070: //keep trying
071: addExtensor(el);
072: }
073: }
074:
075: public <T> T getExtensor(Class<T> cls) {
076: Object exts[] = extensors.get();
077: if (exts == null) {
078: return null;
079: }
080: for (int x = 0; x < exts.length; x++) {
081: if (cls.isInstance(exts[x])) {
082: return cls.cast(exts[x]);
083: }
084: }
085: return null;
086: }
087:
088: public <T> List<T> getExtensors(Class<T> cls) {
089: Object exts[] = extensors.get();
090: if (exts == null) {
091: return null;
092: }
093: List<T> list = new ArrayList<T>(exts.length);
094: for (int x = 0; x < exts.length; x++) {
095: if (cls.isInstance(exts[x])) {
096: list.add(cls.cast(exts[x]));
097: }
098: }
099: return list;
100: }
101:
102: public AtomicReference<Object[]> getExtensors() {
103: return extensors;
104: }
105:
106: public Object getExtensionAttribute(QName name) {
107: return null == extensionAttributes ? null : extensionAttributes
108: .get(name);
109: }
110:
111: public Map<QName, Object> getExtensionAttributes() {
112: return extensionAttributes;
113: }
114:
115: public void addExtensionAttribute(QName name, Object attr) {
116: if (null == extensionAttributes) {
117: extensionAttributes = new HashMap<QName, Object>();
118: }
119: extensionAttributes.put(name, attr);
120: }
121:
122: public void setExtensionAttributes(Map<QName, Object> attrs) {
123: extensionAttributes = attrs;
124: }
125:
126: /**
127: * Lookup a configuration value. This may be found in the properties holder supplied
128: * (i.e. an EndpointInfo or ServiceInfo), or it may be a property on the Bus itself.
129: * If no value is found, the defaultValue is returned.
130: *
131: * @param defaultValue the default value
132: * @param type the extensor type
133: * @return the configuration value or the default
134: */
135: public <T> T getTraversedExtensor(T defaultValue, Class<T> type) {
136: T extensor = getExtensor(type);
137: if (extensor == null) {
138: return defaultValue;
139: }
140: return extensor;
141: }
142: }
|