private void buildFilters()
{
if (filterDefinitions != null && filterDefinitions.size() > 0)
{
ChainedFilter chainedFilter = new ChainedFilter();
for (FullTextFilterImpl filterDefinition : filterDefinitions.values())
{
FilterDef def = searchFactory.getFilterDefinition(filterDefinition.getName());
Class implClass = def.getImpl();
Object instance;
try
{
instance = implClass.newInstance();
}
catch (Exception e)
{
throw new SearchException("Unable to create @FullTextFilterDef: " + def.getImpl(), e);
}
for (Map.Entry<String, Object> entry : filterDefinition.getParameters().entrySet())
{
def.invoke(entry.getKey(), instance, entry.getValue());
}
if (def.isCache() && def.getKeyMethod() == null && filterDefinition.getParameters().size() > 0)
{
throw new SearchException("Filter with parameters and no @Key method: " + filterDefinition.getName());
}
FilterKey key = null;
if (def.isCache())
{
if (def.getKeyMethod() == null)
{
key = new FilterKey()
{
public int hashCode()
{
return getImpl().hashCode();
}
public boolean equals(Object obj)
{
if (!(obj instanceof FilterKey)) return false;
FilterKey that = (FilterKey) obj;
return this.getImpl().equals(that.getImpl());
}
};
}
else
{
try
{
key = (FilterKey) def.getKeyMethod().invoke(instance);
}
catch (IllegalAccessException e)
{
throw new SearchException("Unable to access @Key method: "
+ def.getImpl().getName() + "." + def.getKeyMethod().getName());
}
catch (InvocationTargetException e)
{
throw new SearchException("Unable to access @Key method: "
+ def.getImpl().getName() + "." + def.getKeyMethod().getName());
}
catch (ClassCastException e)
{
throw new SearchException("@Key method does not return FilterKey: "
+ def.getImpl().getName() + "." + def.getKeyMethod().getName());
}
}
key.setImpl(def.getImpl());
}
Filter filter = def.isCache() ?
searchFactory.getFilterCachingStrategy().getCachedFilter(key) :
null;
if (filter == null)
{
if (def.getFactoryMethod() != null)
{
try
{
filter = (Filter) def.getFactoryMethod().invoke(instance);
}
catch (IllegalAccessException e)
{
throw new SearchException("Unable to access @Factory method: "
+ def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
catch (InvocationTargetException e)
{
throw new SearchException("Unable to access @Factory method: "
+ def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
catch (ClassCastException e)
{
throw new SearchException("@Key method does not return a org.apache.lucene.search.Filter class: "
+ def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
}
else
{
try
{
filter = (Filter) instance;
}
catch (ClassCastException e)
{
throw new SearchException("@Key method does not return a org.apache.lucene.search.Filter class: "
+ def.getImpl().getName() + "." + def.getFactoryMethod().getName());
}
}
if (def.isCache())
{
searchFactory.getFilterCachingStrategy().addCachedFilter(key, filter);
}
}
chainedFilter.addFilter(filter);
}
if (filter != null) chainedFilter.addFilter(filter);
filter = chainedFilter;
}
}