| /* |
| * 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 java.util.Properties; |
| import javax.cache.configuration.Factory; |
| import org.apache.ignite.IgniteCheckedException; |
| import org.apache.ignite.IgniteException; |
| import org.apache.ignite.configuration.CacheConfiguration; |
| import org.apache.ignite.internal.IgniteComponentType; |
| import org.apache.ignite.internal.util.spring.IgniteSpringHelper; |
| import org.apache.ignite.internal.util.tostring.GridToStringExclude; |
| import org.apache.ignite.internal.util.typedef.internal.S; |
| import org.apache.ignite.resources.SpringApplicationContextResource; |
| import org.hibernate.SessionFactory; |
| |
| /** |
| * {@link Factory} implementation for {@link CacheHibernateBlobStore}. |
| * |
| * Use this factory to pass {@link CacheHibernateBlobStore} to {@link CacheConfiguration}. |
| * |
| * <h2 class="header">Java Example</h2> |
| * In this example existing session factory is provided. |
| * <pre name="code" class="java"> |
| * ... |
| * CacheHibernateBlobStoreFactory<String, String> factory = new CacheHibernateBlobStoreFactory<String, String>(); |
| * |
| * factory.setSessionFactory(sesFactory); |
| * ... |
| * </pre> |
| * |
| * <h2 class="header">Spring Example (using Spring ORM)</h2> |
| * <pre name="code" class="xml"> |
| * ... |
| * <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> |
| * ... |
| * </pre> |
| * |
| * <h2 class="header">Spring Example (using Spring ORM and persistent annotations)</h2> |
| * <pre name="code" class="xml"> |
| * ... |
| * <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> |
| * ... |
| * </pre> |
| * |
| * <h2 class="header">Spring Example</h2> |
| * <pre name="code" class="xml"> |
| * ... |
| * <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> |
| * ... |
| * </pre> |
| * <p> |
| * <img src="http://ignite.apache.org/images/spring-small.png"> |
| * <br> |
| * For information about Spring framework visit <a href="http://www.springframework.org/">www.springframework.org</a> |
| */ |
| public class CacheHibernateBlobStoreFactory<K, V> implements Factory<CacheHibernateBlobStore<K, V>> { |
| /** */ |
| private static final long serialVersionUID = 0L; |
| |
| /** Session factory. */ |
| @GridToStringExclude |
| private transient SessionFactory sesFactory; |
| |
| /** Session factory bean name. */ |
| private String sesFactoryBean; |
| |
| /** Path to hibernate configuration file. */ |
| private String hibernateCfgPath; |
| |
| /** Hibernate properties. */ |
| @GridToStringExclude |
| private Properties hibernateProps; |
| |
| /** Application context. */ |
| @SpringApplicationContextResource |
| private Object appContext; |
| |
| /** {@inheritDoc} */ |
| @Override public CacheHibernateBlobStore<K, V> create() { |
| CacheHibernateBlobStore<K, V> store = new CacheHibernateBlobStore<>(); |
| |
| store.setHibernateConfigurationPath(hibernateCfgPath); |
| store.setHibernateProperties(hibernateProps); |
| |
| if (sesFactory != null) |
| store.setSessionFactory(sesFactory); |
| else if (sesFactoryBean != null) { |
| if (appContext == null) |
| throw new IgniteException("Spring application context resource is not injected."); |
| |
| IgniteSpringHelper spring; |
| |
| try { |
| spring = IgniteComponentType.SPRING.create(false); |
| |
| SessionFactory sesFac = spring.loadBeanFromAppContext(appContext, sesFactoryBean); |
| |
| store.setSessionFactory(sesFac); |
| } |
| catch (IgniteCheckedException e) { |
| throw new IgniteException("Failed to load bean in application context [beanName=" + sesFactoryBean + |
| ", igniteConfig=" + appContext + ']'); |
| } |
| } |
| |
| return store; |
| } |
| |
| /** |
| * Sets session factory. |
| * |
| * @param sesFactory Session factory. |
| * @return {@code This} for chaining. |
| * @see CacheHibernateBlobStore#setSessionFactory(SessionFactory) |
| */ |
| public CacheHibernateBlobStoreFactory<K, V> setSessionFactory(SessionFactory sesFactory) { |
| this.sesFactory = sesFactory; |
| |
| return this; |
| } |
| |
| /** |
| * Sets name of the data source bean. |
| * |
| * @param sesFactory Session factory bean name. |
| * @return {@code This} for chaining. |
| * @see CacheHibernateBlobStore#setSessionFactory(SessionFactory) |
| */ |
| public CacheHibernateBlobStoreFactory<K, V> setSessionFactoryBean(String sesFactory) { |
| this.sesFactoryBean = sesFactory; |
| |
| return this; |
| } |
| |
| /** |
| * Sets hibernate configuration path. |
| * <p> |
| * 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. |
| * @return {@code This} for chaining. |
| * @see CacheHibernateBlobStore#setHibernateConfigurationPath(String) |
| */ |
| public CacheHibernateBlobStoreFactory<K, V> setHibernateConfigurationPath(String hibernateCfgPath) { |
| this.hibernateCfgPath = hibernateCfgPath; |
| |
| return this; |
| } |
| |
| /** |
| * Sets Hibernate properties. |
| * |
| * @param hibernateProps Hibernate properties. |
| * @return {@code This} for chaining. |
| * @see CacheHibernateBlobStore#setHibernateProperties(Properties) |
| */ |
| public CacheHibernateBlobStoreFactory<K, V> setHibernateProperties(Properties hibernateProps) { |
| this.hibernateProps = hibernateProps; |
| |
| return this; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public String toString() { |
| return S.toString(CacheHibernateBlobStoreFactory.class, this); |
| } |
| } |