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.transaction.TransactionManager;
024import org.jpos.transaction.TransactionStatusEvent;
025import org.jpos.transaction.TransactionStatusListener;
026import org.jpos.util.NameRegistrar;
027
028import java.io.PrintStream;
029import java.util.Iterator;
030import java.util.Map;
031
032@SuppressWarnings("unused")
033public class TMMON implements CLICommand, TransactionStatusListener {
034    PrintStream p;
035    CLIContext cli;
036    boolean ansi;
037
038    public void exec(CLIContext cli, String[] args) throws Exception {
039        this.p = new PrintStream(cli.getReader().getTerminal().output());
040        this.cli = cli;
041        this.ansi = false; // cli.getReader().getTerminal()
042        if (args.length == 1) {
043            usage(cli);
044            return;
045        }
046        for (int i = 1; i < args.length; i++) {
047            try {
048                Object obj = NameRegistrar.get(args[i]);
049                if (obj instanceof TransactionManager) {
050                    ((TransactionManager) obj).addListener(this);
051                } else {
052                    cli.println("Object '" + args[i]
053                      + "' is not an instance of TransactionManager (" + obj.toString() + ")");
054                }
055            } catch (NameRegistrar.NotFoundException e) {
056                cli.println("TransactionManager '" + args[i] + "' not found -- ignored.");
057            }
058        }
059
060        cli.getReader().readLine();
061
062        for (int i = 1; i < args.length; i++) {
063            try {
064                Object obj = NameRegistrar.get(args[i]);
065                if (obj instanceof TransactionManager) {
066                    ((TransactionManager) obj).removeListener(this);
067                }
068            } catch (NameRegistrar.NotFoundException ignored) {
069                // NOPMD ok to happen
070            }
071        }
072    }
073
074    public void usage(CLIContext cli) {
075        cli.println("Usage: tmmon [tm-name] [tm-name] ...");
076        showTMs(cli);
077    }
078
079    private void showTMs(CLIContext cli) {
080        NameRegistrar nr = NameRegistrar.getInstance();
081        int maxw = 0;
082        Iterator iter = NameRegistrar.getAsMap().entrySet().iterator();
083        StringBuilder sb = new StringBuilder("available transaction managers:");
084        while (iter.hasNext()) {
085            Map.Entry entry = (Map.Entry) iter.next();
086            String key = (String) entry.getKey();
087            if (entry.getValue() instanceof TransactionManager) {
088                sb.append(' ');
089                sb.append(key);
090            }
091        }
092        cli.println(sb.toString());
093    }
094
095    public void update(TransactionStatusEvent e) {
096        cli.println(e.toString());
097    }
098}
099