diff options
Diffstat (limited to 'sandbox/event/modules/policy-transaction/src')
24 files changed, 1661 insertions, 0 deletions
diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/IncompatibleIntentException.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/IncompatibleIntentException.java new file mode 100644 index 0000000000..8a242924ee --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/IncompatibleIntentException.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.transaction; + +/** + * @version $Rev$ $Date$ + */ +public class IncompatibleIntentException extends Exception { + private static final long serialVersionUID = -2993825019200280397L; + + public IncompatibleIntentException() { + super(); + } + + public IncompatibleIntentException(String message, Throwable cause) { + super(message, cause); + } + + public IncompatibleIntentException(String message) { + super(message); + } + + public IncompatibleIntentException(Throwable cause) { + super(cause); + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionImplementationPolicyProvider.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionImplementationPolicyProvider.java new file mode 100644 index 0000000000..3c11fef865 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionImplementationPolicyProvider.java @@ -0,0 +1,61 @@ +/* + * 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.transaction; + +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySetAttachPoint; +import org.apache.tuscany.sca.provider.PolicyProvider; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionImplementationPolicyProvider implements PolicyProvider { + private TransactionManagerHelper helper; + private List<PolicySet> policySets; + + public TransactionImplementationPolicyProvider(TransactionManagerHelper helper, PolicySetAttachPoint attachPoint) { + super(); + this.helper = helper; + this.policySets = attachPoint.getApplicablePolicySets(); + } + + public Interceptor createInterceptor(Operation operation) { + for (PolicySet policySet : policySets) { + for (Object p : policySet.getPolicies()) { + if (p instanceof TransactionPolicy) { + TransactionInterceptor interceptor = + new TransactionInterceptor(helper, false, (TransactionPolicy)p, null); + return interceptor; + } + } + } + return null; + } + + public String getPhase() { + return Phase.IMPLEMENTATION_POLICY; + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionIntent.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionIntent.java new file mode 100644 index 0000000000..b3e90c23cd --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionIntent.java @@ -0,0 +1,28 @@ +/* + * 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.transaction; + +/** + * + * @version $Rev$ $Date$ + */ +public enum TransactionIntent { + managedTransactionLocal, managedTransactionGlobal, noManagedTransaction, propagatesTransacton, suspendsTransaction, transactedOneWay, immediateOneWay +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionInterceptor.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionInterceptor.java new file mode 100644 index 0000000000..0de98592cb --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionInterceptor.java @@ -0,0 +1,122 @@ +/* + * 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.transaction; + +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionInterceptor implements Interceptor { + private Invoker next; + private TransactionManagerHelper helper; + private boolean outbound; + private TransactionPolicy interactionPolicy; + private TransactionPolicy implementationPolicy; + + public TransactionInterceptor(TransactionManagerHelper helper, + boolean outbound, + TransactionPolicy interactionPolicy, + TransactionPolicy implementationPolicy) { + super(); + this.helper = helper; + this.outbound = outbound; + this.interactionPolicy = interactionPolicy; + this.implementationPolicy = implementationPolicy; + } + + /** + * @see org.apache.tuscany.sca.invocation.Interceptor#getNext() + */ + public Invoker getNext() { + return next; + } + + /** + * @see org.apache.tuscany.sca.invocation.Interceptor#setNext(org.apache.tuscany.sca.invocation.Invoker) + */ + public void setNext(Invoker next) { + this.next = next; + } + + /** + * @see org.apache.tuscany.sca.invocation.Invoker#invoke(org.apache.tuscany.sca.invocation.Message) + */ + public Message invoke(Message msg) { + TransactionalInvocation invocation = new TransactionalInvocation(next, msg); + + Message result = null; + if (msg.getOperation().isNonBlocking()) { + + } + TransactionIntent interactionIntent = TransactionIntent.propagatesTransacton; + if (interactionPolicy != null) { + if (interactionPolicy.getAction() == TransactionPolicy.Action.PROPAGATE) { + interactionIntent = TransactionIntent.propagatesTransacton; + } else { + interactionIntent = TransactionIntent.suspendsTransaction; + } + } + TransactionIntent implementationIntent = TransactionIntent.managedTransactionGlobal; + if (implementationPolicy != null) { + switch (implementationPolicy.getAction()) { + case REQUIRE_GLOBAL: + implementationIntent = TransactionIntent.managedTransactionGlobal; + break; + case REQUIRE_LOCAL: + implementationIntent = TransactionIntent.managedTransactionLocal; + break; + default: + implementationIntent = TransactionIntent.noManagedTransaction; + break; + } + } + try { + if (outbound) { + result = helper.handlesOutbound(interactionIntent, implementationIntent, invocation); + } else { + result = helper.handlesInbound(interactionIntent, implementationIntent, invocation); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result; + } + + private static class TransactionalInvocation implements TransactionalAction<Message> { + private final Invoker invoker; + private final Message message; + + public TransactionalInvocation(Invoker invoker, Message message) { + super(); + this.invoker = invoker; + this.message = message; + } + + public Message run() throws Exception { + return invoker.invoke(message); + } + + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerHelper.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerHelper.java new file mode 100644 index 0000000000..defe12967e --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerHelper.java @@ -0,0 +1,243 @@ +/* + * 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.transaction; + +import java.lang.reflect.InvocationTargetException; + +import javax.transaction.HeuristicMixedException; +import javax.transaction.HeuristicRollbackException; +import javax.transaction.InvalidTransactionException; +import javax.transaction.NotSupportedException; +import javax.transaction.RollbackException; +import javax.transaction.Status; +import javax.transaction.SystemException; +import javax.transaction.Transaction; +import javax.transaction.TransactionManager; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionManagerHelper { + // private static final Logger logger = Logger.getLogger(TransactionManagerHelper.class.getName()); + + private TransactionManager tm; + + public TransactionManagerHelper(TransactionManager tm) { + super(); + this.tm = tm; + } + + public Transaction managedGlobalTransactionPreInvoke() throws SystemException, NotSupportedException { + int status = tm.getStatus(); + if (status == Status.STATUS_COMMITTED || status == Status.STATUS_NO_TRANSACTION + || status == Status.STATUS_ROLLEDBACK) { + tm.begin(); + return tm.getTransaction(); + } + return null; + } + + public void managedGlobalTransactionPostInvoke(Transaction created, boolean rollback) + throws InvalidTransactionException, IllegalStateException, SystemException, SecurityException, + HeuristicMixedException, HeuristicRollbackException, RollbackException { + if (created != null) { + int status = created.getStatus(); + if (status == Status.STATUS_MARKED_ROLLBACK) { + created.rollback(); + } else if (status == Status.STATUS_ACTIVE) { + if (rollback) { + created.rollback(); + } else { + created.commit(); + } + } + } + } + + public Transaction suspendsTransactionPreInvoke() throws SystemException { + if (tm.getTransaction() != null) { + return tm.suspend(); + } else { + return null; + } + } + + public void suspendsTransactionPostInvoke(Transaction suspended) throws InvalidTransactionException, + IllegalStateException, SystemException { + if (suspended != null) { + tm.resume(suspended); + } + } + + public TransactionManager getTransactionManager() { + return tm; + } + + public void setTransactionManager(TransactionManager tm) { + this.tm = tm; + } + + public void validateOneway(TransactionIntent onewayIntent, TransactionIntent implIntent) + throws IncompatibleIntentException { + if (onewayIntent == TransactionIntent.transactedOneWay) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(onewayIntent + "<-X->" + implIntent); + } + } + } + + public void validateInbound(TransactionIntent serviceIntent, TransactionIntent implIntent) + throws IncompatibleIntentException { + if (serviceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(serviceIntent + "<-X->" + implIntent); + } + } + } + + public void validateOutbound(TransactionIntent referenceIntent, TransactionIntent implIntent) + throws IncompatibleIntentException { + if (referenceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(referenceIntent + "<-X->" + implIntent); + } + } + } + + public <T> T handlesOutbound(TransactionIntent referenceIntent, + TransactionIntent implIntent, + TransactionalAction<T> action) throws Exception { + + if (implIntent == null) { + implIntent = TransactionIntent.noManagedTransaction; + } + + if (referenceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(referenceIntent + "<-X->" + implIntent); + } else { + // propagates the current TX + return run(action); + } + } else if (referenceIntent == TransactionIntent.suspendsTransaction) { + Transaction tx = suspendsTransactionPreInvoke(); + try { + return run(action); + } finally { + suspendsTransactionPostInvoke(tx); + } + } else { + return run(action); + } + } + + private <T> T run(TransactionalAction<T> action) throws Exception { + // Make sure a global TX is in place + Transaction tx = managedGlobalTransactionPreInvoke(); + boolean rollback = false; + try { + return action.run(); + } catch (InvocationTargetException e) { + throw e; + } catch (Throwable e) { + rollback = true; + if (e instanceof Error) { + throw (Error)e; + } else if (e instanceof RuntimeException) { + throw (RuntimeException)e; + } else { + throw (Exception)e; + } + } finally { + managedGlobalTransactionPostInvoke(tx, rollback); + } + } + + public <T> T handlesInbound(TransactionIntent serviceIntent, + TransactionIntent implIntent, + TransactionalAction<T> action) throws Exception { + if (serviceIntent == null && implIntent == null) { + return run(action); + } + + if (implIntent == null) { + implIntent = TransactionIntent.noManagedTransaction; + } + + if (serviceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(serviceIntent + "<-X->" + implIntent); + } else { + return run(action); + } + } else if (serviceIntent == TransactionIntent.suspendsTransaction) { + Transaction tx1 = suspendsTransactionPreInvoke(); + try { + if (implIntent == TransactionIntent.managedTransactionGlobal) { + return run(action); + } else { + return action.run(); + } + } finally { + suspendsTransactionPostInvoke(tx1); + } + } else { + if (implIntent == TransactionIntent.managedTransactionGlobal) { + // Start a new TX + return run(action); + } else { + return action.run(); + } + } + } + + public <T> void handlesOneWay(TransactionIntent onewayIntent, + TransactionIntent implIntent, + TransactionalAction<T> action) throws Exception { + if (implIntent == null) { + implIntent = TransactionIntent.noManagedTransaction; + } + + if (onewayIntent == null) { + // Assume transactedOneWay + run(action); + return; + } + + if (onewayIntent == TransactionIntent.transactedOneWay) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(onewayIntent + "<-X->" + implIntent); + } else { + run(action); + return; + } + } else { + // TransactionIntent.immediateOneWay + Transaction tx = suspendsTransactionPreInvoke(); + try { + run(action); + return; + } finally { + suspendsTransactionPostInvoke(tx); + } + } + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerWrapper.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerWrapper.java new file mode 100644 index 0000000000..31beab4fca --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerWrapper.java @@ -0,0 +1,90 @@ +/* + * 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.transaction; + +import java.io.File; + +import javax.transaction.TransactionManager; + +import org.apache.geronimo.transaction.log.HOWLLog; +import org.apache.geronimo.transaction.manager.GeronimoTransactionManager; +import org.apache.geronimo.transaction.manager.XidFactory; +import org.apache.geronimo.transaction.manager.XidFactoryImpl; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionManagerWrapper { + private TransactionManager transactionManager; + private HOWLLog howlLog; + + private String logFileDir = "target/logs"; + private String bufferClassName = "org.objectweb.howl.log.BlockLogBuffer"; + private int bufferSizeKBytes = 32; + private boolean checksumEnabled = true; + private boolean adler32Checksum = true; + private int flushSleepTimeMilliseconds = 50; + private String logFileExt = "log"; + private String logFileName = "transaction"; + private int maxBlocksPerFile = -1; + private int maxLogFiles = 2; + private int maxBuffers = 0; + private int minBuffers = 4; + private int threadsWaitingForceThreshold = -1; + private File serverBaseDir = new File(System.getProperty("basedir", System.getProperty("user.dir"))); + + public TransactionManagerWrapper() { + super(); + } + + public void start() { + try { + XidFactory xidFactory = new XidFactoryImpl(); + howlLog = + new HOWLLog(bufferClassName, bufferSizeKBytes, checksumEnabled, adler32Checksum, + flushSleepTimeMilliseconds, logFileDir, logFileExt, logFileName, maxBlocksPerFile, + maxBuffers, maxLogFiles, minBuffers, threadsWaitingForceThreshold, xidFactory, + serverBaseDir); + + howlLog.doStart(); + transactionManager = new GeronimoTransactionManager(1200, xidFactory, howlLog); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + /** + * @see org.apache.tuscany.sca.core.ModuleActivator#stop(org.apache.tuscany.sca.core.ExtensionPointRegistry) + */ + public void stop() { + try { + if (howlLog != null) { + howlLog.doStop(); + } + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + public TransactionManager getTransactionManager() { + return transactionManager; + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionModuleActivator.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionModuleActivator.java new file mode 100644 index 0000000000..3339f2e338 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionModuleActivator.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.transaction; + +import javax.transaction.TransactionManager; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionModuleActivator implements ModuleActivator { + private TransactionManagerWrapper wrapper; + + /** + * @see org.apache.tuscany.sca.core.ModuleActivator#start(org.apache.tuscany.sca.core.ExtensionPointRegistry) + */ + public void start(ExtensionPointRegistry registry) { + if (registry != null) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + TransactionManager transactionManager = utilities.getUtility(TransactionManager.class); + if (transactionManager != null) { + // The transaction manage is provided by the hosting environment +// RuntimeWireProcessorExtensionPoint wireProcessorExtensionPoint = +// registry.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class); +// TransactionManagerHelper helper = new TransactionManagerHelper(transactionManager); +// wireProcessorExtensionPoint.addWireProcessor(new TransactionRuntimeWireProcessor(helper)); + return; + } + } + try { + wrapper = new TransactionManagerWrapper(); + wrapper.start(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + if (registry != null) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + utilities.addUtility(wrapper.getTransactionManager()); +// RuntimeWireProcessorExtensionPoint wireProcessorExtensionPoint = +// registry.getExtensionPoint(RuntimeWireProcessorExtensionPoint.class); +// TransactionManagerHelper helper = new TransactionManagerHelper(wrapper.getTransactionManager()); +// wireProcessorExtensionPoint.addWireProcessor(new TransactionRuntimeWireProcessor(helper)); + } + } + + /** + * @see org.apache.tuscany.sca.core.ModuleActivator#stop(org.apache.tuscany.sca.core.ExtensionPointRegistry) + */ + public void stop(ExtensionPointRegistry registry) { + try { + if (wrapper != null) { + wrapper.stop(); + wrapper = null; + } + if (registry != null && wrapper != null) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + utilities.removeUtility(wrapper.getTransactionManager()); + } + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicy.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicy.java new file mode 100644 index 0000000000..a13748f232 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicy.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.transaction; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.xml.Constants; +import org.apache.tuscany.sca.policy.Policy; + +/** + * The model for Tuscany transaction policy + * + * @version $Rev$ $Date$ + */ +public interface TransactionPolicy extends Policy { + QName NAME = new QName(Constants.SCA10_TUSCANY_NS, "transactionPolicy"); + + enum Action { + PROPAGATE, SUSPEND, REQUIRE_GLOBAL, REQUIRE_LOCAL, REQUIRE_NONE + }; + + int getTransactionTimeout(); + + void setTransactionTimeout(int seconds); + + void setAction(Action action); + + Action getAction(); +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyDefinitionsProvider.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyDefinitionsProvider.java new file mode 100644 index 0000000000..7b10a7d568 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyDefinitionsProvider.java @@ -0,0 +1,71 @@ +/* + * 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.transaction; + +import java.net.URI; +import java.net.URL; + +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.definitions.SCADefinitions; +import org.apache.tuscany.sca.definitions.util.SCADefinitionsUtil; +import org.apache.tuscany.sca.provider.SCADefinitionsProvider; +import org.apache.tuscany.sca.provider.SCADefinitionsProviderException; + +/** + * Provider for Policy Intents and PolicySet definitions related to transaction + * + * @version $Rev$ $Date$ + */ +public class TransactionPolicyDefinitionsProvider implements SCADefinitionsProvider { + private static final String scaDefinitionsFile = "definitions.xml"; + private static final String tuscanyDefinitionsFile = "tuscany_definitions.xml"; + private URLArtifactProcessor urlArtifactProcessor; + + public TransactionPolicyDefinitionsProvider(ExtensionPointRegistry registry) { + URLArtifactProcessorExtensionPoint documentProcessors = + registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + urlArtifactProcessor = (URLArtifactProcessor)documentProcessors.getProcessor(SCADefinitions.class); + } + + public SCADefinitions getSCADefinition() throws SCADefinitionsProviderException { + + try { + SCADefinitions scaTransactionPolicyDefns = null; + SCADefinitions tuscanyTransactionPolicyDefns = null; + + URI uri = new URI(scaDefinitionsFile); + URL defintionsFileUrl = getClass().getResource(scaDefinitionsFile); + scaTransactionPolicyDefns = (SCADefinitions)urlArtifactProcessor.read(null, uri, defintionsFileUrl); + + uri = new URI(tuscanyDefinitionsFile); + defintionsFileUrl = getClass().getResource(tuscanyDefinitionsFile); + tuscanyTransactionPolicyDefns = (SCADefinitions)urlArtifactProcessor.read(null, uri, defintionsFileUrl); + + SCADefinitionsUtil.aggregateSCADefinitions(tuscanyTransactionPolicyDefns, scaTransactionPolicyDefns); + + return scaTransactionPolicyDefns; + } catch (Exception e) { + throw new SCADefinitionsProviderException(e); + } + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyImpl.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyImpl.java new file mode 100644 index 0000000000..f178876a72 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyImpl.java @@ -0,0 +1,59 @@ +/* + * 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.transaction; + +import javax.xml.namespace.QName; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionPolicyImpl implements TransactionPolicy { + private boolean unresolved; + private int transactionTimeout = 1200; + private Action action = Action.PROPAGATE; + + public int getTransactionTimeout() { + return transactionTimeout; + } + + public void setTransactionTimeout(int transactionTimeout) { + this.transactionTimeout = transactionTimeout; + } + + public Action getAction() { + return action; + } + + public void setAction(Action policy) { + this.action = policy; + } + + public QName getSchemaName() { + return NAME; + } + + public boolean isUnresolved() { + return unresolved; + } + + public void setUnresolved(boolean unresolved) { + this.unresolved = unresolved; + } +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyProcessor.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyProcessor.java new file mode 100644 index 0000000000..0579890a42 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyProcessor.java @@ -0,0 +1,96 @@ +/* + * 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.transaction; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +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.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionPolicyProcessor implements StAXArtifactProcessor<TransactionPolicy> { + public static final String TIMEOUT = "transactionTimeout"; + public static final String ACTION = "action"; + + public QName getArtifactType() { + return TransactionPolicy.NAME; + } + + public TransactionPolicyProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + } + + public TransactionPolicy read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + TransactionPolicy txPolicy = new TransactionPolicyImpl(); + int event = reader.getEventType(); + while (reader.hasNext()) { + event = reader.getEventType(); + switch (event) { + case START_ELEMENT: { + String timeout = reader.getAttributeValue(null, TIMEOUT); + if (timeout != null) { + txPolicy.setTransactionTimeout(Integer.parseInt(timeout)); + } + String action = reader.getAttributeValue(null, ACTION); + if (action != null) { + txPolicy.setAction(TransactionPolicy.Action.valueOf(action)); + } + break; + } + } + + if (event == END_ELEMENT) { + if (TransactionPolicy.NAME.equals(reader.getName())) { + break; + } + } + + //Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + + return txPolicy; + } + + public void write(TransactionPolicy policy, XMLStreamWriter writer) throws ContributionWriteException, + XMLStreamException { + } + + public Class<TransactionPolicy> getModelType() { + return TransactionPolicy.class; + } + + public void resolve(TransactionPolicy policy, ModelResolver resolver) throws ContributionResolveException { + + } +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyProviderFactory.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyProviderFactory.java new file mode 100644 index 0000000000..be97090378 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionPolicyProviderFactory.java @@ -0,0 +1,92 @@ +/* + * 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.transaction; + +import javax.transaction.TransactionManager; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.policy.PolicySetAttachPoint; +import org.apache.tuscany.sca.provider.PolicyProvider; +import org.apache.tuscany.sca.provider.PolicyProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentReference; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionPolicyProviderFactory implements PolicyProviderFactory<TransactionPolicy> { + private TransactionManagerHelper helper; + + public TransactionPolicyProviderFactory(ExtensionPointRegistry registry) { + super(); + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + TransactionManager tm = utilities.getUtility(TransactionManager.class); + this.helper = new TransactionManagerHelper(tm); + } + + /** + * @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, Implementation implementation) { + if (component instanceof PolicySetAttachPoint) { + return new TransactionImplementationPolicyProvider(helper, (PolicySetAttachPoint)component); + } else { + return null; + } + } + + /** + * @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(RuntimeComponent component, + RuntimeComponentReference reference, + Binding binding) { + if (binding instanceof PolicySetAttachPoint) { + return new TransactionReferencePolicyProvider(helper, (PolicySetAttachPoint)binding); + } else { + return null; + } + } + + /** + * @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(RuntimeComponent component, + RuntimeComponentService service, + Binding binding) { + if (binding instanceof PolicySetAttachPoint) { + return new TransactionServicePolicyProvider(helper, (PolicySetAttachPoint)binding); + } else { + return null; + } + } + + /** + * @see org.apache.tuscany.sca.provider.ProviderFactory#getModelType() + */ + public Class<TransactionPolicy> getModelType() { + return TransactionPolicy.class; + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionReferencePolicyProvider.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionReferencePolicyProvider.java new file mode 100644 index 0000000000..0795a4e5cf --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionReferencePolicyProvider.java @@ -0,0 +1,61 @@ +/* + * 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.transaction; + +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySetAttachPoint; +import org.apache.tuscany.sca.provider.PolicyProvider; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionReferencePolicyProvider implements PolicyProvider { + private TransactionManagerHelper helper; + private List<PolicySet> policySets; + + public TransactionReferencePolicyProvider(TransactionManagerHelper helper, PolicySetAttachPoint attachPoint) { + super(); + this.helper = helper; + this.policySets = attachPoint.getApplicablePolicySets(); + } + + public Interceptor createInterceptor(Operation operation) { + for (PolicySet policySet : policySets) { + for (Object p : policySet.getPolicies()) { + if (p instanceof TransactionPolicy) { + TransactionInterceptor interceptor = + new TransactionInterceptor(helper, true, (TransactionPolicy)p, null); + return interceptor; + } + } + } + return null; + } + + public String getPhase() { + return Phase.REFERENCE_POLICY; + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionServicePolicyProvider.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionServicePolicyProvider.java new file mode 100644 index 0000000000..9f9088a380 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionServicePolicyProvider.java @@ -0,0 +1,61 @@ +/* + * 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.transaction; + +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySetAttachPoint; +import org.apache.tuscany.sca.provider.PolicyProvider; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionServicePolicyProvider implements PolicyProvider { + private TransactionManagerHelper helper; + private List<PolicySet> policySets; + + public TransactionServicePolicyProvider(TransactionManagerHelper helper, PolicySetAttachPoint attachPoint) { + super(); + this.helper = helper; + this.policySets = attachPoint.getApplicablePolicySets(); + } + + public Interceptor createInterceptor(Operation operation) { + for (PolicySet policySet : policySets) { + for (Object p : policySet.getPolicies()) { + if (p instanceof TransactionPolicy) { + TransactionInterceptor interceptor = + new TransactionInterceptor(helper, true, (TransactionPolicy)p, null); + return interceptor; + } + } + } + return null; + } + + public String getPhase() { + return Phase.SERVICE_POLICY; + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionalAction.java b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionalAction.java new file mode 100644 index 0000000000..5a7ae5c4cc --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/java/org/apache/tuscany/sca/policy/transaction/TransactionalAction.java @@ -0,0 +1,31 @@ +/* + * 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.transaction; + +/** + * @version $Rev$ $Date$ + */ +public interface TransactionalAction<T> { + /** + * @return + * @throws Exception + */ + T run() throws Exception; +} diff --git a/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor new file mode 100644 index 0000000000..1d034e23a2 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -0,0 +1,19 @@ +# 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.transaction.TransactionPolicyProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.0#transactionPolicy,model=org.apache.tuscany.sca.policy.transaction.TransactionPolicy
\ No newline at end of file diff --git a/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator new file mode 100644 index 0000000000..9a35d62cad --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator @@ -0,0 +1,18 @@ +# 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 ModuleActivator
+org.apache.tuscany.sca.policy.transaction.TransactionModuleActivator
diff --git a/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.PolicyProviderFactory b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.PolicyProviderFactory new file mode 100644 index 0000000000..eeec814f60 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.PolicyProviderFactory @@ -0,0 +1,19 @@ +# 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.transaction.TransactionPolicyProviderFactory;model=org.apache.tuscany.sca.policy.transaction.TransactionPolicy
diff --git a/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.SCADefinitionsProvider b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.SCADefinitionsProvider new file mode 100644 index 0000000000..e9d9fab519 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.SCADefinitionsProvider @@ -0,0 +1,19 @@ +# 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 SCA Definitions Providers +org.apache.tuscany.sca.policy.transaction.TransactionPolicyDefinitionsProvider
\ No newline at end of file diff --git a/sandbox/event/modules/policy-transaction/src/main/resources/log4j.properties b/sandbox/event/modules/policy-transaction/src/main/resources/log4j.properties new file mode 100644 index 0000000000..698ee41d7e --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/resources/log4j.properties @@ -0,0 +1,29 @@ +# 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. +# +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=INFO, CONSOLE + +# CONSOLE is set to be a ConsoleAppender. +log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout + + +# Print only messages of level WARN or above in the package com.foo. +log4j.logger.org.apache.tuscany=INFO
\ No newline at end of file diff --git a/sandbox/event/modules/policy-transaction/src/main/resources/org/apache/tuscany/sca/policy/transaction/definitions.xml b/sandbox/event/modules/policy-transaction/src/main/resources/org/apache/tuscany/sca/policy/transaction/definitions.xml new file mode 100644 index 0000000000..ff51b131b7 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/resources/org/apache/tuscany/sca/policy/transaction/definitions.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="ASCII"?> +<!-- + * 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. +--> +<definitions xmlns="http://www.osoa.org/xmlns/sca/1.0" targetNamespace="http://www.osoa.org/xmlns/sca/1.0" + xmlns:sca="http://www.osoa.org/xmlns/sca/1.0" xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"> + + <intent name="managedTransaction" constrains="implementation"> + <description>Used to indicate the transaction environment desired by a component implementation.</description> + </intent> + <intent name="managedTransaction.global"> + <description> + Used to indicate that a component implementation requires a managed global transaction. + </description> + </intent> + <intent name="managedTransaction.local"> + <description> + Used to indicate that a component implementation requires a managed local transaction. + </description> + </intent> + <intent name="noManagedTransaction" constrains="implementation"> + <description> + Used to indicate that a component implementation will manage its own transaction resources. + </description> + </intent> + <intent name="propagatesTransaction" constrains="binding"> + <description> + Used to indicate that a reference will propagate any client transaction or that a service will be dispatched + under any received transaction. + </description> + </intent> + <intent name="suspendsTransaction" constrains="binding"> + <description> + Used to indicate that a reference will not propagate any client transaction or that a service will not be + dispatched under any received transaction. + </description> + </intent> + <intent name="transactedOneWay" constrains="binding"> + <description> + Used to indicate that the component requires the SCA runtime to transact OneWay send of messages as part of + any client global transaction or to transact oneWay message receipt as part of any service global + transaction. + </description> + </intent> + <intent name="immediateOneWay" constrains="binding"> + <description> + Used to indicate that the component requires the SCA runtime to process the sending or receiving of OneWay + messages immediately, regardless of any transaction under which the sending/receiving component runs. + </description> + </intent> +</definitions>
\ No newline at end of file diff --git a/sandbox/event/modules/policy-transaction/src/main/resources/org/apache/tuscany/sca/policy/transaction/tuscany_definitions.xml b/sandbox/event/modules/policy-transaction/src/main/resources/org/apache/tuscany/sca/policy/transaction/tuscany_definitions.xml new file mode 100644 index 0000000000..324fa03da6 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/main/resources/org/apache/tuscany/sca/policy/transaction/tuscany_definitions.xml @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="ASCII"?> +<!-- + * 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. +--> +<definitions xmlns="http://www.osoa.org/xmlns/sca/1.0" targetNamespace="http://tuscany.apache.org/xmlns/sca/1.0" + xmlns:sca="http://www.osoa.org/xmlns/sca/1.0" xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0"> + + <policySet name="ManagedTransactionPolicySet" provides="managedTransaction" appliesTo="implementation"> + <intentMap provides="managedTransaction" default="global"> + <qualifier name="global"> + <tuscany:transactionPolicy transactionTimeout="1200" action="REQUIRE_GLOBAL" /> + </qualifier> + <qualifier name="local"> + <tuscany:transactionPolicy transactionTimeout="1200" action="REQUIRE_LOCAL" /> + </qualifier> + </intentMap> + </policySet> + + <policySet name="NoManagedTransactionPolicySet" provides="noManagedTransaction" + appliesTo="implementation"> + <tuscany:transactionPolicy action="REQUIRE_NONE" /> + </policySet> + + <policySet name="PropagatesTransactionPolicySet" provides="propagatesTransaction" appliesTo="binding"> + <tuscany:transactionPolicy action="PROPAGATE" /> + </policySet> + + <policySet name="SuspendsTransactionPolicySet" provides="suspendsTransaction" appliesTo="binding"> + <tuscany:transactionPolicy action="SUSPEND" /> + </policySet> + + <policySet name="TransactedOneWayPolicySet" provides="transactedOneWay" appliesTo="binding"> + <tuscany:transactionPolicy action="PROPAGATE" /> + </policySet> + + <policySet name="ImmediateOneWayPolicySet" provides="immediateOneWay" appliesTo="binding"> + <tuscany:transactionPolicy action="SUSPEND" /> + </policySet> +</definitions>
\ No newline at end of file diff --git a/sandbox/event/modules/policy-transaction/src/test/java/org/apache/tuscany/sca/policy/transaction/PolicyReadTestCase.java b/sandbox/event/modules/policy-transaction/src/test/java/org/apache/tuscany/sca/policy/transaction/PolicyReadTestCase.java new file mode 100644 index 0000000000..e8560d775d --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/test/java/org/apache/tuscany/sca/policy/transaction/PolicyReadTestCase.java @@ -0,0 +1,54 @@ +/* + * 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.transaction; + +import java.io.InputStream; +import java.net.URL; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +/** + * Test the reading of ws config params policy. + * + * @version $Rev$ $Date$ + */ +public class PolicyReadTestCase extends TestCase { + + @Override + public void setUp() throws Exception { + } + + public void testPolicyReading() throws Exception { + + TransactionPolicyProcessor processor = new TransactionPolicyProcessor(null, null); + + URL url = getClass().getResource("/org/apache/tuscany/sca/policy/transaction/tuscany_definitions.xml"); + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + + InputStream urlStream = url.openStream(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream); + + TransactionPolicy policy = processor.read(reader); + assertEquals(1200, policy.getTransactionTimeout()); + } + +} diff --git a/sandbox/event/modules/policy-transaction/src/test/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerHelperTestCase.java b/sandbox/event/modules/policy-transaction/src/test/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerHelperTestCase.java new file mode 100644 index 0000000000..544e0f68c0 --- /dev/null +++ b/sandbox/event/modules/policy-transaction/src/test/java/org/apache/tuscany/sca/policy/transaction/TransactionManagerHelperTestCase.java @@ -0,0 +1,194 @@ +/* + * 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.transaction; + +import java.util.logging.Logger; + +import javax.transaction.Transaction; +import javax.transaction.TransactionManager; +import javax.transaction.xa.XAException; +import javax.transaction.xa.XAResource; +import javax.transaction.xa.Xid; + +import junit.framework.TestCase; + +import org.apache.geronimo.transaction.manager.NamedXAResource; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionManagerHelperTestCase extends TestCase { + private static final Logger logger = Logger.getLogger(TransactionManagerHelper.class.getName()); + + public static class MockXAResource implements NamedXAResource { + + private String rm; + private String id; + private int timeout = 1000; + + public MockXAResource(String rm, String id) { + super(); + this.rm = rm; + this.id = id; + } + + public String getName() { + return rm + ":" + id; + } + + public void commit(Xid xid, boolean onePhase) throws XAException { + logger.info(id + ": commit(" + xid + "," + onePhase + ")"); + } + + public void end(Xid xid, int flags) throws XAException { + logger.info(id + ": end(" + xid + "," + toString(flags) + ")"); + } + + public void forget(Xid xid) throws XAException { + logger.info(id + ": forget(" + xid + ")"); + } + + public int getTransactionTimeout() throws XAException { + return timeout; + } + + public boolean isSameRM(XAResource xares) throws XAException { + if (xares instanceof MockXAResource) { + MockXAResource res = (MockXAResource)xares; + return res.rm.endsWith(rm); + } else { + return false; + } + } + + public int prepare(Xid xid) throws XAException { + logger.info(id + ": prepare(" + xid + ")"); + return XA_OK; + } + + public Xid[] recover(int flag) throws XAException { + return null; + } + + public void rollback(Xid xid) throws XAException { + logger.info(id + ": rollback(" + xid + ")"); + } + + public boolean setTransactionTimeout(int seconds) throws XAException { + this.timeout = seconds; + return true; + } + + public void start(Xid xid, int flags) throws XAException { + logger.info(id + ": start(" + xid + "," + toString(flags) + ")"); + } + + private String toString(int flags) { + StringBuffer sb = new StringBuffer(); + if ((flags & TMENDRSCAN) != 0) { + sb.append("TMENDRSCAN "); + } + if ((flags & TMFAIL) != 0) { + sb.append("TMFAIL "); + } + if ((flags & TMJOIN) != 0) { + sb.append("TMJOIN "); + } + if ((flags & TMONEPHASE) != 0) { + sb.append("TMONEPHASE "); + } + if ((flags & TMRESUME) != 0) { + sb.append("TMRESUME "); + } + if ((flags & TMSTARTRSCAN) != 0) { + sb.append("TMSTARTRSCAN "); + } + if ((flags & TMSUCCESS) != 0) { + sb.append("TMSUCCESS "); + } + if ((flags & TMSUSPEND) != 0) { + sb.append("TMSUSPEND "); + } + if (sb.length() == 0) { + sb.append("TMNOFLAGS"); + } else { + sb.deleteCharAt(sb.length() - 1); + } + return sb.toString(); + } + + } + + public void testHelper() throws Exception { + TransactionManagerWrapper activator = new TransactionManagerWrapper(); + activator.start(); + TransactionManager tm = activator.getTransactionManager(); + // GeronimoUserTransaction tx = new GeronimoUserTransaction(tm); + TransactionManagerHelper helper = new TransactionManagerHelper(tm); + + // No TX yet + assertNull(tm.getTransaction()); + Transaction t1 = helper.managedGlobalTransactionPreInvoke(); + // Should create T1 + assertNotNull(t1); + // The current TX should be T1 + assertSame(t1, tm.getTransaction()); + + XAResource res1 = new MockXAResource("Derby", "001"); + XAResource res2 = new MockXAResource("DB2", "002"); + tm.getTransaction().enlistResource(res1); + tm.getTransaction().enlistResource(res2); + + Transaction suspended = helper.suspendsTransactionPreInvoke(); + suspended.delistResource(res1, XAResource.TMSUSPEND); + suspended.delistResource(res2, XAResource.TMSUSPEND); + + // T1 is suspended + assertSame(t1, suspended); + // No more active TX + assertNull(tm.getTransaction()); + + Transaction t2 = helper.managedGlobalTransactionPreInvoke(); + assertNotNull(t2); + // The current TX should be T2 + assertSame(t2, tm.getTransaction()); + + XAResource res3 = new MockXAResource("Oracle", "003"); + tm.getTransaction().enlistResource(res3); + + tm.getTransaction().delistResource(res3, XAResource.TMSUCCESS); + tm.rollback(); + + // Skip post + // helper.managedGlobalTransactionPostInvoke(t2); + + helper.suspendsTransactionPostInvoke(suspended); + suspended.enlistResource(res1); + suspended.enlistResource(res2); + // T1 is now resumed + assertSame(t1, tm.getTransaction()); + + helper.managedGlobalTransactionPostInvoke(t1, false); + assertNotNull(tm.getTransaction()); + assertEquals(6, t1.getStatus()); + + activator.stop(); + } +} |