From d123827b572d59f86e23d4e7fac42864d646ee60 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Tue, 7 Sep 2010 23:52:54 +0000 Subject: Sandbox to experiment with deployment and management commands. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@993560 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tuscany/sca/policy/logging/Logging.java | 46 ++++++ .../JDKLoggingImplementationPolicyProvider.java | 48 ++++++ .../sca/policy/logging/jdk/JDKLoggingPolicy.java | 97 ++++++++++++ .../logging/jdk/JDKLoggingPolicyBuilder.java | 84 ++++++++++ .../logging/jdk/JDKLoggingPolicyInterceptor.java | 171 +++++++++++++++++++++ .../logging/jdk/JDKLoggingPolicyProcessor.java | 135 ++++++++++++++++ .../jdk/JDKLoggingPolicyProviderFactory.java | 68 ++++++++ .../jdk/JDKLoggingReferencePolicyProvider.java | 45 ++++++ .../jdk/JDKLoggingServicePolicyProvider.java | 44 ++++++ ...ache.tuscany.sca.assembly.builder.PolicyBuilder | 17 ++ ...ca.contribution.processor.StAXArtifactProcessor | 20 +++ ....apache.tuscany.sca.definitions.xml.Definitions | 17 ++ ...ache.tuscany.sca.provider.PolicyProviderFactory | 20 +++ .../tuscany/sca/policy/logging/definitions.xml | 38 +++++ .../sca/policy/logging/jdk/PolicyReadTestCase.java | 94 +++++++++++ .../sca/policy/logging/jdk/mock_policies.xml | 23 +++ 16 files changed, 967 insertions(+) create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/Logging.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingImplementationPolicyProvider.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicy.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyBuilder.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyInterceptor.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProcessor.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProviderFactory.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingReferencePolicyProvider.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingServicePolicyProvider.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.PolicyBuilder create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.definitions.xml.Definitions create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.PolicyProviderFactory create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/org/apache/tuscany/sca/policy/logging/definitions.xml create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/test/java/org/apache/tuscany/sca/policy/logging/jdk/PolicyReadTestCase.java create mode 100644 sandbox/sebastien/java/shell/modules/policy-logging/src/test/resources/org/apache/tuscany/sca/policy/logging/jdk/mock_policies.xml (limited to 'sandbox/sebastien/java/shell/modules/policy-logging/src') diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/Logging.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/Logging.java new file mode 100644 index 0000000000..4bed9520ab --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/Logging.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.logging; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.apache.tuscany.sca.assembly.Base; +import org.oasisopen.sca.annotation.Intent; +import org.oasisopen.sca.annotation.Qualifier; + +@Inherited +@Target({TYPE, FIELD, METHOD, PARAMETER}) +@Retention(RUNTIME) +@Intent(Logging.LOGGING) + +public @interface Logging { + String LOGGING = "{"+Base.SCA11_TUSCANY_NS + "}logging"; + + @Qualifier + String[] value() default ""; +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingImplementationPolicyProvider.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingImplementationPolicyProvider.java new file mode 100644 index 0000000000..66a0ba2b51 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingImplementationPolicyProvider.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.logging.jdk; + +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.invocation.PhasedInterceptor; +import org.apache.tuscany.sca.provider.BasePolicyProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * @version $Rev$ $Date$ + */ +public class JDKLoggingImplementationPolicyProvider extends BasePolicyProvider { + + public JDKLoggingImplementationPolicyProvider(RuntimeComponent component) { + super(JDKLoggingPolicy.class, component); + } + + /** + * @see org.apache.tuscany.sca.provider.PolicyProvider#createInterceptor(org.apache.tuscany.sca.interfacedef.Operation) + */ + public PhasedInterceptor createInterceptor(Operation operation) { + List policies = findPolicies(); + return policies.isEmpty() ? null : new JDKLoggingPolicyInterceptor(subject, getContext(), operation, + policies, Phase.IMPLEMENTATION_POLICY); + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicy.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicy.java new file mode 100644 index 0000000000..6e1bad67c1 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicy.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.policy.logging.jdk; + +import java.util.logging.Level; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Base; + +/** + * Implementation for policies that could be injected as parameter + * into the axis2config. + * + * @version $Rev$ $Date$ + */ +public class JDKLoggingPolicy { + static final String SCA11_NS = Base.SCA11_NS; + static final String SCA11_TUSCANY_NS = Base.SCA11_TUSCANY_NS; + static final QName JDK_LOGGING_POLICY_QNAME = new QName(SCA11_TUSCANY_NS, "jdkLogger"); + + private String loggerName; + private String resourceBundleName; + private Level logLevel; + private boolean useParentHandlers = false; + + public String getLoggerName() { + return loggerName; + } + + public void setLoggerName(String loggerName) { + this.loggerName = loggerName; + } + + public Level getLogLevel() { + return logLevel; + } + + public void setLogLevel(Level logLevel) { + this.logLevel = logLevel; + } + + public String getResourceBundleName() { + return resourceBundleName; + } + + public void setResourceBundleName(String resourceBundleName) { + this.resourceBundleName = resourceBundleName; + } + + public boolean isUseParentHandlers() { + return useParentHandlers; + } + + public void setUseParentHandlers(boolean useParentHandlers) { + this.useParentHandlers = useParentHandlers; + } + + public QName getSchemaName() { + return JDK_LOGGING_POLICY_QNAME; + } + + public boolean isUnresolved() { + return false; + } + + public void setUnresolved(boolean unresolved) { + } + + @Override + public String toString() { + return "JDKLoggingPolicy [logLevel=" + logLevel + + ", loggerName=" + + loggerName + + ", resourceBundleName=" + + resourceBundleName + + ", useParentHandlers=" + + useParentHandlers + + "]"; + } +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyBuilder.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyBuilder.java new file mode 100644 index 0000000000..8a71203b70 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyBuilder.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.logging.jdk; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.PolicyBuilder; +import org.apache.tuscany.sca.policy.PolicyExpression; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; + +/** + * + */ +public class JDKLoggingPolicyBuilder implements PolicyBuilder { + + public boolean build(Endpoint endpoint, BuilderContext context) { + List polices = getPolicies(endpoint); + System.out.println(endpoint + ": " + polices); + return true; + } + + public boolean build(EndpointReference endpointReference, BuilderContext context) { + List polices = getPolicies(endpointReference); + System.out.println(endpointReference + ": " + polices); + return true; + } + + public boolean build(Component component, Implementation implementation, BuilderContext context) { + List polices = getPolicies(implementation); + System.out.println(implementation + ": " + polices); + return true; + } + + public QName getPolicyType() { + return JDKLoggingPolicy.JDK_LOGGING_POLICY_QNAME; + } + + public List getSupportedBindings() { + return null; + } + + private List getPolicies(PolicySubject subject) { + List polices = new ArrayList(); + for (PolicySet ps : subject.getPolicySets()) { + for (PolicyExpression exp : ps.getPolicies()) { + if (getPolicyType().equals(exp.getName())) { + polices.add((JDKLoggingPolicy)exp.getPolicy()); + } + } + } + return polices; + } + + public boolean build(EndpointReference endpointReference, Endpoint endpoint, BuilderContext context) { + return true; + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyInterceptor.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyInterceptor.java new file mode 100644 index 0000000000..b6c83a804a --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyInterceptor.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.policy.logging.jdk; + +import java.util.List; +import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.PhasedInterceptor; +import org.apache.tuscany.sca.policy.PolicySubject; + +/** + * Policy handler to handle PolicySet related to Logging with the QName + * {http://tuscany.apache.org/xmlns/sca/1.1/impl/java}LoggingPolicy + * + * @version $Rev$ $Date$ + */ +public class JDKLoggingPolicyInterceptor implements PhasedInterceptor { + public static final String loggingPolicy = "JDKLoggingPolicy"; + public static final QName policySetQName = new QName(JDKLoggingPolicy.SCA11_TUSCANY_NS, loggingPolicy); + private Logger logger = null; + + private Invoker next; + private Operation operation; + private List policies; + private PolicySubject subject; + private String context; + private String phase; + + public JDKLoggingPolicyInterceptor(PolicySubject subject, + String context, + Operation operation, + List policies, + String phase) { + super(); + this.operation = operation; + this.policies = policies; + this.subject = subject; + this.phase = phase; + this.context = getContext(); + init(); + } + + private String getContext() { + if (subject instanceof Endpoint) { + Endpoint endpoint = (Endpoint)subject; + return endpoint.getURI(); + } else if (subject instanceof EndpointReference) { + EndpointReference endpointReference = (EndpointReference)subject; + return endpointReference.getURI(); + } else if (subject instanceof Component) { + Component component = (Component)subject; + return component.getURI(); + } + return null; + } + + private void init() { + JDKLoggingPolicy policy = policies.get(0); + logger = Logger.getLogger(policy.getLoggerName()); + logger.setLevel(policy.getLogLevel()); + logger.setUseParentHandlers(policy.isUseParentHandlers()); + + boolean found = false; + for (Handler handler : logger.getHandlers()) { + if (handler instanceof ConsoleHandler) { + found = true; + break; + } + } + + if (!found) { + ConsoleHandler consoleHandler = new ConsoleHandler(); + consoleHandler.setLevel(Level.ALL); + logger.addHandler(consoleHandler); + } + + if (logger.isLoggable(Level.FINE)) { + logger.logp(Level.FINE, context, "", "Intents: {0}", subject.getRequiredIntents()); + logger.logp(Level.FINE, context, "", "PolicySets: {0}", subject.getPolicySets()); + } + } + + public Message invoke(Message msg) { + if (logger == null) { + return getNext().invoke(msg); + } + logger.logp(Level.INFO, context, "", "Invoking operation - " + operation.getName()); + Object msgBody = msg.getBody(); + if (msgBody instanceof Object[]) { + + if (logger.isLoggable(Level.FINE)) { + StringBuffer sb = new StringBuffer(); + if (msgBody == null) { + sb.append(""); + } else { + Object[] args = (Object[])msgBody; + for (int i = 0; i < args.length; i++) { + sb.append(args[i]); + if (i != args.length - 1) { + sb.append(", "); + } + } + } + + Object[] logParams = new Object[] {operation.getName(), sb.toString()}; + logger.logp(Level.FINE, context, "", "Invoking operation {0} with arguments {1}", logParams); + } + } + + Message responseMsg = null; + try { + responseMsg = getNext().invoke(msg); + return responseMsg; + } catch (RuntimeException e) { + logger.logp(Level.SEVERE, context, "", "Exception thrown from operation - " + operation.getName(), e); + throw e; + } finally { + if (responseMsg != null) { + logger.logp(Level.INFO, context, "", "Returned from operation - " + operation.getName()); + if (logger.isLoggable(Level.FINE)) { + Object[] logParams = new Object[] {operation.getName(), responseMsg.getBody()}; + logger.logp(Level.FINE, + context, + "", + "Returning from operation {0} with return value {1}", + logParams); + } + } + } + } + + public Invoker getNext() { + return next; + } + + public void setNext(Invoker next) { + this.next = next; + } + + public String getPhase() { + return phase; + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProcessor.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProcessor.java new file mode 100644 index 0000000000..a79aeb0e28 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProcessor.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.policy.logging.jdk; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import java.util.logging.Level; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; + +/** + * + * @version $Rev$ $Date$ + */ +public class JDKLoggingPolicyProcessor implements StAXArtifactProcessor { + private static final QName JDK_LOGGING_POLICY_QNAME = new QName(JDKLoggingPolicy.SCA11_TUSCANY_NS, "jdkLogger"); + private static final String LOG_LEVEL = "logLevel"; + private static final String RESOURCE_BUNDLE = "resourceBundle"; + private static final String USE_PARENT_HANDLERS = "useParentHandlers"; + + public QName getArtifactType() { + return JDK_LOGGING_POLICY_QNAME; + } + + public JDKLoggingPolicyProcessor(FactoryExtensionPoint modelFactories) { + } + + + public JDKLoggingPolicy read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + JDKLoggingPolicy policy = new JDKLoggingPolicy(); + int event = reader.getEventType(); + QName name = null; + + + while (reader.hasNext()) { + event = reader.getEventType(); + switch (event) { + case START_ELEMENT : { + name = reader.getName(); + if ( name.equals(JDK_LOGGING_POLICY_QNAME) ) { + String loggerName = reader.getAttributeValue(null, "name"); + policy.setLoggerName(loggerName); + } else if ( LOG_LEVEL.equals(name.getLocalPart()) ) { + policy.setLogLevel(Level.parse(reader.getElementText())); + } else if ( RESOURCE_BUNDLE.equals(name.getLocalPart()) ) { + policy.setResourceBundleName(reader.getElementText()); + } else if ( USE_PARENT_HANDLERS.equals(name.getLocalPart()) ) { + policy.setUseParentHandlers(Boolean.parseBoolean(reader.getElementText())); + } + break; + } + } + + if ( event == END_ELEMENT ) { + if ( JDK_LOGGING_POLICY_QNAME.equals(reader.getName()) ) { + break; + } + } + + //Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + + return policy; + } + + public void write(JDKLoggingPolicy policy, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + String prefix = "tuscany"; + writer.writeStartElement(prefix, + JDK_LOGGING_POLICY_QNAME.getLocalPart(), + JDK_LOGGING_POLICY_QNAME.getNamespaceURI()); + writer.writeNamespace("tuscany", JDKLoggingPolicy.SCA11_TUSCANY_NS); + + if (policy.getLoggerName() != null) { + writer.writeAttribute("name", policy.getLoggerName()); + } + if ( policy.getLogLevel() != null ) { + writer.writeStartElement(prefix, + LOG_LEVEL, + JDK_LOGGING_POLICY_QNAME.getNamespaceURI()); + writer.writeCharacters(policy.getLogLevel().getLocalizedName()); + writer.writeEndElement(); + } + + if ( policy.getResourceBundleName() != null ) { + writer.writeStartElement(prefix, + RESOURCE_BUNDLE, + JDK_LOGGING_POLICY_QNAME.getNamespaceURI()); + writer.writeCharacters(policy.getResourceBundleName()); + writer.writeEndElement(); + } + + writer.writeEndElement(); + } + + public Class getModelType() { + return JDKLoggingPolicy.class; + } + + public void resolve(JDKLoggingPolicy arg0, ModelResolver arg1, ProcessorContext context) throws ContributionResolveException { + + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProviderFactory.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProviderFactory.java new file mode 100644 index 0000000000..62bf06475b --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingPolicyProviderFactory.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.logging.jdk; + +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.provider.PolicyProvider; +import org.apache.tuscany.sca.provider.PolicyProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; + +/** + * @version $Rev$ $Date$ + */ +public class JDKLoggingPolicyProviderFactory implements PolicyProviderFactory { + private ExtensionPointRegistry registry; + + public JDKLoggingPolicyProviderFactory(ExtensionPointRegistry registry) { + super(); + this.registry = registry; + } + + /** + * @see org.apache.tuscany.sca.provider.PolicyProviderFactory#createImplementationPolicyProvider(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.assembly.Implementation) + */ + public PolicyProvider createImplementationPolicyProvider(RuntimeComponent component) { + return new JDKLoggingImplementationPolicyProvider(component); + } + + /** + * @see org.apache.tuscany.sca.provider.PolicyProviderFactory#createReferencePolicyProvider(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.runtime.RuntimeComponentReference, org.apache.tuscany.sca.assembly.Binding) + */ + public PolicyProvider createReferencePolicyProvider(EndpointReference endpointReference) { + return new JDKLoggingReferencePolicyProvider(endpointReference); + } + + /** + * @see org.apache.tuscany.sca.provider.PolicyProviderFactory#createServicePolicyProvider(org.apache.tuscany.sca.runtime.RuntimeComponent, org.apache.tuscany.sca.runtime.RuntimeComponentService, org.apache.tuscany.sca.assembly.Binding) + */ + public PolicyProvider createServicePolicyProvider(Endpoint endpoint) { + return new JDKLoggingServicePolicyProvider(endpoint); + } + + /** + * @see org.apache.tuscany.sca.provider.ProviderFactory#getModelType() + */ + public Class getModelType() { + return JDKLoggingPolicy.class; + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingReferencePolicyProvider.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingReferencePolicyProvider.java new file mode 100644 index 0000000000..80867f57de --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingReferencePolicyProvider.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.logging.jdk; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.invocation.PhasedInterceptor; +import org.apache.tuscany.sca.provider.BasePolicyProvider; + +/** + * @version $Rev$ $Date$ + */ +public class JDKLoggingReferencePolicyProvider extends BasePolicyProvider { + + public JDKLoggingReferencePolicyProvider(EndpointReference endpointReference) { + super(JDKLoggingPolicy.class, endpointReference); + } + + public PhasedInterceptor createInterceptor(Operation operation) { + List policies = findPolicies(); + return policies.isEmpty() ? null : new JDKLoggingPolicyInterceptor(subject, getContext(), operation, + policies, Phase.REFERENCE_POLICY); + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingServicePolicyProvider.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingServicePolicyProvider.java new file mode 100644 index 0000000000..99f0563603 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/java/org/apache/tuscany/sca/policy/logging/jdk/JDKLoggingServicePolicyProvider.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.logging.jdk; + +import java.util.List; + +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.invocation.PhasedInterceptor; +import org.apache.tuscany.sca.provider.BasePolicyProvider; + +/** + * @version $Rev$ $Date$ + */ +public class JDKLoggingServicePolicyProvider extends BasePolicyProvider { + + public JDKLoggingServicePolicyProvider(Endpoint endpoint) { + super(JDKLoggingPolicy.class, endpoint); + } + + public PhasedInterceptor createInterceptor(Operation operation) { + List policies = findPolicies(); + return policies.isEmpty() ? null : new JDKLoggingPolicyInterceptor(subject, getContext(), operation, policies, Phase.SERVICE_POLICY); + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.PolicyBuilder b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.PolicyBuilder new file mode 100644 index 0000000000..f1fbc54a7a --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.PolicyBuilder @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +org.apache.tuscany.sca.policy.logging.jdk.JDKLoggingPolicyBuilder;qname=http://tuscany.apache.org/xmlns/sca/1.1#jdkLogger diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor new file mode 100644 index 0000000000..9d0a8fb67f --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Implementation class for the artifact processor extension +org.apache.tuscany.sca.policy.logging.jdk.JDKLoggingPolicyProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.1#jdkLogger,model=org.apache.tuscany.sca.policy.logging.jdk.JDKLoggingPolicy + diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.definitions.xml.Definitions b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.definitions.xml.Definitions new file mode 100644 index 0000000000..bb7aed2087 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.definitions.xml.Definitions @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +org/apache/tuscany/sca/policy/logging/definitions.xml diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.PolicyProviderFactory b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.PolicyProviderFactory new file mode 100644 index 0000000000..b9bb454550 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.PolicyProviderFactory @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Implementation class for the policy extension +org.apache.tuscany.sca.policy.logging.jdk.JDKLoggingPolicyProviderFactory;model=org.apache.tuscany.sca.policy.logging.jdk.JDKLoggingPolicy + diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/org/apache/tuscany/sca/policy/logging/definitions.xml b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/org/apache/tuscany/sca/policy/logging/definitions.xml new file mode 100644 index 0000000000..84b7f94e84 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/main/resources/org/apache/tuscany/sca/policy/logging/definitions.xml @@ -0,0 +1,38 @@ + + + + + + + + All messages to and from this implementation must be logged + + + + + + + All messages to and from this implementation must be logged + + + + \ No newline at end of file diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/test/java/org/apache/tuscany/sca/policy/logging/jdk/PolicyReadTestCase.java b/sandbox/sebastien/java/shell/modules/policy-logging/src/test/java/org/apache/tuscany/sca/policy/logging/jdk/PolicyReadTestCase.java new file mode 100644 index 0000000000..0000ec8c06 --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/test/java/org/apache/tuscany/sca/policy/logging/jdk/PolicyReadTestCase.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.policy.logging.jdk; + + +import java.io.InputStream; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.URL; +import java.util.logging.Level; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; + +/** + * Test the reading of ws config params policy. + * + * @version $Rev$ $Date$ + */ +public class PolicyReadTestCase extends TestCase { + private ProcessorContext context = new ProcessorContext(); + + @Override + public void setUp() throws Exception { + } + + @Override + public void tearDown() throws Exception { + } + + public void testPolicyReading() throws Exception { + JDKLoggingPolicyProcessor processor = new JDKLoggingPolicyProcessor(null); + + URL url = getClass().getResource("mock_policies.xml"); + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + + InputStream urlStream = url.openStream(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream); + + + JDKLoggingPolicy policy = processor.read(reader, context); + assertEquals(policy.getLoggerName(), "test.logger"); + assertEquals(policy.getLogLevel(), Level.INFO ); + assertEquals(policy.getResourceBundleName(), "Trace_Messages.properties"); + } + + public void testPolicyWriting() throws Exception { + JDKLoggingPolicyProcessor processor = new JDKLoggingPolicyProcessor(null); + + JDKLoggingPolicy policy = new JDKLoggingPolicy(); + policy.setLoggerName("test.logger"); + policy.setLogLevel(Level.INFO); + policy.setResourceBundleName("Trace_Messages.properties"); + + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + StringWriter sw = new StringWriter(); + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw); + processor.write(policy, writer, context); + writer.close(); + + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + StringReader sr = new StringReader(sw.toString()); + XMLStreamReader reader = inputFactory.createXMLStreamReader(sr); + + policy = processor.read(reader, context); + assertEquals(policy.getLoggerName(), "test.logger"); + assertEquals(policy.getLogLevel(), Level.INFO ); + assertEquals(policy.getResourceBundleName(), "Trace_Messages.properties"); + + } + +} diff --git a/sandbox/sebastien/java/shell/modules/policy-logging/src/test/resources/org/apache/tuscany/sca/policy/logging/jdk/mock_policies.xml b/sandbox/sebastien/java/shell/modules/policy-logging/src/test/resources/org/apache/tuscany/sca/policy/logging/jdk/mock_policies.xml new file mode 100644 index 0000000000..0f1c1f978c --- /dev/null +++ b/sandbox/sebastien/java/shell/modules/policy-logging/src/test/resources/org/apache/tuscany/sca/policy/logging/jdk/mock_policies.xml @@ -0,0 +1,23 @@ + + + + INFO + Trace_Messages.properties + \ No newline at end of file -- cgit v1.2.3