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.context; 017 018import java.util.Date; 019 020import org.apache.log4j.Logger; 021import org.springframework.beans.BeansException; 022import org.springframework.context.support.ClassPathXmlApplicationContext; 023 024import core.tut.pori.dao.SQLDAO; 025import core.tut.pori.dao.SolrDAO; 026import core.tut.pori.utils.StringUtils; 027 028/** 029 * The DAO handler. 030 * 031 * This class can be used to retrieve instances of the DAOs defined by the database context configuration and sub-classed DAO class implementations. 032 * 033 */ 034public class DAOHandler { 035 private static final String DATABASE_CONFIGURATION_FILE = "database-context.xml"; 036 private static final Logger LOGGER = Logger.getLogger(DAOHandler.class); 037 private ClassPathXmlApplicationContext _context = null; 038 039 /** 040 * 041 * @throws BeansException 042 */ 043 public DAOHandler() throws BeansException{ 044 initialize(); 045 } 046 047 /** 048 * 049 * @throws BeansException 050 */ 051 private void initialize() throws BeansException{ 052 LOGGER.debug("Initializing handler..."); 053 Date started = new Date(); 054 _context = new ClassPathXmlApplicationContext(core.tut.pori.properties.SystemProperty.CONFIGURATION_FILE_PATH+DATABASE_CONFIGURATION_FILE); 055 056 LOGGER.debug("DAO Handler initialized in "+StringUtils.getDurationString(started, new Date())); 057 } 058 059 /** 060 * close the handler and release all resources 061 */ 062 public void close(){ 063 if(_context != null){ 064 _context.close(); 065 _context = null; 066 } 067 } 068 069 /** 070 * Note: the comparison is done using exactly the given class, no super or sub class of the type will be returned. 071 * 072 * Do NOT close or cleanup the instances returned by this method, the initialization and destruction is handled automatically. 073 * 074 * @param cls 075 * @return the dao or null if none exists 076 */ 077 public <T extends SQLDAO> T getSQLDAO(Class<T> cls){ 078 try{ 079 for(T candidate : _context.getBeansOfType(cls).values()){ 080 if(candidate.getClass().equals(cls)){ 081 return candidate; 082 } 083 } 084 } catch (BeansException ex){ 085 LOGGER.warn(ex, ex); 086 } 087 return null; 088 } 089 090 /** 091 * Note: the comparison is done using exactly the given class, no super or sub class of the type will be returned. 092 * 093 * Do NOT close or cleanup the instances returned by this method, the initialization and destruction is handled automatically. 094 * 095 * @param cls 096 * @return the dao or null if none exists 097 */ 098 public <T extends SolrDAO> T getSolrDAO(Class<T> cls){ 099 try{ 100 for(T candidate : _context.getBeansOfType(cls).values()){ 101 if(candidate.getClass().equals(cls)){ 102 return candidate; 103 } 104 } 105 } catch (BeansException ex){ 106 LOGGER.warn(ex, ex); 107 } 108 return null; 109 } 110}