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;
020
021import org.jpos.q2.CLICommand;
022import org.jpos.q2.CLIContext;
023import org.jpos.q2.CLISubSystem;
024import org.jpos.security.jceadapter.JCESecurityModule;
025
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * CLI implementation - SSM subsystem
031 *
032 * @author Alwyn Schoeman - alwyn.schoeman@gmail.com
033 */
034public class SSM implements CLISubSystem, CLICommand {
035    /** Default constructor; no instance state to initialise. */
036    public SSM() {}
037    private static final String SYSTEM_KEY = "SSM";
038    private static final String JCE_KEY = "jce-sm";
039
040    @Override
041    public String getPrompt(CLIContext ctx, String[] args) {
042        return "ssm> ";
043    }
044
045    @Override
046    public String[] getCompletionPrefixes(CLIContext ctx, String[] args) {
047        return new String[] { "org.jpos.q2.cli.ssm.actions." };
048    }
049
050    @Override
051    public void exec(CLIContext cli, String[] strings) throws Exception {
052        cli.setActiveSubSystem(SYSTEM_KEY);
053        cli.getUserData().put(SYSTEM_KEY, new HashMap<String, Object>());
054    }
055
056    /**
057     * Returns the {@link JCESecurityModule} stashed in the SSM-subsystem storage.
058     *
059     * @param cliContext CLI context
060     * @return the active security module, or {@code null} if none has been set
061     */
062    public static JCESecurityModule getSecurityModule(CLIContext cliContext) {
063        return (JCESecurityModule) getSystemStorage(cliContext).get(JCE_KEY);
064    }
065
066    /**
067     * Stashes a {@link JCESecurityModule} in the SSM-subsystem storage so other
068     * SSM commands can resolve it.
069     *
070     * @param cliContext CLI context
071     * @param securityModule security module to store
072     */
073    public static void setSecurityModule(CLIContext cliContext, JCESecurityModule securityModule) {
074        getSystemStorage(cliContext).put(JCE_KEY, securityModule);
075    }
076
077    private static Map<String, Object> getSystemStorage(CLIContext cliContext) {
078        return (Map<String, Object>) cliContext.getUserData().get(SYSTEM_KEY);
079    }
080}