Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BD012172AB for ; Thu, 18 Jun 2015 06:35:44 +0000 (UTC) Received: (qmail 70423 invoked by uid 500); 18 Jun 2015 06:35:44 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 70388 invoked by uid 500); 18 Jun 2015 06:35:44 -0000 Mailing-List: contact commits-help@ignite.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.incubator.apache.org Delivered-To: mailing list commits@ignite.incubator.apache.org Received: (qmail 70376 invoked by uid 99); 18 Jun 2015 06:35:44 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Jun 2015 06:35:44 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 0C6211A5CBB for ; Thu, 18 Jun 2015 06:35:44 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.791 X-Spam-Level: * X-Spam-Status: No, score=1.791 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id SnLCh2gFKk6z for ; Thu, 18 Jun 2015 06:35:34 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 9C68C2AD24 for ; Thu, 18 Jun 2015 06:35:27 +0000 (UTC) Received: (qmail 69250 invoked by uid 99); 18 Jun 2015 06:35:21 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Jun 2015 06:35:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E138CE3C86; Thu, 18 Jun 2015 06:35:20 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: anovikov@apache.org To: commits@ignite.incubator.apache.org Date: Thu, 18 Jun 2015 06:35:52 -0000 Message-Id: <8acdc9e16db94383903d38449b1586cd@git.apache.org> In-Reply-To: <6e0f3fa7c0f54cdeac240f324593f046@git.apache.org> References: <6e0f3fa7c0f54cdeac240f324593f046@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [33/50] incubator-ignite git commit: #IGNITE-960 Fixed review notes. Added tests. Added check that CacheConfiguration is serializable. #IGNITE-960 Fixed review notes. Added tests. Added check that CacheConfiguration is serializable. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/a7ca45bb Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/a7ca45bb Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/a7ca45bb Branch: refs/heads/ignite-gg-10416 Commit: a7ca45bb5fb9afa782d2b2ec134cc6524c7c22f8 Parents: b037baf Author: nikolay_tikhonov Authored: Tue Jun 16 17:15:49 2015 +0300 Committer: nikolay_tikhonov Committed: Tue Jun 16 17:15:49 2015 +0300 ---------------------------------------------------------------------- .../cache/store/jdbc/CacheJdbcBlobStore.java | 22 +- .../store/jdbc/CacheJdbcBlobStoreFactory.java | 287 +++++++++++++++++++ .../cache/store/jdbc/CacheJdbcPojoStore.java | 3 + .../store/jdbc/CacheJdbcPojoStoreFactory.java | 145 ++++++++++ .../processors/cache/GridCacheProcessor.java | 18 ++ .../util/spring/IgniteSpringHelper.java | 10 + .../hibernate/CacheHibernateBlobStore.java | 87 +----- .../CacheHibernateBlobStoreFactory.java | 180 ++++++++++++ .../CacheHibernateStoreFactorySelfTest.java | 66 +++++ .../testsuites/IgniteHibernateTestSuite.java | 2 + .../store/jdbc/CacheJdbcBlobStoreFactory.java | 237 --------------- .../util/spring/IgniteSpringHelperImpl.java | 17 ++ .../src/test/config/incorrect-store-cache.xml | 2 +- .../test/config/pojo-incorrect-store-cache.xml | 56 ++++ modules/spring/src/test/config/store-cache.xml | 2 +- modules/spring/src/test/config/store-cache1.xml | 2 +- .../jdbc/CacheJdbcBlobStoreFactorySelfTest.java | 2 +- .../jdbc/CacheJdbcPojoStoreFactorySelfTest.java | 194 +++++++++++++ .../testsuites/IgniteSpringTestSuite.java | 5 + 19 files changed, 991 insertions(+), 346 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStore.java index 6852260..e01c879 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStore.java @@ -19,6 +19,7 @@ package org.apache.ignite.cache.store.jdbc; import org.apache.ignite.*; import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.util.tostring.*; import org.apache.ignite.internal.util.typedef.*; @@ -65,27 +66,8 @@ import java.util.concurrent.atomic.*; *
  • Insert entry query (see {@link #setInsertQuery(String)})
  • *
  • Delete entry query (see {@link #setDeleteQuery(String)})
  • * - *

    Java Example

    - *
    - *     ...
    - *     GridCacheJdbcBlobStore<String, String> store = new GridCacheJdbcBlobStore<String, String>();
    - *     ...
    - * 
    - *

    Spring Example

    - *
    - *     ...
    - *     <bean id="cache.jdbc.store"
    - *         class="org.apache.ignite.cache.store.jdbc.CacheJdbcBlobStore">
    - *         <property name="connectionUrl" value="jdbc:h2:mem:"/>
    - *         <property name="createTableQuery"
    - *             value="create table if not exists ENTRIES (key other, val other)"/>
    - *     </bean>
    - *     ...
    - * 
    *

    - * - *
    - * For information about Spring framework visit www.springframework.org + * Use {@link CacheJdbcBlobStoreFactory} factory to pass {@link CacheJdbcBlobStore} to {@link CacheConfiguration}. */ public class CacheJdbcBlobStore extends CacheStoreAdapter { /** Default connection URL (value is jdbc:h2:mem:jdbcCacheStore;DB_CLOSE_DELAY=-1). */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java new file mode 100644 index 0000000..29a83c9 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java @@ -0,0 +1,287 @@ +/* + * 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.store.jdbc; + +import org.apache.ignite.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.internal.util.spring.*; +import org.apache.ignite.internal.util.tostring.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.resources.*; + +import javax.cache.configuration.*; +import javax.sql.*; + +/** + * {@link Factory} implementation for {@link CacheJdbcBlobStore}. + * + * Use this factory to pass {@link CacheJdbcBlobStore} to {@link CacheConfiguration}. + * + *

    Spring Example

    + *
    + *     <bean id= "simpleDataSource" class="org.h2.jdbcx.JdbcDataSource"/>
    + *
    + *     <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
    + *          ...
    + *          <property name="cacheConfiguration">
    + *               <list>
    + *                  <bean class="org.apache.ignite.configuration.CacheConfiguration">
    + *                      ...
    + *                      <property name="cacheStoreFactory">
    + *                          <bean class="org.apache.ignite.cache.store.jdbc.CacheJdbcBlobStoreFactory">
    + *                              <property name="user" value = "GridGain" />
    + *                              <property name="dataSourceBean" value = "simpleDataSource" />
    + *                          </bean>
    + *                      </property>
    + *                  </bean>
    + *               </list>
    + *          </property>
    + *     </bean>
    + * 
    + *

    + * + *
    + * For information about Spring framework visit www.springframework.org + */ +public class CacheJdbcBlobStoreFactory implements Factory> { + /** Connection URL. */ + private String connUrl = CacheJdbcBlobStore.DFLT_CONN_URL; + + /** Query to create table. */ + private String createTblQry = CacheJdbcBlobStore.DFLT_CREATE_TBL_QRY; + + /** Query to load entry. */ + private String loadQry = CacheJdbcBlobStore.DFLT_LOAD_QRY; + + /** Query to update entry. */ + private String updateQry = CacheJdbcBlobStore.DFLT_UPDATE_QRY; + + /** Query to insert entries. */ + private String insertQry = CacheJdbcBlobStore.DFLT_INSERT_QRY; + + /** Query to delete entries. */ + private String delQry = CacheJdbcBlobStore.DFLT_DEL_QRY; + + /** User name for database access. */ + private String user; + + /** Password for database access. */ + @GridToStringExclude + private String passwd; + + /** Flag for schema initialization. */ + private boolean initSchema = true; + + /** Name of data source bean. */ + private String dataSrcBean; + + /** Data source. */ + private DataSource dataSrc; + + /** Application context. */ + @SpringApplicationContextResource + private Object appContext; + + /** {@inheritDoc} */ + @Override public CacheJdbcBlobStore create() { + CacheJdbcBlobStore store = new CacheJdbcBlobStore<>(); + + store.setInitSchema(initSchema); + store.setConnectionUrl(connUrl); + store.setCreateTableQuery(createTblQry); + store.setLoadQuery(loadQry); + store.setUpdateQuery(updateQry); + store.setInsertQuery(insertQry); + store.setDeleteQuery(delQry); + store.setUser(user); + store.setPassword(passwd); + + if (dataSrc != null) + store.setDataSource(dataSrc); + else if (dataSrcBean != null) { + if (appContext == null) + throw new IgniteException("Spring application context resource is not injected."); + + IgniteSpringHelper spring; + + try { + spring = IgniteComponentType.SPRING.create(false); + + DataSource data = spring.loadBeanFromAppContext(appContext, dataSrcBean); + + store.setDataSource(data); + } + catch (IgniteCheckedException e) { + throw new IgniteException("Failed to load bean in application context [beanName=" + dataSrcBean + + ", igniteConfig=" + appContext + ']'); + } + } + + return store; + } + + /** + * Flag indicating whether DB schema should be initialized by Ignite (default behaviour) or + * was explicitly created by user. + * + * @param initSchema Initialized schema flag. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setInitSchema(boolean) + */ + public CacheJdbcBlobStoreFactory setInitSchema(boolean initSchema) { + this.initSchema = initSchema; + + return this; + } + + /** + * Sets connection URL. + * + * @param connUrl Connection URL. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setConnectionUrl(String) + */ + public CacheJdbcBlobStoreFactory setConnectionUrl(String connUrl) { + this.connUrl = connUrl; + + return this; + } + + /** + * Sets create table query. + * + * @param createTblQry Create table query. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setCreateTableQuery(String) + */ + public CacheJdbcBlobStoreFactory setCreateTableQuery(String createTblQry) { + this.createTblQry = createTblQry; + + return this; + } + + /** + * Sets load query. + * + * @param loadQry Load query + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setLoadQuery(String) + */ + public CacheJdbcBlobStoreFactory setLoadQuery(String loadQry) { + this.loadQry = loadQry; + + return this; + } + + /** + * Sets update entry query. + * + * @param updateQry Update entry query. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setUpdateQuery(String) + */ + public CacheJdbcBlobStoreFactory setUpdateQuery(String updateQry) { + this.updateQry = updateQry; + + return this; + } + + /** + * Sets insert entry query. + * + * @param insertQry Insert entry query. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setInsertQuery(String) + */ + public CacheJdbcBlobStoreFactory setInsertQuery(String insertQry) { + this.insertQry = insertQry; + + return this; + } + + /** + * Sets delete entry query. + * + * @param delQry Delete entry query. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setDeleteQuery(String) + */ + public CacheJdbcBlobStoreFactory setDeleteQuery(String delQry) { + this.delQry = delQry; + + return this; + } + + /** + * Sets user name for database access. + * + * @param user User name. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setUser(String) + */ + public CacheJdbcBlobStoreFactory setUser(String user) { + this.user = user; + + return this; + } + + /** + * Sets password for database access. + * + * @param passwd Password. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setPassword(String) + */ + public CacheJdbcBlobStoreFactory setPassword(String passwd) { + this.passwd = passwd; + + return this; + } + + /** + * Sets name of the data source bean. + * + * @param dataSrcBean Data source bean name. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setDataSource(DataSource) + */ + public CacheJdbcBlobStoreFactory setDataSourceBean(String dataSrcBean) { + this.dataSrcBean = dataSrcBean; + + return this; + } + + /** + * Sets data source. Data source should be fully configured and ready-to-use. + * + * @param dataSrc Data source. + * @return {@code This} for chaining. + * @see CacheJdbcBlobStore#setDataSource(DataSource) + */ + public CacheJdbcBlobStoreFactory setDataSource(DataSource dataSrc) { + this.dataSrc = dataSrc; + + return this; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(CacheJdbcBlobStoreFactory.class, this); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java index 682f018..da00d95 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java @@ -19,6 +19,7 @@ package org.apache.ignite.cache.store.jdbc; import org.apache.ignite.cache.*; import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.internal.util.typedef.internal.*; import org.jetbrains.annotations.*; @@ -33,6 +34,8 @@ import java.util.*; * Implementation of {@link CacheStore} backed by JDBC and POJO via reflection. * * This implementation stores objects in underlying database using java beans mapping description via reflection. + *

    + * Use {@link CacheJdbcPojoStoreFactory} factory to pass {@link CacheJdbcPojoStore} to {@link CacheConfiguration}. * * @param The type of keys handled by this loader. * @param The type of values generated by this loader. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactory.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactory.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactory.java new file mode 100644 index 0000000..4cb680a --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactory.java @@ -0,0 +1,145 @@ +/* + * 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.store.jdbc; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.jdbc.dialect.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.internal.util.spring.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.resources.*; + +import javax.cache.configuration.*; +import javax.sql.*; + +/** + * {@link Factory} implementation for {@link CacheJdbcPojoStore}. + * + * Use this factory to pass {@link CacheJdbcPojoStore} to {@link CacheConfiguration}. + * + *

    Spring Example

    + *
    + *     <bean id= "simpleDataSource" class="org.h2.jdbcx.JdbcDataSource"/>
    + *
    + *     <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
    + *          ...
    + *          <property name="cacheConfiguration">
    + *               <list>
    + *                  <bean class="org.apache.ignite.configuration.CacheConfiguration">
    + *                      ...
    + *                      <property name="cacheStoreFactory">
    + *                          <bean class="org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory">
    + *                              <property name="dataSourceBean" value = "simpleDataSource" />
    + *                          </bean>
    + *                      </property>
    + *                  </bean>
    + *               </list>
    + *          </property>
    + *     </bean>
    + * 
    + *

    + * + *
    + * For information about Spring framework visit www.springframework.org + */ +public class CacheJdbcPojoStoreFactory implements Factory> { + /** Name of data source bean. */ + private String dataSrcBean; + + /** Data source. */ + private DataSource dataSrc; + + /** Database dialect. */ + protected JdbcDialect dialect; + + /** Application context. */ + @SpringApplicationContextResource + private Object appContext; + + /** {@inheritDoc} */ + @Override public CacheJdbcPojoStore create() { + CacheJdbcPojoStore store = new CacheJdbcPojoStore<>(); + + store.setDialect(dialect); + + if (dataSrc != null) + store.setDataSource(dataSrc); + else if (dataSrcBean != null) { + if (appContext == null) + throw new IgniteException("Spring application context resource is not injected."); + + IgniteSpringHelper spring; + + try { + spring = IgniteComponentType.SPRING.create(false); + + DataSource data = spring.loadBeanFromAppContext(appContext, dataSrcBean); + + store.setDataSource(data); + } + catch (Exception e) { + throw new IgniteException("Failed to load bean in application context [beanName=" + dataSrcBean + + ", igniteConfig=" + appContext + ']'); + } + } + + return store; + } + + /** + * Sets name of the data source bean. + * + * @param dataSrcBean Data source bean name. + * @return {@code This} for chaining. + * @see CacheJdbcPojoStore#setDataSource(DataSource) + */ + public CacheJdbcPojoStoreFactory setDataSourceBean(String dataSrcBean) { + this.dataSrcBean = dataSrcBean; + + return this; + } + + /** + * Sets data source. Data source should be fully configured and ready-to-use. + * + * @param dataSrc Data source. + * @return {@code This} for chaining. + * @see CacheJdbcPojoStore#setDataSource(DataSource) + */ + public CacheJdbcPojoStoreFactory setDataSource(DataSource dataSrc) { + this.dataSrc = dataSrc; + + return this; + } + + /** + * Set database dialect. + * + * @param dialect Database dialect. + * @see CacheJdbcPojoStore#setDialect(JdbcDialect) + */ + public void setDialect(JdbcDialect dialect) { + this.dialect = dialect; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(CacheJdbcPojoStoreFactory.class, this); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java index ab7c48d..2f7f22c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java @@ -1888,6 +1888,13 @@ public class GridCacheProcessor extends GridProcessorAdapter { req.failIfExists(failIfExists); if (ccfg != null) { + try { + checkSerializable(ccfg); + } + catch (IgniteCheckedException e) { + return new GridFinishedFuture<>(e); + } + if (desc != null && !desc.cancelled()) { if (failIfExists) return new GridFinishedFuture<>(new CacheExistsException("Failed to start cache " + @@ -2962,6 +2969,17 @@ public class GridCacheProcessor extends GridProcessorAdapter { if (val == null) return; + if (val.getCacheStoreFactory() != null) { + try { + marshaller.unmarshal(marshaller.marshal(val.getCacheStoreFactory()), + val.getCacheStoreFactory().getClass().getClassLoader()); + } + catch (IgniteCheckedException e) { + throw new IgniteCheckedException("Failed to validate cache configuration. " + + "Cache store factory is not serializable. Cache name: " + U.maskName(val.getName()), e); + } + } + try { marshaller.unmarshal(marshaller.marshal(val), val.getClass().getClassLoader()); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/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 a9e9e93..345bb7c 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 @@ -133,6 +133,16 @@ public interface IgniteSpringHelper { public T loadBean(InputStream stream, String beanName) throws IgniteCheckedException; /** + * Loads bean instance by name from application context. + * + * @param appContext Sprint application context. + * @param beanName Bean name. + * @return Bean instance. + * @throws IgniteCheckedException In case of error. + */ + public T loadBeanFromAppContext(Object appContext, 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/a7ca45bb/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStore.java ---------------------------------------------------------------------- diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStore.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStore.java index 77093b5..e6c542c 100644 --- a/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStore.java +++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStore.java @@ -19,6 +19,7 @@ package org.apache.ignite.cache.store.hibernate; import org.apache.ignite.*; import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.util.tostring.*; import org.apache.ignite.internal.util.typedef.*; @@ -59,92 +60,8 @@ import java.util.concurrent.atomic.*; *

    * If hibernate properties are provided, mapping * {@code GridCacheHibernateStoreEntry.hbm.xml} is included automatically. - * - *

    Java Example

    - * In this example existing session factory is provided. - *
    - *     ...
    - *     CacheHibernateBlobStore<String, String> store = new CacheHibernateBlobStore<String, String>();
    - *
    - *     store.setSessionFactory(sesFactory);
    - *     ...
    - * 
    - * - *

    Spring Example (using Spring ORM)

    - *
    - *   ...
    - *   <bean id="cache.hibernate.store"
    - *       class="org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStore">
    - *       <property name="sessionFactory">
    - *           <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    - *               <property name="hibernateProperties">
    - *                   <value>
    - *                       connection.url=jdbc:h2:mem:
    - *                       show_sql=true
    - *                       hbm2ddl.auto=true
    - *                       hibernate.dialect=org.hibernate.dialect.H2Dialect
    - *                   </value>
    - *               </property>
    - *               <property name="mappingResources">
    - *                   <list>
    - *                       <value>
    - *                           org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStoreEntry.hbm.xml
    - *                       </value>
    - *                   </list>
    - *               </property>
    - *           </bean>
    - *       </property>
    - *   </bean>
    - *   ...
    - * 
    - * - *

    Spring Example (using Spring ORM and persistent annotations)

    - *
    - *     ...
    - *     <bean id="cache.hibernate.store1"
    - *         class="org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStore">
    - *         <property name="sessionFactory">
    - *             <bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    - *                 <property name="hibernateProperties">
    - *                     <value>
    - *                         connection.url=jdbc:h2:mem:
    - *                         show_sql=true
    - *                         hbm2ddl.auto=true
    - *                         hibernate.dialect=org.hibernate.dialect.H2Dialect
    - *                     </value>
    - *                 </property>
    - *                 <property name="annotatedClasses">
    - *                     <list>
    - *                         <value>
    - *                             org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStoreEntry
    - *                         </value>
    - *                     </list>
    - *                 </property>
    - *             </bean>
    - *         </property>
    - *     </bean>
    - *     ...
    - * 
    - * - *

    Spring Example

    - *
    - *     ...
    - *     <bean id="cache.hibernate.store2"
    - *         class="org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStore">
    - *         <property name="hibernateProperties">
    - *             <props>
    - *                 <prop key="connection.url">jdbc:h2:mem:</prop>
    - *                 <prop key="hbm2ddl.auto">update</prop>
    - *                 <prop key="show_sql">true</prop>
    - *             </props>
    - *         </property>
    - *     </bean>
    - *     ...
    - * 
    *

    - * - *
    - * For information about Spring framework visit www.springframework.org + * Use {@link CacheHibernateBlobStoreFactory} factory to pass {@link CacheHibernateBlobStore} to {@link CacheConfiguration}. */ public class CacheHibernateBlobStore extends CacheStoreAdapter { /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStoreFactory.java ---------------------------------------------------------------------- diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStoreFactory.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStoreFactory.java new file mode 100644 index 0000000..6b89a9e --- /dev/null +++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStoreFactory.java @@ -0,0 +1,180 @@ +/* + * 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.store.hibernate; + +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.util.tostring.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.hibernate.*; + +import javax.cache.configuration.*; +import java.util.*; + +/** + * {@link Factory} implementation for {@link CacheHibernateBlobStore}. + * + * Use this factory to pass {@link CacheHibernateBlobStore} to {@link CacheConfiguration}. + * + *

    Java Example

    + * In this example existing session factory is provided. + *
    + *     ...
    + *     CacheHibernateBlobStoreFactory<String, String> factory = new CacheHibernateBlobStoreFactory<String, String>();
    + *
    + *     factory.setSessionFactory(sesFactory);
    + *     ...
    + * 
    + * + *

    Spring Example (using Spring ORM)

    + *
    + *   ...
    + *   <bean id="cache.hibernate.store.factory"
    + *       class="org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStoreFactory">
    + *       <property name="sessionFactory">
    + *           <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    + *               <property name="hibernateProperties">
    + *                   <value>
    + *                       connection.url=jdbc:h2:mem:
    + *                       show_sql=true
    + *                       hbm2ddl.auto=true
    + *                       hibernate.dialect=org.hibernate.dialect.H2Dialect
    + *                   </value>
    + *               </property>
    + *               <property name="mappingResources">
    + *                   <list>
    + *                       <value>
    + *                           org/apache/ignite/cache/store/hibernate/CacheHibernateBlobStoreEntry.hbm.xml
    + *                       </value>
    + *                   </list>
    + *               </property>
    + *           </bean>
    + *       </property>
    + *   </bean>
    + *   ...
    + * 
    + * + *

    Spring Example (using Spring ORM and persistent annotations)

    + *
    + *     ...
    + *     <bean id="cache.hibernate.store.factory1"
    + *         class="org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStoreFactory">
    + *         <property name="sessionFactory">
    + *             <bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    + *                 <property name="hibernateProperties">
    + *                     <value>
    + *                         connection.url=jdbc:h2:mem:
    + *                         show_sql=true
    + *                         hbm2ddl.auto=true
    + *                         hibernate.dialect=org.hibernate.dialect.H2Dialect
    + *                     </value>
    + *                 </property>
    + *                 <property name="annotatedClasses">
    + *                     <list>
    + *                         <value>
    + *                             org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStoreEntry
    + *                         </value>
    + *                     </list>
    + *                 </property>
    + *             </bean>
    + *         </property>
    + *     </bean>
    + *     ...
    + * 
    + * + *

    Spring Example

    + *
    + *     ...
    + *     <bean id="cache.hibernate.store.factory2"
    + *         class="org.apache.ignite.cache.store.hibernate.CacheHibernateBlobStoreFactory">
    + *         <property name="hibernateProperties">
    + *             <props>
    + *                 <prop key="connection.url">jdbc:h2:mem:</prop>
    + *                 <prop key="hbm2ddl.auto">update</prop>
    + *                 <prop key="show_sql">true</prop>
    + *             </props>
    + *         </property>
    + *     </bean>
    + *     ...
    + * 
    + *

    + * + *
    + * For information about Spring framework visit www.springframework.org + */ +public class CacheHibernateBlobStoreFactory implements Factory> { + /** Session factory. */ + @GridToStringExclude + private SessionFactory sesFactory; + + /** Path to hibernate configuration file. */ + private String hibernateCfgPath; + + /** Hibernate properties. */ + @GridToStringExclude + private Properties hibernateProps; + + + /** {@inheritDoc} */ + @Override public CacheHibernateBlobStore create() { + CacheHibernateBlobStore store = new CacheHibernateBlobStore<>(); + + store.setSessionFactory(sesFactory); + store.setHibernateConfigurationPath(hibernateCfgPath); + store.setHibernateProperties(hibernateProps); + + return store; + } + + /** + * Sets session factory. + * + * @param sesFactory Session factory. + * @see CacheHibernateBlobStore#setSessionFactory(SessionFactory) + */ + public void setSessionFactory(SessionFactory sesFactory) { + this.sesFactory = sesFactory; + } + + /** + * Sets hibernate configuration path. + *

    + * This may be either URL or file path or classpath resource. + * + * @param hibernateCfgPath URL or file path or classpath resource + * pointing to hibernate configuration XML file. + * @see CacheHibernateBlobStore#setHibernateConfigurationPath(String) + */ + public void setHibernateConfigurationPath(String hibernateCfgPath) { + this.hibernateCfgPath = hibernateCfgPath; + } + + /** + * Sets Hibernate properties. + * + * @param hibernateProps Hibernate properties. + * @see CacheHibernateBlobStore#setHibernateProperties(Properties) + */ + public void setHibernateProperties(Properties hibernateProps) { + this.hibernateProps = hibernateProps; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(CacheHibernateBlobStoreFactory.class, this); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/hibernate/src/test/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreFactorySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/hibernate/src/test/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreFactorySelfTest.java b/modules/hibernate/src/test/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreFactorySelfTest.java new file mode 100644 index 0000000..225fc11 --- /dev/null +++ b/modules/hibernate/src/test/java/org/apache/ignite/cache/store/hibernate/CacheHibernateStoreFactorySelfTest.java @@ -0,0 +1,66 @@ +/* + * 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.store.hibernate; + +import org.apache.ignite.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.testframework.*; +import org.apache.ignite.testframework.junits.common.*; + +/** + * Test for Cache jdbc blob store factory. + */ +public class CacheHibernateStoreFactorySelfTest extends GridCommonAbstractTest { + /** + * @throws Exception If failed. + */ + public void testCacheConfiguration() throws Exception { + try (Ignite ignite1 = startGrid(0)) { + IgniteCache cache1 = ignite1.getOrCreateCache(cacheConfiguration()); + + checkStore(cache1); + } + } + + /** + * @return Cache configuration with store. + */ + private CacheConfiguration cacheConfiguration() { + CacheConfiguration cfg = new CacheConfiguration<>(); + + CacheHibernateBlobStoreFactory factory = new CacheHibernateBlobStoreFactory(); + + factory.setHibernateConfigurationPath("/org/apache/ignite/cache/store/hibernate/hibernate.cfg.xml"); + + cfg.setCacheStoreFactory(factory); + + return cfg; + } + + /** + * @param cache Ignite cache. + * @throws Exception If store parameters is not the same as in configuration xml. + */ + private void checkStore(IgniteCache cache) throws Exception { + CacheHibernateBlobStore store = (CacheHibernateBlobStore)cache.getConfiguration(CacheConfiguration.class) + .getCacheStoreFactory().create(); + + assertEquals("/org/apache/ignite/cache/store/hibernate/hibernate.cfg.xml", + GridTestUtils.getFieldValue(store, CacheHibernateBlobStore.class, "hibernateCfgPath")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/hibernate/src/test/java/org/apache/ignite/testsuites/IgniteHibernateTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/hibernate/src/test/java/org/apache/ignite/testsuites/IgniteHibernateTestSuite.java b/modules/hibernate/src/test/java/org/apache/ignite/testsuites/IgniteHibernateTestSuite.java index 3ac5ec4..2965250 100644 --- a/modules/hibernate/src/test/java/org/apache/ignite/testsuites/IgniteHibernateTestSuite.java +++ b/modules/hibernate/src/test/java/org/apache/ignite/testsuites/IgniteHibernateTestSuite.java @@ -43,6 +43,8 @@ public class IgniteHibernateTestSuite extends TestSuite { suite.addTestSuite(CacheHibernateStoreSessionListenerSelfTest.class); + suite.addTestSuite(CacheHibernateStoreFactorySelfTest.class); + return suite; } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java b/modules/spring/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java deleted file mode 100644 index b33b3b6..0000000 --- a/modules/spring/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactory.java +++ /dev/null @@ -1,237 +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.store.jdbc; - -import org.apache.ignite.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.util.tostring.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.apache.ignite.resources.*; -import org.springframework.context.*; - -import javax.cache.configuration.*; -import javax.sql.*; - -/** - * {@link Factory} implementation for {@link CacheJdbcBlobStore}. - * - * Use this factory to pass {@link CacheJdbcBlobStore} to {@link CacheConfiguration}. - * - *

    Spring Example

    - *
     *
    - *     <bean id= "simpleDataSource" class="org.h2.jdbcx.JdbcDataSource"/>
    - *
    - *     <bean id="cache.jdbc.store"
    - *         class="org.apache.ignite.cache.store.jdbc.CacheJdbcBlobStore">
    - *         <property name="connectionUrl" value="jdbc:h2:mem:"/>
    - *         <property name="createTableQuery"
    - *             value="create table if not exists ENTRIES (key other, val other)"/>
    - *     </bean>
    - *
    - *     <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
    - *          ...
    - *          <property name="cacheConfiguration">
    - *               <list>
    - *                  <bean class="org.apache.ignite.configuration.CacheConfiguration">
    - *                      ...
    - *                      <property name="cacheStoreFactory">
    - *                          <bean class="org.apache.ignite.cache.store.jdbc.CacheJdbcBlobStoreFactory">
    - *                              <property name="user" value = "GridGain" />
    - *                              <property name="dataSourceBean" value = "simpleDataSource" />
    - *                          </bean>
    - *                      </property>
    - *                  </bean>
    - *               </list>
    - *          </property>
    - *     </bean>
    - * 
    - *

    - * - *
    - * For information about Spring framework visit www.springframework.org - */ -public class CacheJdbcBlobStoreFactory implements Factory> { - /** Connection URL. */ - private String connUrl = CacheJdbcBlobStore.DFLT_CONN_URL; - - /** Query to create table. */ - private String createTblQry = CacheJdbcBlobStore.DFLT_CREATE_TBL_QRY; - - /** Query to load entry. */ - private String loadQry = CacheJdbcBlobStore.DFLT_LOAD_QRY; - - /** Query to update entry. */ - private String updateQry = CacheJdbcBlobStore.DFLT_UPDATE_QRY; - - /** Query to insert entries. */ - private String insertQry = CacheJdbcBlobStore.DFLT_INSERT_QRY; - - /** Query to delete entries. */ - private String delQry = CacheJdbcBlobStore.DFLT_DEL_QRY; - - /** User name for database access. */ - private String user; - - /** Password for database access. */ - @GridToStringExclude - private String passwd; - - /** Flag for schema initialization. */ - private boolean initSchema = true; - - /** Name of data source bean. */ - private String dataSrcBean; - - /** Application context. */ - @SpringApplicationContextResource - private ApplicationContext appContext; - - /** {@inheritDoc} */ - @Override public CacheJdbcBlobStore create() { - CacheJdbcBlobStore store = new CacheJdbcBlobStore<>(); - - store.setInitSchema(initSchema); - store.setConnectionUrl(connUrl); - store.setCreateTableQuery(createTblQry); - store.setLoadQuery(loadQry); - store.setUpdateQuery(updateQry); - store.setInsertQuery(insertQry); - store.setDeleteQuery(delQry); - store.setUser(user); - store.setPassword(passwd); - - if (dataSrcBean != null) { - if (appContext == null) - throw new IgniteException("Spring application context resource is not injected."); - - if (!appContext.containsBean(dataSrcBean)) - throw new IgniteException("Failed to find bean in application context [beanName=" + dataSrcBean + - ", appContext=" + appContext + ']'); - - DataSource data = (DataSource)appContext.getBean(dataSrcBean); - - store.setDataSource(data); - } - - return store; - } - - /** - * Flag indicating whether DB schema should be initialized by Ignite (default behaviour) or - * was explicitly created by user. - * - * @param initSchema Initialized schema flag. - * @return {@code This} for chaining. - * @see CacheJdbcBlobStore#setInitSchema(boolean) - */ - public CacheJdbcBlobStoreFactory setInitSchema(boolean initSchema) { - this.initSchema = initSchema; - - return this; - } - - /** - * Sets connection URL. - * - * @param connUrl Connection URL. - * @see CacheJdbcBlobStore#setConnectionUrl(String) - */ - public void setConnectionUrl(String connUrl) { - this.connUrl = connUrl; - } - - /** - * See {@link CacheJdbcBlobStore#setCreateTableQuery(String)}. - * - * @param createTblQry Create table query. - */ - public void setCreateTableQuery(String createTblQry) { - this.createTblQry = createTblQry; - } - - /** - * See {@link CacheJdbcBlobStore#setLoadQuery(String)}. - * - * @param loadQry Load query - */ - public void setLoadQuery(String loadQry) { - this.loadQry = loadQry; - } - - /** - * See {@link CacheJdbcBlobStore#setUpdateQuery(String)}. - * - * @param updateQry Update entry query. - */ - public void setUpdateQuery(String updateQry) { - this.updateQry = updateQry; - } - - /** - * See {@link CacheJdbcBlobStore#setInsertQuery(String)}. - * - * @param insertQry Insert entry query. - */ - public void setInsertQuery(String insertQry) { - this.insertQry = insertQry; - } - - /** - * See {@link CacheJdbcBlobStore#setDeleteQuery(String)}. - * - * @param delQry Delete entry query. - */ - public void setDeleteQuery(String delQry) { - this.delQry = delQry; - } - - /** - * See {@link CacheJdbcBlobStore#setUser(String)}. - * - * @param user User name. - */ - public void setUser(String user) { - this.user = user; - } - - /** - * See {@link CacheJdbcBlobStore#setPassword(String)}. - * - * @param passwd Password. - */ - public void setPassword(String passwd) { - this.passwd = passwd; - } - - /** - * Sets name of the data source bean. - * - * See {@link CacheJdbcBlobStore#setDataSource(DataSource)} - * for more information. - * - * @param dataSrcBean Data source bean name. - */ - public void setDataSourceBean(String dataSrcBean) { - this.dataSrcBean = dataSrcBean; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(CacheJdbcBlobStoreFactory.class, this); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/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 2c7c7e1..cbb200d 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 @@ -192,6 +192,23 @@ public class IgniteSpringHelperImpl implements IgniteSpringHelper { } } + /** {@inheritDoc} */ + @Override public T loadBeanFromAppContext(Object appContext, String beanName) throws IgniteCheckedException { + ApplicationContext springCtx = (ApplicationContext)appContext; + + try { + return (T)springCtx.getBean(beanName); + } + catch (NoSuchBeanDefinitionException e) { + throw new IgniteCheckedException("Spring bean with provided name doesn't exist " + + ", beanName=" + beanName + ']'); + } + catch (BeansException e) { + throw new IgniteCheckedException("Failed to load Spring bean with provided name " + + ", beanName=" + beanName + ']', e); + } + } + /** * @param stream Input stream containing Spring XML configuration. * @return Context. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/test/config/incorrect-store-cache.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/config/incorrect-store-cache.xml b/modules/spring/src/test/config/incorrect-store-cache.xml index 9a0b7c6..2ccda21 100644 --- a/modules/spring/src/test/config/incorrect-store-cache.xml +++ b/modules/spring/src/test/config/incorrect-store-cache.xml @@ -43,7 +43,7 @@ - + 127.0.0.1:47500..47509 http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/test/config/pojo-incorrect-store-cache.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/config/pojo-incorrect-store-cache.xml b/modules/spring/src/test/config/pojo-incorrect-store-cache.xml new file mode 100644 index 0000000..5627bd0 --- /dev/null +++ b/modules/spring/src/test/config/pojo-incorrect-store-cache.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 127.0.0.1:47500..47509 + + + + + + + + http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/test/config/store-cache.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/config/store-cache.xml b/modules/spring/src/test/config/store-cache.xml index 992a22c..07a4ab0 100644 --- a/modules/spring/src/test/config/store-cache.xml +++ b/modules/spring/src/test/config/store-cache.xml @@ -45,7 +45,7 @@ - + 127.0.0.1:47500..47509 http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/test/config/store-cache1.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/config/store-cache1.xml b/modules/spring/src/test/config/store-cache1.xml index 9209e52..bff88e9 100644 --- a/modules/spring/src/test/config/store-cache1.xml +++ b/modules/spring/src/test/config/store-cache1.xml @@ -48,7 +48,7 @@ - + 127.0.0.1:47500..47509 http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactorySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactorySelfTest.java b/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactorySelfTest.java index 4a693a7..6d3d3fd 100644 --- a/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactorySelfTest.java +++ b/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcBlobStoreFactorySelfTest.java @@ -78,7 +78,7 @@ public class CacheJdbcBlobStoreFactorySelfTest extends GridCommonAbstractTest { } return null; } - }, IgniteException.class, "Cannot find bean in application context."); + }, IgniteException.class, "Failed to load bean in application context"); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactorySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactorySelfTest.java b/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactorySelfTest.java new file mode 100644 index 0000000..7b709d5 --- /dev/null +++ b/modules/spring/src/test/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStoreFactorySelfTest.java @@ -0,0 +1,194 @@ +/* + * 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.store.jdbc; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.jdbc.dialect.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.testframework.*; +import org.apache.ignite.testframework.junits.common.*; +import org.h2.jdbcx.*; +import sun.jdbc.odbc.ee.*; + +import javax.cache.*; +import java.io.*; +import java.util.*; +import java.util.concurrent.*; + +/** + * Test for Cache jdbc blob store factory. + */ +public class CacheJdbcPojoStoreFactorySelfTest extends GridCommonAbstractTest { + /** Cache name. */ + private static final String CACHE_NAME = "test"; + + /** + * @throws Exception If failed. + */ + public void testCacheConfiguration() throws Exception { + try (Ignite ignite = Ignition.start("modules/spring/src/test/config/node.xml")) { + try (Ignite ignite1 = Ignition.start("modules/spring/src/test/config/node1.xml")) { + try (IgniteCache cache = ignite.getOrCreateCache(cacheConfiguration())) { + try (IgniteCache cache1 = ignite1.getOrCreateCache(cacheConfiguration())) { + checkStore(cache, JdbcDataSource.class); + + checkStore(cache1, ConnectionPoolDataSource.class); + } + } + } + } + } + + /** + * @throws Exception If failed. + */ + public void testSerializable() throws Exception { + GridTestUtils.assertThrows(log, new Callable() { + @Override public Object call() throws Exception { + try (Ignite ignite = Ignition.start("modules/spring/src/test/config/node.xml")) { + try (IgniteCache cache = ignite.getOrCreateCache(cacheConfigurationH2Dialect())) { + checkStore(cache, JdbcDataSource.class); + } + } + + return null; + } + }, CacheException.class, "Failed to validate cache configuration. Cache store factory is not serializable."); + } + + /** + * @throws Exception If failed. + */ + public void testIncorrectBeanConfiguration() throws Exception { + GridTestUtils.assertThrows(log, new Callable() { + @Override public Object call() throws Exception { + try(Ignite ignite = Ignition.start("modules/spring/src/test/config/pojo-incorrect-store-cache.xml")) { + ignite.cache(CACHE_NAME).getConfiguration(CacheConfiguration.class). + getCacheStoreFactory().create(); + } + return null; + } + }, IgniteException.class, "Failed to load bean in application context"); + } + + /** + * @return Cache configuration with store. + */ + private CacheConfiguration cacheConfiguration() { + CacheConfiguration cfg = new CacheConfiguration<>(); + + CacheJdbcPojoStoreFactory factory = new CacheJdbcPojoStoreFactory<>(); + + factory.setDataSourceBean("simpleDataSource"); + + factory.setDialect(new DummyDialect()); + + cfg.setCacheStoreFactory(factory); + + return cfg; + } + + /** + * @return Cache configuration with store. + */ + private CacheConfiguration cacheConfigurationH2Dialect() { + CacheConfiguration cfg = new CacheConfiguration<>(); + + CacheJdbcPojoStoreFactory factory = new CacheJdbcPojoStoreFactory<>(); + + factory.setDataSourceBean("simpleDataSource"); + + factory.setDialect(new H2Dialect()); + + cfg.setCacheStoreFactory(factory); + + return cfg; + } + + /** + * @param cache Ignite cache. + * @param dataSrcClass Data source class. + * @throws Exception If store parameters is not the same as in configuration xml. + */ + private void checkStore(IgniteCache cache, Class dataSrcClass) throws Exception { + CacheJdbcPojoStore store = (CacheJdbcPojoStore)cache.getConfiguration(CacheConfiguration.class). + getCacheStoreFactory().create(); + + assertEquals(dataSrcClass, + GridTestUtils.getFieldValue(store, CacheAbstractJdbcStore.class, "dataSrc").getClass()); + } + + /** + * + */ + public static class DummyDialect implements JdbcDialect, Serializable { + /** {@inheritDoc} */ + @Override public String loadCacheSelectRangeQuery(String fullTblName, Collection keyCols) { + return null; + } + + /** {@inheritDoc} */ + @Override public String loadCacheRangeQuery(String fullTblName, Collection keyCols, + Iterable uniqCols, boolean appendLowerBound, boolean appendUpperBound) { + return null; + } + + /** {@inheritDoc} */ + @Override public String loadCacheQuery(String fullTblName, Iterable uniqCols) { + return null; + } + + /** {@inheritDoc} */ + @Override public String loadQuery(String fullTblName, Collection keyCols, Iterable cols, + int keyCnt) { + return null; + } + + /** {@inheritDoc} */ + @Override public String insertQuery(String fullTblName, Collection keyCols, + Collection valCols) { + return null; + } + + /** {@inheritDoc} */ + @Override public String updateQuery(String fullTblName, Collection keyCols, Iterable valCols) { + return null; + } + + /** {@inheritDoc} */ + @Override public boolean hasMerge() { + return false; + } + + /** {@inheritDoc} */ + @Override public String mergeQuery(String fullTblName, Collection keyCols, + Collection uniqCols) { + return null; + } + + /** {@inheritDoc} */ + @Override public String removeQuery(String fullTblName, Iterable keyCols) { + return null; + } + + /** {@inheritDoc} */ + @Override public int getMaxParameterCount() { + return 0; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/a7ca45bb/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java index 12dd494..0c2e99e 100644 --- a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java +++ b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java @@ -18,6 +18,7 @@ package org.apache.ignite.testsuites; import junit.framework.*; +import org.apache.ignite.cache.store.jdbc.*; import org.apache.ignite.cache.store.spring.*; import org.apache.ignite.internal.*; import org.apache.ignite.p2p.*; @@ -50,6 +51,10 @@ public class IgniteSpringTestSuite extends TestSuite { suite.addTestSuite(CacheSpringStoreSessionListenerSelfTest.class); + suite.addTestSuite(CacheJdbcBlobStoreFactorySelfTest.class); + + suite.addTestSuite(CacheJdbcPojoStoreFactorySelfTest.class); + return suite; } }