1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */17packageorg.apache.jetspeed.testhelpers;
1819import java.util.Map;
20import java.util.Properties;
2122import org.springframework.beans.factory.support.DefaultListableBeanFactory;
23import org.springframework.context.support.GenericApplicationContext;
24import org.springframework.transaction.interceptor.TransactionProxyFactoryBean;
25import org.springframework.orm.ojb.PersistenceBrokerTransactionManager;
26import org.springframework.orm.ojb.support.LocalOjbConfigurer;
2728publicclassOJBHelperextendsDatasourceHelper29 {
3031publicstaticfinal String DATASOURCE_BEAN = "JetspeedDS";
3233private GenericApplicationContext appCtx;
3435private DefaultListableBeanFactory bf;
3637publicOJBHelper(Map context)
38 {
39super(context);
40 }
4142publicvoid setUp() throws Exception
43 {
44super.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 }
5455publicvoid tearDown() throws Exception
56 {
57 bf.destroySingletons();
58super.tearDown();
59 }
6061/***62 * Surrounds the <code>object</code> with <code>TransactionProxyFactoryBean</code> that implements all63 * interfaces specified in <code>interfacesToProxyAs</code>64 * 65 * @param object66 * object to wrap with a TX Proxy67 * @param interfacesToProxyAs68 * interfeaces to proxy as69 * @return Tx Wrapped version of the priginal object70 * @throws Exception71 */72public Object getTxProxiedObject(Object object, String[] interfacesToProxyAs) throws Exception
73 {
74 Class[] ifaces = new Class[interfacesToProxyAs.length];
75for(int i = 0; i < interfacesToProxyAs.length; i++) {
76 ifaces[i] = Class.forName(interfacesToProxyAs[i]);
77 }
7879 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();
87return txfb.getObject();
88 }
8990 }