View Javadoc

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.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  }