From 70d7f6845d7a42b255b7f76b67ab291d335dd803 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 20 Apr 2011 20:43:44 +0300 Subject: [PATCH] SGF-42 + add GemfireDaoSupport class --- .../gemfire/support/GemfireDaoSupport.java | 87 +++++++++++++++++++ .../data/gemfire/support/package-info.java | 9 ++ .../support/GemfireDaoSupportTests.java | 56 ++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/main/java/org/springframework/data/gemfire/support/GemfireDaoSupport.java create mode 100644 src/main/java/org/springframework/data/gemfire/support/package-info.java create mode 100644 src/test/java/org/springframework/data/gemfire/support/GemfireDaoSupportTests.java diff --git a/src/main/java/org/springframework/data/gemfire/support/GemfireDaoSupport.java b/src/main/java/org/springframework/data/gemfire/support/GemfireDaoSupport.java new file mode 100644 index 00000000..e45b5c0b --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/support/GemfireDaoSupport.java @@ -0,0 +1,87 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed 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.springframework.data.gemfire.support; + +import org.springframework.dao.support.DaoSupport; +import org.springframework.data.gemfire.GemfireTemplate; +import org.springframework.util.Assert; + +import com.gemstone.gemfire.cache.Region; + +/** + * Convenient super class for GemFire data access objects. Intended for + * GemfireTemplate usage. + * + *

Requires an EntityManagerFactory or EntityManager to be set, + * providing a JpaTemplate based on it to subclasses. Can alternatively + * be initialized directly via a JpaTemplate, to reuse the latter's + * settings such as the EntityManagerFactory, JpaDialect, flush mode, etc. + * + *

This class will create its own GemfireTemplate if an EntityManagerFactory + * or EntityManager reference is passed in. A custom JpaTemplate instance + * can be used through overriding createJpaTemplate. + * + * @author Costin Leau + */ +public class GemfireDaoSupport extends DaoSupport { + + private GemfireTemplate gemfireTemplate; + + /** + * Sets the GemFire Region to be used by this DAO. + * Will automatically create a GemfireTemplate for the given Region. + * + * @param region + */ + public void setRegion(Region region) { + this.gemfireTemplate = createGemfireTemplate(region); + } + + /** + * Creates a GemfireTemplate for the given Region. + *

Can be overridden in subclasses to provide a GemfireTemplate instance + * with different configuration, or a custom GemfireTemplate subclass. + * @param region the GemFire Region to create a GemfireTemplate for + * @return the new GemfireTemplate instance + * @see #setRegion + */ + protected GemfireTemplate createGemfireTemplate(Region region) { + return new GemfireTemplate(region); + } + + /** + * Set the GemfireTemplate for this DAO explicitly, + * as an alternative to specifying a GemFire {@link Region}. + * @see #setRegion + */ + public final void setGemfireTemplate(GemfireTemplate gemfireTemplate) { + this.gemfireTemplate = gemfireTemplate; + } + + /** + * Return the GemfireTemplate for this DAO, pre-initialized + * with the Region or set explicitly. + */ + public final GemfireTemplate getGemfireTemplate() { + return gemfireTemplate; + } + + @Override + protected final void checkDaoConfig() { + Assert.notNull(gemfireTemplate, "region or gemfireTemplate is required"); + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/data/gemfire/support/package-info.java b/src/main/java/org/springframework/data/gemfire/support/package-info.java new file mode 100644 index 00000000..e8a2b621 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/support/package-info.java @@ -0,0 +1,9 @@ + +/** + * + * Classes supporting the org.springframework.data.gemfire package. + * Contains a DAO base class for GemfireTemplate usage. + * + */ +package org.springframework.data.gemfire.support; + diff --git a/src/test/java/org/springframework/data/gemfire/support/GemfireDaoSupportTests.java b/src/test/java/org/springframework/data/gemfire/support/GemfireDaoSupportTests.java new file mode 100644 index 00000000..5b369dda --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/support/GemfireDaoSupportTests.java @@ -0,0 +1,56 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed 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.springframework.data.gemfire.support; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.springframework.data.gemfire.GemfireTemplate; + +/** + * @author Costin Leau + * + */ +public class GemfireDaoSupportTests extends TestCase { + + public void testGemfireDaoSupportWithTemplate() throws Exception { + GemfireTemplate template = new GemfireTemplate(); + final List test = new ArrayList(); + GemfireDaoSupport dao = new GemfireDaoSupport() { + protected void initDao() { + test.add("test"); + } + }; + dao.setGemfireTemplate(template); + dao.afterPropertiesSet(); + assertNotNull("template not created", dao.getGemfireTemplate()); + assertEquals("incorrect template", template, dao.getGemfireTemplate()); + assertEquals("initDao not called", test.size(), 1); + } + + public void testInvalidDaoTemplate() throws Exception { + GemfireDaoSupport dao = new GemfireDaoSupport() { + }; + try { + dao.afterPropertiesSet(); + fail("expected exception"); + } catch (IllegalArgumentException iae) { + // okay + } + } +} \ No newline at end of file