Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-789 [created] da04a0ec9


# IGNITE-789 WIP.


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

Branch: refs/heads/ignite-789
Commit: da04a0ec96e3a6f4cf71a331a498b57686dbe1cd
Parents: 8e31b7c
Author: AKuznetsov <akuznet...@gridgain.com>
Authored: Thu Apr 23 09:26:39 2015 +0700
Committer: AKuznetsov <akuznet...@gridgain.com>
Committed: Thu Apr 23 09:26:39 2015 +0700

----------------------------------------------------------------------
 .../main/java/org/apache/ignite/Ignition.java   | 19 +++++
 .../org/apache/ignite/internal/IgnitionEx.java  | 17 ++++
 .../util/spring/IgniteSpringHelper.java         | 11 +++
 .../visor/cache/VisorCacheStartNearTask.java    | 78 ++++++++++++++++++
 .../visor/cache/VisorCacheStartTask.java        | 86 ++++++++++++++++++++
 .../util/spring/IgniteSpringHelperImpl.java     | 39 ++++++++-
 6 files changed, 246 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/da04a0ec/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..e0ddd6e 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.*;
 
@@ -382,6 +383,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 springXmlCfg Spring XML configuration input stream (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 springXmlCfg, String 
beanName) throws IgniteException {
+        try {
+            return IgnitionEx.loadSpringBean(springXmlCfg, 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.

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/da04a0ec/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 56ec3ae..29182aa 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
@@ -916,6 +916,23 @@ public class IgnitionEx {
     }
 
     /**
+     * Loads spring bean by name.
+     *
+     * @param springXmlCfg Spring XML input stream.
+     * @param beanName Bean name.
+     * @return Bean instance.
+     * @throws IgniteCheckedException In case of error.
+     */
+    public static <T> T loadSpringBean(InputStream springXmlCfg, String 
beanName) throws IgniteCheckedException {
+        A.notNull(springXmlCfg, "springXmlUrl");
+        A.notNull(beanName, "beanName");
+
+        IgniteSpringHelper spring = SPRING.create(false);
+
+        return spring.loadBean(springXmlCfg, 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/da04a0ec/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..ff47937 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.*;
 
@@ -81,6 +82,16 @@ public interface IgniteSpringHelper {
     public <T> T loadBean(URL url, String beanName) throws 
IgniteCheckedException;
 
     /**
+     * Loads bean instance by name.
+     *
+     * @param inputStream Spring XML file URL.
+     * @param beanName Bean name.
+     * @return Bean instance.
+     * @throws IgniteCheckedException In case of error.
+     */
+    public <T> T loadBean(InputStream inputStream, 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/da04a0ec/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartNearTask.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartNearTask.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartNearTask.java
new file mode 100644
index 0000000..87c3dfa
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartNearTask.java
@@ -0,0 +1,78 @@
+/*
+ * 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.internal.visor.cache;
+
+import org.apache.ignite.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.internal.processors.task.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.apache.ignite.internal.visor.*;
+import org.apache.ignite.lang.*;
+
+import java.io.*;
+import java.nio.charset.*;
+
+/**
+ * Task that stop specified caches on specified node.
+ */
+@GridInternal
+public class VisorCacheStartNearTask extends 
VisorOneNodeTask<IgniteBiTuple<String, String>, Void> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** {@inheritDoc} */
+    @Override protected VisorCacheStartJob job(IgniteBiTuple<String, String> 
arg) {
+        return new VisorCacheStartJob(arg, debug);
+    }
+
+    /**
+     * Job that stop specified caches.
+     */
+    private static class VisorCacheStartJob extends 
VisorJob<IgniteBiTuple<String, String>, Void> {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /**
+         * Create job.
+         *
+         * @param arg Contains XML configurations of cache and near cache 
tuple.
+         * @param debug Debug flag.
+         */
+        private VisorCacheStartJob(IgniteBiTuple<String, String> arg, boolean 
debug) {
+            super(arg, debug);
+        }
+
+        /** {@inheritDoc} */
+        @Override protected Void run(IgniteBiTuple<String, String> arg) {
+            assert arg.get1() != null;
+            assert arg.get2() != null;
+
+            NearCacheConfiguration nearCfg = Ignition.loadSpringBean(
+                new 
ByteArrayInputStream(arg.get2().getBytes(StandardCharsets.UTF_8)), 
"nearCacheConfiguration");
+
+            ignite.createNearCache(arg.get1(), nearCfg);
+
+            return null;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return S.toString(VisorCacheStartJob.class, this);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/da04a0ec/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartTask.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartTask.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartTask.java
new file mode 100644
index 0000000..b158ca2
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/visor/cache/VisorCacheStartTask.java
@@ -0,0 +1,86 @@
+/*
+ * 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.internal.visor.cache;
+
+import org.apache.ignite.*;
+import org.apache.ignite.configuration.*;
+import org.apache.ignite.internal.processors.task.*;
+import org.apache.ignite.internal.util.typedef.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.apache.ignite.internal.visor.*;
+import org.apache.ignite.lang.*;
+
+import java.io.*;
+
+/**
+ * Task that stop specified caches on specified node.
+ */
+@GridInternal
+public class VisorCacheStartTask extends 
VisorOneNodeTask<IgniteBiTuple<String, String>, Void> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** {@inheritDoc} */
+    @Override protected VisorCacheStartJob job(IgniteBiTuple<String, String> 
arg) {
+        return new VisorCacheStartJob(arg, debug);
+    }
+
+    /**
+     * Job that stop specified caches.
+     */
+    private static class VisorCacheStartJob extends 
VisorJob<IgniteBiTuple<String, String>, Void> {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /**
+         * Create job.
+         *
+         * @param arg Contains XML configurations of cache and near cache 
tuple.
+         * @param debug Debug flag.
+         */
+        private VisorCacheStartJob(IgniteBiTuple<String, String> arg, boolean 
debug) {
+            super(arg, debug);
+        }
+
+        /** {@inheritDoc} */
+        @Override protected Void run(IgniteBiTuple<String, String> arg) {
+            String ccfg = arg.get1();
+
+            assert ccfg != null;
+
+            CacheConfiguration cfg = Ignition.loadSpringBean(
+                new ByteArrayInputStream(ccfg.getBytes()), 
"cacheConfiguration");
+
+            if (!F.isEmpty(arg.get2())) {
+                NearCacheConfiguration nearCfg = Ignition.loadSpringBean(
+                    new ByteArrayInputStream(arg.get2().getBytes()), 
"nearCacheConfiguration");
+
+                ignite.createCache(cfg, nearCfg);
+            }
+            else
+                ignite.createCache(cfg);
+
+            return null;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return S.toString(VisorCacheStartJob.class, this);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/da04a0ec/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..ba88085 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
@@ -133,18 +133,49 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
         }
     }
 
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override public <T> T loadBean(InputStream is, String beanName) throws 
IgniteCheckedException {
+        ApplicationContext springCtx = initContext(new 
InputStreamResource(is));
+
+        try {
+            return (T)springCtx.getBean(beanName);
+        }
+        catch (NoSuchBeanDefinitionException e) {
+            throw new IgniteCheckedException("Spring bean with provided name 
doesn't exist [stream=" + is +
+                ", beanName=" + beanName + ']');
+        }
+        catch (BeansException e) {
+            throw new IgniteCheckedException("Failed to load Spring bean with 
provided name [stream=" + is +
+                ", beanName=" + beanName + ']', e);
+        }
+    }
+
     /**
      * @param url XML file URL.
      * @return Context.
      * @throws IgniteCheckedException In case of error.
      */
     private ApplicationContext initContext(URL url) throws 
IgniteCheckedException {
+        return initContext(new UrlResource(url));
+    }
+
+    /**
+     * @param res Resource to load Spring configuration.
+     * @return Context.
+     * @throws IgniteCheckedException In case of error.
+     */
+    private ApplicationContext initContext(Resource res) throws 
IgniteCheckedException {
         GenericApplicationContext springCtx;
 
         try {
             springCtx = new GenericApplicationContext();
 
-            new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new 
UrlResource(url));
+            XmlBeanDefinitionReader reader = new 
XmlBeanDefinitionReader(springCtx);
+
+            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
+
+            reader.loadBeanDefinitions(res);
 
             springCtx.refresh();
         }
@@ -152,10 +183,10 @@ public class IgniteSpringHelperImpl implements 
IgniteSpringHelper {
             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=" + url + ']', e);
+                    "[resource=" + res + ']', e);
             else
-                throw new IgniteCheckedException("Failed to instantiate Spring 
XML application context [springUrl=" +
-                    url + ", err=" + e.getMessage() + ']', e);
+                throw new IgniteCheckedException("Failed to instantiate Spring 
XML application context [resource=" +
+                    res + ", err=" + e.getMessage() + ']', e);
         }
 
         return springCtx;

Reply via email to