ignite-779

Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/5718e8a5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/5718e8a5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/5718e8a5

Branch: refs/heads/ignite-80
Commit: 5718e8a5eceec1f4a20767882ab6754511544204
Parents: b4b28fd
Author: avinogradov <avinogra...@gridgain.com>
Authored: Fri Apr 24 17:14:40 2015 +0300
Committer: avinogradov <avinogra...@gridgain.com>
Committed: Fri Apr 24 17:14:40 2015 +0300

----------------------------------------------------------------------
 .../main/java/org/apache/ignite/Ignition.java   |  44 ++++
 .../org/apache/ignite/internal/IgnitionEx.java  | 154 ++++++++++++++
 .../util/spring/IgniteSpringHelper.java         |  51 +++++
 .../util/spring/IgniteSpringHelperImpl.java     | 200 ++++++++++++++++---
 .../spring/IgniteStartFromStreamCfgTest.java    |  50 +++++
 .../testsuites/IgniteSpringTestSuite.java       |   2 +
 6 files changed, 469 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5718e8a5/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 8af5d4c..47f3665 100644
--- a/modules/core/src/main/java/org/apache/ignite/Ignition.java
+++ b/modules/core/src/main/java/org/apache/ignite/Ignition.java
@@ -22,6 +22,7 @@ import org.apache.ignite.internal.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
 import org.jetbrains.annotations.*;
 
+import java.io.*;
 import java.net.*;
 import java.util.*;
 
@@ -361,6 +362,31 @@ public class Ignition {
     }
 
     /**
+     * Starts all grids specified within given Spring XML configuration input 
stream. If grid with given name
+     * is already started, then exception is thrown. In this case all 
instances that may
+     * have been started so far will be stopped too.
+     * <p>
+     * Usually Spring XML configuration input stream will contain only one 
Grid definition. Note that
+     * Grid configuration bean(s) is retrieved form configuration input stream 
by type, so the name of
+     * the Grid configuration bean is ignored.
+     *
+     * @param springCfgStream Input stream containing Spring XML 
configuration. This cannot be {@code null}.
+     * @return Started grid. If Spring configuration contains multiple grid 
instances,
+     *      then the 1st found instance is returned.
+     * @throws IgniteException If grid could not be started or configuration
+     *      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(InputStream springCfgStream) throws 
IgniteException {
+        try {
+            return IgnitionEx.start(springCfgStream);
+        }
+        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.
      *
@@ -397,6 +423,24 @@ 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 springXmlStream Input stream containing Spring XML configuration 
(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(InputStream springXmlStream, String 
beanName) throws IgniteException {
+        try {
+            return IgnitionEx.loadSpringBean(springXmlStream, 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/5718e8a5/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 cde8316..469ba7b 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
@@ -568,6 +568,26 @@ public class IgnitionEx {
     }
 
     /**
+     * Loads all grid configurations specified within given input stream.
+     * <p>
+     * Usually Spring XML input stream will contain only one Grid definition. 
Note that
+     * Grid configuration bean(s) is retrieved form configuration input stream 
by type, so the name of
+     * the Grid configuration bean is ignored.
+     *
+     * @param springCfgStream Input stream contained Spring XML configuration. 
This cannot be {@code null}.
+     * @return Tuple containing all loaded configurations and Spring context 
used to load them.
+     * @throws IgniteCheckedException If grid could not be started or 
configuration
+     *      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 IgniteBiTuple<Collection<IgniteConfiguration>, ? extends 
GridSpringResourceContext> loadConfigurations(
+        InputStream springCfgStream) throws IgniteCheckedException {
+        IgniteSpringHelper spring = SPRING.create(false);
+
+        return spring.loadConfigurations(springCfgStream);
+    }
+
+    /**
      * Loads all grid configurations specified within given Spring XML 
configuration file.
      * <p>
      * Usually Spring XML configuration file will contain only one Grid 
definition. Note that
@@ -717,6 +737,52 @@ public class IgnitionEx {
     }
 
     /**
+     * Starts all grids specified within given Spring XML configuration input 
stream. If grid with given name
+     * is already started, then exception is thrown. In this case all 
instances that may
+     * have been started so far will be stopped too.
+     * <p>
+     * Usually Spring XML configuration input stream will contain only one 
Grid definition. Note that
+     * Grid configuration bean(s) is retrieved form configuration input stream 
by type, so the name of
+     * the Grid configuration bean is ignored.
+     *
+     * @param springCfgStream Input stream containing Spring XML 
configuration. This cannot be {@code null}.
+     * @return Started grid. If Spring configuration contains multiple grid 
instances,
+     *      then the 1st found instance is returned.
+     * @throws IgniteCheckedException If grid could not be started or 
configuration
+     *      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(InputStream springCfgStream) throws 
IgniteCheckedException {
+        return start(springCfgStream, null, null);
+    }
+
+    /**
+     * Starts all grids specified within given Spring XML configuration input 
stream. If grid with given name
+     * is already started, then exception is thrown. In this case all 
instances that may
+     * have been started so far will be stopped too.
+     * <p>
+     * Usually Spring XML configuration input stream will contain only one 
Grid definition. Note that
+     * Grid configuration bean(s) is retrieved form configuration input stream 
by type, so the name of
+     * the Grid configuration bean is ignored.
+     *
+     * @param springCfgStream Input stream containing Spring XML 
configuration. This cannot be {@code null}.
+     * @param gridName Grid name that will override default.
+     * @param springCtx Optional Spring application context, possibly {@code 
null}.
+     *      Spring bean definitions for bean injection are taken from this 
context.
+     *      If provided, this context can be injected into grid tasks and grid 
jobs using
+     *      {@link SpringApplicationContextResource 
@IgniteSpringApplicationContextResource} annotation.
+     * @return Started grid. If Spring configuration contains multiple grid 
instances,
+     *      then the 1st found instance is returned.
+     * @throws IgniteCheckedException If grid could not be started or 
configuration
+     *      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(InputStream springCfgStream, @Nullable String 
gridName,
+        @Nullable GridSpringResourceContext springCtx) throws 
IgniteCheckedException {
+        return start(springCfgStream, gridName, springCtx, null);
+    }
+
+    /**
      * Internal Spring-based start routine.
      *
      * @param springCfgUrl Spring XML configuration file URL. This cannot be 
{@code null}.
@@ -763,6 +829,77 @@ public class IgnitionEx {
                 U.removeJavaNoOpLogger(savedHnds);
         }
 
+        return startConfigurations(cfgMap, springCfgUrl, gridName, springCtx, 
cfgClo);
+    }
+
+    /**
+     * Internal Spring-based start routine.
+     *
+     * @param springCfgStream Input stream containing Spring XML 
configuration. This cannot be {@code null}.
+     * @param gridName Grid name that will override default.
+     * @param springCtx Optional Spring application context.
+     * @param cfgClo Optional closure to change configuration before it is 
used to start the grid.
+     * @return Started grid.
+     * @throws IgniteCheckedException If failed.
+     */
+    private static Ignite start(final InputStream springCfgStream, @Nullable 
String gridName,
+        @Nullable GridSpringResourceContext springCtx,
+        @Nullable IgniteClosure<IgniteConfiguration, IgniteConfiguration> 
cfgClo)
+        throws IgniteCheckedException {
+        A.notNull(springCfgStream, "springCfgUrl");
+
+        boolean isLog4jUsed = 
U.gridClassLoader().getResource("org/apache/log4j/Appender.class") != null;
+
+        IgniteBiTuple<Object, Object> t = null;
+
+        if (isLog4jUsed) {
+            try {
+                t = U.addLog4jNoOpLogger();
+            }
+            catch (IgniteCheckedException ignore) {
+                isLog4jUsed = false;
+            }
+        }
+
+        Collection<Handler> savedHnds = null;
+
+        if (!isLog4jUsed)
+            savedHnds = U.addJavaNoOpLogger();
+
+        IgniteBiTuple<Collection<IgniteConfiguration>, ? extends 
GridSpringResourceContext> cfgMap;
+
+        try {
+            cfgMap = loadConfigurations(springCfgStream);
+        }
+        finally {
+            if (isLog4jUsed && t != null)
+                U.removeLog4jNoOpLogger(t);
+
+            if (!isLog4jUsed)
+                U.removeJavaNoOpLogger(savedHnds);
+        }
+
+        return startConfigurations(cfgMap, null, gridName, springCtx, cfgClo);
+    }
+
+    /**
+     * Internal Spring-based start routine. Starts loaded configurations.
+     *
+     * @param cfgMap Configuration map.
+     * @param springCfgUrl Spring XML configuration file URL.
+     * @param gridName Grid name that will override default.
+     * @param springCtx Optional Spring application context.
+     * @param cfgClo Optional closure to change configuration before it is 
used to start the grid.
+     * @return Started grid.
+     * @throws IgniteCheckedException If failed.
+     */
+    private static Ignite startConfigurations(
+        IgniteBiTuple<Collection<IgniteConfiguration>, ? extends 
GridSpringResourceContext> cfgMap,
+        URL springCfgUrl,
+        @Nullable String gridName,
+        @Nullable GridSpringResourceContext springCtx,
+        @Nullable IgniteClosure<IgniteConfiguration, IgniteConfiguration> 
cfgClo)
+        throws IgniteCheckedException {
         List<IgniteNamedInstance> grids = new ArrayList<>(cfgMap.size());
 
         try {
@@ -916,6 +1053,23 @@ public class IgnitionEx {
     }
 
     /**
+     * Loads spring bean by name.
+     *
+     * @param springXmlStream Input stream containing Spring XML configuration.
+     * @param beanName Bean name.
+     * @return Bean instance.
+     * @throws IgniteCheckedException In case of error.
+     */
+    public static <T> T loadSpringBean(InputStream springXmlStream, String 
beanName) throws IgniteCheckedException {
+        A.notNull(springXmlStream, "springXmlPath");
+        A.notNull(beanName, "beanName");
+
+        IgniteSpringHelper spring = SPRING.create(false);
+
+        return spring.loadBean(springXmlStream, 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/5718e8a5/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
index 6bdfbac..4b377f0 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelper.java
@@ -22,6 +22,7 @@ import org.apache.ignite.configuration.*;
 import org.apache.ignite.internal.processors.resource.*;
 import org.apache.ignite.lang.*;
 
+import java.io.*;
 import java.net.*;
 import java.util.*;
 
@@ -48,6 +49,23 @@ public interface IgniteSpringHelper {
         URL cfgUrl, String... excludedProps) throws IgniteCheckedException;
 
     /**
+     * Loads all grid configurations specified within given configuration 
input stream.
+     * <p>
+     * Usually Spring XML configuration input stream will contain only one 
Grid definition. Note that
+     * Grid configuration bean(s) is retrieved form configuration input stream 
by type, so the name of
+     * the Grid configuration bean is ignored.
+     *
+     * @param cfgStream Configuration input stream. This cannot be {@code 
null}.
+     * @param excludedProps Properties to exclude.
+     * @return Tuple containing all loaded configurations and Spring context 
used to load them.
+     * @throws IgniteCheckedException If grid could not be started or 
configuration
+     *      read. This exception will be thrown also if grid with given name 
has already
+     *      been started or Spring XML configuration file is invalid.
+     */
+    public IgniteBiTuple<Collection<IgniteConfiguration>, ? extends 
GridSpringResourceContext> loadConfigurations(
+        InputStream cfgStream, String... excludedProps) throws 
IgniteCheckedException;
+
+    /**
      * Loads all configurations with given type specified within given 
configuration file.
      *
      * @param cfgUrl Configuration file path or URL. This cannot be {@code 
null}.
@@ -60,6 +78,18 @@ public interface IgniteSpringHelper {
         URL cfgUrl, Class<T> cl, String... excludedProps) throws 
IgniteCheckedException;
 
     /**
+     * Loads all configurations with given type specified within given 
configuration input stream.
+     *
+     * @param cfgStream Configuration input stream. This cannot be {@code 
null}.
+     * @param cl Required type of configuration.
+     * @param excludedProps Properties to exclude.
+     * @return Tuple containing all loaded configurations and Spring context 
used to load them.
+     * @throws IgniteCheckedException If configuration could not be read.
+     */
+    public <T> IgniteBiTuple<Collection<T>, ? extends 
GridSpringResourceContext> loadConfigurations(
+        InputStream cfgStream, Class<T> cl, String... excludedProps) throws 
IgniteCheckedException;
+
+    /**
      * Loads bean instances that match the given types from given 
configuration file.
      *
      * @param cfgUrl Configuration file path or URL. This cannot be {@code 
null}.
@@ -81,6 +111,27 @@ public interface IgniteSpringHelper {
     public <T> T loadBean(URL url, String beanName) throws 
IgniteCheckedException;
 
     /**
+     * Loads bean instances that match the given types from given 
configuration input stream.
+     *
+     * @param cfgStream Input stream containing Spring XML configuration. This 
cannot be {@code null}.
+     * @param beanClasses Beans classes.
+     * @return Bean class -> loaded bean instance map, if configuration does 
not contain bean with required type the
+     *       map value is {@code null}.
+     * @throws IgniteCheckedException If failed to load configuration.
+     */
+    public Map<Class<?>, Object> loadBeans(InputStream cfgStream, Class<?>... 
beanClasses) throws IgniteCheckedException;
+
+    /**
+     * Loads bean instance by name.
+     *
+     * @param stream Input stream containing Spring XML configuration.
+     * @param beanName Bean name.
+     * @return Bean instance.
+     * @throws IgniteCheckedException In case of error.
+     */
+    public <T> T loadBean(InputStream stream, 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/5718e8a5/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
 
b/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
index 0c69b53..17bc7a0 100644
--- 
a/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
+++ 
b/modules/spring/src/main/java/org/apache/ignite/internal/util/spring/IgniteSpringHelperImpl.java
@@ -102,6 +102,32 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
     }
 
     /** {@inheritDoc} */
+    @Override public IgniteBiTuple<Collection<IgniteConfiguration>, ? extends 
GridSpringResourceContext> loadConfigurations(
+        InputStream cfgStream, String... excludedProps) throws 
IgniteCheckedException {
+        return loadConfigurations(cfgStream, IgniteConfiguration.class, 
excludedProps);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T> IgniteBiTuple<Collection<T>, ? extends 
GridSpringResourceContext> loadConfigurations(
+        InputStream cfgStream, Class<T> cl, String... excludedProps) throws 
IgniteCheckedException {
+        ApplicationContext springCtx = applicationContext(cfgStream, 
excludedProps);
+        Map<String, T> cfgMap;
+
+        try {
+            cfgMap = springCtx.getBeansOfType(cl);
+        }
+        catch (BeansException e) {
+            throw new IgniteCheckedException("Failed to instantiate bean 
[type=" + cl +
+                ", err=" + e.getMessage() + ']', e);
+        }
+
+        if (cfgMap == null || cfgMap.isEmpty())
+            throw new IgniteCheckedException("Failed to find configuration in: 
" + cfgStream);
+
+        return F.t(cfgMap.values(), new 
GridSpringResourceContextImpl(springCtx));
+    }
+
+    /** {@inheritDoc} */
     @Override public Map<Class<?>, Object> loadBeans(URL cfgUrl, Class<?>... 
beanClasses) throws IgniteCheckedException {
         assert beanClasses.length > 0;
 
@@ -133,6 +159,69 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
         }
     }
 
+    /** {@inheritDoc} */
+    @Override public Map<Class<?>, Object> loadBeans(InputStream cfgStream, 
Class<?>... beanClasses) throws IgniteCheckedException {
+        assert beanClasses.length > 0;
+
+        ApplicationContext springCtx = initContext(cfgStream);
+
+        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(InputStream cfgStream, String beanName) 
throws IgniteCheckedException {
+        ApplicationContext springCtx = initContext(cfgStream);
+
+        try {
+            return (T)springCtx.getBean(beanName);
+        }
+        catch (NoSuchBeanDefinitionException e) {
+            throw new IgniteCheckedException("Spring bean with provided name 
doesn't exist " +
+                ", beanName=" + beanName + ']');
+        }
+        catch (BeansException e) {
+            throw new IgniteCheckedException("Failed to load Spring bean with 
provided name " +
+                ", beanName=" + beanName + ']', e);
+        }
+    }
+
+    /**
+     * @param stream Input stream containing Spring XML configuration.
+     * @return Context.
+     * @throws IgniteCheckedException In case of error.
+     */
+    private ApplicationContext initContext(InputStream stream) throws 
IgniteCheckedException {
+        GenericApplicationContext springCtx;
+
+        try {
+            springCtx = new GenericApplicationContext();
+
+            XmlBeanDefinitionReader reader = new 
XmlBeanDefinitionReader(springCtx);
+
+            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
+
+            reader.loadBeanDefinitions(new InputStreamResource(stream));
+
+            springCtx.refresh();
+        }
+        catch (BeansException e) {
+            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) ", e);
+            else
+                throw new IgniteCheckedException("Failed to instantiate Spring 
XML application context" +
+                    ", err=" + e.getMessage() + ']', e);
+        }
+
+        return springCtx;
+    }
+
     /**
      * @param url XML file URL.
      * @return Context.
@@ -249,38 +338,7 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
      */
     public static ApplicationContext applicationContext(URL cfgUrl, final 
String... excludedProps) throws IgniteCheckedException {
         try {
-            GenericApplicationContext springCtx = new 
GenericApplicationContext();
-
-            BeanFactoryPostProcessor postProc = new BeanFactoryPostProcessor() 
{
-                @Override public void 
postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
-                    throws BeansException {
-                    for (String beanName : 
beanFactory.getBeanDefinitionNames()) {
-                        BeanDefinition def = 
beanFactory.getBeanDefinition(beanName);
-
-                        if (def.getBeanClassName() != null) {
-                            try {
-                                Class.forName(def.getBeanClassName());
-                            }
-                            catch (ClassNotFoundException ignored) {
-                                ((BeanDefinitionRegistry) 
beanFactory).removeBeanDefinition(beanName);
-
-                                continue;
-                            }
-                        }
-
-                        MutablePropertyValues vals = def.getPropertyValues();
-
-                        for (PropertyValue val : new 
ArrayList<>(vals.getPropertyValueList())) {
-                            for (String excludedProp : excludedProps) {
-                                if (val.getName().equals(excludedProp))
-                                    vals.removePropertyValue(val);
-                            }
-                        }
-                    }
-                }
-            };
-
-            springCtx.addBeanFactoryPostProcessor(postProc);
+            GenericApplicationContext springCtx = 
prepareSpringContext(excludedProps);
 
             new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new 
UrlResource(cfgUrl));
 
@@ -298,4 +356,82 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
                     cfgUrl + ", err=" + e.getMessage() + ']', e);
         }
     }
+
+    /**
+     * Creates Spring application context. Optionally excluded properties can 
be specified,
+     * it means that if such a property is found in {@link 
org.apache.ignite.configuration.IgniteConfiguration}
+     * then it is removed before the bean is instantiated.
+     * For example, {@code streamerConfiguration} can be excluded from the 
configs that Visor uses.
+     *
+     * @param cfgStream Stream where config file is located.
+     * @param excludedProps Properties to be excluded.
+     * @return Spring application context.
+     * @throws IgniteCheckedException If configuration could not be read.
+     */
+    public static ApplicationContext applicationContext(InputStream cfgStream, 
final String... excludedProps) throws IgniteCheckedException {
+        try {
+            GenericApplicationContext springCtx = 
prepareSpringContext(excludedProps);
+
+            XmlBeanDefinitionReader reader = new 
XmlBeanDefinitionReader(springCtx);
+
+            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
+
+            reader.loadBeanDefinitions(new InputStreamResource(cfgStream));
+
+            springCtx.refresh();
+
+            return springCtx;
+        }
+        catch (BeansException e) {
+            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) ", e);
+            else
+                throw new IgniteCheckedException("Failed to instantiate Spring 
XML application context [err=" +
+                    e.getMessage() + ']', e);
+        }
+    }
+
+    /**
+     * Prepares Spring context.
+     *
+     * @param excludedProps Properties to be excluded.
+     * @return
+     */
+    private static GenericApplicationContext prepareSpringContext(final 
String... excludedProps){
+        GenericApplicationContext springCtx = new GenericApplicationContext();
+
+        BeanFactoryPostProcessor postProc = new BeanFactoryPostProcessor() {
+            @Override public void 
postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
+                throws BeansException {
+                for (String beanName : beanFactory.getBeanDefinitionNames()) {
+                    BeanDefinition def = 
beanFactory.getBeanDefinition(beanName);
+
+                    if (def.getBeanClassName() != null) {
+                        try {
+                            Class.forName(def.getBeanClassName());
+                        }
+                        catch (ClassNotFoundException ignored) {
+                            ((BeanDefinitionRegistry) 
beanFactory).removeBeanDefinition(beanName);
+
+                            continue;
+                        }
+                    }
+
+                    MutablePropertyValues vals = def.getPropertyValues();
+
+                    for (PropertyValue val : new 
ArrayList<>(vals.getPropertyValueList())) {
+                        for (String excludedProp : excludedProps) {
+                            if (val.getName().equals(excludedProp))
+                                vals.removePropertyValue(val);
+                        }
+                    }
+                }
+            }
+        };
+
+        springCtx.addBeanFactoryPostProcessor(postProc);
+
+        return springCtx;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5718e8a5/modules/spring/src/test/java/org/apache/ignite/spring/IgniteStartFromStreamCfgTest.java
----------------------------------------------------------------------
diff --git 
a/modules/spring/src/test/java/org/apache/ignite/spring/IgniteStartFromStreamCfgTest.java
 
b/modules/spring/src/test/java/org/apache/ignite/spring/IgniteStartFromStreamCfgTest.java
new file mode 100644
index 0000000..f47c6e0
--- /dev/null
+++ 
b/modules/spring/src/test/java/org/apache/ignite/spring/IgniteStartFromStreamCfgTest.java
@@ -0,0 +1,50 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.ignite.spring;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.apache.ignite.testframework.junits.common.*;
+
+import java.io.*;
+import java.net.*;
+
+/**
+ * Checks starts from Stream.
+ */
+public class IgniteStartFromStreamCfgTest extends GridCommonAbstractTest {
+    /** Tests starts from Stream. */
+    public void testStartFromStream() throws Exception {
+        String cfg = "examples/config/example-cache.xml";
+
+        URL cfgLocation = U.resolveIgniteUrl(cfg);
+
+        Ignite grid = Ignition.start(new 
FileInputStream(cfgLocation.getFile()));
+
+        grid.cache(null).put("1", "1");
+
+        assert grid.cache(null).get("1").equals("1");
+
+        IgniteConfiguration icfg = Ignition.loadSpringBean(new 
FileInputStream(cfgLocation.getFile()), "ignite.cfg");
+
+        assert icfg.getCacheConfiguration()[0].getAtomicityMode() == 
CacheAtomicityMode.ATOMIC;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5718e8a5/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
----------------------------------------------------------------------
diff --git 
a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
 
b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
index cd6d14f..dd3bb79 100644
--- 
a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
+++ 
b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
@@ -45,6 +45,8 @@ public class IgniteSpringTestSuite extends TestSuite {
 
         suite.addTest(new TestSuite(IgniteDynamicCacheConfigTest.class));
 
+        suite.addTest(new TestSuite(IgniteStartFromStreamCfgTest.class));
+
         return suite;
     }
 }

Reply via email to