@Test public void testSerialization() throws Exception { SerializablePerson sp1 = new SerializablePerson(); sp1.setName("Tony"); SerializablePerson sp2 = new SerializablePerson(); sp1.setName("Gordon"); HotSwappableTargetSource hts = new HotSwappableTargetSource(sp1); ProxyFactory pf = new ProxyFactory(); pf.addInterface(Person.class); pf.setTargetSource(hts); pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor())); Person p = (Person) pf.getProxy(); assertEquals(sp1.getName(), p.getName()); hts.swap(sp2); assertEquals(sp2.getName(), p.getName()); p = (Person) SerializationTestUtils.serializeAndDeserialize(p); // We need to get a reference to the client-side targetsource hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource(); assertEquals(sp2.getName(), p.getName()); hts.swap(sp1); assertEquals(sp1.getName(), p.getName()); } }
/** * * @param invalid * @return the message */ private IllegalArgumentException testRejectsSwapToInvalidValue(Object invalid) { HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper"); IllegalArgumentException aopex = null; try { swapper.swap(invalid); fail("Shouldn't be able to swap to invalid value [" + invalid + "]"); } catch (IllegalArgumentException ex) { // Ok aopex = ex; } // It shouldn't be corrupted, it should still work testBasicFunctionality(); return aopex; }
private void doSwap() { synchronized(hotSwapTargetSource){ DataSource target = (DataSource) getHotSwapTargetSource().getTarget(); if (target == masterDataSource) { getHotSwapTargetSource().swap(standbyDataSource); currentDetectorDataSource = standbyDetectorDataSource; } else { getHotSwapTargetSource().swap(masterDataSource); currentDetectorDataSource = masterDetectorDataSource; } } }
public static Object makeSpringAOPToStringTrigger ( Object o ) throws Exception { return makeToStringTrigger(o, x -> { return new HotSwappableTargetSource(x); }); }
public Object invoke(MethodInvocation invocation) throws Throwable { if (!StringUtils.equalsIgnoreCase(invocation.getMethod().getName(), "getConnection")) { return invocation.proceed(); } try { return invocation.proceed(); // need to check with detecting sql? } catch (Throwable t) { if (t instanceof SQLException) { // we use SQLStateSQLExceptionTranslator to translate SQLExceptions , but it doesn't mean it will work as we expected, // so maybe more scope should be covered. we will check out later with runtime data statistics. DataAccessException dae = sqlExTranslator.translate( "translate to check whether it's a resource failure exception", null, (SQLException) t); if (dae instanceof DataAccessResourceFailureException) { logger.warn("failed to get Connection from data source with exception:\n{}", t); doSwap(); return invocation.getMethod().invoke(targetSource.getTarget(), invocation.getArguments()); } } // other exception conditions should be handled by application, // 'cause we don't have enough context information to decide what to do here. throw t; } }
@Test public void testValidSwaps() { SideEffectBean target1 = (SideEffectBean) beanFactory.getBean("target1"); SideEffectBean target2 = (SideEffectBean) beanFactory.getBean("target2"); SideEffectBean proxied = (SideEffectBean) beanFactory.getBean("swappable"); assertEquals(target1.getCount(), proxied.getCount()); proxied.doWork(); assertEquals(INITIAL_COUNT + 1, proxied.getCount()); HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper"); Object old = swapper.swap(target2); assertEquals("Correct old target was returned", target1, old); // TODO should be able to make this assertion: need to fix target handling // in AdvisedSupport //assertEquals(target2, ((Advised) proxied).getTarget()); assertEquals(20, proxied.getCount()); proxied.doWork(); assertEquals(21, target2.getCount()); // Swap it back swapper.swap(target1); assertEquals(target1.getCount(), proxied.getCount()); }
private void doSwap() { synchronized (targetSource) { DataSource target = (DataSource) getTargetSource().getTarget(); if (target == mainDataSource) { logger.warn("hot swap from '" + target + "' to '" + standbyDataSource + "'."); getTargetSource().swap(standbyDataSource); } else { logger.warn("hot swap from '" + target + "' to '" + mainDataSource + "'."); getTargetSource().swap(mainDataSource); } } }
HotSwappableTargetSource targetSource = new HotSwappableTargetSource(activeDataSource); ProxyFactory pf = new ProxyFactory(); pf.setInterfaces(new Class[] { DataSource.class });
HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3); pc.setTargetSource(hts); assertEquals(tb3.getAge(), proxy.getAge()); assertEquals(nop.getCount(), 3); hts.swap(tb1); assertEquals(tb1.getAge(), proxy.getAge()); tb1.setName("Colin");
/** * Test that we can set the target to a dynamic TargetSource. */ @Test public void testDynamicTargetSource() { // Install facade CallCountingTransactionManager txMan = new CallCountingTransactionManager(); PlatformTransactionManagerFacade.delegate = txMan; TestBean tb = (TestBean) factory.getBean("hotSwapped"); assertEquals(666, tb.getAge()); int newAge = 557; tb.setAge(newAge); assertEquals(newAge, tb.getAge()); TestBean target2 = new TestBean(); target2.setAge(65); HotSwappableTargetSource ts = (HotSwappableTargetSource) factory.getBean("swapper"); ts.swap(target2); assertEquals(target2.getAge(), tb.getAge()); tb.setAge(newAge); assertEquals(newAge, target2.getAge()); assertEquals(0, txMan.inflight); assertEquals(2, txMan.commits); assertEquals(0, txMan.rollbacks); }
this.setTargetSource(new HotSwappableTargetSource(localSakaiComponentBean)); ComponentManager.loadComponent(sakaiComponentName, this); Method getTargetSource = obj.getClass().getMethod("getTargetSource"); HotSwappableTargetSource hsts = (HotSwappableTargetSource) getTargetSource.invoke(obj); hsts.swap(localSakaiComponentBean); } catch (Exception e) { log.error("Unable to update reloadable SakaiComponent: " + sakaiComponentName, e);
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "test.xml", "spring.xml" }) public class Test { @Resource(name="beanSwappable") Bean b; @Resource(name = "beanSwap") HotSwappableTargetSource beanSwap; public void swap() { Bean b = << create mock version >> beanSwap.swap(b); // run test code which } }