001/** 002 * Copyright 2014 Tampere University of Technology, Pori Department 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package core.tut.pori.dao; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Map.Entry; 021 022import org.apache.log4j.Logger; 023import org.apache.solr.client.solrj.SolrClient; 024import org.springframework.beans.BeansException; 025import org.springframework.context.ApplicationContext; 026import org.springframework.context.ApplicationContextAware; 027 028/** 029 * Base class for SOLR DAOs. 030 * 031 * Subclassing this class will automatically add the new class to DAOHandler, and will be retrievable 032 * run-time from ServiceInitializer.getDAOHandler().getSolrDAO(...) 033 * 034 */ 035public abstract class SolrDAO implements ApplicationContextAware{ 036 /** Specifies time to wait in milliseconds before invoking "soft commit" for Solr index. */ 037 public static final int SOLR_COMMIT_WITHIN = 1000; 038 /** Maximum document count for SOLR queries */ 039 public static final int MAX_DOCUMENT_COUNT = Integer.MAX_VALUE; 040 /** default value for solr id field */ 041 public static final String SOLR_FIELD_ID = "id"; 042 private Map<String, SimpleSolrTemplate> _templates = null; 043 044 /** 045 * 046 * @param beanId 047 * @return the SimpleSolrTemplate for the requested beanId or null if not found 048 */ 049 public SimpleSolrTemplate getSolrTemplate(String beanId){ 050 return (_templates == null ? null : _templates.get(beanId)); 051 } 052 053 @Override 054 public void setApplicationContext(ApplicationContext context) throws BeansException { 055 Map<String, SolrClient> map = context.getBeansOfType(SolrClient.class); 056 int size = map.size(); 057 Logger.getLogger(SolrDAO.class).debug("Found: "+size+" SolrServers for "+getClass().toString()); 058 if(size > 0){ 059 _templates = new HashMap<>(size); 060 for(Entry<String, SolrClient> e : map.entrySet()){ 061 _templates.put(e.getKey(), new SimpleSolrTemplate(e.getValue())); // create new templates for each dao instance, but share the same solrserver instance 062 } 063 } // if 064 } 065}