# ignite-63
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/2f2b55e7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/2f2b55e7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/2f2b55e7 Branch: refs/heads/ignite-63 Commit: 2f2b55e7808e2258638f42b796616ce6e2e6acfa Parents: 6c05042 Author: sboikov <sboi...@gridgain.com> Authored: Fri Jan 23 12:46:50 2015 +0300 Committer: sboikov <sboi...@gridgain.com> Committed: Fri Jan 23 12:46:50 2015 +0300 ---------------------------------------------------------------------- .../examples/misc/springbean/spring-bean.xml | 2 +- ...idHadoopDefaultMapReducePlannerSelfTest.java | 2 +- .../java/org/apache/ignite/GridGainSpring.java | 117 ------- .../java/org/apache/ignite/GridSpringBean.java | 342 ------------------- .../java/org/apache/ignite/IgniteSpring.java | 117 +++++++ .../org/apache/ignite/IgniteSpringBean.java | 342 +++++++++++++++++++ .../ignite/cache/spring/GridSpringCache.java | 185 ---------- .../cache/spring/GridSpringCacheManager.java | 241 ------------- .../spring/GridSpringDynamicCacheManager.java | 316 ----------------- .../apache/ignite/cache/spring/SpringCache.java | 185 ++++++++++ .../ignite/cache/spring/SpringCacheManager.java | 241 +++++++++++++ .../cache/spring/SpringDynamicCacheManager.java | 316 +++++++++++++++++ .../ignite/internal/GridFactorySelfTest.java | 2 +- .../GridSpringBeanSerializationSelfTest.java | 8 +- .../GridSpringDynamicCacheManagerSelfTest.java | 2 +- .../org/apache/ignite/spring/spring-caching.xml | 2 +- .../ignite/spring/spring-dynamic-caching.xml | 2 +- 17 files changed, 1211 insertions(+), 1211 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/examples/src/main/java/org/apache/ignite/examples/misc/springbean/spring-bean.xml ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/misc/springbean/spring-bean.xml b/examples/src/main/java/org/apache/ignite/examples/misc/springbean/spring-bean.xml index 8670632..e194309 100644 --- a/examples/src/main/java/org/apache/ignite/examples/misc/springbean/spring-bean.xml +++ b/examples/src/main/java/org/apache/ignite/examples/misc/springbean/spring-bean.xml @@ -29,7 +29,7 @@ <description>Main Spring file for grid configuration.</description> <!-- Example of bean definition with given configuration. --> - <bean id="mySpringBean" class="org.apache.ignite.GridSpringBean"> + <bean id="mySpringBean" class="org.apache.ignite.IgniteSpringBean"> <property name="configuration"> <bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> <!-- Set to local host address just for examples. --> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/hadoop/src/test/java/org/apache/ignite/internal/processors/hadoop/GridHadoopDefaultMapReducePlannerSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/hadoop/src/test/java/org/apache/ignite/internal/processors/hadoop/GridHadoopDefaultMapReducePlannerSelfTest.java b/modules/hadoop/src/test/java/org/apache/ignite/internal/processors/hadoop/GridHadoopDefaultMapReducePlannerSelfTest.java index 9ef184d..d1932d8 100644 --- a/modules/hadoop/src/test/java/org/apache/ignite/internal/processors/hadoop/GridHadoopDefaultMapReducePlannerSelfTest.java +++ b/modules/hadoop/src/test/java/org/apache/ignite/internal/processors/hadoop/GridHadoopDefaultMapReducePlannerSelfTest.java @@ -930,7 +930,7 @@ public class GridHadoopDefaultMapReducePlannerSelfTest extends GridHadoopAbstrac * Mocked Grid. */ @SuppressWarnings("ExternalizableWithoutPublicNoArgConstructor") - private static class MockGrid extends GridSpringBean implements GridEx { + private static class MockGrid extends IgniteSpringBean implements GridEx { /** {@inheritDoc} */ @Override public IgniteFs ggfsx(String name) { assert F.eq("ggfs", name); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/GridGainSpring.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/GridGainSpring.java b/modules/spring/src/main/java/org/apache/ignite/GridGainSpring.java deleted file mode 100644 index 9b72771..0000000 --- a/modules/spring/src/main/java/org/apache/ignite/GridGainSpring.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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; - -import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.*; -import org.apache.ignite.internal.processors.resource.*; -import org.jetbrains.annotations.*; -import org.springframework.context.*; - -import java.net.*; - -/** - * Factory methods to start GridGain with optional Spring application context, this context can be injected into - * grid tasks and grid jobs using {@link org.apache.ignite.resources.IgniteSpringApplicationContextResource @IgniteSpringApplicationContextResource} - * annotation. - * <p> - * You can also instantiate grid directly from Spring without using {@code GridGain}. - * For more information refer to {@link GridSpringBean} documentation. - */ -public class GridGainSpring { - /** - * Starts grid with default configuration. By default this method will - * use grid configuration defined in {@code GRIDGAIN_HOME/config/default-config.xml} - * configuration file. If such file is not found, then all system defaults will be used. - * - * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @IgniteSpringApplicationContextResource} annotation. - * @return Started grid. - * @throws IgniteCheckedException If default grid could not be started. This exception will be thrown - * also if default grid has already been started. - */ - public static Ignite start(@Nullable ApplicationContext springCtx) throws IgniteCheckedException { - return GridGainEx.start(new GridSpringResourceContextImpl(springCtx)); - } - - /** - * Starts grid with given configuration. - * - * @param cfg Grid configuration. This cannot be {@code null}. - * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @IgniteSpringApplicationContextResource} annotation. - * @return Started grid. - * @throws IgniteCheckedException If grid could not be started. This exception will be thrown - * also if named grid has already been started. - */ - public static Ignite start(IgniteConfiguration cfg, @Nullable ApplicationContext springCtx) throws IgniteCheckedException { - return GridGainEx.start(cfg, new GridSpringResourceContextImpl(springCtx)); - } - - /** - * Starts all grids specified within given Spring XML configuration file. 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 file will contain only one Grid definition. Note that - * Grid configuration bean(s) is retrieved form configuration file by type, so the name of - * the Grid configuration bean is ignored. - * - * @param springCfgPath Spring XML configuration file path or URL. This cannot be {@code null}. - * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @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(String springCfgPath, @Nullable ApplicationContext springCtx) throws IgniteCheckedException { - return GridGainEx.start(springCfgPath, null, new GridSpringResourceContextImpl(springCtx)); - } - - /** - * Starts all grids specified within given Spring XML configuration file URL. 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 file will contain only one Grid definition. Note that - * Grid configuration bean(s) is retrieved form configuration file by type, so the name of - * the Grid configuration bean is ignored. - * - * @param springCfgUrl Spring XML configuration file URL. This cannot be {@code null}. - * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @GridSpringApplicationContextResource} 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(URL springCfgUrl, @Nullable ApplicationContext springCtx) throws IgniteCheckedException { - return GridGainEx.start(springCfgUrl, null, new GridSpringResourceContextImpl(springCtx)); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/GridSpringBean.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/GridSpringBean.java b/modules/spring/src/main/java/org/apache/ignite/GridSpringBean.java deleted file mode 100644 index 65a7ab7..0000000 --- a/modules/spring/src/main/java/org/apache/ignite/GridSpringBean.java +++ /dev/null @@ -1,342 +0,0 @@ -/* - * 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; - -import org.apache.ignite.cache.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.plugin.*; -import org.apache.ignite.product.*; -import org.apache.ignite.hadoop.*; -import org.apache.ignite.plugin.security.*; -import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.jetbrains.annotations.*; -import org.springframework.beans.*; -import org.springframework.beans.factory.*; -import org.springframework.context.*; - -import java.io.*; -import java.util.*; -import java.util.concurrent.*; - -/** - * Grid Spring bean allows to bypass {@link Ignition} methods. - * In other words, this bean class allows to inject new grid instance from - * Spring configuration file directly without invoking static - * {@link Ignition} methods. This class can be wired directly from - * Spring and can be referenced from within other Spring beans. - * By virtue of implementing {@link DisposableBean} and {@link InitializingBean} - * interfaces, {@code GridSpringBean} automatically starts and stops underlying - * grid instance. - * <p> - * <h1 class="header">Spring Configuration Example</h1> - * Here is a typical example of describing it in Spring file: - * <pre name="code" class="xml"> - * <bean id="mySpringBean" class="org.apache.ignite.GridSpringBean"> - * <property name="configuration"> - * <bean id="grid.cfg" class="org.gridgain.grid.GridConfiguration"> - * <property name="gridName" value="mySpringGrid"/> - * </bean> - * </property> - * </bean> - * </pre> - * Or use default configuration: - * <pre name="code" class="xml"> - * <bean id="mySpringBean" class="org.apache.ignite.GridSpringBean"/> - * </pre> - * <h1 class="header">Java Example</h1> - * Here is how you may access this bean from code: - * <pre name="code" class="java"> - * AbstractApplicationContext ctx = new FileSystemXmlApplicationContext("/path/to/spring/file"); - * - * // Register Spring hook to destroy bean automatically. - * ctx.registerShutdownHook(); - * - * Grid grid = (Grid)ctx.getBean("mySpringBean"); - * </pre> - * <p> - */ -public class GridSpringBean implements Ignite, DisposableBean, InitializingBean, - ApplicationContextAware, Externalizable { - /** */ - private static final long serialVersionUID = 0L; - - /** */ - private Ignite g; - - /** */ - private IgniteConfiguration cfg; - - /** */ - private ApplicationContext appCtx; - - /** {@inheritDoc} */ - @Override public IgniteConfiguration configuration() { - return cfg; - } - - /** - * Sets grid configuration. - * - * @param cfg Grid configuration. - */ - public void setConfiguration(IgniteConfiguration cfg) { - this.cfg = cfg; - } - - /** {@inheritDoc} */ - @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { - appCtx = ctx; - } - - /** {@inheritDoc} */ - @Override public void destroy() throws Exception { - // If there were some errors when afterPropertiesSet() was called. - if (g != null) { - // Do not cancel started tasks, wait for them. - G.stop(g.name(), false); - } - } - - /** {@inheritDoc} */ - @Override public void afterPropertiesSet() throws Exception { - if (cfg == null) - cfg = new IgniteConfiguration(); - - g = GridGainSpring.start(cfg, appCtx); - } - - /** {@inheritDoc} */ - @Override public IgniteLogger log() { - assert cfg != null; - - return cfg.getGridLogger(); - } - - /** {@inheritDoc} */ - @Override public IgniteProduct product() { - assert g != null; - - return g.product(); - } - - /** {@inheritDoc} */ - @Override public Collection<GridCache<?, ?>> caches() { - assert g != null; - - return g.caches(); - } - - /** {@inheritDoc} */ - @Override public Collection<IgniteStreamer> streamers() { - assert g != null; - - return g.streamers(); - } - - /** {@inheritDoc} */ - @Override public IgniteCompute compute() { - assert g != null; - - return g.compute(); - } - - /** {@inheritDoc} */ - @Override public IgniteManaged managed() { - assert g != null; - - return g.managed(); - } - - /** {@inheritDoc} */ - @Override public IgniteMessaging message() { - assert g != null; - - return g.message(); - } - - /** {@inheritDoc} */ - @Override public IgniteEvents events() { - assert g != null; - - return g.events(); - } - - /** {@inheritDoc} */ - @Override public ExecutorService executorService() { - assert g != null; - - return g.executorService(); - } - - /** {@inheritDoc} */ - @Override public IgniteCluster cluster() { - assert g != null; - - return g.cluster(); - } - - /** {@inheritDoc} */ - @Override public IgniteCompute compute(ClusterGroup prj) { - assert g != null; - - return g.compute(prj); - } - - /** {@inheritDoc} */ - @Override public IgniteMessaging message(ClusterGroup prj) { - assert g != null; - - return g.message(prj); - } - - /** {@inheritDoc} */ - @Override public IgniteEvents events(ClusterGroup prj) { - assert g != null; - - return g.events(prj); - } - - /** {@inheritDoc} */ - @Override public IgniteManaged managed(ClusterGroup prj) { - assert g != null; - - return g.managed(prj); - } - - /** {@inheritDoc} */ - @Override public ExecutorService executorService(ClusterGroup prj) { - assert g != null; - - return g.executorService(prj); - } - - /** {@inheritDoc} */ - @Override public IgniteScheduler scheduler() { - assert g != null; - - return g.scheduler(); - } - - /** {@inheritDoc} */ - @Override public GridSecurity security() { - assert g != null; - - return g.security(); - } - - /** {@inheritDoc} */ - @Override public IgnitePortables portables() { - assert g != null; - - return g.portables(); - } - - /** {@inheritDoc} */ - @Override public String name() { - assert g != null; - - return g.name(); - } - - /** {@inheritDoc} */ - @Override public <K, V> GridCache<K, V> cache(String name) { - assert g != null; - - return g.cache(name); - } - - /** {@inheritDoc} */ - @Override public <K, V> IgniteCache<K, V> jcache(@Nullable String name) { - assert g != null; - - return g.jcache(name); - } - - /** {@inheritDoc} */ - @Override public IgniteTransactions transactions() { - assert g != null; - - return g.transactions(); - } - - /** {@inheritDoc} */ - @Override public <K, V> IgniteDataLoader<K, V> dataLoader(@Nullable String cacheName) { - assert g != null; - - return g.dataLoader(cacheName); - } - - /** {@inheritDoc} */ - @Override public IgniteFs fileSystem(String name) { - assert g != null; - - return g.fileSystem(name); - } - - /** {@inheritDoc} */ - @Override public Collection<IgniteFs> fileSystems() { - assert g != null; - - return g.fileSystems(); - } - - /** {@inheritDoc} */ - @Override public GridHadoop hadoop() { - assert g != null; - - return g.hadoop(); - } - - /** {@inheritDoc} */ - @Nullable @Override public IgniteStreamer streamer(@Nullable String name) { - assert g != null; - - return g.streamer(name); - } - - /** {@inheritDoc} */ - @Override public <T extends IgnitePlugin> T plugin(String name) throws PluginNotFoundException { - assert g != null; - - return g.plugin(name); - } - - /** {@inheritDoc} */ - @Override public void close() throws IgniteCheckedException { - g.close(); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(GridSpringBean.class, this); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(g); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - g = (Ignite)in.readObject(); - - cfg = g.configuration(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/IgniteSpring.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/IgniteSpring.java b/modules/spring/src/main/java/org/apache/ignite/IgniteSpring.java new file mode 100644 index 0000000..74f9e1b --- /dev/null +++ b/modules/spring/src/main/java/org/apache/ignite/IgniteSpring.java @@ -0,0 +1,117 @@ +/* + * 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; + +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.internal.processors.resource.*; +import org.jetbrains.annotations.*; +import org.springframework.context.*; + +import java.net.*; + +/** + * Factory methods to start GridGain with optional Spring application context, this context can be injected into + * grid tasks and grid jobs using {@link org.apache.ignite.resources.IgniteSpringApplicationContextResource @IgniteSpringApplicationContextResource} + * annotation. + * <p> + * You can also instantiate grid directly from Spring without using {@code GridGain}. + * For more information refer to {@link IgniteSpringBean} documentation. + */ +public class IgniteSpring { + /** + * Starts grid with default configuration. By default this method will + * use grid configuration defined in {@code GRIDGAIN_HOME/config/default-config.xml} + * configuration file. If such file is not found, then all system defaults will be used. + * + * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @IgniteSpringApplicationContextResource} annotation. + * @return Started grid. + * @throws IgniteCheckedException If default grid could not be started. This exception will be thrown + * also if default grid has already been started. + */ + public static Ignite start(@Nullable ApplicationContext springCtx) throws IgniteCheckedException { + return GridGainEx.start(new GridSpringResourceContextImpl(springCtx)); + } + + /** + * Starts grid with given configuration. + * + * @param cfg Grid configuration. This cannot be {@code null}. + * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @IgniteSpringApplicationContextResource} annotation. + * @return Started grid. + * @throws IgniteCheckedException If grid could not be started. This exception will be thrown + * also if named grid has already been started. + */ + public static Ignite start(IgniteConfiguration cfg, @Nullable ApplicationContext springCtx) throws IgniteCheckedException { + return GridGainEx.start(cfg, new GridSpringResourceContextImpl(springCtx)); + } + + /** + * Starts all grids specified within given Spring XML configuration file. 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 file will contain only one Grid definition. Note that + * Grid configuration bean(s) is retrieved form configuration file by type, so the name of + * the Grid configuration bean is ignored. + * + * @param springCfgPath Spring XML configuration file path or URL. This cannot be {@code null}. + * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @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(String springCfgPath, @Nullable ApplicationContext springCtx) throws IgniteCheckedException { + return GridGainEx.start(springCfgPath, null, new GridSpringResourceContextImpl(springCtx)); + } + + /** + * Starts all grids specified within given Spring XML configuration file URL. 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 file will contain only one Grid definition. Note that + * Grid configuration bean(s) is retrieved form configuration file by type, so the name of + * the Grid configuration bean is ignored. + * + * @param springCfgUrl Spring XML configuration file URL. This cannot be {@code null}. + * @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 org.apache.ignite.resources.IgniteSpringApplicationContextResource @GridSpringApplicationContextResource} 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(URL springCfgUrl, @Nullable ApplicationContext springCtx) throws IgniteCheckedException { + return GridGainEx.start(springCfgUrl, null, new GridSpringResourceContextImpl(springCtx)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/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 new file mode 100644 index 0000000..94d7116 --- /dev/null +++ b/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java @@ -0,0 +1,342 @@ +/* + * 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; + +import org.apache.ignite.cache.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.plugin.*; +import org.apache.ignite.product.*; +import org.apache.ignite.hadoop.*; +import org.apache.ignite.plugin.security.*; +import org.apache.ignite.internal.util.typedef.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.jetbrains.annotations.*; +import org.springframework.beans.*; +import org.springframework.beans.factory.*; +import org.springframework.context.*; + +import java.io.*; +import java.util.*; +import java.util.concurrent.*; + +/** + * Grid Spring bean allows to bypass {@link Ignition} methods. + * In other words, this bean class allows to inject new grid instance from + * Spring configuration file directly without invoking static + * {@link Ignition} methods. This class can be wired directly from + * Spring and can be referenced from within other Spring beans. + * By virtue of implementing {@link DisposableBean} and {@link InitializingBean} + * interfaces, {@code GridSpringBean} automatically starts and stops underlying + * grid instance. + * <p> + * <h1 class="header">Spring Configuration Example</h1> + * Here is a typical example of describing it in Spring file: + * <pre name="code" class="xml"> + * <bean id="mySpringBean" class="org.apache.ignite.GridSpringBean"> + * <property name="configuration"> + * <bean id="grid.cfg" class="org.gridgain.grid.GridConfiguration"> + * <property name="gridName" value="mySpringGrid"/> + * </bean> + * </property> + * </bean> + * </pre> + * Or use default configuration: + * <pre name="code" class="xml"> + * <bean id="mySpringBean" class="org.apache.ignite.GridSpringBean"/> + * </pre> + * <h1 class="header">Java Example</h1> + * Here is how you may access this bean from code: + * <pre name="code" class="java"> + * AbstractApplicationContext ctx = new FileSystemXmlApplicationContext("/path/to/spring/file"); + * + * // Register Spring hook to destroy bean automatically. + * ctx.registerShutdownHook(); + * + * Grid grid = (Grid)ctx.getBean("mySpringBean"); + * </pre> + * <p> + */ +public class IgniteSpringBean implements Ignite, DisposableBean, InitializingBean, + ApplicationContextAware, Externalizable { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private Ignite g; + + /** */ + private IgniteConfiguration cfg; + + /** */ + private ApplicationContext appCtx; + + /** {@inheritDoc} */ + @Override public IgniteConfiguration configuration() { + return cfg; + } + + /** + * Sets grid configuration. + * + * @param cfg Grid configuration. + */ + public void setConfiguration(IgniteConfiguration cfg) { + this.cfg = cfg; + } + + /** {@inheritDoc} */ + @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { + appCtx = ctx; + } + + /** {@inheritDoc} */ + @Override public void destroy() throws Exception { + // If there were some errors when afterPropertiesSet() was called. + if (g != null) { + // Do not cancel started tasks, wait for them. + G.stop(g.name(), false); + } + } + + /** {@inheritDoc} */ + @Override public void afterPropertiesSet() throws Exception { + if (cfg == null) + cfg = new IgniteConfiguration(); + + g = IgniteSpring.start(cfg, appCtx); + } + + /** {@inheritDoc} */ + @Override public IgniteLogger log() { + assert cfg != null; + + return cfg.getGridLogger(); + } + + /** {@inheritDoc} */ + @Override public IgniteProduct product() { + assert g != null; + + return g.product(); + } + + /** {@inheritDoc} */ + @Override public Collection<GridCache<?, ?>> caches() { + assert g != null; + + return g.caches(); + } + + /** {@inheritDoc} */ + @Override public Collection<IgniteStreamer> streamers() { + assert g != null; + + return g.streamers(); + } + + /** {@inheritDoc} */ + @Override public IgniteCompute compute() { + assert g != null; + + return g.compute(); + } + + /** {@inheritDoc} */ + @Override public IgniteManaged managed() { + assert g != null; + + return g.managed(); + } + + /** {@inheritDoc} */ + @Override public IgniteMessaging message() { + assert g != null; + + return g.message(); + } + + /** {@inheritDoc} */ + @Override public IgniteEvents events() { + assert g != null; + + return g.events(); + } + + /** {@inheritDoc} */ + @Override public ExecutorService executorService() { + assert g != null; + + return g.executorService(); + } + + /** {@inheritDoc} */ + @Override public IgniteCluster cluster() { + assert g != null; + + return g.cluster(); + } + + /** {@inheritDoc} */ + @Override public IgniteCompute compute(ClusterGroup prj) { + assert g != null; + + return g.compute(prj); + } + + /** {@inheritDoc} */ + @Override public IgniteMessaging message(ClusterGroup prj) { + assert g != null; + + return g.message(prj); + } + + /** {@inheritDoc} */ + @Override public IgniteEvents events(ClusterGroup prj) { + assert g != null; + + return g.events(prj); + } + + /** {@inheritDoc} */ + @Override public IgniteManaged managed(ClusterGroup prj) { + assert g != null; + + return g.managed(prj); + } + + /** {@inheritDoc} */ + @Override public ExecutorService executorService(ClusterGroup prj) { + assert g != null; + + return g.executorService(prj); + } + + /** {@inheritDoc} */ + @Override public IgniteScheduler scheduler() { + assert g != null; + + return g.scheduler(); + } + + /** {@inheritDoc} */ + @Override public GridSecurity security() { + assert g != null; + + return g.security(); + } + + /** {@inheritDoc} */ + @Override public IgnitePortables portables() { + assert g != null; + + return g.portables(); + } + + /** {@inheritDoc} */ + @Override public String name() { + assert g != null; + + return g.name(); + } + + /** {@inheritDoc} */ + @Override public <K, V> GridCache<K, V> cache(String name) { + assert g != null; + + return g.cache(name); + } + + /** {@inheritDoc} */ + @Override public <K, V> IgniteCache<K, V> jcache(@Nullable String name) { + assert g != null; + + return g.jcache(name); + } + + /** {@inheritDoc} */ + @Override public IgniteTransactions transactions() { + assert g != null; + + return g.transactions(); + } + + /** {@inheritDoc} */ + @Override public <K, V> IgniteDataLoader<K, V> dataLoader(@Nullable String cacheName) { + assert g != null; + + return g.dataLoader(cacheName); + } + + /** {@inheritDoc} */ + @Override public IgniteFs fileSystem(String name) { + assert g != null; + + return g.fileSystem(name); + } + + /** {@inheritDoc} */ + @Override public Collection<IgniteFs> fileSystems() { + assert g != null; + + return g.fileSystems(); + } + + /** {@inheritDoc} */ + @Override public GridHadoop hadoop() { + assert g != null; + + return g.hadoop(); + } + + /** {@inheritDoc} */ + @Nullable @Override public IgniteStreamer streamer(@Nullable String name) { + assert g != null; + + return g.streamer(name); + } + + /** {@inheritDoc} */ + @Override public <T extends IgnitePlugin> T plugin(String name) throws PluginNotFoundException { + assert g != null; + + return g.plugin(name); + } + + /** {@inheritDoc} */ + @Override public void close() throws IgniteCheckedException { + g.close(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IgniteSpringBean.class, this); + } + + /** {@inheritDoc} */ + @Override public void writeExternal(ObjectOutput out) throws IOException { + out.writeObject(g); + } + + /** {@inheritDoc} */ + @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + g = (Ignite)in.readObject(); + + cfg = g.configuration(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCache.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCache.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCache.java deleted file mode 100644 index 749eefb..0000000 --- a/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCache.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * 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.cache.spring; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.lang.*; -import org.apache.ignite.internal.util.typedef.*; -import org.springframework.cache.*; -import org.springframework.cache.support.*; - -import java.io.*; - -/** - * Spring cache implementation. - */ -class GridSpringCache implements Cache, Serializable { - /** */ - private String name; - - /** */ - private Ignite ignite; - - /** */ - private GridCacheProjection<Object, Object> cache; - - /** */ - private IgniteClosure<Object, Object> keyFactory; - - /** - * @param name Cache name. - * @param ignite Ignite instance. - * @param cache Cache. - * @param keyFactory Key factory. - */ - GridSpringCache(String name, Ignite ignite, GridCacheProjection<?, ?> cache, - IgniteClosure<Object, Object> keyFactory) { - assert cache != null; - - this.name = name; - this.ignite = ignite; - this.cache = (GridCacheProjection<Object, Object>)cache; - this.keyFactory = keyFactory != null ? keyFactory : F.identity(); - } - - /** {@inheritDoc} */ - @Override public String getName() { - return name; - } - - /** {@inheritDoc} */ - @Override public Object getNativeCache() { - return cache; - } - - /** {@inheritDoc} */ - @Override public ValueWrapper get(Object key) { - try { - Object val = cache.get(keyFactory.apply(key)); - - return val != null ? new SimpleValueWrapper(val) : null; - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to get value from cache [cacheName=" + cache.name() + - ", key=" + key + ']', e); - } - } - - /** {@inheritDoc} */ - @Override public <T> T get(Object key, Class<T> type) { - try { - Object val = cache.get(keyFactory.apply(key)); - - if (val != null && type != null && !type.isInstance(val)) - throw new IllegalStateException("Cached value is not of required type [cacheName=" + cache.name() + - ", key=" + key + ", val=" + val + ", requiredType=" + type + ']'); - - return (T)val; - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to get value from cache [cacheName=" + cache.name() + - ", key=" + key + ']', e); - } - } - - /** {@inheritDoc} */ - @Override public void put(Object key, Object val) { - try { - cache.putx(keyFactory.apply(key), val); - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to put value to cache [cacheName=" + cache.name() + - ", key=" + key + ", val=" + val + ']', e); - } - } - - /** {@inheritDoc} */ - @Override public ValueWrapper putIfAbsent(Object key, Object val) { - try { - Object old = cache.putIfAbsent(keyFactory.apply(key), val); - - return old != null ? new SimpleValueWrapper(old) : null; - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to put value to cache [cacheName=" + cache.name() + - ", key=" + key + ", val=" + val + ']', e); - } - } - - /** {@inheritDoc} */ - @Override public void evict(Object key) { - try { - cache.removex(keyFactory.apply(key)); - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to remove value from cache [cacheName=" + cache.name() + - ", key=" + key + ']', e); - } - } - - /** {@inheritDoc} */ - @Override public void clear() { - try { - ignite.compute(cache.gridProjection()).broadcast(new ClearClosure(cache)); - } - catch (IgniteCheckedException e) { - throw new IgniteException("Failed to clear cache [cacheName=" + cache.name() + ']', e); - } - } - - /** - * Closure that removes all entries from cache. - */ - private static class ClearClosure extends CAX implements Externalizable { - /** */ - private static final long serialVersionUID = 0L; - - /** Cache projection. */ - private GridCacheProjection<Object, Object> cache; - - /** - * For {@link Externalizable}. - */ - public ClearClosure() { - // No-op. - } - - /** - * @param cache Cache projection. - */ - private ClearClosure(GridCacheProjection<Object, Object> cache) { - this.cache = cache; - } - - /** {@inheritDoc} */ - @Override public void applyx() throws IgniteCheckedException { - cache.removeAll(); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(cache); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - cache = (GridCacheProjection<Object, Object>)in.readObject(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCacheManager.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCacheManager.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCacheManager.java deleted file mode 100644 index e1c2660..0000000 --- a/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringCacheManager.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * 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.cache.spring; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.lang.*; -import org.apache.ignite.internal.util.typedef.*; -import org.springframework.beans.factory.*; -import org.springframework.cache.*; - -import java.util.*; - -/** - * Implementation of Spring cache abstraction based on GridGain cache. - * <h1 class="header">Overview</h1> - * Spring cache abstraction allows to enable caching for Java methods - * so that the result of a method execution is stored in some storage. If - * later the same method is called with the same set of parameters, - * the result will be retrieved from that storage instead of actually - * executing the method. For more information, refer to - * <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html"> - * Spring Cache Abstraction documentation</a>. - * <h1 class="header">How To Enable Caching</h1> - * To enable caching based on GridGain cache in your Spring application, - * you will need to do the following: - * <ul> - * <li> - * Start a GridGain node with configured cache in the same JVM - * where you application is running. - * </li> - * <li> - * Configure {@code GridSpringCacheManager} as a cache provider - * in Spring application context. - * </li> - * </ul> - * {@code GridSpringCacheManager} can start a node itself on its startup - * based on provided GridGain configuration. You can provide path to a - * Spring configuration XML file, like below (path can be absolute or - * relative to {@code GRIDGAIN_HOME}): - * <pre name="code" class="xml"> - * <beans xmlns="http://www.springframework.org/schema/beans" - * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - * xmlns:cache="http://www.springframework.org/schema/cache" - * xsi:schemaLocation=" - * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> - * <-- Provide configuration file path. --> - * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> - * <property name="configurationPath" value="examples/config/spring-cache.xml"/> - * </bean> - * - * <-- Use annotation-driven caching configuration. --> - * <cache:annotation-driven/> - * </beans> - * </pre> - * Or you can provide a {@link IgniteConfiguration} bean, like below: - * <pre name="code" class="xml"> - * <beans xmlns="http://www.springframework.org/schema/beans" - * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - * xmlns:cache="http://www.springframework.org/schema/cache" - * xsi:schemaLocation=" - * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> - * <-- Provide configuration bean. --> - * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> - * <property name="configuration"> - * <bean id="gridCfg" class="org.gridgain.grid.GridConfiguration"> - * ... - * </bean> - * </property> - * </bean> - * - * <-- Use annotation-driven caching configuration. --> - * <cache:annotation-driven/> - * </beans> - * </pre> - * Note that providing both configuration path and configuration bean is illegal - * and results in {@link IllegalArgumentException}. - * <p> - * If you already have GridGain node running within your application, - * simply provide correct Grid name, like below (if there is no Grid - * instance with such name, exception will be thrown): - * <pre name="code" class="xml"> - * <beans xmlns="http://www.springframework.org/schema/beans" - * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - * xmlns:cache="http://www.springframework.org/schema/cache" - * xsi:schemaLocation=" - * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> - * <-- Provide Grid name. --> - * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> - * <property name="gridName" value="myGrid"/> - * </bean> - * - * <-- Use annotation-driven caching configuration. --> - * <cache:annotation-driven/> - * </beans> - * </pre> - * This can be used, for example, when you are running your application - * in a J2EE Web container and use {@gglink org.gridgain.grid.startup.servlet.GridServletContextListenerStartup} - * for node startup. - * <p> - * If neither {@link #setConfigurationPath(String) configurationPath}, - * {@link #setConfiguration(IgniteConfiguration) configuration}, nor - * {@link #setGridName(String) gridName} are provided, cache manager - * will try to use default Grid instance (the one with the {@code null} - * name). If it doesn't exist, exception will be thrown. - * <h1>Starting Remote Nodes</h1> - * Remember that the node started inside your application is an entry point - * to the whole topology it connects to. You can start as many remote standalone - * nodes as you need using {@code bin/ggstart.{sh|bat}} scripts provided in - * GridGain distribution, and all these nodes will participate - * in caching data. - */ -public class GridSpringCacheManager implements CacheManager, InitializingBean { - /** Grid configuration file path. */ - private String cfgPath; - - /** Ignite configuration. */ - private IgniteConfiguration cfg; - - /** Grid name. */ - private String gridName; - - /** Ignite instance. */ - protected Ignite grid; - - /** - * Gets configuration file path. - * - * @return Grid configuration file path. - */ - public String getConfigurationPath() { - return cfgPath; - } - - /** - * Sets configuration file path. - * - * @param cfgPath Grid configuration file path. - */ - public void setConfigurationPath(String cfgPath) { - this.cfgPath = cfgPath; - } - - /** - * Gets configuration bean. - * - * @return Grid configuration bean. - */ - public IgniteConfiguration getConfiguration() { - return cfg; - } - - /** - * Sets configuration bean. - * - * @param cfg Grid configuration bean. - */ - public void setConfiguration(IgniteConfiguration cfg) { - this.cfg = cfg; - } - - /** - * Gets grid name. - * - * @return Grid name. - */ - public String getGridName() { - return gridName; - } - - /** - * Sets grid name. - * - * @param gridName Grid name. - */ - public void setGridName(String gridName) { - this.gridName = gridName; - } - - /** {@inheritDoc} */ - @SuppressWarnings("IfMayBeConditional") - @Override public void afterPropertiesSet() throws Exception { - assert grid == null; - - if (cfgPath != null && cfg != null) { - throw new IllegalArgumentException("Both 'configurationPath' and 'configuration' are " + - "provided. Set only one of these properties if you need to start a GridGain node inside of " + - "GridSpringCacheManager. If you already have a node running, omit both of them and set" + - "'gridName' property."); - } - - if (cfgPath != null) - grid = Ignition.start(cfgPath); - else if (cfg != null) - grid = Ignition.start(cfg); - else - grid = Ignition.ignite(gridName); - } - - /** {@inheritDoc} */ - @Override public Cache getCache(String name) { - assert grid != null; - - try { - return new GridSpringCache(name, grid, grid.cache(name), null); - } - catch (IllegalArgumentException ignored) { - return null; - } - } - - /** {@inheritDoc} */ - @Override public Collection<String> getCacheNames() { - assert grid != null; - - return F.viewReadOnly(grid.caches(), new IgniteClosure<GridCache<?,?>, String>() { - @Override public String apply(GridCache<?, ?> c) { - return c.name(); - } - }); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringDynamicCacheManager.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringDynamicCacheManager.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringDynamicCacheManager.java deleted file mode 100644 index fc12034..0000000 --- a/modules/spring/src/main/java/org/apache/ignite/cache/spring/GridSpringDynamicCacheManager.java +++ /dev/null @@ -1,316 +0,0 @@ -/* - * 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.cache.spring; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.internal.*; -import org.apache.ignite.internal.processors.cache.*; -import org.apache.ignite.lang.*; -import org.apache.ignite.internal.util.tostring.*; -import org.apache.ignite.internal.util.typedef.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.springframework.cache.*; -import org.springframework.cache.annotation.*; - -import java.io.*; -import java.util.*; - -/** - * Extension of {@link GridSpringCacheManager} that adds an option to - * emulate dynamic cache creation for you Spring-based applications. - * <p> - * All the data will be actually cached in one GridGain cache. It's - * name should be provided to this cache manager via - * {@link #setDataCacheName(String)} configuration property. - * <p> - * Under the hood, this cache manager will create a cache projection - * for each cache name provided in {@link Cacheable}, {@link CachePut}, - * etc. annotations. Note that you're still able to use caches configured in - * GridGain configuration. Cache projection will be created only - * cache with provided name doesn't exist. - * <h1 class="header">Configuration</h1> - * {@link GridSpringDynamicCacheManager} inherits all configuration - * properties from {@link GridSpringCacheManager} (see it's JavaDoc - * for more information on how to enable GridGain-based caching in - * a Spring application). - * <p> - * Additionally you will need to set a GridGain cache name where the data for - * all dynamically created caches will be stored. By default its name - * is {@code null}, which refers to default cache. Here is the example - * of how to configure a named cache: - * <pre name="code" class="xml"> - * <beans xmlns="http://www.springframework.org/schema/beans" - * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - * xmlns:cache="http://www.springframework.org/schema/cache" - * xsi:schemaLocation=" - * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> - * <-- Provide configuration file path --> - * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> - * <property name="dataCacheName" value="myDataCache"/> - * </bean> - * - * ... - * </beans> - * </pre> - * - * @see GridSpringCacheManager - */ -public class GridSpringDynamicCacheManager extends GridSpringCacheManager { - /** Data cache name. */ - private String dataCacheName; - - /** Meta cache. */ - private GridCacheProjectionEx<MetaKey, Cache> metaCache; - - /** Data cache. */ - private GridCache<DataKey, Object> dataCache; - - /** - * Sets data cache name. - * - * @return Data cache name. - */ - public String getDataCacheName() { - return dataCacheName; - } - - /** - * Gets data cache name. - * - * @param dataCacheName Data cache name. - */ - public void setDataCacheName(String dataCacheName) { - this.dataCacheName = dataCacheName; - } - - /** {@inheritDoc} */ - @Override public void afterPropertiesSet() throws Exception { - super.afterPropertiesSet(); - - metaCache = ((GridEx)grid).utilityCache(MetaKey.class, Cache.class); - dataCache = grid.cache(dataCacheName); - } - - /** {@inheritDoc} */ - @Override public Cache getCache(final String name) { - Cache cache = super.getCache(name); - - if (cache != null) - return cache; - - try { - MetaKey key = new MetaKey(name); - - cache = metaCache.get(key); - - if (cache == null) { - cache = new GridSpringCache(name, grid, dataCache.projection(new ProjectionFilter(name)), - new IgniteClosure<Object, Object>() { - @Override public Object apply(Object o) { - return new DataKey(name, o); - } - }); - - Cache old = metaCache.putIfAbsent(key, cache); - - if (old != null) - cache = old; - } - - return cache; - } - catch (IgniteCheckedException e) { - throw new IgniteException(e); - } - } - - /** {@inheritDoc} */ - @Override public Collection<String> getCacheNames() { - Collection<String> names = F.view(super.getCacheNames(), new IgnitePredicate<String>() { - @Override public boolean apply(String name) { - return !F.eq(name, dataCacheName); - } - }); - - return F.concat(false, names, F.transform(metaCache.entrySetx(), - new IgniteClosure<Map.Entry<MetaKey, Cache>, String>() { - @Override public String apply(Map.Entry<MetaKey, Cache> e) { - return e.getKey().name; - } - })); - } - - /** - * Meta key. - */ - private static class MetaKey extends GridCacheUtilityKey<MetaKey> implements Externalizable { - /** Cache name. */ - private String name; - - /** - * For {@link Externalizable}. - */ - public MetaKey() { - // No-op. - } - - /** - * @param name Cache name. - */ - private MetaKey(String name) { - this.name = name; - } - - /** {@inheritDoc} */ - @Override protected boolean equalsx(MetaKey key) { - return name != null ? name.equals(key.name) : key.name == null; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - return name.hashCode(); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - U.writeString(out, name); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - name = U.readString(in); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(MetaKey.class, this); - } - } - - /** - * Data key. - */ - private static class DataKey implements Externalizable { - /** Cache name. */ - private String name; - - /** Key. */ - @GridToStringInclude - private Object key; - - /** - * @param name Cache name. - * @param key Key. - */ - private DataKey(String name, Object key) { - this.name = name; - this.key = key; - } - - /** - * For {@link Externalizable}. - */ - public DataKey() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - DataKey key0 = (DataKey)o; - - return name != null ? name.equals(key0.name) : key0.name == null && - key != null ? key.equals(key0.key) : key0.key == null; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = name != null ? name.hashCode() : 0; - - res = 31 * res + (key != null ? key.hashCode() : 0); - - return res; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - U.writeString(out, name); - out.writeObject(key); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - name = U.readString(in); - key = in.readObject(); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(DataKey.class, this); - } - } - - /** - * Projection filter. - */ - private static class ProjectionFilter implements IgniteBiPredicate<DataKey, Object>, Externalizable { - /** Cache name. */ - private String name; - - /** - * For {@link Externalizable}. - */ - public ProjectionFilter() { - // No-op. - } - - /** - * @param name Cache name. - */ - private ProjectionFilter(String name) { - this.name = name; - } - - /** {@inheritDoc} */ - @Override public boolean apply(DataKey key, Object val) { - return name != null ? name.equals(key.name) : key.name == null; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - U.writeString(out, name); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - name = U.readString(in); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(ProjectionFilter.class, this); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java new file mode 100644 index 0000000..b372117 --- /dev/null +++ b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCache.java @@ -0,0 +1,185 @@ +/* + * 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.cache.spring; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.internal.util.typedef.*; +import org.springframework.cache.*; +import org.springframework.cache.support.*; + +import java.io.*; + +/** + * Spring cache implementation. + */ +class SpringCache implements Cache, Serializable { + /** */ + private String name; + + /** */ + private Ignite ignite; + + /** */ + private GridCacheProjection<Object, Object> cache; + + /** */ + private IgniteClosure<Object, Object> keyFactory; + + /** + * @param name Cache name. + * @param ignite Ignite instance. + * @param cache Cache. + * @param keyFactory Key factory. + */ + SpringCache(String name, Ignite ignite, GridCacheProjection<?, ?> cache, + IgniteClosure<Object, Object> keyFactory) { + assert cache != null; + + this.name = name; + this.ignite = ignite; + this.cache = (GridCacheProjection<Object, Object>)cache; + this.keyFactory = keyFactory != null ? keyFactory : F.identity(); + } + + /** {@inheritDoc} */ + @Override public String getName() { + return name; + } + + /** {@inheritDoc} */ + @Override public Object getNativeCache() { + return cache; + } + + /** {@inheritDoc} */ + @Override public ValueWrapper get(Object key) { + try { + Object val = cache.get(keyFactory.apply(key)); + + return val != null ? new SimpleValueWrapper(val) : null; + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to get value from cache [cacheName=" + cache.name() + + ", key=" + key + ']', e); + } + } + + /** {@inheritDoc} */ + @Override public <T> T get(Object key, Class<T> type) { + try { + Object val = cache.get(keyFactory.apply(key)); + + if (val != null && type != null && !type.isInstance(val)) + throw new IllegalStateException("Cached value is not of required type [cacheName=" + cache.name() + + ", key=" + key + ", val=" + val + ", requiredType=" + type + ']'); + + return (T)val; + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to get value from cache [cacheName=" + cache.name() + + ", key=" + key + ']', e); + } + } + + /** {@inheritDoc} */ + @Override public void put(Object key, Object val) { + try { + cache.putx(keyFactory.apply(key), val); + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to put value to cache [cacheName=" + cache.name() + + ", key=" + key + ", val=" + val + ']', e); + } + } + + /** {@inheritDoc} */ + @Override public ValueWrapper putIfAbsent(Object key, Object val) { + try { + Object old = cache.putIfAbsent(keyFactory.apply(key), val); + + return old != null ? new SimpleValueWrapper(old) : null; + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to put value to cache [cacheName=" + cache.name() + + ", key=" + key + ", val=" + val + ']', e); + } + } + + /** {@inheritDoc} */ + @Override public void evict(Object key) { + try { + cache.removex(keyFactory.apply(key)); + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to remove value from cache [cacheName=" + cache.name() + + ", key=" + key + ']', e); + } + } + + /** {@inheritDoc} */ + @Override public void clear() { + try { + ignite.compute(cache.gridProjection()).broadcast(new ClearClosure(cache)); + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to clear cache [cacheName=" + cache.name() + ']', e); + } + } + + /** + * Closure that removes all entries from cache. + */ + private static class ClearClosure extends CAX implements Externalizable { + /** */ + private static final long serialVersionUID = 0L; + + /** Cache projection. */ + private GridCacheProjection<Object, Object> cache; + + /** + * For {@link Externalizable}. + */ + public ClearClosure() { + // No-op. + } + + /** + * @param cache Cache projection. + */ + private ClearClosure(GridCacheProjection<Object, Object> cache) { + this.cache = cache; + } + + /** {@inheritDoc} */ + @Override public void applyx() throws IgniteCheckedException { + cache.removeAll(); + } + + /** {@inheritDoc} */ + @Override public void writeExternal(ObjectOutput out) throws IOException { + out.writeObject(cache); + } + + /** {@inheritDoc} */ + @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + cache = (GridCacheProjection<Object, Object>)in.readObject(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2f2b55e7/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java new file mode 100644 index 0000000..d8cb69c --- /dev/null +++ b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java @@ -0,0 +1,241 @@ +/* + * 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.cache.spring; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.internal.util.typedef.*; +import org.springframework.beans.factory.*; +import org.springframework.cache.*; + +import java.util.*; + +/** + * Implementation of Spring cache abstraction based on GridGain cache. + * <h1 class="header">Overview</h1> + * Spring cache abstraction allows to enable caching for Java methods + * so that the result of a method execution is stored in some storage. If + * later the same method is called with the same set of parameters, + * the result will be retrieved from that storage instead of actually + * executing the method. For more information, refer to + * <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html"> + * Spring Cache Abstraction documentation</a>. + * <h1 class="header">How To Enable Caching</h1> + * To enable caching based on GridGain cache in your Spring application, + * you will need to do the following: + * <ul> + * <li> + * Start a GridGain node with configured cache in the same JVM + * where you application is running. + * </li> + * <li> + * Configure {@code GridSpringCacheManager} as a cache provider + * in Spring application context. + * </li> + * </ul> + * {@code GridSpringCacheManager} can start a node itself on its startup + * based on provided GridGain configuration. You can provide path to a + * Spring configuration XML file, like below (path can be absolute or + * relative to {@code GRIDGAIN_HOME}): + * <pre name="code" class="xml"> + * <beans xmlns="http://www.springframework.org/schema/beans" + * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + * xmlns:cache="http://www.springframework.org/schema/cache" + * xsi:schemaLocation=" + * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> + * <-- Provide configuration file path. --> + * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> + * <property name="configurationPath" value="examples/config/spring-cache.xml"/> + * </bean> + * + * <-- Use annotation-driven caching configuration. --> + * <cache:annotation-driven/> + * </beans> + * </pre> + * Or you can provide a {@link IgniteConfiguration} bean, like below: + * <pre name="code" class="xml"> + * <beans xmlns="http://www.springframework.org/schema/beans" + * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + * xmlns:cache="http://www.springframework.org/schema/cache" + * xsi:schemaLocation=" + * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> + * <-- Provide configuration bean. --> + * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> + * <property name="configuration"> + * <bean id="gridCfg" class="org.gridgain.grid.GridConfiguration"> + * ... + * </bean> + * </property> + * </bean> + * + * <-- Use annotation-driven caching configuration. --> + * <cache:annotation-driven/> + * </beans> + * </pre> + * Note that providing both configuration path and configuration bean is illegal + * and results in {@link IllegalArgumentException}. + * <p> + * If you already have GridGain node running within your application, + * simply provide correct Grid name, like below (if there is no Grid + * instance with such name, exception will be thrown): + * <pre name="code" class="xml"> + * <beans xmlns="http://www.springframework.org/schema/beans" + * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + * xmlns:cache="http://www.springframework.org/schema/cache" + * xsi:schemaLocation=" + * http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + * http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> + * <-- Provide Grid name. --> + * <bean id="cacheManager" class="org.apache.ignite.cache.spring.GridSpringCacheManager"> + * <property name="gridName" value="myGrid"/> + * </bean> + * + * <-- Use annotation-driven caching configuration. --> + * <cache:annotation-driven/> + * </beans> + * </pre> + * This can be used, for example, when you are running your application + * in a J2EE Web container and use {@gglink org.gridgain.grid.startup.servlet.GridServletContextListenerStartup} + * for node startup. + * <p> + * If neither {@link #setConfigurationPath(String) configurationPath}, + * {@link #setConfiguration(IgniteConfiguration) configuration}, nor + * {@link #setGridName(String) gridName} are provided, cache manager + * will try to use default Grid instance (the one with the {@code null} + * name). If it doesn't exist, exception will be thrown. + * <h1>Starting Remote Nodes</h1> + * Remember that the node started inside your application is an entry point + * to the whole topology it connects to. You can start as many remote standalone + * nodes as you need using {@code bin/ggstart.{sh|bat}} scripts provided in + * GridGain distribution, and all these nodes will participate + * in caching data. + */ +public class SpringCacheManager implements CacheManager, InitializingBean { + /** Grid configuration file path. */ + private String cfgPath; + + /** Ignite configuration. */ + private IgniteConfiguration cfg; + + /** Grid name. */ + private String gridName; + + /** Ignite instance. */ + protected Ignite grid; + + /** + * Gets configuration file path. + * + * @return Grid configuration file path. + */ + public String getConfigurationPath() { + return cfgPath; + } + + /** + * Sets configuration file path. + * + * @param cfgPath Grid configuration file path. + */ + public void setConfigurationPath(String cfgPath) { + this.cfgPath = cfgPath; + } + + /** + * Gets configuration bean. + * + * @return Grid configuration bean. + */ + public IgniteConfiguration getConfiguration() { + return cfg; + } + + /** + * Sets configuration bean. + * + * @param cfg Grid configuration bean. + */ + public void setConfiguration(IgniteConfiguration cfg) { + this.cfg = cfg; + } + + /** + * Gets grid name. + * + * @return Grid name. + */ + public String getGridName() { + return gridName; + } + + /** + * Sets grid name. + * + * @param gridName Grid name. + */ + public void setGridName(String gridName) { + this.gridName = gridName; + } + + /** {@inheritDoc} */ + @SuppressWarnings("IfMayBeConditional") + @Override public void afterPropertiesSet() throws Exception { + assert grid == null; + + if (cfgPath != null && cfg != null) { + throw new IllegalArgumentException("Both 'configurationPath' and 'configuration' are " + + "provided. Set only one of these properties if you need to start a GridGain node inside of " + + "GridSpringCacheManager. If you already have a node running, omit both of them and set" + + "'gridName' property."); + } + + if (cfgPath != null) + grid = Ignition.start(cfgPath); + else if (cfg != null) + grid = Ignition.start(cfg); + else + grid = Ignition.ignite(gridName); + } + + /** {@inheritDoc} */ + @Override public Cache getCache(String name) { + assert grid != null; + + try { + return new SpringCache(name, grid, grid.cache(name), null); + } + catch (IllegalArgumentException ignored) { + return null; + } + } + + /** {@inheritDoc} */ + @Override public Collection<String> getCacheNames() { + assert grid != null; + + return F.viewReadOnly(grid.caches(), new IgniteClosure<GridCache<?,?>, String>() { + @Override public String apply(GridCache<?, ?> c) { + return c.name(); + } + }); + } +}