JDBCProperties.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.jdbc;

  21. import java.util.HashMap;
  22. import java.util.Map;

  23. import org.openspcoop2.generic_project.beans.IProjectInfo;
  24. import org.openspcoop2.generic_project.exception.ServiceException;
  25. import org.openspcoop2.generic_project.utils.LoaderProperties;
  26. import org.openspcoop2.generic_project.utils.Utilities;
  27. import org.openspcoop2.utils.resources.ClassLoaderUtilities;

  28. /**
  29.  * JDBCProperties
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class JDBCProperties extends LoaderProperties {

  36.     public JDBCProperties(IProjectInfo project) throws ServiceException {
  37.         super(Utilities.normalizedProjectName(project.getProjectName())+".dao.jdbc.properties");
  38.     }
  39.     public JDBCProperties(Package packageName, IProjectInfo project) throws ServiceException {
  40.         super(packageName.getName().replaceAll("\\.", "/")+"/"+Utilities.normalizedProjectName(project.getProjectName())+".dao.jdbc.properties");
  41.     }

  42.     private static Map<String, JDBCProperties> mapJDBCProperties = new HashMap<String, JDBCProperties>();
  43.     private static synchronized void initJDBCProperties(Package packageName, IProjectInfo project) throws ServiceException{
  44.         String key = project.getProjectName();
  45.         if(mapJDBCProperties.containsKey(key)==false){
  46.             JDBCProperties jdbcProps = null;
  47.             if(packageName!=null) {
  48.                 jdbcProps = new JDBCProperties(packageName,project);
  49.             }
  50.             else {
  51.                 jdbcProps = new JDBCProperties(project);
  52.             }
  53.             mapJDBCProperties.put(key, jdbcProps);
  54.         }
  55.     }
  56.    
  57.     public static JDBCProperties getInstance(IProjectInfo project) throws ServiceException {
  58.         return getInstance(null, project);
  59.     }
  60.     public static JDBCProperties getInstance(Package packageName, IProjectInfo project) throws ServiceException {
  61.         String key = project.getProjectName();
  62.         if(mapJDBCProperties.containsKey(key)==false){
  63.             initJDBCProperties(packageName, project);
  64.         }
  65.         return mapJDBCProperties.get(key);
  66.     }
  67.    
  68.     @SuppressWarnings("unchecked")
  69.     public <CRUD> Class<CRUD> getServiceCRUDClass(String object) throws ServiceException{
  70.         try{
  71.             String p = this.properties.getProperty("service.crud."+object);
  72.             if(p!=null){
  73.                 p = p.trim();
  74.             }else{
  75.                 throw new ServiceException("Property 'service.crud."+object+"' not found in the file properties "+this.filePropertiesName);
  76.             }
  77.             Class<?> serviceClass = Class.forName(p);
  78.             return (Class<CRUD>) serviceClass;
  79.         }catch(Exception e){
  80.             throw new ServiceException("Loading service CRUD class failed: "+e.getMessage(),e);
  81.         }
  82.     }
  83.     @SuppressWarnings("unchecked")
  84.     public <CRUD> CRUD getServiceCRUD(String object) throws ServiceException{
  85.         try{
  86.             return (CRUD) ClassLoaderUtilities.newInstance(getServiceCRUDClass(object));
  87.         }catch(Exception e){
  88.             throw new ServiceException("Loading service CRUD class failed: "+e.getMessage(),e);
  89.         }
  90.     }
  91.    
  92.     @SuppressWarnings("unchecked")
  93.     public <SEARCH> Class<SEARCH> getServiceSearchClass(String object) throws ServiceException{
  94.         try{
  95.             String p = this.properties.getProperty("service.search."+object);
  96.             if(p!=null){
  97.                 p = p.trim();
  98.             }else{
  99.                 throw new ServiceException("Property 'service.search."+object+"' not found in the file properties "+this.filePropertiesName);
  100.             }
  101.             Class<?> serviceClass = Class.forName(p);
  102.             return (Class<SEARCH>) serviceClass;
  103.         }catch(Exception e){
  104.             throw new ServiceException("Loading service Search class failed: "+e.getMessage(),e);
  105.         }
  106.     }
  107.     @SuppressWarnings("unchecked")
  108.     public <SEARCH> SEARCH getServiceSearch(String object) throws ServiceException{
  109.         try{
  110.             return (SEARCH) ClassLoaderUtilities.newInstance(getServiceSearchClass(object));
  111.         }catch(Exception e){
  112.             throw new ServiceException("Loading service Search class failed: "+e.getMessage(),e);
  113.         }
  114.     }
  115. }