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/** 033 * CLI command that subscribes to one or more named transaction managers and 034 * mirrors their {@link TransactionStatusListener} events to the terminal until 035 * the user presses Enter. 036 */ 037@SuppressWarnings("unused") 038public class TMMON implements CLICommand, TransactionStatusListener { 039 /** Default constructor; no instance state to initialise. */ 040 public TMMON() {} 041 PrintStream p; 042 CLIContext cli; 043 boolean ansi; 044 045 public void exec(CLIContext cli, String[] args) throws Exception { 046 this.p = new PrintStream(cli.getReader().getTerminal().output()); 047 this.cli = cli; 048 this.ansi = false; // cli.getReader().getTerminal() 049 if (args.length == 1) { 050 usage(cli); 051 return; 052 } 053 for (int i = 1; i < args.length; i++) { 054 try { 055 Object obj = NameRegistrar.get(args[i]); 056 if (obj instanceof TransactionManager) { 057 ((TransactionManager) obj).addListener(this); 058 } else { 059 cli.println("Object '" + args[i] 060 + "' is not an instance of TransactionManager (" + obj.toString() + ")"); 061 } 062 } catch (NameRegistrar.NotFoundException e) { 063 cli.println("TransactionManager '" + args[i] + "' not found -- ignored."); 064 } 065 } 066 067 cli.getReader().readLine(); 068 069 for (int i = 1; i < args.length; i++) { 070 try { 071 Object obj = NameRegistrar.get(args[i]); 072 if (obj instanceof TransactionManager) { 073 ((TransactionManager) obj).removeListener(this); 074 } 075 } catch (NameRegistrar.NotFoundException ignored) { 076 // NOPMD ok to happen 077 } 078 } 079 } 080 081 /** 082 * Prints command usage and lists the registered transaction managers. 083 * 084 * @param cli the CLI context to write to 085 */ 086 public void usage(CLIContext cli) { 087 cli.println("Usage: tmmon [tm-name] [tm-name] ..."); 088 showTMs(cli); 089 } 090 091 private void showTMs(CLIContext cli) { 092 NameRegistrar nr = NameRegistrar.getInstance(); 093 int maxw = 0; 094 Iterator iter = NameRegistrar.getAsMap().entrySet().iterator(); 095 StringBuilder sb = new StringBuilder("available transaction managers:"); 096 while (iter.hasNext()) { 097 Map.Entry entry = (Map.Entry) iter.next(); 098 String key = (String) entry.getKey(); 099 if (entry.getValue() instanceof TransactionManager) { 100 sb.append(' '); 101 sb.append(key); 102 } 103 } 104 cli.println(sb.toString()); 105 } 106 107 public void update(TransactionStatusEvent e) { 108 cli.println(e.toString()); 109 } 110} 111