AbstractBaseDiagnosticManagerCore.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.io.File;
  22. import java.io.FileInputStream;
  23. import java.net.URI;
  24. import java.net.URL;
  25. import java.util.Properties;

  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.date.DateManager;
  28. import org.openspcoop2.utils.date.SystemDate;
  29. import org.openspcoop2.utils.logger.config.DiagnosticConfig;

  30. /**
  31.  * AbstractBaseDiagnosticManagerCore
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public abstract class AbstractBaseDiagnosticManagerCore  {

  38.     protected DiagnosticManager diagnosticManager;
  39.     private boolean throwExceptionPlaceholderFailedResolution;
  40.     private Properties diagnosticProperties;
  41.    
  42.     public AbstractBaseDiagnosticManagerCore(DiagnosticConfig diagnosticConfig) throws UtilsException{
  43.        
  44.         DiagnosticConfig.validate(diagnosticConfig); // garantisce che getThrowExceptionPlaceholderFailedResolution non torni null. e readProperties contenga una configurazione valida
  45.         try{
  46.            
  47.             this.diagnosticProperties = DiagnosticConfig.readProperties(diagnosticConfig);
  48.                        
  49.             this.throwExceptionPlaceholderFailedResolution = diagnosticConfig.getThrowExceptionPlaceholderFailedResolution();
  50.            
  51.         }catch(Exception e){
  52.             throw new UtilsException(e.getMessage(),e);
  53.         }
  54.     }

  55.     public void init(IContext context,ILogger loggerForCallback) throws UtilsException{
  56.        
  57.         try{
  58.            
  59.             DateManager.initializeDataManager(SystemDate.class.getName(), null, null);
  60.            
  61.             this.diagnosticManager = new DiagnosticManager(this.diagnosticProperties, context, this.throwExceptionPlaceholderFailedResolution, loggerForCallback);
  62.            
  63.         }catch(Exception e){
  64.             throw new UtilsException(e.getMessage(),e);
  65.         }
  66.     }
  67.    
  68.    
  69.     // ---- STATIC
  70.    
  71.     public static Properties getProperties(File file) throws UtilsException{
  72.         String filePath = "fs";
  73.         try{
  74.             if(file==null){
  75.                 throw new Exception("Resource file undefined");
  76.             }
  77.             filePath = file.getAbsolutePath();
  78.             if(file.exists()){
  79.                 Properties p = new Properties();
  80.                 FileInputStream fin = null;
  81.                 try{
  82.                     fin = new FileInputStream(file);
  83.                     p.load(fin);
  84.                 }finally{
  85.                     try{
  86.                         if(fin!=null) {
  87.                             fin.close();
  88.                         }
  89.                     }catch(Exception eClose){
  90.                         // ignore
  91.                     }
  92.                 }
  93.                 return p;
  94.             }
  95.             else{
  96.                 throw new UtilsException("Resource not exists");
  97.             }
  98.         }catch(Exception e){
  99.             throw new UtilsException("Reading Properties failed (resource ["+filePath+"]): "+e.getMessage(),e);
  100.         }
  101.     }
  102.     public static Properties getProperties(String name) throws UtilsException{
  103.         try{
  104.             if(name==null){
  105.                 throw new Exception("Resource name undefined");
  106.             }
  107.             File f = new File(name);
  108.             if(f.exists()){
  109.                 return getProperties(f);
  110.             }
  111.             else{
  112.                 String newName = null;
  113.                 if(name.trim().startsWith("/")){
  114.                     newName = name;
  115.                 }
  116.                 else{
  117.                     newName = "/" + name;
  118.                 }
  119.                 URL url = AbstractBaseDiagnosticManagerCore.class.getResource(newName);
  120.                 if(url!=null){
  121.                     return getProperties(url);
  122.                 }
  123.                 else{
  124.                     throw new UtilsException("Resource ["+name+"] not found");
  125.                 }
  126.             }
  127.         }catch(Exception e){
  128.             throw new UtilsException("Reading Properties failed (resource ["+name+"]): "+e.getMessage(),e);
  129.         }
  130.     }
  131.     public static Properties getProperties(URL url) throws UtilsException{
  132.         URI uri = null;
  133.         try{
  134.             if(url==null){
  135.                 throw new Exception("Resource URL undefined");
  136.             }
  137.             uri = url.toURI();
  138.         }catch(Exception e){
  139.             throw new UtilsException("Reading Properties failed (url ["+url+"]): "+e.getMessage(),e);
  140.         }
  141.         return getProperties(uri);

  142.     }
  143.     public static Properties getProperties(URI uri) throws UtilsException{
  144.         try{
  145.             if(uri==null){
  146.                 throw new Exception("Resource URI undefined");
  147.             }
  148.             File f = new File(uri);
  149.             return getProperties(f);
  150.         }catch(Exception e){
  151.             throw new UtilsException("Reading Properties failed (uri ["+uri+"]): "+e.getMessage(),e);
  152.         }
  153.     }

  154. }