All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jongsoft.highchart.phantomjs.DriverPoolFactory Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * The MIT License
 *
 * Copyright 2016 Jong Soft.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.jongsoft.highchart.phantomjs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.lang.String.format;

public class DriverPoolFactory {

    private static final Logger LOG = LoggerFactory.getLogger(DriverPoolFactory.class);
    
    private static final DriverPool INSTANCE = new DriverPool();
    
    static {
        new DriverPoolFactory();
    }

    public DriverPoolFactory() {
        // Extract the javascript files into a tmp directory
        LOG.trace("Creating temporary directory for expanding the javascript files to.");
        File tempDirectory = new File(DriverPoolFactory.getExpansionDir());
        if (!tempDirectory.exists() && !tempDirectory.mkdirs()) {
            LOG.error("Could not create the temporary directory for expanding the javascript files to.");
        }
        
        String files[] = new String[] {"highchart-map.js", "highchart.js", "highcharts-more.js"};
        for (String javascriptFile : files) {
            try (InputStream resourceAsStream = DriverPoolFactory.class.getClassLoader().getResourceAsStream("javascript/" + javascriptFile)) {
                if (resourceAsStream != null) {
                    LOG.trace(format("Expanding file %s to temporary directory.", javascriptFile));
                    IOUtils.copy(resourceAsStream, new FileOutputStream(DriverPoolFactory.getExpansionDir() + "/" + javascriptFile));
                }
            } catch (IOException e) {
                LOG.error(format("Unable to unpack file %s to target location %s", javascriptFile, tempDirectory.getAbsolutePath()), e);
            }
        }
        
        if (tempDirectory.list().length != files.length) {
            LOG.error(format("Some JavaScript files might not have been extracted properly expected %s files but got %s files", 
                    tempDirectory.list().length, files.length));
        }
    }
    
    public static String getExpansionDir() {
        return System.getProperty("java.io.tmpdir") + "/highchart-renderer/javascript";
    }

    public static DriverPool getDriverPool(boolean newInstance) {
        if (newInstance) {
            return new DriverPool();
        }

        return INSTANCE;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy