AbstractXMLServiceManager.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.generic_project.dao.xml;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;
  24. import java.net.HttpURLConnection;
  25. import java.net.URL;
  26. import java.net.URLConnection;

  27. import org.openspcoop2.generic_project.beans.IProjectInfo;
  28. import org.openspcoop2.generic_project.exception.ServiceException;
  29. import org.openspcoop2.generic_project.serializer.AbstractDeserializerBase;
  30. import org.openspcoop2.utils.LoggerWrapperFactory;
  31. import org.openspcoop2.utils.xml.ValidatoreXSD;
  32. import org.slf4j.Logger;

  33. /**
  34.  * AbstractXMLServiceManager
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public abstract class AbstractXMLServiceManager<XML> {

  41.     /** Contesto di Unmarshall. */
  42.     private AbstractDeserializerBase deserializer;
  43.     private Class<XML> cXmlRoot;
  44.     /** XML Path */
  45.     protected String xmlPath;
  46.     /** 'Root' XML */
  47.     protected XML rootXml;
  48.     /** XSD Validator */
  49.     private ValidatoreXSD xsdValidator = null;
  50.     /** LastModified */
  51.     private long lastModified = 0;
  52.     /** Logger */
  53.     private Logger log = null;
  54.     /** Refresh Timeout */
  55.     private static final int timeoutRefresh = 30;
  56.    
  57.    
  58.     /** ************* parsing XML ************** */
  59.     private void parsingXML() throws ServiceException{

  60.         /* --- XSD -- */
  61.         FileInputStream fXML = null;
  62.         try{
  63.             if(this.xmlPath.startsWith("http://") || this.xmlPath.startsWith("file://")){
  64.                 this.xsdValidator.valida(this.xmlPath);  
  65.             }else{
  66.                 fXML = new FileInputStream(this.xmlPath);
  67.                 this.xsdValidator.valida(fXML);
  68.             }
  69.         }catch (Exception e) {
  70.             throw new ServiceException("Xsd validation failure: "+e.getMessage(),e);
  71.         }finally{
  72.             if(fXML!=null){
  73.                 try{
  74.                     fXML.close();
  75.                 }catch(Exception e){}
  76.             }
  77.         }

  78.         /* ---- InputStream ---- */
  79.         InputStream iStream = null;
  80.         HttpURLConnection httpConn = null;
  81.         if(this.xmlPath.startsWith("http://") || this.xmlPath.startsWith("file://")){
  82.             try{
  83.                 URL url = new URL(this.xmlPath);
  84.                 URLConnection connection = url.openConnection();
  85.                 httpConn = (HttpURLConnection) connection;
  86.                 httpConn.setRequestMethod("GET");
  87.                 httpConn.setDoOutput(true);
  88.                 httpConn.setDoInput(true);
  89.                 iStream = httpConn.getInputStream();
  90.             }catch(Exception e) {
  91.                 try{  
  92. //                  if(iStream!=null)
  93. //                      iStream.close();
  94.                     if(httpConn !=null)
  95.                         httpConn.disconnect();
  96.                 } catch(Exception ef) {}
  97.                 throw new ServiceException("Creating InputStream (HTTP) failure: "+e.getMessage(),e);
  98.             }
  99.             this.lastModified = System.currentTimeMillis();
  100.         }else{
  101.             try{  
  102.                 iStream = new FileInputStream(this.xmlPath);
  103.             }catch(java.io.FileNotFoundException e) {
  104.                 throw new ServiceException("Creating InputStream (FILE) failure"+e.getMessage(),e);
  105.             }
  106.             try{
  107.                 this.lastModified = (new File(this.xmlPath)).lastModified();
  108.             }catch(Exception e){
  109.                 try{  
  110.                     if(iStream!=null)
  111.                         iStream.close();
  112.                 } catch(java.io.IOException ef) {}
  113.                 throw new ServiceException("Reading xml ["+this.xmlPath+"] failure: "+e.getMessage(),e);
  114.             }
  115.         }

  116.         /* ---- Unmarshall ---- */
  117.         try{  
  118.             this.rootXml = (XML) this.deserializer.xmlToObj(iStream, this.cXmlRoot);
  119.         } catch(Exception e) {
  120.             try{  
  121.                 if(iStream!=null)
  122.                     iStream.close();
  123.                 if(httpConn !=null)
  124.                     httpConn.disconnect();
  125.             } catch(Exception ef) {}
  126.             throw new ServiceException("Unmarshall document xml failure: "+e.getMessage(),e);
  127.         }

  128.         /* ---- Close ---- */
  129.         try{  
  130.             if(iStream!=null)
  131.                 iStream.close();
  132.             if(httpConn !=null)
  133.                 httpConn.disconnect();
  134.         } catch(Exception e) {
  135.             throw new ServiceException("Close InputStream failure: "+e.getMessage(),e);
  136.         }
  137.     }




  138.     /* ********  COSTRUTTORI e METODI DI RELOAD  ******** */
  139.     protected AbstractXMLServiceManager(Class<XML> cXmlRoot, AbstractDeserializerBase deserializer, String xsdPath, File xmlPath) throws ServiceException{
  140.         this(cXmlRoot, deserializer, xsdPath, xmlPath,null);
  141.     }
  142.     protected AbstractXMLServiceManager(Class<XML> cXmlRoot, AbstractDeserializerBase deserializer, String xsdPath, File xmlPath,Logger alog) throws ServiceException{
  143.         this(cXmlRoot,deserializer, xsdPath, xmlPath.getAbsolutePath(),alog);
  144.     }
  145.     protected AbstractXMLServiceManager(Class<XML> cXmlRoot, AbstractDeserializerBase deserializer, String xsdPath, String xmlPath) throws ServiceException{
  146.         this(cXmlRoot, deserializer, xsdPath,  xmlPath,null);
  147.     }
  148.     protected AbstractXMLServiceManager(Class<XML> cXmlRoot, AbstractDeserializerBase deserializer, String xsdPath, String xmlPath,Logger alog) throws ServiceException{

  149.         if(alog==null){
  150.             this.log = LoggerWrapperFactory.getLogger(AbstractXMLServiceManager.class);
  151.         }else
  152.             this.log = alog;

  153.         if(xmlPath == null){
  154.             this.log.error("New Instance failure: url/xmlPath is null");
  155.             throw new ServiceException("New Instance failure: url/xmlPath is null");
  156.         }
  157.         this.xmlPath = xmlPath;
  158.        
  159.         /* --- XSD Validator -- */
  160.         if(xsdPath == null){
  161.             this.log.error("New Instance failure: xsdPath is null");
  162.             throw new ServiceException("New Instance failure: xsdPath is null");
  163.         }
  164.         InputStream is = null;
  165.         try{
  166.             File f = new File(xsdPath);
  167.             if(f.exists()){
  168.                 is = new FileInputStream(f);
  169.             }else{
  170.                 is = cXmlRoot.getResourceAsStream(xsdPath);
  171.                 if(is==null){
  172.                     is = cXmlRoot.getResourceAsStream("/"+xsdPath);
  173.                 }
  174.             }
  175.             if(is==null){
  176.                 throw new Exception("Creating InputStream from xsdPath["+xsdPath+"] failure");
  177.             }
  178.             this.xsdValidator = new ValidatoreXSD(null,is);
  179.         }catch (Exception e) {
  180.             this.log.error("Init xsd schema failure: "+e.getMessage(),e);
  181.             throw new ServiceException("Init xsd schema failure: "+e.getMessage(),e);
  182.         }finally{
  183.             try{
  184.                 if(is!=null){
  185.                     is.close();
  186.                 }
  187.             }catch(Exception eClose){
  188.                 // close
  189.             }
  190.         }

  191.         /* ---- Uunmarshall  ---- */
  192.         this.cXmlRoot = cXmlRoot;
  193.         this.deserializer = deserializer;

  194.         this.parsingXML();
  195.     }
  196.    
  197.     public void refreshXML() throws ServiceException{
  198.         refreshXML_engine(false);
  199.     }
  200.     public void refreshXML(boolean forcedWithoutCheckModified) throws ServiceException{
  201.         refreshXML_engine(forcedWithoutCheckModified);
  202.     }
  203.    
  204.     private synchronized void refreshXML_engine(boolean forcedWithoutCheckModified) throws ServiceException{

  205.         File fTest = null;
  206.         boolean refresh = forcedWithoutCheckModified;
  207.         if(forcedWithoutCheckModified==false){
  208.             if(this.xmlPath.startsWith("http://") || this.xmlPath.startsWith("file://")){
  209.                 long now = System.currentTimeMillis();
  210.                 if( (now-this.lastModified) > (AbstractXMLServiceManager.timeoutRefresh*1000) ){
  211.                     refresh=true;
  212.                 }
  213.             }else{
  214.                 fTest = new File(this.xmlPath);
  215.                 if(this.lastModified != fTest.lastModified()){
  216.                     refresh = true;
  217.                 }
  218.             }
  219.         }
  220.         if(refresh){

  221.             try{
  222.                 this.parsingXML();
  223.             }catch(Exception e){
  224.                 this.log.error("Refresh failure: "+e.getMessage(),e);
  225.                 throw new ServiceException("Refresh failure: "+e.getMessage(),e);
  226.             }
  227.             if(this.xmlPath.startsWith("http://")==false && this.xmlPath.startsWith("file://")==false){
  228.                 this.log.warn("Reloaded context.");
  229.             }
  230.            
  231.         }

  232.         if(this.rootXml == null){
  233.             this.log.error("Refresh failure: rootXml is null after the refresh");
  234.             throw new ServiceException("Refresh failure: rootXml is null after the refresh");
  235.         }

  236.     }
  237.    
  238.     public void checkRemoteXML() throws ServiceException{
  239.         if(this.xmlPath.startsWith("http://")){
  240.             throw new ServiceException("You can not use the CRUD service with a remote XML");
  241.         }
  242.     }

  243.     public Logger getLog() {
  244.         return this.log;
  245.     }

  246.     public XML getRootXml() {
  247.         return this.rootXml;
  248.     }

  249.     /* Logger */
  250.     public static void configureDefaultLog4jProperties(IProjectInfo project) throws ServiceException{
  251.         XMLLoggerProperties loggerProperties = new XMLLoggerProperties(project);
  252.         loggerProperties.configureLog4j();
  253.     }
  254.     public static void configureLog4jProperties(File log4jProperties) throws ServiceException{
  255.         XMLLoggerProperties loggerProperties = new XMLLoggerProperties(null,log4jProperties);
  256.         loggerProperties.configureLog4j();
  257.     }
  258. }