001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.q2.install;
020
021import org.apache.commons.cli.*;
022
023import java.io.File;
024import java.io.FileOutputStream;
025import java.io.IOException;
026import java.io.InputStream;
027import java.util.List;
028
029/**
030 * @author vsalaman
031 */
032public class Install
033{
034    private static final String DEFAULT_PREFIX = "META-INF/q2/installs/";
035
036    public static void main(String[] args) throws Exception
037    {
038        CommandLineParser parser = new DefaultParser();
039        Options options = new Options ();
040        options.addOption ("p", "prefix", true, String.format("prefix, defaults to '%s'", DEFAULT_PREFIX));
041        options.addOption ("q", "quiet", false, "do not show information about files being extracted");
042        options.addOption ("f", "force", false, "override existing files in output directory");
043        options.addOption ("o", "outputDir", true, "output directory, defaults to " + new File(".").getAbsolutePath());
044        options.addOption ("h", "help", false, "Usage information");
045
046        CommandLine line = parser.parse (options, args);
047        if (line.hasOption ("h")) {
048            HelpFormatter helpFormatter = new HelpFormatter ();
049            helpFormatter.printHelp ("install", options);
050            return;
051        }
052        String prefix = line.hasOption("p") ? line.getOptionValue("prefix") : DEFAULT_PREFIX;
053        String outputBasePath = line.hasOption("o") ? line.getOptionValue("o") : ".";
054        new Install().install(
055            line.hasOption("f"),
056            new File(outputBasePath),
057            !line.hasOption("q"),
058            prefix
059        );
060    }
061
062    public void install(boolean allowOverride,File outputBasePath, boolean verbose, String prefix) throws IOException
063    {
064        if(!outputBasePath.exists())
065        {
066            outputBasePath.mkdirs();
067        }
068        
069        List<String> moduleConfigs = ModuleUtils.getModuleEntries(prefix);
070        for (String resource : moduleConfigs)
071        {
072            final String s = resource.substring(prefix.length());
073            int end = s.lastIndexOf("/");
074            String dirPrefix = end < 0 ? null : s.substring(0, end);
075            if (dirPrefix != null)
076            {
077                File dir = new File(outputBasePath,dirPrefix);
078                if (!dir.exists()) {
079                    if (verbose)
080                        System.out.println("Created " + dir.getAbsolutePath());
081                    dir.mkdirs();
082                }
083            }
084            String path = s.replaceAll("/", "\\" + File.separator);
085            File outputFile = new File(outputBasePath,path);
086            if(outputFile.exists() && !allowOverride)
087            {
088                if (verbose) {
089                    System.out.printf ("%s exists, use --force to override%n", outputFile);
090                }
091                //outputFile = new File(outputBasePath,path+".sample");
092                continue;
093            }
094            copyResourceToFile(resource, outputFile, verbose);
095        }
096    }
097
098    private void copyResourceToFile(String resource, File destination, boolean verbose) throws IOException
099    {
100        InputStream source=null;
101        try
102        {
103            source = getClass().getClassLoader().getResourceAsStream(resource);
104            FileOutputStream output = new FileOutputStream(destination);
105            if (verbose) {
106                System.out.println("extracting " + destination);
107            }
108            try
109            {
110                byte[] buffer = new byte[4096];
111                int n;
112                while (-1 != (n = source.read(buffer)))
113                {
114                    output.write(buffer, 0, n);
115                }
116            }
117            finally
118            {
119                try { output.close(); } catch (IOException ex) {
120                    ex.printStackTrace(System.err);
121                }
122            }
123        }
124        finally
125        {
126            try { if(source!=null) source.close(); } catch (IOException ex) {
127                ex.printStackTrace(System.err);
128            }
129        }
130    }
131}