001: package org.objectweb.celtix.bus.ws.rm;
002:
003: import java.util.List;
004: import java.util.logging.Level;
005: import java.util.logging.Logger;
006:
007: import javax.wsdl.Definition;
008: import javax.wsdl.Port;
009: import javax.wsdl.WSDLException;
010: import javax.wsdl.extensions.ExtensibilityElement;
011: import javax.wsdl.extensions.UnknownExtensibilityElement;
012: import javax.xml.bind.JAXBContext;
013: import javax.xml.bind.JAXBElement;
014: import javax.xml.bind.JAXBException;
015: import javax.xml.bind.Unmarshaller;
016:
017: import org.w3c.dom.Element;
018: import org.w3c.dom.NodeList;
019:
020: import org.objectweb.celtix.Bus;
021: import org.objectweb.celtix.common.logging.LogUtils;
022: import org.objectweb.celtix.configuration.Configuration;
023: import org.objectweb.celtix.configuration.ConfigurationProvider;
024: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
025: import org.objectweb.celtix.ws.rm.policy.RMAssertionType;
026: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
027:
028: public class RMPolicyProvider implements ConfigurationProvider {
029:
030: private static final Logger LOG = LogUtils
031: .getL7dLogger(RMPolicyProvider.class);
032: private Port port;
033: private Definition def;
034:
035: public RMPolicyProvider(Bus b, EndpointReferenceType epr) {
036: try {
037: port = EndpointReferenceUtils.getPort(b.getWSDLManager(),
038: epr);
039: def = EndpointReferenceUtils.getWSDLDefinition(b
040: .getWSDLManager(), epr);
041: } catch (WSDLException ex) {
042: LOG.log(Level.SEVERE, "POLICY_PROVIDER_CREATION_EXC", ex);
043: }
044: }
045:
046: public void init(Configuration configuration) {
047: // not needed
048: }
049:
050: public Object getObject(String name) {
051: if (!"rmAssertion".equals(name)) {
052: return null;
053: }
054:
055: Element policyElem = getPolicy(port.getBinding()
056: .getExtensibilityElements(), null);
057: if (null == policyElem) {
058: policyElem = getPolicy(port.getExtensibilityElements(),
059: null);
060: }
061: if (null != policyElem) {
062: return getRMAssertion(policyElem);
063: }
064:
065: return null;
066:
067: }
068:
069: private Element getPolicy(List<?> extensibilityElements, String name) {
070: for (Object ep : extensibilityElements) {
071: ExtensibilityElement ext = (ExtensibilityElement) ep;
072: if (ext instanceof UnknownExtensibilityElement) {
073: UnknownExtensibilityElement uExt = (UnknownExtensibilityElement) ext;
074: if (RMUtils.getPolicyConstants().getPolicyQName()
075: .equals(ext.getElementType())) {
076: Element elem = uExt.getElement();
077: String id = elem.getAttributeNS(RMUtils
078: .getPolicyConstants().getWSUNamespaceURI(),
079: "Id");
080: if (null == name || name.equals(id)) {
081: return elem;
082: }
083: } else if (RMUtils.getPolicyConstants()
084: .getPolicyReferenceQName().equals(
085: ext.getElementType())) {
086: Element elem = uExt.getElement();
087: String uri = elem.getAttribute("URI");
088: if (uri.startsWith("#") && uri.length() > 1) {
089: Element referenced = getPolicy(def
090: .getExtensibilityElements(), uri
091: .substring(1));
092: if (null != referenced) {
093: return referenced;
094: }
095: } else {
096: LOG.log(Level.SEVERE,
097: "POLICY_REFERENCE_RESOLUTION_EXC", uri);
098: }
099: }
100: }
101: }
102: return null;
103: }
104:
105: private RMAssertionType getRMAssertion(Element policyElement) {
106: RMAssertionType rma = null;
107: NodeList nl = policyElement.getElementsByTagNameNS(RMUtils
108: .getRMConstants().getRMPolicyNamespaceURI(),
109: "RMAssertion");
110: if (nl.getLength() > 0) {
111: JAXBContext context = null;
112: String packageName = RMUtils.getWSRMPolicyFactory()
113: .getClass().getPackage().getName();
114: try {
115: context = JAXBContext.newInstance(packageName,
116: getClass().getClassLoader());
117: Unmarshaller u = context.createUnmarshaller();
118: Object obj = u.unmarshal(nl.item(0));
119: if (obj instanceof JAXBElement<?>) {
120: JAXBElement<?> el = (JAXBElement<?>) obj;
121: obj = el.getValue();
122: }
123: rma = (RMAssertionType) obj;
124: } catch (JAXBException ex) {
125: LOG.log(Level.SEVERE, "RMASSERTION_UNMARSHAL_EXC", ex);
126: }
127: }
128: return rma;
129: }
130:
131: public boolean setObject(String name, Object value) {
132: return false;
133: }
134:
135: public boolean save() {
136: //TODO
137: return false;
138: }
139:
140: }
|