Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.25 KB | None | 0 0
  1. /*
  2.  * This file is part of rasdaman community.
  3.  *
  4.  * Rasdaman community is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * Rasdaman community is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12.  * See the GNU  General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU  General Public License
  15.  * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
  16.  *
  17.  * Copyright 2003 - 2017 Peter Baumann / rasdaman GmbH.
  18.  *
  19.  * For more information please see <http://www.rasdaman.org>
  20.  * or contact Peter Baumann via <baumann@rasdaman.com>.
  21.  */
  22. package org.rasdaman;
  23.  
  24. import java.io.File;
  25. import java.io.FileNotFoundException;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.sql.SQLException;
  29. import java.util.Properties;
  30. import static org.rasdaman.InitAllConfigurationsApplicationService.APPLICATION_PROPERTIES_FILE;
  31. import org.rasdaman.config.ConfigManager;
  32. import static org.rasdaman.InitAllConfigurationsApplicationService.KEY_GDAL_JAVA_DIR;
  33. import static org.rasdaman.InitAllConfigurationsApplicationService.KEY_PETASCOPE_CONF_DIR;
  34. import static org.rasdaman.InitAllConfigurationsApplicationService.addLibraryPath;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.boot.SpringApplication;
  39. import org.springframework.boot.autoconfigure.SpringBootApplication;
  40. import org.springframework.boot.builder.SpringApplicationBuilder;
  41. import org.springframework.boot.web.support.SpringBootServletInitializer;
  42. import org.springframework.cache.annotation.EnableCaching;
  43. import org.springframework.context.annotation.Bean;
  44. import org.springframework.context.annotation.ComponentScan;
  45. import org.springframework.context.annotation.PropertySource;
  46. import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  47. import org.springframework.core.io.FileSystemResource;
  48. import petascope.controller.handler.service.AbstractHandler;
  49. import petascope.exceptions.ExceptionCode;
  50. import petascope.exceptions.PetascopeException;
  51. import petascope.exceptions.WCSException;
  52. import petascope.rasdaman.exceptions.RasdamanException;
  53. import petascope.util.DatabaseUtil;
  54. import petascope.util.ras.TypeRegistry;
  55. import petascope.wcs2.parsers.request.xml.XMLAbstractParser;
  56.  
  57. @SpringBootApplication
  58. @EnableCaching
  59. @ComponentScan({"org.rasdaman", "petascope"})
  60. // NOTE: classpath is important when running as war package or it will have error resource not found
  61. @PropertySource({"classpath:application.properties"})
  62. /**
  63.  * This class initialize the Petascope properties then run the application as
  64.  * jar file
  65.  *
  66.  * @author <a href="mailto:bphamhuu@jacobs-university.net">Bang Pham Huu</a>
  67.  */
  68. public class ApplicationMain extends SpringBootServletInitializer {
  69.  
  70.     private static final Logger log = LoggerFactory.getLogger(ApplicationMain.class);
  71.  
  72.     @Autowired
  73.     AbstractHandler abstractHandler;
  74.  
  75.     /**
  76.      * NOTE: This one is use to load Petascope properties from external file
  77.      * when Spring Environment is not wired (i.e: null). Then all the
  78.      * configurations for application from petascope.properties is loaded.
  79.      *
  80.      * @throws java.sql.SQLException
  81.      * @throws java.lang.ClassNotFoundException
  82.      * @PropertySource in class level will not work as it requires a constant
  83.      * path to petascope.properties.
  84.      *
  85.      * @return
  86.      * @throws FileNotFoundException
  87.      * @throws IOException
  88.      */
  89.     @Bean
  90.     public PropertySourcesPlaceholderConfigurer placeholderConfigurer() throws FileNotFoundException, IOException, SQLException, ClassNotFoundException, PetascopeException, InterruptedException {
  91.         String resourceName = APPLICATION_PROPERTIES_FILE; // could also be a constant
  92.         Properties properties = new Properties();
  93.         InputStream resourceStream = this.getClass().getResourceAsStream("/" + resourceName);
  94.         properties.load(resourceStream);
  95.  
  96.         PropertySourcesPlaceholderConfigurer propertyResourcePlaceHolderConfigurer = new PropertySourcesPlaceholderConfigurer();
  97.         File initialFile = new File(properties.getProperty(KEY_PETASCOPE_CONF_DIR) + "/" + ConfigManager.PETASCOPE_PROPERTIES_FILE);
  98.         propertyResourcePlaceHolderConfigurer.setLocation(new FileSystemResource(initialFile));
  99.  
  100.         // Init all properties for ConfigManager
  101.         initConfigurations(properties);
  102.  
  103.         // NOTE: If legacy petascopedb exists, don't start web application or Liquibase will populate tables to same database
  104.         if (DatabaseUtil.legacyPetascopeDatabaseExists()) {
  105.             throw new PetascopeException(ExceptionCode.InternalSqlError, "petascopedb 9.4 or older already exists, "
  106.                     + "please run the migrate_petascopedb.sh script to migrate to the new petascope schema first.");
  107.         }
  108.  
  109.         return propertyResourcePlaceHolderConfigurer;
  110.     }
  111.  
  112.     @Override
  113.     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  114.         // This one will be invoked only when running in web application container (i.e: not as jar file)
  115.         return builder.sources(ApplicationMain.class);
  116.     }
  117.  
  118.     public static void main(String[] args) throws Exception {        
  119.         SpringApplication.run(ApplicationMain.class, args);
  120.     }
  121.  
  122.     /**
  123.      * Initialize all the configurations for GDAL libraries, ConfigManager and
  124.      * OGC WCS XML Schema
  125.      */
  126.     private void initConfigurations(Properties properties) throws SQLException, ClassNotFoundException, PetascopeException, IOException, InterruptedException {                
  127.         String GDAL_JAVA_DIR = properties.getProperty(KEY_GDAL_JAVA_DIR);
  128.         String CONF_DIR = properties.getProperty(KEY_PETASCOPE_CONF_DIR);
  129.         try {
  130.             // Load the GDAL native libraries (no need to set in IDE with VM options: -Djava.library.path="/usr/lib/java/gdal/")        
  131.             addLibraryPath("gdal_java", GDAL_JAVA_DIR);
  132.             // Load properties for Spring, Hibernate from external petascope.properties
  133.             ConfigManager.init(CONF_DIR);
  134.             // Load all the type registry (set, mdd, base types) of rasdaman
  135.             TypeRegistry.getInstance();
  136.             // Load the WCS Schema to validation if it is needed
  137.             XMLAbstractParser.loadWcsSchema();
  138.  
  139.             // Create the new database if not exist
  140.             DatabaseUtil.createDatabaseIfNotExist(null);
  141.         } catch (RasdamanException ex) {
  142.             throw new RuntimeException("Could not initialize config manager for petascope.properties", ex);
  143.         } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
  144.             throw new RuntimeException("Could not add GDAL java native library from '" + GDAL_JAVA_DIR + "' to java library path.", ex);
  145.         } catch (WCSException ex) {
  146.             throw new RuntimeException("Could not load the OGC WCS Schema to validate POST/SOAP requests.", ex);
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement