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.deploy; 020 021import java.util.Iterator; 022import java.util.Set; 023 024import javax.management.MBeanServer; 025import javax.management.ObjectInstance; 026import javax.management.ObjectName; 027 028import org.jpos.q2.CLICommand; 029import org.jpos.q2.CLIContext; 030import org.jpos.q2.Q2; 031import org.jpos.q2.QBean; 032/** 033* CLI implementation - Deploy subsystem 034* 035* @author Felipph Calado - luizfelipph@gmail.com 036*/ 037public class SERVICE implements CLICommand { 038 /** Default constructor; no instance state to initialise. */ 039 public SERVICE() {} 040 041 @Override 042 public void exec(CLIContext ctx, String[] args) throws Exception { 043 044 if (args.length < 3) { 045 ctx.println("Usage: service <service> start|stop|restart"); 046 return; 047 } 048 if (!"start".equals(args[2]) && !"stop".equals(args[2]) && !"restart".equals(args[2])) { 049 ctx.println("Invalid operation: " + args[2]); 050 return; 051 } 052 String command = ""; 053 int waiting = 0; 054 int executed = 0; 055 056 switch (args[2]) { 057 case "start": 058 command = "start"; 059 waiting = QBean.STARTING; 060 executed = QBean.STARTED; 061 break; 062 case "stop": 063 command = "stop"; 064 waiting = QBean.STOPPING; 065 executed = QBean.STOPPED; 066 break; 067 case "restart": 068 args[2] = "stop"; 069 this.exec(ctx, args); 070 args[2] = "start"; 071 this.exec(ctx, args); 072 return; 073 } 074 075 MBeanServer server = ctx.getCLI().getQ2().getMBeanServer(); 076 077 ObjectName on = new ObjectName(Q2.QBEAN_NAME + args[1]); 078 Set<ObjectInstance> b = server.queryMBeans(on, null); 079 Iterator<ObjectInstance> it = b.iterator(); 080 if (it.hasNext()) { 081 ObjectInstance instance = it.next(); 082 if ((Integer) server.getAttribute(instance.getObjectName(), "State") == executed) { 083 ctx.println(args[2] + ": " + args[1] + " already done."); 084 return; 085 } 086 087 server.invoke(instance.getObjectName(), command, null, null); 088 try { 089 for (int i = 0; i < 100; i++) { 090 if ((Integer) server.getAttribute(instance.getObjectName(), "State") == executed) { 091 ctx.println(args[2] + ": " + args[1] + " done"); 092 return; 093 } 094 Thread.sleep(2000);// wait to stop... 095 ctx.println("waiting for " + args[1] + " to " + args[2]); 096 } 097 ctx.println(args[2] + ": " + args[1] + " is stuck in " + waiting + " state"); 098 return; 099 } catch (Exception e) { 100 ctx.println("Can't get state: " + args[1]); 101 } 102 103 return; 104 105 } else { 106 ctx.println(args[1] + " not found"); 107 } 108 109 } 110}