1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.ws.api.config.management.policy; 27 28 import com.sun.istack.internal.logging.Logger; 29 import com.sun.xml.internal.ws.policy.AssertionSet; 30 import com.sun.xml.internal.ws.policy.Policy; 31 import com.sun.xml.internal.ws.policy.PolicyAssertion; 32 import com.sun.xml.internal.ws.policy.PolicyException; 33 import com.sun.xml.internal.ws.policy.PolicyMap; 34 import com.sun.xml.internal.ws.policy.PolicyMapKey; 35 import com.sun.xml.internal.ws.policy.SimpleAssertion; 36 import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData; 37 import com.sun.xml.internal.ws.policy.spi.AssertionCreationException; 38 import com.sun.xml.internal.ws.resources.ManagementMessages; 39 40 import java.util.Collection; 41 import java.util.Iterator; 42 import javax.xml.namespace.QName; 43 import javax.xml.ws.WebServiceException; 44 45 /** 46 * Base class for the #ManagedClientAssertion and #ManagedServiceAssertion. Provides 47 * convenience methods to directly access the policy assertion parameters. 48 * 49 * @author Fabian Ritzmann 50 */ 51 public abstract class ManagementAssertion extends SimpleAssertion { 52 53 /** 54 * To be able to distinguish between explicit settings and no setting. 55 */ 56 public static enum Setting { NOT_SET, OFF, ON } 57 58 /** 59 * The name of the management attribute. 60 */ 61 protected static final QName MANAGEMENT_ATTRIBUTE_QNAME = new QName("management"); 62 /** 63 * The name of the monitoring attribute. 64 */ 65 protected static final QName MONITORING_ATTRIBUTE_QNAME = new QName("monitoring"); 66 67 /** 68 * The name of the id attribute. 69 */ 70 private static final QName ID_ATTRIBUTE_QNAME = new QName("id"); 71 /** 72 * The name of the start attribute. 73 */ 74 private static final QName START_ATTRIBUTE_QNAME = new QName("start"); 75 76 private static final Logger LOGGER = Logger.getLogger(ManagementAssertion.class); 77 78 /** 79 * Return ManagementAssertion if one can be found in the policy map under 80 * the given service and port name. 81 * 82 * @param <T> The implementation class of the assertion. 83 * @param name The fully qualified name of the server or client assertion. 84 * @param policyMap The policy map. May be null. 85 * @param serviceName The WSDL service name. May not be null. 86 * @param portName The WSDL port name. May not be null. 87 * @param type The implementation class of the assertion. 88 * @return An instance of ManagementAssertion or null. 89 * @throws WebServiceException If computing the effective policy of the endpoint scope failed. 90 */ 91 protected static <T extends ManagementAssertion> T getAssertion(final QName name, 92 final PolicyMap policyMap, QName serviceName, QName portName, Class<T> type) 93 throws WebServiceException { 94 try { 95 PolicyAssertion assertion = null; 96 if (policyMap != null) { 97 final PolicyMapKey key = PolicyMap.createWsdlEndpointScopeKey(serviceName, portName); 98 final Policy policy = policyMap.getEndpointEffectivePolicy(key); 99 if (policy != null) { 100 final Iterator<AssertionSet> assertionSets = policy.iterator(); 101 if (assertionSets.hasNext()) { 102 final AssertionSet assertionSet = assertionSets.next(); 103 final Iterator<PolicyAssertion> assertions = assertionSet.get(name).iterator(); 104 if (assertions.hasNext()) { 105 assertion = assertions.next(); 106 } 107 } 108 } 109 } 110 return assertion == null ? null : assertion.getImplementation(type); 111 } catch (PolicyException ex) { 112 throw LOGGER.logSevereException(new WebServiceException( 113 ManagementMessages.WSM_1001_FAILED_ASSERTION(name), ex)); 114 } 115 } 116 117 /** 118 * Create a new ManagementAssertion instance. 119 * 120 * @param name The fully qualified name of the server or client assertion. Must 121 * not be null. 122 * @param data The assertion data. Must not be null. 123 * @param assertionParameters Parameters of the assertion. May be null. 124 * @throws AssertionCreationException Thrown if the creation of the assertion failed. 125 */ 126 protected ManagementAssertion(final QName name, AssertionData data, Collection<PolicyAssertion> assertionParameters) 127 throws AssertionCreationException { 128 super(data, assertionParameters); 129 if (!name.equals(data.getName())) { 130 throw LOGGER.logSevereException(new AssertionCreationException(data, 131 ManagementMessages.WSM_1002_EXPECTED_MANAGEMENT_ASSERTION(name))); 132 } 133 if (isManagementEnabled() && !data.containsAttribute(ID_ATTRIBUTE_QNAME)) { 134 throw LOGGER.logSevereException(new AssertionCreationException(data, 135 ManagementMessages.WSM_1003_MANAGEMENT_ASSERTION_MISSING_ID(name))); 136 } 137 } 138 139 /** 140 * Returns the value of the id attribute. May not be null. 141 * 142 * @return The value of the id attribute. 143 */ 144 public String getId() { 145 return this.getAttributeValue((ID_ATTRIBUTE_QNAME)); 146 } 147 148 /** 149 * Returns the value of the start attribute. May be null. 150 * 151 * @return The value of the start attribute. 152 */ 153 public String getStart() { 154 return this.getAttributeValue((START_ATTRIBUTE_QNAME)); 155 } 156 157 /** 158 * Returns the value of the managment attribute depending on whether this is 159 * a client-side or server-side assertion. 160 * 161 * @return The value of the managment attribute. 162 */ 163 public abstract boolean isManagementEnabled(); 164 165 /** 166 * Returns the value of the monitoring attribute. 167 * 168 * @return The value of the monitoring attribute. 169 */ 170 public Setting monitoringAttribute() { 171 final String monitoring = this.getAttributeValue(MONITORING_ATTRIBUTE_QNAME); 172 Setting result = Setting.NOT_SET; 173 if (monitoring != null) { 174 if (monitoring.trim().toLowerCase().equals("on") 175 || Boolean.parseBoolean(monitoring)) { 176 result = Setting.ON; 177 } 178 else { 179 result = Setting.OFF; 180 } 181 } 182 return result; 183 } 184 185 }