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.iso.ISOUtil;
022import org.jpos.q2.CLICommand;
023import org.jpos.q2.CLIContext;
024import org.jpos.util.Loggeable;
025import org.jpos.util.NameRegistrar;
026
027import java.util.Iterator;
028import java.util.Map;
029
030/** CLI command that lists the registered NameRegistrar entries. */
031public class SHOWNR implements CLICommand {
032    /** Default constructor; no instance state to initialise. */
033    public SHOWNR() {}
034    public void exec(CLIContext cli, String[] args) throws Exception {
035        boolean all = args.length > 1 && "-a".equals(args[1]);
036        int i = 1;
037        if (all) {
038            i++;
039        }
040        if (args.length > i) {
041            showOne(cli, args[i], all); }
042        else {
043            showAll(cli, all);
044        }
045    }
046
047    private void showOne(CLIContext cli, String name, boolean detail) {
048        try {
049            Object obj = NameRegistrar.get(name);
050            cli.println(name + " : " + obj.toString());
051            if (detail && obj instanceof Loggeable) {
052                cli.printLoggeable((Loggeable) obj, "");
053            }
054        }
055        catch (NameRegistrar.NotFoundException e) {
056            cli.println("Object not found in NameRegistrar");
057        }
058    }
059
060    private void showAll(CLIContext cli, boolean detail) {
061        NameRegistrar nr = NameRegistrar.getInstance();
062        int maxw = 0;
063        Iterator iter = NameRegistrar.getAsMap().entrySet().iterator();
064        while (iter.hasNext()) {
065            Map.Entry entry = (Map.Entry) iter.next();
066            maxw = Math.max(maxw, entry.getKey().toString().length());
067        }
068        iter = NameRegistrar.getAsMap().entrySet().iterator();
069        maxw++;
070        while (iter.hasNext()) {
071            Map.Entry entry = (Map.Entry) iter.next();
072            cli.println(
073                    ISOUtil.strpad(entry.getKey().toString(), maxw) +
074                    entry.getValue().toString()
075            );
076            if (detail && entry.getValue() instanceof Loggeable) {
077                cli.printLoggeable((Loggeable) entry.getValue(), "   ");
078            }
079        }
080    }
081}