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.cli.ssm.actions;
020
021import org.jpos.core.ConfigurationException;
022import org.jpos.core.SimpleConfiguration;
023import org.jpos.q2.CLICommand;
024import org.jpos.q2.CLIContext;
025import org.jpos.q2.cli.SSM;
026import org.jpos.security.jceadapter.JCESecurityModule;
027import org.jpos.util.Logger;
028import org.jpos.util.SimpleLogListener;
029
030import java.io.PrintStream;
031import java.util.Properties;
032
033/**
034 * Initialize {@link JCESecurityModule} and stores it in the context of the ssm subsystem.
035 *
036 * @author Alwyn Schoeman - alwyn.schoeman@gmail.com
037 */
038public class INIT implements CLICommand {
039    /** Default constructor; no instance state to initialise. */
040    public INIT() {}
041
042    @Override
043    public void exec(CLIContext cli, String[] strings) throws Exception {
044        int numArgs = strings.length;
045        if (numArgs == 1) {
046            cli.println("Usage: init -lmk filename -rebuildlmk -jce <provider class name>");
047            return;
048        }
049        Properties cfgProps = new Properties();
050        SimpleConfiguration cfg = new SimpleConfiguration(cfgProps);
051        int curArg = 1; // First string is command name
052        while (curArg <= numArgs) {
053            if (curArg < numArgs && strings[curArg].compareToIgnoreCase("-lmk") == 0) {
054                curArg++;
055                cfgProps.setProperty("lmk", strings[curArg++]);
056            }
057            if (curArg < numArgs && strings[curArg].compareToIgnoreCase("-jce") == 0) {
058                curArg++;
059                cfgProps.setProperty("provider", strings[curArg++]);
060            }
061            if (curArg < numArgs && strings[curArg].compareToIgnoreCase("-rebuildlmk") == 0) {
062                cfgProps.setProperty("rebuildlmk", "true");
063                curArg++;
064            }
065            curArg++;
066        }
067        JCESecurityModule sm = new JCESecurityModule();
068        Logger logger = new Logger();
069        logger.addListener(new SimpleLogListener(new PrintStream(cli.getOutputStream())));
070        sm.setLogger(logger, "jce-security-module");
071        try {
072            sm.setConfiguration(cfg);
073            SSM.setSecurityModule(cli, sm);
074        } catch (ConfigurationException e) {
075            cli.printThrowable(e);
076        }
077    }
078}