01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.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: public static final String DATASOURCE_BEAN = "JetspeedDS";
31:
32: private GenericApplicationContext appCtx;
33:
34: private DefaultListableBeanFactory bf;
35:
36: public OJBHelper(Map context) {
37: super (context);
38: }
39:
40: public void setUp() throws Exception {
41: super .setUp();
42: bf = new DefaultListableBeanFactory();
43: bf.registerSingleton(DATASOURCE_BEAN, datasource);
44: LocalOjbConfigurer ojbConfigurer = new LocalOjbConfigurer();
45: ojbConfigurer.setBeanFactory(bf);
46: addBeanFactory(bf);
47: appCtx = new GenericApplicationContext(bf);
48: bf.preInstantiateSingletons();
49: getContext().put(APP_CONTEXT, appCtx);
50: }
51:
52: public void tearDown() throws Exception {
53: bf.destroySingletons();
54: super .tearDown();
55: }
56:
57: /**
58: * Surrounds the <code>object</code> with <code>TransactionProxyFactoryBean</code> that implements all
59: * interfaces specified in <code>interfacesToProxyAs</code>
60: *
61: * @param object
62: * object to wrap with a TX Proxy
63: * @param interfacesToProxyAs
64: * interfeaces to proxy as
65: * @return Tx Wrapped version of the priginal object
66: * @throws Exception
67: */
68: public Object getTxProxiedObject(Object object,
69: String[] interfacesToProxyAs) throws Exception {
70: Class[] ifaces = new Class[interfacesToProxyAs.length];
71: for (int i = 0; i < interfacesToProxyAs.length; i++) {
72: ifaces[i] = Class.forName(interfacesToProxyAs[i]);
73: }
74:
75: TransactionProxyFactoryBean txfb = new TransactionProxyFactoryBean();
76: txfb
77: .setTransactionManager(new PersistenceBrokerTransactionManager());
78: Properties txProps = new Properties();
79: txProps.setProperty("*", "PROPAGATION_REQUIRED");
80: txfb.setTransactionAttributes(txProps);
81: txfb.setTarget(object);
82: txfb.setProxyInterfaces(ifaces);
83: txfb.afterPropertiesSet();
84: return txfb.getObject();
85: }
86:
87: }
|