/** * Returns a specific bean given its name * * @param name * @return */ public static final Object bean(String name) { return bean(name, context); }
/** * Loads all extensions implementing or extending <code>extensionPoint</code>. * * <p>This method uses the "default" application context to perform the lookup. See {@link * #setApplicationContext(ApplicationContext)}. * * @param extensionPoint The class or interface of the extensions. * @return A collection of the extensions, or an empty collection. */ public static final <T> List<T> extensions(Class<T> extensionPoint) { return extensions(extensionPoint, context); }
/** * Looks up for a named string property in the order defined by {@link #getProperty(String, * ApplicationContext)} using the internally cached spring application context. * * <p>Care should be taken when using this method. It should not be called during startup or * from tests cases as the internal context will not have been set. * * @param propertyName The property name to lookup. * @return The property value, or null if not found */ public static String getProperty(String propertyName) { return getProperty(propertyName, context); }
/** * Loads a single bean by its type. * * <p>This method returns null if there is no such bean. An exception is thrown if multiple * beans of the specified type exist. * * @param type THe type of the bean to lookup. * @throws IllegalArgumentException If there are multiple beans of the specified type in the * context. */ public static final <T> T bean(Class<T> type) throws IllegalArgumentException { checkContext(context); return context != null ? bean(type, context) : null; }
/** * Sets the lock provider bean name ({@link #lockProvider}) based on the provided {@link * LockProvider} bean. If the passed lockProvider is not a bean, sets the bean name to null. * * @param lockProvider The lock provider bean * @throws IOException */ @Override public void setLockProvider(LockProvider lockProvider) throws IOException { // default to null this.lockProvider = null; if (lockProvider != null) { String[] lockProviderNames = GeoWebCacheExtensions.getBeansNamesOrderedByPriority(LockProvider.class); for (String beanName : lockProviderNames) { if (lockProvider.equals(GeoWebCacheExtensions.bean(beanName))) { this.lockProvider = beanName; } } } }
/** * * Reinitialization is tricky, because we can't really just lock all the blobstores, because * this would cause people to queue on something that we may not want to exist post reinit. * * <p>So we'll just set the current blobstore set free, ready for garbage collection, and * generate a new one. */ public void reInit() { List<BlobStoreConfiguration> extensions = GeoWebCacheExtensions.extensions(BlobStoreConfiguration.class); this.configs = new ArrayList<BlobStoreConfiguration>(extensions); this.layers = GeoWebCacheExtensions.bean(TileLayerDispatcher.class); initialize(); } /**
public void testEnvironment() { ApplicationContext appContext = createMock(ApplicationContext.class); GeoWebCacheEnvironment genv = new GeoWebCacheEnvironment(); assertEquals(0, GeoWebCacheExtensions.extensionsCache.size()); expect(appContext.getBeanNamesForType(GeoWebCacheEnvironment.class)) .andReturn(new String[] {"geoWebCacheEnvironment"}); expect(appContext.getBean("geoWebCacheEnvironment")).andReturn(genv); Map<String, GeoWebCacheEnvironment> genvMap = new HashMap<>(); genvMap.put("geoWebCacheEnvironment", genv); expect(appContext.getBeansOfType(GeoWebCacheEnvironment.class)) .andReturn(genvMap) .anyTimes(); replay(appContext); GeoWebCacheExtensions gse = new GeoWebCacheExtensions(); gse.setApplicationContext(appContext); List<GeoWebCacheEnvironment> extensions = GeoWebCacheExtensions.extensions(GeoWebCacheEnvironment.class); assertNotNull(extensions); assertEquals(1, extensions.size()); assertTrue(extensions.contains(genv)); assertTrue(GeoWebCacheEnvironment.ALLOW_ENV_PARAMETRIZATION); }
@Test public void testBeanString() { ApplicationContext appContext = createMock(ApplicationContext.class); GeoWebCacheExtensions gse = new GeoWebCacheExtensions(); gse.setApplicationContext(null); assertNull(GeoWebCacheExtensions.bean("beanName")); gse.setApplicationContext(appContext); expect(appContext.getBean("beanName")).andReturn(null); // call #1 expect(appContext.getBean("beanName")).andReturn(this); // call #2 replay(appContext); assertNull(GeoWebCacheExtensions.bean("beanName")); // call #1 assertSame(this, GeoWebCacheExtensions.bean("beanName")); // call #2 verify(appContext); }
@Before public void setUp() throws Exception { System.setProperty("BUCKET", "MYBUCKET"); System.setProperty("CONNECTIONS", "30"); System.setProperty("ENABLED", "true"); System.setProperty("ALLOW_ENV_PARAMETRIZATION", "true"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appContextTestS3.xml"); GeoWebCacheExtensions gse = new GeoWebCacheExtensions(); gse.setApplicationContext(context); }
checkContext(context); if (context != null) { try { names = getBeansNamesOrderedByPriority(extensionPoint, context);
/** * Return all bean names that correspond to the provided extension type. If the provided * extensions type implements the {@link GeoWebCacheExtensionPriority} interface, they are * returned sorted by priority * * <p>We return the bean names and not the beans themselves because we cache the beans by name * rather than the bean itself to avoid breaking the singleton directive. * * @param extensionType type of beans to return * @return Array of sorted bean names */ public static <T> String[] getBeansNamesOrderedByPriority(Class<T> extensionType) { return getBeansNamesOrderedByPriority(extensionType, context); }
@Test public void testExtensionsWithPriority() { // creating a spring context with some beans that will implements priority interface ApplicationContext appContext = createMock(ApplicationContext.class); BeanWithPriority beanA = new BeanWithPriority(15, "beanA"); BeanWithPriority beanB = new BeanWithPriority(3, "beanB"); BeanWithPriority beanC = new BeanWithPriority(27, "beanC"); Map<String, BeanWithPriority> beans = new HashMap<>(); beans.put("beanA", beanA); beans.put("beanB", beanB); beans.put("beanC", beanC); // defining invocations expectations expect(appContext.getBeansOfType(BeanWithPriority.class)).andReturn(beans); expect(appContext.getBean("beanA")).andReturn(beanA); expect(appContext.getBean("beanB")).andReturn(beanB); expect(appContext.getBean("beanC")).andReturn(beanC); replay(appContext); // registering our mocked spring application context GeoWebCacheExtensions gwcExtensions = new GeoWebCacheExtensions(); gwcExtensions.setApplicationContext(appContext); // the cache should be empty assertThat(GeoWebCacheExtensions.extensionsCache.size(), is(0)); // we should get beans ordered by their priority List<BeanWithPriority> extensions = GeoWebCacheExtensions.extensions(BeanWithPriority.class); assertThat(extensions.size(), is(3)); assertThat(extensions, Matchers.contains(beanB, beanA, beanC)); verify(appContext); }
@Test public void testSetApplicationContext() { ApplicationContext appContext1 = createMock(ApplicationContext.class); ApplicationContext appContext2 = createMock(ApplicationContext.class); GeoWebCacheExtensions gse = new GeoWebCacheExtensions(); gse.setApplicationContext(appContext1); GeoWebCacheExtensions.extensionsCache.put( GeoWebCacheExtensionsTest.class, new String[] {"fake"}); assertSame(appContext1, GeoWebCacheExtensions.context); gse.setApplicationContext(appContext2); assertSame(appContext2, GeoWebCacheExtensions.context); assertEquals(0, GeoWebCacheExtensions.extensionsCache.size()); }
XMLConfiguration getXmlConfiguration() { XMLConfiguration mainConfig = GeoWebCacheExtensions.bean(XMLConfiguration.class); return mainConfig; }
/** @return All registered SecurityFilter extensions */ public Collection<SecurityFilter> getFilters() { return GeoWebCacheExtensions.extensions(SecurityFilter.class, applicationContext); }
/** * If a context is explicitly provided that is not the one set through setApplicationContext(), * the extensions() method shall look into it and bypass the cache */ @Test public void testExtensionsApplicationContext() { ApplicationContext appContext = createMock(ApplicationContext.class); ApplicationContext customAppContext = createMock(ApplicationContext.class); GeoWebCacheExtensions gse = new GeoWebCacheExtensions(); gse.setApplicationContext(appContext); // context beans Map<String, GeoWebCacheExtensionsTest> beans = new HashMap<>(); beans.put("itDoesntMatterForThePurpose", this); // setApplicationContext cleared the static cache assertEquals(0, GeoWebCacheExtensions.extensionsCache.size()); // set the expectation over the app context used as argument expect(customAppContext.getBeansOfType(GeoWebCacheExtensionsTest.class)).andReturn(beans); expect(customAppContext.getBean("itDoesntMatterForThePurpose")).andReturn(this); replay(customAppContext); replay(appContext); List<GeoWebCacheExtensionsTest> extensions = GeoWebCacheExtensions.extensions(GeoWebCacheExtensionsTest.class, customAppContext); assertNotNull(extensions); assertEquals(1, extensions.size()); assertSame(this, extensions.get(0)); // cache should be untouched after this since our own context were used assertEquals(0, GeoWebCacheExtensions.extensionsCache.size()); verify(appContext); verify(customAppContext); }
protected String resolveSystemProperty(String key) { try { String value = GeoWebCacheExtensions.getProperty(key); if (value == null) { value = System.getenv(key); } return value; } catch (Throwable ex) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Could not access system property '" + key + "': " + ex); } return null; } }
protected void setUp() throws Exception { super.setUp(); System.setProperty("TEST_JDBC_DRIVER", "org.postgresql.Driver"); System.setProperty("TEST_JDBC_URL", "jdbc:postgresql:gttest"); System.setProperty("TEST_JDBC_USER", "test"); System.setProperty("TEST_JDBC_PASSWORD", "toast"); System.setProperty("ALLOW_ENV_PARAMETRIZATION", "true"); GeoWebCacheExtensions gse = new GeoWebCacheExtensions(); GeoWebCacheEnvironment genv = new GeoWebCacheEnvironment(); gse.setApplicationContext(appContext); expect(appContext.getBeanNamesForType(GeoWebCacheEnvironment.class)) .andReturn(new String[] {"environment"}); expect(appContext.getBean("environment")).andReturn(genv); Map<String, GeoWebCacheEnvironment> genvMap = new HashMap<>(); genvMap.put("environment", genv); expect(appContext.getBeansOfType(GeoWebCacheEnvironment.class)) .andReturn(genvMap) .anyTimes(); expect(appContext.getBean("environment")).andReturn(genv).anyTimes(); replay(appContext); }
CompositeBlobStore getCompositeBlobStore() { CompositeBlobStore compositeBlobStore = GeoWebCacheExtensions.bean(CompositeBlobStore.class); checkNotNull(compositeBlobStore); return compositeBlobStore; }
/** Protected no-argument constructor to allow run-time instrumentation */ protected WMTSService() { super(SERVICE_WMTS); extensions.addAll(GeoWebCacheExtensions.extensions(WMTSExtension.class)); }