Repository: incubator-ignite Updated Branches: refs/heads/ignite-45 a97444aa3 -> 34046ec8b
IGNITE-45 - loadSpringBean method Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/28b6b51a Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/28b6b51a Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/28b6b51a Branch: refs/heads/ignite-45 Commit: 28b6b51a6361d5eceed464662e0017c97346da67 Parents: a97444a Author: Valentin Kulichenko <vkuliche...@gridgain.com> Authored: Mon Mar 23 17:31:33 2015 -0700 Committer: Valentin Kulichenko <vkuliche...@gridgain.com> Committed: Mon Mar 23 17:31:33 2015 -0700 ---------------------------------------------------------------------- .../src/main/java/org/apache/ignite/Ignite.java | 60 ---------------- .../main/java/org/apache/ignite/Ignition.java | 38 +++++++++- .../apache/ignite/internal/IgniteKernal.java | 76 -------------------- .../org/apache/ignite/internal/IgnitionEx.java | 36 ++++++++++ .../spring/IgniteSpringProcessor.java | 10 +++ .../ignite/testframework/junits/IgniteMock.java | 30 -------- .../org/apache/ignite/IgniteSpringBean.java | 42 ----------- .../spring/IgniteSpringProcessorImpl.java | 47 +++++++++--- .../ignite/internal/GridFactorySelfTest.java | 24 +++++++ .../internal/IgniteDynamicCacheConfigTest.java | 76 ++++++++++---------- 10 files changed, 184 insertions(+), 255 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/core/src/main/java/org/apache/ignite/Ignite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/Ignite.java b/modules/core/src/main/java/org/apache/ignite/Ignite.java index 2631703..48889a8 100644 --- a/modules/core/src/main/java/org/apache/ignite/Ignite.java +++ b/modules/core/src/main/java/org/apache/ignite/Ignite.java @@ -197,17 +197,6 @@ public interface Ignite extends AutoCloseable { public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg); /** - * Dynamically starts new cache specified within given Spring XML configuration file. - * <p> - * If local node is an affinity node, this method will return the instance of started cache. - * Otherwise, it will create a client cache on local node. - * - * @param springCfgPath Spring XML configuration file path or URL. - * @return Instance of started cache. - */ - public <K, V> IgniteCache<K, V> createCache(String springCfgPath); - - /** * Gets existing cache with the given name or creates new one with the given configuration. * * @param cacheCfg Cache configuration to use. @@ -216,14 +205,6 @@ public interface Ignite extends AutoCloseable { public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg); /** - * Gets existing cache with the given name or creates new one specified within given Spring XML configuration file. - * - * @param springCfgPath Spring XML configuration file path or URL. - * @return Existing or newly created cache. - */ - public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath); - - /** * Dynamically starts new cache with the given cache configuration. * <p> * If local node is an affinity node, this method will return the instance of started cache. @@ -238,18 +219,6 @@ public interface Ignite extends AutoCloseable { NearCacheConfiguration<K, V> nearCfg); /** - * Dynamically starts new cache specified within given Spring XML configuration file. - * <p> - * If local node is an affinity node, this method will return the instance of started cache. - * Otherwise, it will create a near cache with the given configuration on local node. - * - * @param springCfgPath Spring XML configuration file for cache path or URL. - * @param nearSpringCfgPath Spring XML configuration file for near cache path or URL. - * @return Instance of started cache. - */ - public <K, V> IgniteCache<K, V> createCache(String springCfgPath, String nearSpringCfgPath); - - /** * Gets existing cache with the given cache configuration or creates one if it does not exist. * * @param cacheCfg Cache configuration. @@ -260,15 +229,6 @@ public interface Ignite extends AutoCloseable { NearCacheConfiguration<K, V> nearCfg); /** - * Gets existing cache specified within given Spring XML configuration file or creates one if it does not exist. - * - * @param springCfgPath Spring XML configuration file for cache path or URL. - * @param nearSpringCfgPath Spring XML configuration file for near cache path or URL. - * @return {@code IgniteCache} instance. - */ - public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath, String nearSpringCfgPath); - - /** * Starts a near cache on local node if cache was previously started with one of the * {@link #createCache(CacheConfiguration)} or {@link #createCache(CacheConfiguration, NearCacheConfiguration)} * methods. @@ -280,17 +240,6 @@ public interface Ignite extends AutoCloseable { public <K, V> IgniteCache<K, V> createNearCache(@Nullable String cacheName, NearCacheConfiguration<K, V> nearCfg); /** - * Starts a near cache on local node if cache was previously started with one of the - * {@link #createCache(CacheConfiguration)} or {@link #createCache(CacheConfiguration, NearCacheConfiguration)} - * methods. - * - * @param cacheName Cache name. - * @param nearSpringCfgPath Spring XML configuration file for near cache path or URL. - * @return Cache instance. - */ - public <K, V> IgniteCache<K, V> createNearCache(@Nullable String cacheName, String nearSpringCfgPath); - - /** * Gets existing near cache with the given name or creates a new one. * * @param cacheName Cache name. @@ -300,15 +249,6 @@ public interface Ignite extends AutoCloseable { public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, NearCacheConfiguration<K, V> nearCfg); /** - * Gets existing near cache with the given name or creates a new one. - * - * @param cacheName Cache name. - * @param nearSpringCfgPath Spring XML configuration file for near cache path or URL. - * @return {@code IgniteCache} instance. - */ - public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, String nearSpringCfgPath); - - /** * Stops dynamically started cache. * * @param cacheName Cache name to stop. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/core/src/main/java/org/apache/ignite/Ignition.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/Ignition.java b/modules/core/src/main/java/org/apache/ignite/Ignition.java index 3017342..8af5d4c 100644 --- a/modules/core/src/main/java/org/apache/ignite/Ignition.java +++ b/modules/core/src/main/java/org/apache/ignite/Ignition.java @@ -326,7 +326,7 @@ public class Ignition { * read. This exception will be thrown also if grid with given name has already * been started or Spring XML configuration file is invalid. */ - public static Ignite start(@Nullable String springCfgPath) throws IgniteException { + public static Ignite start(String springCfgPath) throws IgniteException { try { return IgnitionEx.start(springCfgPath); } @@ -361,6 +361,42 @@ public class Ignition { } /** + * Loads Spring bean by its name from given Spring XML configuration file. If bean + * with such name doesn't exist, exception is thrown. + * + * @param springXmlPath Spring XML configuration file path (cannot be {@code null}). + * @param beanName Bean name (cannot be {@code null}). + * @return Loaded bean instance. + * @throws IgniteException If bean with provided name was not found or in case any other error. + */ + public static <T> T loadSpringBean(String springXmlPath, String beanName) throws IgniteException { + try { + return IgnitionEx.loadSpringBean(springXmlPath, beanName); + } + catch (IgniteCheckedException e) { + throw U.convertException(e); + } + } + + /** + * Loads Spring bean by its name from given Spring XML configuration file. If bean + * with such name doesn't exist, exception is thrown. + * + * @param springXmlUrl Spring XML configuration file URL (cannot be {@code null}). + * @param beanName Bean name (cannot be {@code null}). + * @return Loaded bean instance. + * @throws IgniteException If bean with provided name was not found or in case any other error. + */ + public static <T> T loadSpringBean(URL springXmlUrl, String beanName) throws IgniteException { + try { + return IgnitionEx.loadSpringBean(springXmlUrl, beanName); + } + catch (IgniteCheckedException e) { + throw U.convertException(e); + } + } + + /** * Gets an instance of default no-name grid. Note that * caller of this method should not assume that it will return the same * instance every time. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java index 676b9f6..3f75727 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java @@ -56,7 +56,6 @@ import org.apache.ignite.internal.processors.security.*; import org.apache.ignite.internal.processors.segmentation.*; import org.apache.ignite.internal.processors.service.*; import org.apache.ignite.internal.processors.session.*; -import org.apache.ignite.internal.processors.spring.*; import org.apache.ignite.internal.processors.task.*; import org.apache.ignite.internal.processors.timeout.*; import org.apache.ignite.internal.util.*; @@ -77,7 +76,6 @@ import javax.management.*; import java.io.*; import java.lang.management.*; import java.lang.reflect.*; -import java.net.*; import java.text.*; import java.util.*; import java.util.concurrent.*; @@ -2282,11 +2280,6 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createCache(String springCfgPath) { - return createCache(getCacheConfiguration(springCfgPath)); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg) { A.notNull(cacheCfg, "cacheCfg"); @@ -2306,11 +2299,6 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath) { - return getOrCreateCache(getCacheConfiguration(springCfgPath)); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createCache( CacheConfiguration<K, V> cacheCfg, NearCacheConfiguration<K, V> nearCfg @@ -2334,11 +2322,6 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createCache(String springCfgPath, String nearSpringCfgPath) { - return createCache(getCacheConfiguration(springCfgPath), getNearCacheConfiguration(nearSpringCfgPath)); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg, NearCacheConfiguration<K, V> nearCfg) { A.notNull(cacheCfg, "cacheCfg"); @@ -2360,11 +2343,6 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath, String nearSpringCfgPath) { - return getOrCreateCache(getCacheConfiguration(springCfgPath), getNearCacheConfiguration(nearSpringCfgPath)); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createNearCache(String cacheName, NearCacheConfiguration<K, V> nearCfg) { A.notNull(nearCfg, "nearCfg"); @@ -2384,11 +2362,6 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createNearCache(@Nullable String cacheName, String nearSpringCfgPath) { - return createNearCache(cacheName, getNearCacheConfiguration(nearSpringCfgPath)); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, NearCacheConfiguration<K, V> nearCfg) { A.notNull(nearCfg, "nearCfg"); @@ -2409,12 +2382,6 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, - String nearSpringCfgPath) { - return getOrCreateNearCache(cacheName, getNearCacheConfiguration(nearSpringCfgPath)); - } - - /** {@inheritDoc} */ @Override public void destroyCache(String cacheName) { guard(); @@ -2854,47 +2821,4 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable { @Override public String toString() { return S.toString(IgniteKernal.class, this); } - - /** - * @param springCfgPath Spring XML configuration file path or URL. - * @return Cache configuration. - */ - private CacheConfiguration getCacheConfiguration(String springCfgPath) { - return getConfiguration(springCfgPath, CacheConfiguration.class); - } - - /** - * @param nearSpringCfgPath Spring XML configuration file path or URL. - * @return Cache configuration. - */ - private NearCacheConfiguration getNearCacheConfiguration(String nearSpringCfgPath) { - return getConfiguration(nearSpringCfgPath, NearCacheConfiguration.class); - } - - /** - * @param springCfgPath Spring XML configuration file path or URL. - * @param cl Required type of configuration. - * @return Cache configuration. - */ - private <T> T getConfiguration(String springCfgPath, Class<T> cl) { - try { - if (springCfgPath == null) - return null; - - URL url = U.resolveSpringUrl(springCfgPath); - - IgniteSpringProcessor spring = SPRING.create(false); - - IgniteBiTuple<Collection<T>, ? extends GridSpringResourceContext> cfgMap = - spring.loadConfigurations(url, cl); - - if (cfgMap.get1().size() != 1) - throw new IgniteException("File " + url.toString() + " should have one cache configuration."); - - return F.first(cfgMap.get1()); - } - catch (IgniteCheckedException e) { - throw U.convertException(e); - } - } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/core/src/main/java/org/apache/ignite/internal/IgnitionEx.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/IgnitionEx.java b/modules/core/src/main/java/org/apache/ignite/internal/IgnitionEx.java index 0f72ac4..39eae15 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/IgnitionEx.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/IgnitionEx.java @@ -880,6 +880,42 @@ public class IgnitionEx { } /** + * Loads spring bean by name. + * + * @param springXmlPath Spring XML file path. + * @param beanName Bean name. + * @return Bean instance. + * @throws IgniteCheckedException In case of error. + */ + public static <T> T loadSpringBean(String springXmlPath, String beanName) throws IgniteCheckedException { + A.notNull(springXmlPath, "springXmlPath"); + A.notNull(beanName, "beanName"); + + URL url = U.resolveSpringUrl(springXmlPath); + + assert url != null; + + return loadSpringBean(url, beanName); + } + + /** + * Loads spring bean by name. + * + * @param springXmlUrl Spring XML file URL. + * @param beanName Bean name. + * @return Bean instance. + * @throws IgniteCheckedException In case of error. + */ + public static <T> T loadSpringBean(URL springXmlUrl, String beanName) throws IgniteCheckedException { + A.notNull(springXmlUrl, "springXmlUrl"); + A.notNull(beanName, "beanName"); + + IgniteSpringProcessor spring = SPRING.create(false); + + return spring.loadBean(springXmlUrl, beanName); + } + + /** * Gets an instance of default no-name grid. Note that * caller of this method should not assume that it will return the same * instance every time. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/core/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessor.java index 86e5f78..e0c4df5 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessor.java @@ -71,6 +71,16 @@ public interface IgniteSpringProcessor { public Map<Class<?>, Object> loadBeans(URL cfgUrl, Class<?>... beanClasses) throws IgniteCheckedException; /** + * Loads bean instance by name. + * + * @param url Spring XML file URL. + * @param beanName Bean name. + * @return Bean instance. + * @throws IgniteCheckedException In case of error. + */ + public <T> T loadBean(URL url, String beanName) throws IgniteCheckedException; + + /** * Gets user version for given class loader by checking * {@code META-INF/ignite.xml} file for {@code userVersion} attribute. If * {@code ignite.xml} file is not found, or user version is not specified there, http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java index d3405c4..9d4d365 100644 --- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java +++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/IgniteMock.java @@ -171,62 +171,32 @@ public class IgniteMock implements Ignite { } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createCache(@Nullable String springCfgPath) { - return null; - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg) { return null; } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath) { - return null; - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg, @Nullable NearCacheConfiguration<K, V> nearCfg) { return null; } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createCache(String springCfgPath, String nearSpringCfgPath) { - return null; - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createNearCache(String cacheName, NearCacheConfiguration<K, V> nearCfg) { return null; } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createNearCache(@Nullable String cacheName, String nearSpringCfgPath) { - return null; - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg, NearCacheConfiguration<K, V> nearCfg) { return null; } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath, String nearSpringCfgPath) { - return null; - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, NearCacheConfiguration<K, V> nearCfg) { return null; } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, String nearSpringCfgPath) { - return null; - } - - /** {@inheritDoc} */ @Override public void destroyCache(String cacheName) { // No-op. } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java b/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java index 883387c..99c7f2a 100644 --- a/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java +++ b/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java @@ -240,13 +240,6 @@ public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBea } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createCache(@Nullable String springCfgPath) { - assert g != null; - - return g.createCache(springCfgPath); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg) { assert g != null; @@ -254,13 +247,6 @@ public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBea } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath) { - assert g != null; - - return g.getOrCreateCache(springCfgPath); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg, NearCacheConfiguration<K, V> nearCfg) { assert g != null; @@ -269,13 +255,6 @@ public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBea } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createCache(String springCfgPath, String nearSpringCfgPath) { - assert g != null; - - return g.createCache(springCfgPath, nearSpringCfgPath); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateCache(CacheConfiguration<K, V> cacheCfg, NearCacheConfiguration<K, V> nearCfg) { assert g != null; @@ -283,13 +262,6 @@ public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBea } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateCache(String springCfgPath, String nearSpringCfgPath) { - assert g != null; - - return g.getOrCreateCache(springCfgPath, nearSpringCfgPath); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> createNearCache(String cacheName, NearCacheConfiguration<K, V> nearCfg) { assert g != null; @@ -297,13 +269,6 @@ public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBea } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> createNearCache(@Nullable String cacheName, String nearSpringCfgPath) { - assert g != null; - - return g.createNearCache(cacheName, nearSpringCfgPath); - } - - /** {@inheritDoc} */ @Override public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, NearCacheConfiguration<K, V> nearCfg) { assert g != null; @@ -311,13 +276,6 @@ public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBea } /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> getOrCreateNearCache(@Nullable String cacheName, String nearSpringCfgPath) { - assert g != null; - - return g.getOrCreateNearCache(cacheName, nearSpringCfgPath); - } - - /** {@inheritDoc} */ @Override public void destroyCache(String cacheName) { assert g != null; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/spring/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessorImpl.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessorImpl.java b/modules/spring/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessorImpl.java index c8676d9..545b596 100644 --- a/modules/spring/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessorImpl.java +++ b/modules/spring/src/main/java/org/apache/ignite/internal/processors/spring/IgniteSpringProcessorImpl.java @@ -105,12 +105,46 @@ public class IgniteSpringProcessorImpl implements IgniteSpringProcessor { @Override public Map<Class<?>, Object> loadBeans(URL cfgUrl, Class<?>... beanClasses) throws IgniteCheckedException { assert beanClasses.length > 0; + ApplicationContext springCtx = initContext(cfgUrl); + + Map<Class<?>, Object> beans = new HashMap<>(); + + for (Class<?> cls : beanClasses) + beans.put(cls, bean(springCtx, cls)); + + return beans; + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public <T> T loadBean(URL url, String beanName) throws IgniteCheckedException { + ApplicationContext springCtx = initContext(url); + + try { + return (T)springCtx.getBean(beanName); + } + catch (NoSuchBeanDefinitionException e) { + throw new IgniteCheckedException("Spring bean with provided name doesn't exist [url=" + url + + ", beanName=" + beanName + ']'); + } + catch (BeansException e) { + throw new IgniteCheckedException("Failed to load Spring bean with provided name [url=" + url + + ", beanName=" + beanName + ']', e); + } + } + + /** + * @param url XML file URL. + * @return Context. + * @throws IgniteCheckedException In case of error. + */ + private ApplicationContext initContext(URL url) throws IgniteCheckedException { GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); - new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(cfgUrl)); + new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url)); springCtx.refresh(); } @@ -118,18 +152,13 @@ public class IgniteSpringProcessorImpl implements IgniteSpringProcessor { if (X.hasCause(e, ClassNotFoundException.class)) throw new IgniteCheckedException("Failed to instantiate Spring XML application context " + "(make sure all classes used in Spring configuration are present at CLASSPATH) " + - "[springUrl=" + cfgUrl + ']', e); + "[springUrl=" + url + ']', e); else throw new IgniteCheckedException("Failed to instantiate Spring XML application context [springUrl=" + - cfgUrl + ", err=" + e.getMessage() + ']', e); + url + ", err=" + e.getMessage() + ']', e); } - Map<Class<?>, Object> beans = new HashMap<>(); - - for (Class<?> cls : beanClasses) - beans.put(cls, bean(springCtx, cls)); - - return beans; + return springCtx; } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/spring/src/test/java/org/apache/ignite/internal/GridFactorySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/internal/GridFactorySelfTest.java b/modules/spring/src/test/java/org/apache/ignite/internal/GridFactorySelfTest.java index 8f4c5dd..402bd86 100644 --- a/modules/spring/src/test/java/org/apache/ignite/internal/GridFactorySelfTest.java +++ b/modules/spring/src/test/java/org/apache/ignite/internal/GridFactorySelfTest.java @@ -575,6 +575,30 @@ public class GridFactorySelfTest extends GridCommonAbstractTest { /** * @throws Exception If failed. */ + public void testLoadBean() throws Exception { + final String path = "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"; + + GridTestUtils.assertThrows( + log, + new Callable<Object>() { + @Override public Object call() throws Exception { + Ignition.loadSpringBean(path, "wrongName"); + + return null; + } + }, + IgniteException.class, + null + ); + + CacheConfiguration cfg = Ignition.loadSpringBean(path, "cache-configuration"); + + assertEquals("TestDynamicCache", cfg.getName()); + } + + /** + * @throws Exception If failed. + */ @Override protected void afterTest() throws Exception { G.stopAll(false); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/28b6b51a/modules/spring/src/test/java/org/apache/ignite/internal/IgniteDynamicCacheConfigTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/internal/IgniteDynamicCacheConfigTest.java b/modules/spring/src/test/java/org/apache/ignite/internal/IgniteDynamicCacheConfigTest.java index b5424df..59da597 100644 --- a/modules/spring/src/test/java/org/apache/ignite/internal/IgniteDynamicCacheConfigTest.java +++ b/modules/spring/src/test/java/org/apache/ignite/internal/IgniteDynamicCacheConfigTest.java @@ -17,7 +17,6 @@ package org.apache.ignite.internal; - import org.apache.ignite.*; import org.apache.ignite.cache.*; import org.apache.ignite.configuration.*; @@ -86,12 +85,13 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testDynamicCacheStartFromConfig() throws Exception { - IgniteCache cache = ignite(0).createCache("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + IgniteCache cache = ignite(0).createCache(load( + "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); assertEquals("TestDynamicCache", cache.getName()); - IgniteCache cache1 = ignite(0).getOrCreateCache( - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + IgniteCache cache1 = ignite(0).getOrCreateCache(load( + "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); assertEquals(cache, cache1); } @@ -105,16 +105,16 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { try { startGrid(nodeCount() + 1); - IgniteCache cache = ignite(0).createCache( - "modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml"); + IgniteCache cache = ignite(0).createCache(load( + "modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml")); assertEquals(CACHE_NAME, cache.getName()); - IgniteCache<Integer, Integer> clientCache1 = ignite(nodeCount() + 1).createNearCache(CACHE_NAME, - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + IgniteCache clientCache1 = ignite(nodeCount() + 1).createNearCache(CACHE_NAME, + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); IgniteCache clientCache2 = ignite(nodeCount() + 1).getOrCreateNearCache(CACHE_NAME, - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); assertEquals(clientCache1, clientCache2); @@ -137,16 +137,16 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { startGrid(clientNode); IgniteCache cache = ignite(clientNode).createCache( - "modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml", - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + load("modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml"), + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); assertEquals(cache.getName(), CACHE_NAME); - IgniteCache<Integer, Integer> clientCache1 = ignite(clientNode).createNearCache(CACHE_NAME, - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + IgniteCache clientCache1 = ignite(clientNode).createNearCache(CACHE_NAME, + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); IgniteCache clientCache2 = ignite(clientNode).getOrCreateNearCache(CACHE_NAME, - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); assertEquals(clientCache1, clientCache2); @@ -163,8 +163,8 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { public void testGetOrCreateNearCache() throws Exception { testAttribute = false; - IgniteCache cache = ignite(0).createCache( - "modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml"); + IgniteCache cache = ignite(0).createCache(load( + "modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml")); try { int clientNode = nodeCount() + 1; @@ -172,16 +172,16 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { startGrid(clientNode); IgniteCache cache1 = ignite(clientNode).getOrCreateCache( - "modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml", - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + load("modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml"), + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); assertEquals(cache.getName(), cache1.getName()); - IgniteCache<Integer, Integer> clientCache1 = ignite(clientNode).createNearCache(CACHE_NAME, - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + IgniteCache clientCache1 = ignite(clientNode).createNearCache(CACHE_NAME, + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); IgniteCache clientCache2 = ignite(clientNode).getOrCreateNearCache(CACHE_NAME, - "modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"); + loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); assertEquals(clientCache1, clientCache2); @@ -197,21 +197,7 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { */ public void testDynamicCacheStartFromNotExistConfig() throws Exception { try { - ignite(0).getOrCreateCache("config/cache.xml"); - - fail(); - } - catch (IgniteException e) { - // No-op. - } - } - - /** - * @throws Exception If failed. - */ - public void testDynamicCacheStartFromInvalidConfig() throws Exception { - try { - ignite(0).getOrCreateCache("modules/spring/src/test/java/org/apache/ignite/internal/invalid-cache.xml"); + ignite(0).getOrCreateCache(load("config/cache.xml")); fail(); } @@ -230,7 +216,7 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { srv = GridEmbeddedHttpServer.startHttpServer().withFileDownloadingHandler(null, GridTestUtils.resolveIgnitePath("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml")); - IgniteCache cache = ignite(0).createCache(srv.getBaseUrl()); + IgniteCache cache = ignite(0).createCache(load(srv.getBaseUrl())); assertEquals("TestDynamicCache", cache.getName()); } @@ -239,4 +225,20 @@ public class IgniteDynamicCacheConfigTest extends GridCommonAbstractTest { srv.stop(1); } } + + /** + * @param path Path. + * @return Configuration. + */ + private CacheConfiguration load(String path) { + return Ignition.loadSpringBean(path, "cache-configuration"); + } + + /** + * @param path Path. + * @return Configuration. + */ + private NearCacheConfiguration loadNear(String path) { + return Ignition.loadSpringBean(path, "nearCache-configuration"); + } }