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
040    @Override
041    public void exec(CLIContext cli, String[] strings) throws Exception {
042        int numArgs = strings.length;
043        if (numArgs == 1) {
044            cli.println("Usage: init -lmk filename -rebuildlmk -jce <provider class name>");
045            return;
046        }
047        Properties cfgProps = new Properties();
048        SimpleConfiguration cfg = new SimpleConfiguration(cfgProps);
049        int curArg = 1; // First string is command name
050        while (curArg <= numArgs) {
051            if (curArg < numArgs && strings[curArg].compareToIgnoreCase("-lmk") == 0) {
052                curArg++;
053                cfgProps.setProperty("lmk", strings[curArg++]);
054            }
055            if (curArg < numArgs && strings[curArg].compareToIgnoreCase("-jce") == 0) {
056                curArg++;
057                cfgProps.setProperty("provider", strings[curArg++]);
058            }
059            if (curArg < numArgs && strings[curArg].compareToIgnoreCase("-rebuildlmk") == 0) {
060                cfgProps.setProperty("rebuildlmk", "true");
061                curArg++;
062            }
063            curArg++;
064        }
065        JCESecurityModule sm = new JCESecurityModule();
066        Logger logger = new Logger();
067        logger.addListener(new SimpleLogListener(new PrintStream(cli.getOutputStream())));
068        sm.setLogger(logger, "jce-security-module");
069        try {
070            sm.setConfiguration(cfg);
071            SSM.setSecurityModule(cli, sm);
072        } catch (ConfigurationException e) {
073            cli.printThrowable(e);
074        }
075    }
076}