Author: navssurtani
Date: 2008-06-30 12:42:15 -0400 (Mon, 30 Jun 2008)
New Revision: 6126
Added:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheCfgImpl.java
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java
Log:
Implemented Cfg interface from org.hibernate.search.Cfg
Added: searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheCfgImpl.java
===================================================================
--- searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheCfgImpl.java
(rev 0)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheCfgImpl.java 2008-06-30
16:42:15 UTC (rev 6126)
@@ -0,0 +1,64 @@
+package org.jboss.cache.search;
+
+import org.hibernate.search.cfg.Cfg;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.RootClass;
+
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Class that implements {@link org.hibernate.search.cfg.Cfg} so that within JBossCache
Searchable, there is no
+ * need for a Hibernate Core configuration object.
+ *
+ * @author Navin Surtani - navin(a)surtani.org
+ */
+public class SearchableCacheCfgImpl implements Cfg
+{
+ protected Map<String, PersistentClass> classes;
+ private Properties properties;
+
+ public SearchableCacheCfgImpl(Class[] classArray, Properties properties)
+ {
+ // null chks
+ this.properties = properties;
+ if (this.properties == null) this.properties = new Properties();
+
+ classes = new HashMap<String, PersistentClass>();
+
+ // loop thru your classArray
+ // populate your Map
+ // you bog
+
+ for (int i = 0; i <= classArray.length; i++)
+ {
+ String classname = classArray[i].getName();
+ RootClass rc = new RootClass();
+ rc.setClassName(classname);
+ classes.put(classname, rc);
+ }
+ }
+
+ public Iterator getClassMappings()
+ {
+ return classes.values().iterator();
+ }
+
+ public PersistentClass getClassMapping(String name)
+ {
+ return classes.get(name);
+ }
+
+ public String getProperty(String propertyName)
+ {
+ return properties.getProperty(propertyName);
+ }
+
+ public Properties getProperties()
+ {
+ return properties;
+ }
+
+}
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java
===================================================================
---
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java 2008-06-30
13:36:04 UTC (rev 6125)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/SearchableCacheFactory.java 2008-06-30
16:42:15 UTC (rev 6126)
@@ -2,8 +2,11 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.search.impl.SearchFactoryImpl;
+import org.hibernate.search.cfg.Cfg;
import org.jboss.cache.Cache;
import org.jboss.cache.CacheStatus;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import java.util.Properties;
@@ -12,14 +15,13 @@
*/
public class SearchableCacheFactory
{
- private SearchFactoryImpl searchFactory;
-
+ private static final Log log = LogFactory.getLog(SearchableCacheFactory.class);
/**
* Creates a searchable cache from a cache object and a class array, without the
properties object.
*
- * @param c
- * @param classes
- * @return
+ * @param c - the Cache
+ * @param classes - Class array to be added
+ * @return a SearchableCache
*/
public SearchableCache createSearchableCache(Cache<?, ?> c, Class... classes)
{
@@ -29,29 +31,33 @@
/**
* This method creates a searchable cache as well but requires the properties object.
*
- * @param c
- * @param properties
- * @param classes
- * @return
+ * @param c - the Cache
+ * @param properties - java.util.properties
+ * @param classes - a class array
+ * @return a SearchableCache
*/
public SearchableCache createSearchableCache(Cache<?, ?> c, Properties
properties, Class... classes)
{
// assume cache is already created and running.
// otherwise, start the cache!!
- if (c.getCacheStatus() != CacheStatus.STARTED) c.start();
+ if (c.getCacheStatus() != CacheStatus.STARTED)
+ {
+ if (log.isInfoEnabled()) log.info("Cache not started. Starting cache
first.");
+ c.start();
+ }
+ if (classes.length == 0)
+ {
+ if (log.isWarnEnabled()) log.warn("You haven't passed in any classes to
index. Is this an error?");
+ }
+
// Step 1: Add cache listener to listen for events happening in the cache.
SearchableListener listener = new SearchableListener();
c.addCacheListener(listener);
// step 2: create hibernate search searchImpl
- Configuration cfg = new Configuration();
- if (properties != null) cfg.setProperties(properties);
+ Cfg cfg = new SearchableCacheCfgImpl(classes, properties);
// set classes in the cfg
- if (classes != null)
- {
- for (Class clazz : classes) cfg.addClass(clazz);
- }
SearchFactoryImpl searchImpl = new SearchFactoryImpl(cfg);
Show replies by date