diff --git a/.classpath b/.classpath index ee649b3c..2351c6f7 100644 --- a/.classpath +++ b/.classpath @@ -13,10 +13,10 @@ + - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 3531c59f..fcea869f 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,5 @@ # -#Fri Aug 19 11:36:57 EEST 2011 +#Fri Sep 16 13:59:45 EEST 2011 org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.compliance=1.5 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve diff --git a/src/main/java/org/springframework/data/gemfire/IndexFactoryBean.java b/src/main/java/org/springframework/data/gemfire/IndexFactoryBean.java new file mode 100644 index 00000000..a46efdc1 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/IndexFactoryBean.java @@ -0,0 +1,165 @@ +/* + * 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; + +import java.util.Collection; + +import org.springframework.beans.factory.BeanNameAware; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +import com.gemstone.gemfire.cache.RegionService; +import com.gemstone.gemfire.cache.client.Pool; +import com.gemstone.gemfire.cache.client.PoolManager; +import com.gemstone.gemfire.cache.query.Index; +import com.gemstone.gemfire.cache.query.IndexType; +import com.gemstone.gemfire.cache.query.QueryService; + +/** + * Factory bean for easy declarative creation of GemFire Indexes. + * + * @author Costin Leau + */ +public class IndexFactoryBean implements InitializingBean, BeanNameAware, FactoryBean { + + private Index index; + private QueryService queryService; + private String poolName; + private RegionService cache; + private String beanName; + private String name, expression, from, imports; + private IndexType type; + + public void afterPropertiesSet() throws Exception { + if (queryService == null) { + if (cache != null) { + queryService = cache.getQueryService(); + } + } + + if (queryService != null && StringUtils.hasText(poolName)) { + Pool pool = PoolManager.find(poolName); + Assert.notNull(pool, "No pool named [" + poolName + "] found"); + queryService = pool.getQueryService(); + } + + Assert.notNull(queryService, "Query service required for index creation"); + + String indexName = StringUtils.hasText(name) ? name : beanName; + index = createIndex(queryService, indexName); + } + + private Index createIndex(QueryService queryService, String indexName) throws Exception { + Collection indexes = queryService.getIndexes(); + for (Index index : indexes) { + if (indexName.equals(index.getName())) { + return index; + } + } + + Index index = null; + + if (StringUtils.hasText(imports)) { + index = queryService.createIndex(indexName, type, expression, from, imports); + } + else { + index = queryService.createIndex(indexName, type, expression, from); + } + + return index; + } + + public Index getObject() { + return index; + } + + public Class getObjectType() { + return (index != null ? index.getClass() : Index.class); + } + + public boolean isSingleton() { + return true; + } + + /** + * Sets the underlying cache used for creating indexes. + * + * @param cache cache used for creating indexes. + */ + public void setCache(RegionService cache) { + this.queryService = cache.getQueryService(); + } + + /** + * Sets the query service used for creating indexes. + * + * @param service query service used for creating indexes. + */ + public void setQueryService(QueryService service) { + this.queryService = service; + } + + /** + * Sets the beanName of the {@link Pool} used for used for creating indexes. + * + * @param poolName the beanName of the pool used for creating indexes. + */ + public void setPoolName(String poolName) { + this.poolName = poolName; + } + + public void setBeanName(String name) { + this.beanName = name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @param expression the expression to set + */ + public void setExpression(String expression) { + this.expression = expression; + } + + /** + * @param from the from to set + */ + public void setFrom(String from) { + this.from = from; + } + + /** + * @param imports the imports to set + */ + public void setImports(String imports) { + this.imports = imports; + } + + /** + * @param type the type to set + */ + public void setType(IndexType type) { + this.type = type; + } +} \ No newline at end of file diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd index 9ad8f8b6..97bf572d 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd @@ -1138,4 +1138,43 @@ Whether the resulting GemFire continuous query is durable or not. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file