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