LoggerFactory.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.logger;

  21. import java.lang.reflect.Constructor;

  22. import org.openspcoop2.utils.UtilsException;

  23. /**
  24.  * LoggerFactory
  25.  *
  26.  * @author Poli Andrea (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class LoggerFactory {

  31.     private static Class<ILogger> loggerImpl = null;
  32.     private static Object [] parameters = null;
  33.     private static Class<?> [] parameterTypes = null;
  34.    
  35.     @SuppressWarnings("unchecked")
  36.     public static void initialize(String implementationClassName, Object ... pars) throws UtilsException, ClassNotFoundException{
  37.         Class<ILogger> c = (Class<ILogger>) Class.forName(implementationClassName);
  38.         initialize(c, pars);
  39.     }
  40.     public static void initialize(Class<ILogger> implementationClass, Object ... pars) throws UtilsException {
  41.         try{
  42.             loggerImpl = (Class<ILogger>)implementationClass;
  43.             parameters = pars;
  44.             if(parameters!=null){
  45.                 parameterTypes = new Class<?>[parameters.length];
  46.                 for (int i = 0; i < parameters.length; i++) {
  47.                     parameterTypes[i] = parameters[i].getClass();
  48.                 }
  49.             }
  50.         }catch(Exception e){
  51.             throw new UtilsException("Expected class assignable from "+ILogger.class.getName()+". Found: "+implementationClass.getName()+" . Error: "+e.getMessage(),e);
  52.         }
  53.     }
  54.    
  55.     public static ILogger newLogger() throws UtilsException{
  56.         return _newLogger(null);
  57.     }
  58.     public static ILogger newLogger(IContext context) throws UtilsException{
  59.         return _newLogger(context);
  60.     }
  61.     private static ILogger _newLogger(IContext context) throws UtilsException{
  62.        
  63.         if(loggerImpl==null){
  64.             throw new UtilsException("LoggerFactory not Initialized");
  65.         }
  66.        
  67.         try{
  68.             Constructor<ILogger> c = loggerImpl.getConstructor(parameterTypes);
  69.             ILogger logger = (ILogger) c.newInstance(parameters);
  70.             if(context!=null){
  71.                 logger.initLogger(context);
  72.             }
  73.             else{
  74.                 logger.initLogger();
  75.             }
  76.             return logger;
  77.         }catch(Exception e){
  78.             throw new UtilsException(e.getMessage(),e);
  79.         }
  80.     }
  81.    
  82.     public static ILogger newLogger(String implementationClassName) throws UtilsException{
  83.         return _newLogger(implementationClassName, null);
  84.     }
  85.     public static ILogger newLogger(String implementationClassName, IContext context) throws UtilsException{
  86.         return _newLogger(implementationClassName, context);
  87.     }
  88.     @SuppressWarnings("unchecked")
  89.     private static ILogger _newLogger(String implementationClassName, IContext context) throws UtilsException{
  90.         Class<ILogger> c = null;
  91.         try {
  92.             c = (Class<ILogger>) Class.forName(implementationClassName);
  93.         }catch(Exception e){
  94.             throw new UtilsException("Expected class assignable from "+ILogger.class.getName()+". Found: "+implementationClassName+" . Error: "+e.getMessage(),e);
  95.         }
  96.         return _newLogger(c, context);
  97.     }
  98.    
  99.     public static ILogger newLogger(Class<? extends ILogger> implementationClass) throws UtilsException{
  100.         return _newLogger(implementationClass, null);
  101.     }
  102.     public static ILogger newLogger(Class<? extends ILogger> implementationClass, IContext context) throws UtilsException{
  103.         return _newLogger(implementationClass, context);
  104.     }
  105.     private static ILogger _newLogger(Class<? extends ILogger> implementationClass, IContext context) throws UtilsException{
  106.        
  107.         try{
  108.             Constructor<? extends ILogger> c = implementationClass.getConstructor(parameterTypes);
  109.             ILogger logger = (ILogger) c.newInstance(parameters);
  110.             if(context!=null){
  111.                 logger.initLogger(context);
  112.             }
  113.             else{
  114.                 logger.initLogger();
  115.             }
  116.             return logger;
  117.         }catch(Exception e){
  118.             throw new UtilsException(e.getMessage(),e);
  119.         }
  120.     }
  121. }