Java tutorial
/** * Copyright (C) 2008-2010, Squale Project - http://www.squale.org * * This file is part of Squale. * * Squale is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or any later version. * * Squale is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Squale. If not, see <http://www.gnu.org/licenses/>. */ /* * Cr le 16 fvr. 06 * * Pour changer le modle de ce fichier gnr, allez : * Fentre>Prfrences>Java>Gnration de code>Code et commentaires */ package org.squale.jraf.bootstrap; import javax.servlet.ServletContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.squale.jraf.commons.exception.JrafIllegalStateException; /** * @author DIKITE Tidiani * Cette classe a pour rle d'initialiser le context spring. * L'initialisation du context n'est effectue qu'une fois, sauf * dans le cadre de l'excution de tests unitaires JUNIT. * Voir les commentaires sur des mthodes pour plus d'informations. * */ public class ApplicationContextFactoryInitializer { /** logger */ private static final Log log = LogFactory.getLog(ApplicationContextFactoryInitializer.class); /**Objet initialiser.*/ private static Object initObject = null; /**La limite d'init. Une seule initialisation est permise dans un contexte * autre que cleui des tests unitaires. */ private static int limite = 0; /** * Initialise l'application contexte. * @param object - Object */ public static void init(Object object) { // Initialisation du contexte dans le cadre de tests unitaires. // Dans ce cas de figure, le contexte peut tre initialis // plusieurs fois. if (object.equals("applicationTest.xml")) { log.debug("Initialisation du context spring dans un cas de tests unitaires ..."); initObject = object; } else {// Cas de figure o l'application est excute dans un contexte autre // que celui des tests unitaires. if (limite > 0) { throw new JrafIllegalStateException( "Impossible d'initialiser l'Application context une seconde fois. ELle est dj initialise !"); } initObject = object; limite++; } } /** * Retourne l'application context selon le type d'environnement * d'excution. * @return ApplicationCOntext. */ public static ApplicationContext getApplicationContext() { if (initObject == null) { throw new JrafIllegalStateException("Application context n'est pas initialise ..."); // Test si l'instance de l'objet pass en paramtre est // un ServletContext. } else if (initObject instanceof ServletContext) { log.debug("Dbut d'initialisation du fichier de configuration spring en mode servlet."); ServletContext servletContext = (ServletContext) initObject; return WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); // Test si l'instance de l'objet est une chaine de caractre. } else if (initObject instanceof String) { log.debug("Initialisation du fichier de configuration spring : " + initObject); String contextResourceLocation = (String) initObject; ApplicationContext applicationContext = null; try { applicationContext = new ClassPathXmlApplicationContext(contextResourceLocation); } catch (BeansException e) { e.printStackTrace(); throw e; } return applicationContext; } else { // Lancement de l'exception. throw new JrafIllegalStateException( "Le contexte doit tre initialis avec un String ou un ServletContext ."); } } }