aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.api.config.management.policy; aoqi@0: aoqi@0: import com.sun.istack.internal.logging.Logger; aoqi@0: import com.sun.xml.internal.ws.api.server.WSEndpoint; aoqi@0: import com.sun.xml.internal.ws.policy.PolicyAssertion; aoqi@0: import com.sun.xml.internal.ws.policy.PolicyMap; aoqi@0: import com.sun.xml.internal.ws.policy.PolicyConstants; aoqi@0: import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData; aoqi@0: import com.sun.xml.internal.ws.policy.spi.AssertionCreationException; aoqi@0: import com.sun.xml.internal.ws.resources.ManagementMessages; aoqi@0: aoqi@0: import java.util.Collection; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.LinkedList; aoqi@0: import java.util.Map; aoqi@0: import javax.xml.namespace.QName; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: aoqi@0: /** aoqi@0: * The server-side ManagedService policy assertion. aoqi@0: * aoqi@0: * @author Fabian Ritzmann aoqi@0: */ aoqi@0: public class ManagedServiceAssertion extends ManagementAssertion { aoqi@0: aoqi@0: public static final QName MANAGED_SERVICE_QNAME = aoqi@0: new QName(PolicyConstants.SUN_MANAGEMENT_NAMESPACE, "ManagedService"); aoqi@0: aoqi@0: private static final QName COMMUNICATION_SERVER_IMPLEMENTATIONS_PARAMETER_QNAME = new QName( aoqi@0: PolicyConstants.SUN_MANAGEMENT_NAMESPACE, "CommunicationServerImplementations"); aoqi@0: private static final QName COMMUNICATION_SERVER_IMPLEMENTATION_PARAMETER_QNAME = new QName( aoqi@0: PolicyConstants.SUN_MANAGEMENT_NAMESPACE, "CommunicationServerImplementation"); aoqi@0: private static final QName CONFIGURATOR_IMPLEMENTATION_PARAMETER_QNAME = new QName( aoqi@0: PolicyConstants.SUN_MANAGEMENT_NAMESPACE, "ConfiguratorImplementation"); aoqi@0: private static final QName CONFIG_SAVER_IMPLEMENTATION_PARAMETER_QNAME = new QName( aoqi@0: PolicyConstants.SUN_MANAGEMENT_NAMESPACE, "ConfigSaverImplementation"); aoqi@0: private static final QName CONFIG_READER_IMPLEMENTATION_PARAMETER_QNAME = new QName( aoqi@0: PolicyConstants.SUN_MANAGEMENT_NAMESPACE, "ConfigReaderImplementation"); aoqi@0: private static final QName CLASS_NAME_ATTRIBUTE_QNAME = new QName("className"); aoqi@0: /** aoqi@0: * The name of the endpointDisposeDelay attribute. aoqi@0: */ aoqi@0: private static final QName ENDPOINT_DISPOSE_DELAY_ATTRIBUTE_QNAME = new QName("endpointDisposeDelay"); aoqi@0: aoqi@0: private static final Logger LOGGER = Logger.getLogger(ManagedServiceAssertion.class); aoqi@0: aoqi@0: /** aoqi@0: * Return ManagedService assertion if there is one associated with the endpoint. aoqi@0: * aoqi@0: * @param endpoint The endpoint. Must not be null. aoqi@0: * @return The policy assertion if found. Null otherwise. aoqi@0: * @throws WebServiceException If computing the effective policy of the endpoint failed. aoqi@0: */ aoqi@0: public static ManagedServiceAssertion getAssertion(WSEndpoint endpoint) throws WebServiceException { aoqi@0: LOGGER.entering(endpoint); aoqi@0: // getPolicyMap is deprecated because it is only supposed to be used by Metro code aoqi@0: // and not by other clients. aoqi@0: @SuppressWarnings("deprecation") aoqi@0: final PolicyMap policyMap = endpoint.getPolicyMap(); aoqi@0: final ManagedServiceAssertion assertion = ManagementAssertion.getAssertion(MANAGED_SERVICE_QNAME, aoqi@0: policyMap, endpoint.getServiceName(), endpoint.getPortName(), ManagedServiceAssertion.class); aoqi@0: LOGGER.exiting(assertion); aoqi@0: return assertion; aoqi@0: } aoqi@0: aoqi@0: public ManagedServiceAssertion(AssertionData data, Collection assertionParameters) aoqi@0: throws AssertionCreationException { aoqi@0: super(MANAGED_SERVICE_QNAME, data, assertionParameters); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the value of the management attribute. True if unset or set to "true" aoqi@0: * or "on". False otherwise. aoqi@0: * aoqi@0: * @return The value of the management attribute. aoqi@0: */ aoqi@0: public boolean isManagementEnabled() { aoqi@0: final String management = this.getAttributeValue(MANAGEMENT_ATTRIBUTE_QNAME); aoqi@0: boolean result = true; aoqi@0: if (management != null) { aoqi@0: if (management.trim().toLowerCase().equals("on")) { aoqi@0: result = true; aoqi@0: } aoqi@0: else { aoqi@0: result = Boolean.parseBoolean(management); aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the value of the endpointDisposeDelay attribute or the default value aoqi@0: * otherwise. aoqi@0: * aoqi@0: * @param defaultDelay The default value that is returned if this attribute is aoqi@0: * not set aoqi@0: * @return The value of the endpointDisposeDelay attribute or the default value aoqi@0: * otherwise. aoqi@0: */ aoqi@0: public long getEndpointDisposeDelay(final long defaultDelay) throws WebServiceException { aoqi@0: long result = defaultDelay; aoqi@0: final String delayText = getAttributeValue(ENDPOINT_DISPOSE_DELAY_ATTRIBUTE_QNAME); aoqi@0: if (delayText != null) { aoqi@0: try { aoqi@0: result = Long.parseLong(delayText); aoqi@0: } catch (NumberFormatException e) { aoqi@0: throw LOGGER.logSevereException(new WebServiceException( aoqi@0: ManagementMessages.WSM_1008_EXPECTED_INTEGER_DISPOSE_DELAY_VALUE(delayText), e)); aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * A list of CommunicationServerImplementation elements that were set as aoqi@0: * parameters of this assertion. aoqi@0: * aoqi@0: * @return A list of CommunicationServerImplementation elements that were set as aoqi@0: * parameters of this assertion. May be empty. aoqi@0: */ aoqi@0: public Collection getCommunicationServerImplementations() { aoqi@0: final Collection result = new LinkedList(); aoqi@0: final Iterator parameters = getParametersIterator(); aoqi@0: while (parameters.hasNext()) { aoqi@0: final PolicyAssertion parameter = parameters.next(); aoqi@0: if (COMMUNICATION_SERVER_IMPLEMENTATIONS_PARAMETER_QNAME.equals(parameter.getName())) { aoqi@0: final Iterator implementations = parameter.getParametersIterator(); aoqi@0: if (!implementations.hasNext()) { aoqi@0: throw LOGGER.logSevereException(new WebServiceException( aoqi@0: ManagementMessages.WSM_1005_EXPECTED_COMMUNICATION_CHILD())); aoqi@0: } aoqi@0: while (implementations.hasNext()) { aoqi@0: final PolicyAssertion implementation = implementations.next(); aoqi@0: if (COMMUNICATION_SERVER_IMPLEMENTATION_PARAMETER_QNAME.equals(implementation.getName())) { aoqi@0: result.add(getImplementation(implementation)); aoqi@0: } aoqi@0: else { aoqi@0: throw LOGGER.logSevereException(new WebServiceException( aoqi@0: ManagementMessages.WSM_1004_EXPECTED_XML_TAG( aoqi@0: COMMUNICATION_SERVER_IMPLEMENTATION_PARAMETER_QNAME, implementation.getName()))); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The ConfiguratorImplementation that was set as parameter of this assertion. aoqi@0: * aoqi@0: * @return The ConfiguratorImplementation that was set as parameter of this assertion. aoqi@0: * May be null. aoqi@0: */ aoqi@0: public ImplementationRecord getConfiguratorImplementation() { aoqi@0: return findImplementation(CONFIGURATOR_IMPLEMENTATION_PARAMETER_QNAME); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The ConfigSaverImplementation that was set as parameter of this assertion. aoqi@0: * aoqi@0: * @return The ConfigSaverImplementation that was set as parameter of this assertion. aoqi@0: * May be null. aoqi@0: */ aoqi@0: public ImplementationRecord getConfigSaverImplementation() { aoqi@0: return findImplementation(CONFIG_SAVER_IMPLEMENTATION_PARAMETER_QNAME); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The ConfigReaderImplementation that was set as parameter of this assertion. aoqi@0: * aoqi@0: * @return The ConfigReaderImplementation that was set as parameter of this assertion. aoqi@0: * May be null. aoqi@0: */ aoqi@0: public ImplementationRecord getConfigReaderImplementation() { aoqi@0: return findImplementation(CONFIG_READER_IMPLEMENTATION_PARAMETER_QNAME); aoqi@0: } aoqi@0: aoqi@0: private ImplementationRecord findImplementation(QName implementationName) { aoqi@0: final Iterator parameters = getParametersIterator(); aoqi@0: while (parameters.hasNext()) { aoqi@0: final PolicyAssertion parameter = parameters.next(); aoqi@0: if (implementationName.equals(parameter.getName())) { aoqi@0: return getImplementation(parameter); aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: private ImplementationRecord getImplementation(PolicyAssertion rootParameter) { aoqi@0: final String className = rootParameter.getAttributeValue(CLASS_NAME_ATTRIBUTE_QNAME); aoqi@0: final HashMap parameterMap = new HashMap(); aoqi@0: final Iterator implementationParameters = rootParameter.getParametersIterator(); aoqi@0: final Collection nestedParameters = new LinkedList(); aoqi@0: while (implementationParameters.hasNext()) { aoqi@0: final PolicyAssertion parameterAssertion = implementationParameters.next(); aoqi@0: final QName parameterName = parameterAssertion.getName(); aoqi@0: if (parameterAssertion.hasParameters()) { aoqi@0: final Map nestedParameterMap = new HashMap(); aoqi@0: final Iterator parameters = parameterAssertion.getParametersIterator(); aoqi@0: while (parameters.hasNext()) { aoqi@0: final PolicyAssertion parameter = parameters.next(); aoqi@0: String value = parameter.getValue(); aoqi@0: if (value != null) { aoqi@0: value = value.trim(); aoqi@0: } aoqi@0: nestedParameterMap.put(parameter.getName(), value); aoqi@0: } aoqi@0: nestedParameters.add(new NestedParameters(parameterName, nestedParameterMap)); aoqi@0: } aoqi@0: else { aoqi@0: String value = parameterAssertion.getValue(); aoqi@0: if (value != null) { aoqi@0: value = value.trim(); aoqi@0: } aoqi@0: parameterMap.put(parameterName, value); aoqi@0: } aoqi@0: } aoqi@0: return new ImplementationRecord(className, parameterMap, nestedParameters); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return the implementation class name along with all parameters for the aoqi@0: * implementation. aoqi@0: */ aoqi@0: public static class ImplementationRecord { aoqi@0: aoqi@0: private final String implementation; aoqi@0: private final Map parameters; aoqi@0: private final Collection nestedParameters; aoqi@0: aoqi@0: protected ImplementationRecord(String implementation, Map parameters, aoqi@0: Collection nestedParameters) { aoqi@0: this.implementation = implementation; aoqi@0: this.parameters = parameters; aoqi@0: this.nestedParameters = nestedParameters; aoqi@0: } aoqi@0: aoqi@0: public String getImplementation() { aoqi@0: return this.implementation; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The parameters that were set for this implementation element. aoqi@0: * aoqi@0: * @return The parameters that were set for this implementation element. aoqi@0: * May be null. aoqi@0: */ aoqi@0: public Map getParameters() { aoqi@0: return this.parameters; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Implementation elements may contain element parameters that contain aoqi@0: * further parameters. aoqi@0: * aoqi@0: * @return The nested parameters that were set for this implementation element. aoqi@0: * May be null. aoqi@0: */ aoqi@0: public Collection getNestedParameters() { aoqi@0: return this.nestedParameters; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean equals(Object obj) { aoqi@0: if (obj == null) { aoqi@0: return false; aoqi@0: } aoqi@0: if (getClass() != obj.getClass()) { aoqi@0: return false; aoqi@0: } aoqi@0: final ImplementationRecord other = (ImplementationRecord) obj; aoqi@0: if ((this.implementation == null) ? aoqi@0: (other.implementation != null) : !this.implementation.equals(other.implementation)) { aoqi@0: return false; aoqi@0: } aoqi@0: if (this.parameters != other.parameters && (this.parameters == null || !this.parameters.equals(other.parameters))) { aoqi@0: return false; aoqi@0: } aoqi@0: if (this.nestedParameters != other.nestedParameters && aoqi@0: (this.nestedParameters == null || !this.nestedParameters.equals(other.nestedParameters))) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int hashCode() { aoqi@0: int hash = 3; aoqi@0: hash = 53 * hash + (this.implementation != null ? this.implementation.hashCode() : 0); aoqi@0: hash = 53 * hash + (this.parameters != null ? this.parameters.hashCode() : 0); aoqi@0: hash = 53 * hash + (this.nestedParameters != null ? this.nestedParameters.hashCode() : 0); aoqi@0: return hash; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String toString() { aoqi@0: final StringBuilder text = new StringBuilder("ImplementationRecord: "); aoqi@0: text.append("implementation = \"").append(this.implementation).append("\", "); aoqi@0: text.append("parameters = \"").append(this.parameters).append("\", "); aoqi@0: text.append("nested parameters = \"").append(this.nestedParameters).append("\""); aoqi@0: return text.toString(); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * The nested parameters that may be set as part of an implementation element. aoqi@0: */ aoqi@0: public static class NestedParameters { aoqi@0: aoqi@0: private final QName name; aoqi@0: private final Map parameters; aoqi@0: aoqi@0: private NestedParameters(QName name, Map parameters) { aoqi@0: this.name = name; aoqi@0: this.parameters = parameters; aoqi@0: } aoqi@0: aoqi@0: public QName getName() { aoqi@0: return this.name; aoqi@0: } aoqi@0: aoqi@0: public Map getParameters() { aoqi@0: return this.parameters; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean equals(Object obj) { aoqi@0: if (obj == null) { aoqi@0: return false; aoqi@0: } aoqi@0: if (getClass() != obj.getClass()) { aoqi@0: return false; aoqi@0: } aoqi@0: final NestedParameters other = (NestedParameters) obj; aoqi@0: if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { aoqi@0: return false; aoqi@0: } aoqi@0: if (this.parameters != other.parameters && (this.parameters == null || !this.parameters.equals(other.parameters))) { aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int hashCode() { aoqi@0: int hash = 5; aoqi@0: hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0); aoqi@0: hash = 59 * hash + (this.parameters != null ? this.parameters.hashCode() : 0); aoqi@0: return hash; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String toString() { aoqi@0: final StringBuilder text = new StringBuilder("NestedParameters: "); aoqi@0: text.append("name = \"").append(this.name).append("\", "); aoqi@0: text.append("parameters = \"").append(this.parameters).append("\""); aoqi@0: return text.toString(); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: }