1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.testhelpers;
18
19 import java.util.Map;
20 import java.util.Properties;
21
22 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
23 import org.springframework.context.support.GenericApplicationContext;
24 import org.springframework.transaction.interceptor.TransactionProxyFactoryBean;
25 import org.springframework.orm.ojb.PersistenceBrokerTransactionManager;
26 import org.springframework.orm.ojb.support.LocalOjbConfigurer;
27
28 public class OJBHelper extends DatasourceHelper
29 {
30
31 public static final String DATASOURCE_BEAN = "JetspeedDS";
32
33 private GenericApplicationContext appCtx;
34
35 private DefaultListableBeanFactory bf;
36
37 public OJBHelper(Map context)
38 {
39 super(context);
40 }
41
42 public void setUp() throws Exception
43 {
44 super.setUp();
45 bf = new DefaultListableBeanFactory();
46 bf.registerSingleton(DATASOURCE_BEAN, datasource);
47 LocalOjbConfigurer ojbConfigurer = new LocalOjbConfigurer();
48 ojbConfigurer.setBeanFactory(bf);
49 addBeanFactory(bf);
50 appCtx = new GenericApplicationContext(bf);
51 bf.preInstantiateSingletons();
52 getContext().put(APP_CONTEXT, appCtx);
53 }
54
55 public void tearDown() throws Exception
56 {
57 bf.destroySingletons();
58 super.tearDown();
59 }
60
61 /***
62 * Surrounds the <code>object</code> with <code>TransactionProxyFactoryBean</code> that implements all
63 * interfaces specified in <code>interfacesToProxyAs</code>
64 *
65 * @param object
66 * object to wrap with a TX Proxy
67 * @param interfacesToProxyAs
68 * interfeaces to proxy as
69 * @return Tx Wrapped version of the priginal object
70 * @throws Exception
71 */
72 public Object getTxProxiedObject(Object object, String[] interfacesToProxyAs) throws Exception
73 {
74 Class[] ifaces = new Class[interfacesToProxyAs.length];
75 for(int i = 0; i < interfacesToProxyAs.length; i++) {
76 ifaces[i] = Class.forName(interfacesToProxyAs[i]);
77 }
78
79 TransactionProxyFactoryBean txfb = new TransactionProxyFactoryBean();
80 txfb.setTransactionManager(new PersistenceBrokerTransactionManager());
81 Properties txProps = new Properties();
82 txProps.setProperty("*", "PROPAGATION_REQUIRED");
83 txfb.setTransactionAttributes(txProps);
84 txfb.setTarget(object);
85 txfb.setProxyInterfaces(ifaces);
86 txfb.afterPropertiesSet();
87 return txfb.getObject();
88 }
89
90 }