LogReaderFactory.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.  * LogReaderFactory
  25.  *
  26.  * @author Poli Andrea (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class LogReaderFactory {

  31.     private static Class<ILogReader> 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<ILogReader> c = (Class<ILogReader>) Class.forName(implementationClassName);
  38.         initialize(c, pars);
  39.     }
  40.     public static void initialize(Class<ILogReader> implementationClass, Object ... pars) throws UtilsException {
  41.         try{
  42.             loggerImpl = (Class<ILogReader>)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 "+ILogReader.class.getName()+". Found: "+implementationClass.getName()+" . Error: "+e.getMessage(),e);
  52.         }
  53.     }
  54.    
  55.     public static ILogReader newReader() throws UtilsException{
  56.        
  57.         if(loggerImpl==null){
  58.             throw new UtilsException("LogReaderFactory not Initialized");
  59.         }
  60.        
  61.         try{
  62.             Constructor<ILogReader> c = loggerImpl.getConstructor(parameterTypes);
  63.             ILogReader reader = (ILogReader) c.newInstance(parameters);
  64.             reader.initReader();
  65.             return reader;
  66.         }catch(Exception e){
  67.             throw new UtilsException(e.getMessage(),e);
  68.         }
  69.     }
  70. }