KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > AbstractEIPTransactionalTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.eip;
18
19 import javax.resource.spi.ConnectionManager JavaDoc;
20 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
21 import javax.sql.DataSource JavaDoc;
22 import javax.sql.XADataSource JavaDoc;
23 import javax.transaction.TransactionManager JavaDoc;
24
25 import org.apache.activemq.broker.BrokerService;
26 import org.apache.derby.jdbc.EmbeddedXADataSource;
27 import org.apache.geronimo.connector.outbound.GenericConnectionManager;
28 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
29 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.XATransactions;
30 import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
31 import org.apache.geronimo.transaction.context.TransactionContextManager;
32 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
33 import org.apache.geronimo.transaction.manager.XidFactoryImpl;
34 import org.apache.servicemix.client.DefaultServiceMixClient;
35 import org.apache.servicemix.jbi.container.JBIContainer;
36 import org.apache.servicemix.jbi.nmr.flow.Flow;
37 import org.apache.servicemix.jbi.nmr.flow.jca.JCAFlow;
38 import org.apache.servicemix.jbi.nmr.flow.seda.SedaFlow;
39 import org.apache.servicemix.store.Store;
40 import org.apache.servicemix.store.jdbc.JdbcStoreFactory;
41 import org.apache.servicemix.tck.ExchangeCompletedListener;
42 import org.tranql.connector.AllExceptionsAreFatalSorter;
43 import org.tranql.connector.jdbc.AbstractXADataSourceMCF;
44
45 public abstract class AbstractEIPTransactionalTest extends AbstractEIPTest {
46
47     protected BrokerService broker;
48     protected TransactionManager JavaDoc tm;
49     protected DataSource JavaDoc dataSource;
50     protected Store store;
51     
52     protected void setUp() throws Exception JavaDoc {
53         // Create an AMQ broker
54
broker = new BrokerService();
55         broker.setUseJmx(false);
56         broker.setPersistent(false);
57         broker.addConnector("tcp://localhost:61616");
58         broker.start();
59         
60         TransactionManagerImpl exTransactionManager = new TransactionManagerImpl(600, new XidFactoryImpl(), null, null);
61         TransactionContextManager transactionContextManager = new TransactionContextManager(exTransactionManager, exTransactionManager);
62         tm = (TransactionManager JavaDoc) new GeronimoTransactionManager(transactionContextManager);
63         
64         // Create an embedded database for testing tx results when commit / rollback
65
ConnectionManager JavaDoc cm = new GenericConnectionManager(
66                         new XATransactions(true, true),
67                         new NoPool(),
68                         false,
69                         null,
70                         transactionContextManager,
71                         "connectionManager",
72                         GenericConnectionManager.class.getClassLoader());
73         ManagedConnectionFactory JavaDoc mcf = new DerbyDataSourceMCF("target/testdb");
74         dataSource = (DataSource JavaDoc) mcf.createConnectionFactory(cm);
75
76         JdbcStoreFactory storeFactory = new JdbcStoreFactory();
77         storeFactory.setDataSource(dataSource);
78         storeFactory.setTransactional(true);
79         store = storeFactory.open("store");
80         
81         JCAFlow jcaFlow = new JCAFlow();
82         jcaFlow.setTransactionContextManager(transactionContextManager);
83         
84         jbi = new JBIContainer();
85         jbi.setFlows(new Flow[] { new SedaFlow(), jcaFlow });
86         jbi.setEmbedded(true);
87         jbi.setUseMBeanServer(false);
88         jbi.setCreateMBeanServer(false);
89         jbi.setTransactionManager(tm);
90         jbi.setAutoEnlistInTransaction(true);
91         listener = new ExchangeCompletedListener();
92         jbi.addListener(listener);
93         jbi.init();
94         jbi.start();
95
96         client = new DefaultServiceMixClient(jbi);
97     }
98     
99     protected void tearDown() throws Exception JavaDoc {
100         listener.assertExchangeCompleted();
101         jbi.shutDown();
102         broker.stop();
103     }
104
105     protected void configurePattern(EIPEndpoint endpoint) {
106         endpoint.setStore(store);
107     }
108     
109     public static class DerbyDataSourceMCF extends AbstractXADataSourceMCF {
110         private static final long serialVersionUID = 7971682207810098396L;
111         protected DerbyDataSourceMCF(String JavaDoc dbName) {
112             super(createXADS(dbName), new AllExceptionsAreFatalSorter());
113         }
114         public String JavaDoc getPassword() {
115             return null;
116         }
117         public String JavaDoc getUserName() {
118             return null;
119         }
120         protected static XADataSource JavaDoc createXADS(String JavaDoc dbName) {
121             EmbeddedXADataSource xads = new EmbeddedXADataSource();
122             xads.setDatabaseName(dbName);
123             xads.setCreateDatabase("create");
124             return xads;
125         }
126     }
127     
128 }
129
Popular Tags