public void testProxyForUnaryCardinality() throws Exception { long time = 1234; Date date = new Date(time); ServiceRegistration reg = publishService(date); fb.setAvailability(Availability.MANDATORY); fb.setInterfaces(new Class<?>[] { Date.class }); fb.afterPropertiesSet(); ImportedOsgiServiceProxy refAware = null; try { Object result = fb.getObject(); assertTrue(result instanceof Date); // check it's our object assertEquals(time, ((Date) result).getTime()); assertTrue(result instanceof SpringProxy); assertTrue(result instanceof ImportedOsgiServiceProxy); assertTrue(result instanceof InfrastructureProxy); refAware = (ImportedOsgiServiceProxy) result; assertNotNull(refAware.getServiceReference()); assertEquals("wrong target returned", date, ((InfrastructureProxy) result).getWrappedObject()); } finally { if (reg != null) reg.unregister(); } // test reference after the service went down assertNotNull(refAware.getServiceReference()); assertNull(refAware.getServiceReference().getBundle()); }
public void testServiceReferenceProperties() throws Exception { long time = 1234; Date date = new Date(time); Dictionary dict = new Properties(); dict.put("foo", "bar"); dict.put("george", "michael"); ServiceRegistration reg = publishService(date, dict); fb.setAvailability(Availability.MANDATORY); fb.setFilter("(&(foo=bar)(george=michael))"); fb.setInterfaces(new Class<?>[] { Date.class }); fb.afterPropertiesSet(); try { Object result = fb.getObject(); assertTrue(result instanceof Date); // check it's our object assertEquals(time, ((Date) result).getTime()); ImportedOsgiServiceProxy refAware = (ImportedOsgiServiceProxy) result; assertTrue(doesMapContainsDictionary(dict, OsgiServiceReferenceUtils.getServicePropertiesAsMap(refAware.getServiceReference()))); InfrastructureProxy targetAware = (InfrastructureProxy) result; assertEquals(date, targetAware.getWrappedObject()); } finally { if (reg != null) reg.unregister(); } }
public void testSpringInfrastructureProxyOnImportersWithTheSameRef() throws Exception { Object service = new Object(); ServiceInvoker invokerA = new ServiceStaticInterceptor(createObjectTrackingBundleContext(service), ref); ServiceInvoker invokerB = new ServiceStaticInterceptor(createObjectTrackingBundleContext(service), ref); InfrastructureProxy proxyA = new InfrastructureOsgiProxyAdvice(invokerA); InfrastructureProxy proxyB = new InfrastructureOsgiProxyAdvice(invokerB); // though this is not normal, we want the interceptors to be different to make sure the wrapped object // gets properly delegated assertFalse("invokers should not be equal (they have different bundle contexts)", invokerA.equals(invokerB)); assertFalse("proxies should not be equal", proxyA.equals(proxyB)); assertSame(proxyA.getWrappedObject(), proxyB.getWrappedObject()); }
public void testSpringInfrastructureProxyOnImportersWithDifferentRefs() throws Exception { Object service = new Object(); BundleContext ctx = createObjectTrackingBundleContext(service); ServiceInvoker invokerA = new ServiceStaticInterceptor(ctx, new MockServiceReference()); ServiceInvoker invokerB = new ServiceStaticInterceptor(ctx, new MockServiceReference()); InfrastructureProxy proxyA = new InfrastructureOsgiProxyAdvice(invokerA); InfrastructureProxy proxyB = new InfrastructureOsgiProxyAdvice(invokerB); assertFalse("invokers should not be equal (they have different service references)", invokerA.equals(invokerB)); assertFalse("proxies should not be equal", proxyA.equals(proxyB)); assertFalse("target objects should not be equal", proxyA.getWrappedObject().equals(proxyB.getWrappedObject())); }
public void testNakedTargetPropertyReturnedByTheInfrastructureProxy() throws Exception { Object service = new Object(); ServiceInvoker invoker = new ServiceStaticInterceptor(createObjectTrackingBundleContext(service), ref); InfrastructureProxy proxy = new InfrastructureOsgiProxyAdvice(invoker); assertSame(TestUtils.invokeMethod(invoker, "getTarget", null), proxy.getWrappedObject()); assertSame(service, proxy.getWrappedObject()); }
public void testCreatedProxy() throws Exception { MockServiceReference ref = new MockServiceReference(); Object proxy = proxyCreator.createServiceProxy(ref).proxy; assertTrue(proxy instanceof ImportedOsgiServiceProxy); assertTrue(proxy instanceof InfrastructureProxy); }
/** * Unwrap the given resource handle if necessary; otherwise return * the given handle as-is. * @see org.springframework.core.InfrastructureProxy#getWrappedObject() */ static Object unwrapResourceIfNecessary(Object resource) { Assert.notNull(resource, "Resource must not be null"); Object resourceRef = resource; // unwrap infrastructure proxy if (resourceRef instanceof InfrastructureProxy) { resourceRef = ((InfrastructureProxy) resourceRef).getWrappedObject(); } if (aopAvailable) { // now unwrap scoped proxy resourceRef = ScopedProxyUnwrapper.unwrapIfNecessary(resourceRef); } return resourceRef; }
public void testProxyForUnaryCardinality() throws Exception { long time = 1234; Date date = new Date(time); ServiceRegistration reg = publishService(date); fb.setCardinality(Cardinality.C_1__1); fb.setInterfaces(new Class[] { Date.class }); fb.afterPropertiesSet(); ImportedOsgiServiceProxy refAware = null; try { Object result = fb.getObject(); assertTrue(result instanceof Date); // check it's our object assertEquals(time, ((Date) result).getTime()); assertTrue(result instanceof SpringProxy); assertTrue(result instanceof ImportedOsgiServiceProxy); assertTrue(result instanceof InfrastructureProxy); refAware = (ImportedOsgiServiceProxy) result; assertNotNull(refAware.getServiceReference()); assertEquals("wrong target returned", date, ((InfrastructureProxy) result).getWrappedObject()); } finally { if (reg != null) reg.unregister(); } // test reference after the service went down assertNotNull(refAware.getServiceReference()); assertNull(refAware.getServiceReference().getBundle()); }
public void testServiceReferenceProperties() throws Exception { long time = 1234; Date date = new Date(time); Dictionary dict = new Properties(); dict.put("foo", "bar"); dict.put("george", "michael"); ServiceRegistration reg = publishService(date, dict); fb.setCardinality(Cardinality.C_1__1); fb.setFilter("(&(foo=bar)(george=michael))"); fb.setInterfaces(new Class[] { Date.class }); fb.afterPropertiesSet(); try { Object result = fb.getObject(); assertTrue(result instanceof Date); // check it's our object assertEquals(time, ((Date) result).getTime()); ImportedOsgiServiceProxy refAware = (ImportedOsgiServiceProxy) result; assertTrue(doesMapContainsDictionary(dict, OsgiServiceReferenceUtils.getServicePropertiesAsMap(refAware.getServiceReference()))); InfrastructureProxy targetAware = (InfrastructureProxy) result; assertEquals(date, targetAware.getWrappedObject()); } finally { if (reg != null) reg.unregister(); } }
public void testFactoryBeanForMultipleServicesAsClasses() throws Exception { fb.setAvailability(Availability.OPTIONAL); fb.setInterfaces(new Class<?>[] { Date.class }); fb.afterPropertiesSet(); List registrations = new ArrayList(3); long time = 321; Date dateA = new Date(time); try { Object result = fb.getObject(); assertTrue(result instanceof Collection); Collection col = (Collection) result; assertTrue(col.isEmpty()); Iterator iter = col.iterator(); assertFalse(iter.hasNext()); registrations.add(publishService(dateA)); try { iter.next(); fail("should have thrown exception"); } catch (NoSuchElementException ex) { // expected } assertTrue(iter.hasNext()); Object service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertEquals(dateA, ((InfrastructureProxy) service).getWrappedObject()); assertFalse(iter.hasNext()); time = 111; Date dateB = new Date(time); registrations.add(publishService(dateB)); assertTrue(iter.hasNext()); service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertFalse(dateA.equals(((InfrastructureProxy) service).getWrappedObject())); assertEquals(dateB, ((InfrastructureProxy) service).getWrappedObject()); } finally { cleanRegistrations(registrations); } }
public void testIteratorWhenServiceGoesDown() throws Exception { fb.setAvailability(Availability.OPTIONAL); fb.setInterfaces(new Class<?>[] { Date.class }); fb.afterPropertiesSet(); long time = 123; Date date = new Date(time); Properties props = new Properties(); props.put("Moroccan", "Sunset"); List registrations = new ArrayList(3); try { Collection col = (Collection) fb.getObject(); Iterator iter = col.iterator(); assertFalse(iter.hasNext()); registrations.add(publishService(date, props)); assertTrue(iter.hasNext()); // deregister service ((ServiceRegistration) registrations.remove(0)).unregister(); // has to successed Object obj = iter.next(); assertTrue(obj instanceof ImportedOsgiServiceProxy); assertTrue(obj instanceof Date); assertTrue(obj instanceof InfrastructureProxy); // the properties will contain the ObjectClass also assertEquals(((ImportedOsgiServiceProxy) obj).getServiceReference().getProperty("Moroccan"), "Sunset"); try { // make sure the service is dead ((Date) obj).getTime(); fail("should have thrown exception"); } catch (ServiceUnavailableException ex) { // proxy is dead } } finally { cleanRegistrations(registrations); } }
public void testProxyForMultipleCardinality() throws Exception { fb.setAvailability(Availability.OPTIONAL); fb.setInterfaces(new Class<?>[] { Date.class }); fb.afterPropertiesSet(); List registrations = new ArrayList(3); long time = 321; Date dateA = new Date(time); try { Object result = fb.getObject(); assertTrue(result instanceof Collection); Collection col = (Collection) result; assertTrue(col.isEmpty()); Iterator iter = col.iterator(); assertFalse(iter.hasNext()); registrations.add(publishService(dateA)); assertTrue(iter.hasNext()); Object service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertTrue(service instanceof ImportedOsgiServiceProxy); assertNotNull(((ImportedOsgiServiceProxy) service).getServiceReference()); assertSame(dateA, ((InfrastructureProxy) service).getWrappedObject()); assertFalse(iter.hasNext()); time = 111; Date dateB = new Date(time); registrations.add(publishService(dateB)); assertTrue(iter.hasNext()); service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertTrue(service instanceof ImportedOsgiServiceProxy); assertNotNull(((ImportedOsgiServiceProxy) service).getServiceReference()); assertTrue(service instanceof InfrastructureProxy); assertSame(dateB, ((InfrastructureProxy) service).getWrappedObject()); } finally { for (int i = 0; i < registrations.size(); i++) { ((ServiceRegistration) registrations.get(i)).unregister(); } } }
public void testFactoryBeanForMultipleServicesAsClasses() throws Exception { fb.setCardinality(Cardinality.C_0__N); fb.setInterfaces(new Class[] { Date.class }); fb.afterPropertiesSet(); List registrations = new ArrayList(3); long time = 321; Date dateA = new Date(time); try { Object result = fb.getObject(); assertTrue(result instanceof Collection); Collection col = (Collection) result; assertTrue(col.isEmpty()); Iterator iter = col.iterator(); assertFalse(iter.hasNext()); registrations.add(publishService(dateA)); try { iter.next(); fail("should have thrown exception"); } catch (NoSuchElementException ex) { // expected } assertTrue(iter.hasNext()); Object service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertEquals(dateA, ((InfrastructureProxy) service).getWrappedObject()); assertFalse(iter.hasNext()); time = 111; Date dateB = new Date(time); registrations.add(publishService(dateB)); assertTrue(iter.hasNext()); service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertFalse(dateA.equals(((InfrastructureProxy) service).getWrappedObject())); assertEquals(dateB, ((InfrastructureProxy) service).getWrappedObject()); } finally { cleanRegistrations(registrations); } }
public void testIteratorWhenServiceGoesDown() throws Exception { fb.setCardinality(Cardinality.C_0__N); fb.setInterfaces(new Class[] { Date.class }); fb.afterPropertiesSet(); long time = 123; Date date = new Date(time); Properties props = new Properties(); props.put("Moroccan", "Sunset"); List registrations = new ArrayList(3); try { Collection col = (Collection) fb.getObject(); Iterator iter = col.iterator(); assertFalse(iter.hasNext()); registrations.add(publishService(date, props)); assertTrue(iter.hasNext()); // deregister service ((ServiceRegistration) registrations.remove(0)).unregister(); // has to successed Object obj = iter.next(); assertTrue(obj instanceof ImportedOsgiServiceProxy); assertTrue(obj instanceof Date); assertTrue(obj instanceof InfrastructureProxy); // the properties will contain the ObjectClass also assertEquals(((ImportedOsgiServiceProxy) obj).getServiceReference().getProperty("Moroccan"), "Sunset"); try { // make sure the service is dead ((Date) obj).getTime(); fail("should have thrown exception"); } catch (ServiceUnavailableException ex) { // proxy is dead } } finally { cleanRegistrations(registrations); } }
public void testProxyForMultipleCardinality() throws Exception { fb.setCardinality(Cardinality.C_0__N); fb.setInterfaces(new Class[] { Date.class }); fb.afterPropertiesSet(); List registrations = new ArrayList(3); long time = 321; Date dateA = new Date(time); try { Object result = fb.getObject(); assertTrue(result instanceof Collection); Collection col = (Collection) result; assertTrue(col.isEmpty()); Iterator iter = col.iterator(); assertFalse(iter.hasNext()); registrations.add(publishService(dateA)); assertTrue(iter.hasNext()); Object service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertTrue(service instanceof ImportedOsgiServiceProxy); assertNotNull(((ImportedOsgiServiceProxy) service).getServiceReference()); assertSame(dateA, ((InfrastructureProxy) service).getWrappedObject()); assertFalse(iter.hasNext()); time = 111; Date dateB = new Date(time); registrations.add(publishService(dateB)); assertTrue(iter.hasNext()); service = iter.next(); assertTrue(service instanceof Date); assertEquals(time, ((Date) service).getTime()); assertTrue(service instanceof ImportedOsgiServiceProxy); assertNotNull(((ImportedOsgiServiceProxy) service).getServiceReference()); assertTrue(service instanceof InfrastructureProxy); assertSame(dateB, ((InfrastructureProxy) service).getWrappedObject()); } finally { for (int i = 0; i < registrations.size(); i++) { ((ServiceRegistration) registrations.get(i)).unregister(); } } }
/** * Build the Hibernate {@code SessionFactory} through background bootstrapping, * using the given executor for a parallel initialization phase * (e.g. a {@link org.springframework.core.task.SimpleAsyncTaskExecutor}). * <p>{@code SessionFactory} initialization will then switch into background * bootstrap mode, with a {@code SessionFactory} proxy immediately returned for * injection purposes instead of waiting for Hibernate's bootstrapping to complete. * However, note that the first actual call to a {@code SessionFactory} method will * then block until Hibernate's bootstrapping completed, if not ready by then. * For maximum benefit, make sure to avoid early {@code SessionFactory} calls * in init methods of related beans, even for metadata introspection purposes. * * @see #buildSessionFactory() * @since 4.3 */ public SessionFactory buildSessionFactory(AsyncTaskExecutor bootstrapExecutor) { Assert.notNull(bootstrapExecutor, "AsyncTaskExecutor must not be null"); return (SessionFactory) Proxy.newProxyInstance(this.resourcePatternResolver.getClassLoader(), new Class<?>[]{SessionFactoryImplementor.class, InfrastructureProxy.class}, new BootstrapSessionFactoryInvocationHandler(bootstrapExecutor)); }