Search in sources :

Example 1 with ConfigurationLocator

use of org.mifos.framework.util.ConfigurationLocator in project head by mifos.

the class BirtReportsUploadAction method getUploadStorageDirectory.

public static String getUploadStorageDirectory() {
    ConfigurationLocator configurationLocator = new ConfigurationLocator();
    String reportPath = configurationLocator.getConfigurationDirectory() + "/uploads";
    String uploadsDir = reportPath;
    if (File.separatorChar == '\\') {
        // windows platform
        uploadsDir = uploadsDir.replaceAll("/", "\\\\");
    }
    return uploadsDir;
}
Also used : ConfigurationLocator(org.mifos.framework.util.ConfigurationLocator)

Example 2 with ConfigurationLocator

use of org.mifos.framework.util.ConfigurationLocator in project head by mifos.

the class ETLReportDWHelper method execute.

@Override
public void execute(final long timeInMillis) throws BatchJobException {
    new ApplicationContextHolder();
    ArrayList<String> errors = new ArrayList<String>();
    ApplicationContext ach = ApplicationContextHolder.getApplicationContext();
    DriverManagerDataSource ds = (DriverManagerDataSource) ach.getBean("dataSource");
    DriverManagerDataSource dsDW = (DriverManagerDataSource) ach.getBean("dataSourcePentahoDW");
    Pattern pat = Pattern.compile(DATA_WAREHOUSE_DB_NAME_PATTERN);
    Matcher m = pat.matcher(dsDW.getUrl());
    String nameOfDataBase = null;
    if (m.find()) {
        nameOfDataBase = m.group(6);
    }
    if (!nameOfDataBase.equals("")) {
        try {
            dsDW.getConnection();
        } catch (SQLException ex) {
            errors.add("Data Warehouse is not configured");
            throw new BatchJobException("Data warehouse database", errors);
        }
        ConfigurationLocator configurationLocator = new ConfigurationLocator();
        String configPath = configurationLocator.getConfigurationDirectory();
        createPropertiesFileForPentahoDWReports(ds, dsDW);
        String path = configPath + "/ETL/MifosDataWarehouseETL/" + FILENAME;
        String jarPath = configPath + "/ETL/mifos-etl-plugin-1.0-SNAPSHOT.one-jar.jar";
        String javaHome = System.getProperty("java.home") + "/bin/java";
        String pathToLog = configPath + "/ETL/log";
        if (File.separatorChar == '\\') {
            // windows platform
            javaHome = javaHome.replaceAll("/", "\\\\");
            javaHome = '"' + javaHome + '"';
            jarPath = jarPath.replaceAll("/", "\\\\");
            path = path.replaceAll("/", "\\\\");
            pathToLog = pathToLog.replaceAll("/", "\\\\");
        }
        PrintWriter fw = null;
        try {
            boolean hasErrors = false;
            boolean notRun = true;
            ProcessBuilder processBuilder = new ProcessBuilder(javaHome, "-jar", jarPath, path, "false", dsDW.getUsername(), dsDW.getPassword(), dsDW.getUrl());
            processBuilder.redirectErrorStream(true);
            Process p = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            if (new File(pathToLog).exists()) {
                new File(pathToLog).delete();
            }
            File file = new File(pathToLog);
            fw = new PrintWriter(file);
            while ((line = reader.readLine()) != null) {
                fw.println(line);
                if (line.matches("^ERROR.*")) {
                    hasErrors = true;
                }
                notRun = false;
            }
            if (notRun) {
                errors.add("Data Warehouse is not configured properly");
                throw new BatchJobException("Data warehouse database", errors);
            }
            if (hasErrors) {
                errors.add("ETL error, for more details see log file: " + pathToLog);
                throw new BatchJobException("ETL error", errors);
            }
        } catch (IOException ex) {
            throw new BatchJobException(ex.getCause());
        } finally {
            if (fw != null) {
                fw.close();
            }
        }
    } else {
        errors.add("Data Warehouse is not configured");
        throw new BatchJobException("Data warehouse database", errors);
    }
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ConfigurationLocator(org.mifos.framework.util.ConfigurationLocator) ApplicationContext(org.springframework.context.ApplicationContext) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) BufferedReader(java.io.BufferedReader) ApplicationContextHolder(org.mifos.application.servicefacade.ApplicationContextHolder) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 3 with ConfigurationLocator

use of org.mifos.framework.util.ConfigurationLocator in project head by mifos.

the class PluginManager method initializePluginClassLoader.

/**
     * Extend classloader by loading jars from ${MIFOS_CONF}/plugins at runtime
     *
     * @return pluginClassLoader
     */
private ClassLoader initializePluginClassLoader() {
    ConfigurationLocator configurationLocator = new ConfigurationLocator();
    String libDir = configurationLocator.getConfigurationDirectory() + "/plugins";
    File dependencyDirectory = new File(libDir);
    File[] files = dependencyDirectory.listFiles();
    ArrayList<URL> urls = new ArrayList<URL>();
    if (files != null) {
        urls.addAll(getPluginURLs(files));
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread.currentThread().getContextClassLoader());
}
Also used : URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) ConfigurationLocator(org.mifos.framework.util.ConfigurationLocator) File(java.io.File) URL(java.net.URL)

Example 4 with ConfigurationLocator

use of org.mifos.framework.util.ConfigurationLocator in project head by mifos.

the class ImageStorageManager method getStorageLocation.

public static synchronized String getStorageLocation() {
    if (location == null) {
        String pathConfig = MifosConfigurationManager.getInstance().getString(STORAGE_CONFIG_KEY);
        if (StringUtils.isBlank(pathConfig) || pathConfig.equals(ConfigurationLocator.LOCATOR_ENVIRONMENT_PROPERTY_NAME)) {
            ConfigurationLocator cl = new ConfigurationLocator();
            location = cl.getConfigurationDirectory() + BASE_DIR;
        } else {
            location = pathConfig + BASE_DIR;
        }
        checkStoragePermission();
    }
    return location;
}
Also used : ConfigurationLocator(org.mifos.framework.util.ConfigurationLocator)

Example 5 with ConfigurationLocator

use of org.mifos.framework.util.ConfigurationLocator in project head by mifos.

the class ApplicationInitializer method copyResources.

private void copyResources(ServletContext sc) throws IOException {
    URL protocol = ETLReportDWHelper.class.getClassLoader().getResource("sql/release-upgrades.txt");
    ConfigurationLocator configurationLocator = new ConfigurationLocator();
    String configPath = configurationLocator.getConfigurationDirectory();
    String destinationDirectoryForJobs = configPath + "/ETL/MifosDataWarehouseETL";
    String destinationDirectoryForJar = configPath + "/ETL/mifos-etl-plugin-1.0-SNAPSHOT.one-jar.jar";
    String destinationDirectoryForReportJobs = configPath + "/uploads/report";
    String pathFromJar = "/WEB-INF/mifos-etl-plugin-1.0-SNAPSHOT.one-jar.jar";
    String pathFromJobs = "/WEB-INF/MifosDataWarehouseETL/";
    if (File.separatorChar == '\\') {
        destinationDirectoryForJobs = destinationDirectoryForJobs.replaceAll("/", "\\\\");
        destinationDirectoryForJar = destinationDirectoryForJar.replaceAll("/", "\\\\");
        destinationDirectoryForReportJobs = destinationDirectoryForReportJobs.replaceAll("/", "\\\\");
    }
    File directory = new File(destinationDirectoryForJobs);
    directory.mkdirs();
    File jarDest = new File(destinationDirectoryForJar);
    File reportDirectory = new File(destinationDirectoryForReportJobs);
    reportDirectory.mkdirs();
    if (protocol.getProtocol().equals("jar")) {
        FileUtils.cleanDirectory(directory);
        // Mifos WAR
        URL fullPath = sc.getResource(pathFromJar);
        File f = new File(sc.getResource(pathFromJobs).toString().replace("file:", ""));
        for (File fileEntry : f.listFiles()) {
            FileUtils.copyFileToDirectory(fileEntry, directory);
            logger.info("Copy file: " + fileEntry.getName() + " to: " + directory);
        }
        FileUtils.copyURLToFile(fullPath, jarDest);
        logger.info("Copy file: " + fullPath + " to: " + directory);
        String jarPath = "/WEB-INF/lib/mifos-reporting-1.12-SNAPSHOT.jar";
        String jarName = sc.getResource(jarPath).toString().replace("file:", "");
        JarInputStream jarFileStream = new JarInputStream(new FileInputStream(jarName));
        JarEntry jarEntry;
        while (true) {
            jarEntry = jarFileStream.getNextJarEntry();
            if (jarEntry == null) {
                break;
            }
            // Skip irrelevant folders
            String jarEntryName = jarEntry.getName();
            if (!jarEntryName.startsWith("birt/report") && !jarEntryName.startsWith("pentaho")) {
                continue;
            }
            int lastIndexOfDot = jarEntryName.lastIndexOf('.');
            if (lastIndexOfDot != -1 && !jarEntry.isDirectory()) {
                String destinationDirectory = destinationDirectoryForReportJobs;
                String birtReportFolderName = "birt/report";
                if (jarEntryName.startsWith(birtReportFolderName)) {
                    int indexOfLastSlash = jarEntryName.lastIndexOf('/');
                    String folderName = jarEntryName.substring(birtReportFolderName.length(), indexOfLastSlash).replaceAll("/", "");
                    File birtSubFolder = new File(destinationDirectoryForReportJobs + File.separatorChar + folderName);
                    birtSubFolder.mkdirs();
                    destinationDirectory = destinationDirectoryForReportJobs + File.separatorChar + folderName;
                }
                InputStream inputStream = MifosViewerServletContextListener.class.getClassLoader().getResourceAsStream(jarEntryName);
                int lastIndexOfSlash = jarEntryName.lastIndexOf('/');
                String reportFileName = jarEntryName.substring(lastIndexOfSlash + 1);
                File reportFile = new File(destinationDirectory + File.separatorChar + reportFileName);
                FileUtils.copyInputStreamToFile(inputStream, reportFile);
                logger.info("Copy file: " + jarEntryName + " to: " + reportFile);
            }
        }
    } else {
        try {
            // Mifos Cloud Foundry WAR
            URL fullPath = sc.getResource(pathFromJar);
            File f = new File(sc.getRealPath("/") + pathFromJobs);
            if (f.listFiles().equals(null)) {
                throw new NullPointerException();
            }
            FileUtils.cleanDirectory(directory);
            for (File fileEntry : f.listFiles()) {
                FileUtils.copyFileToDirectory(fileEntry, directory);
                logger.info("Copy file: " + fileEntry.getName() + " to: " + directory);
            }
            FileUtils.copyURLToFile(fullPath, jarDest);
            logger.info("Copy file: " + fullPath + " to: " + directory);
            String sourceBirtReportPath = "/report";
            String sourcePentahoReportPath = "/WEB-INF/classes/pentaho";
            File birtReports = new File(sc.getRealPath("/") + sourceBirtReportPath);
            File pentahoReports = new File(sc.getRealPath("/") + sourcePentahoReportPath);
            for (File fileEntry : birtReports.listFiles()) {
                if (fileEntry.isDirectory()) {
                    File destinationDirectory = new File(destinationDirectoryForReportJobs + File.separatorChar + fileEntry.getName());
                    FileUtils.copyDirectory(fileEntry, destinationDirectory);
                    logger.info("Copy directory: " + fileEntry.getName() + " to: " + destinationDirectory);
                    continue;
                }
                FileUtils.copyFileToDirectory(fileEntry, reportDirectory);
                logger.info("Copy file: " + fileEntry.getName() + " to: " + reportDirectory);
            }
            for (File fileEntry : pentahoReports.listFiles()) {
                FileUtils.copyFileToDirectory(fileEntry, reportDirectory);
                logger.info("Copy file: " + fileEntry.getName() + " to: " + reportDirectory);
            }
        } catch (NullPointerException e) {
            // Eclipse
            logger.info("Could not copy Birt/Pentaho report files to Mifos config directory.");
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MifosViewerServletContextListener(org.mifos.reports.MifosViewerServletContextListener) ETLReportDWHelper(org.mifos.framework.components.batchjobs.helpers.ETLReportDWHelper) ConfigurationLocator(org.mifos.framework.util.ConfigurationLocator) JarEntry(java.util.jar.JarEntry) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Aggregations

ConfigurationLocator (org.mifos.framework.util.ConfigurationLocator)25 File (java.io.File)9 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 MifosScheduler (org.mifos.framework.components.batchjobs.MifosScheduler)3 Resource (org.springframework.core.io.Resource)3 FileInputStream (java.io.FileInputStream)2 Properties (java.util.Properties)2 JarInputStream (java.util.jar.JarInputStream)2 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 URLClassLoader (java.net.URLClassLoader)1 SQLException (java.sql.SQLException)1