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 039 @Override 040 public void exec(CLIContext ctx, String[] args) throws Exception { 041 042 if (args.length < 3) { 043 ctx.println("Usage: service <service> start|stop|restart"); 044 return; 045 } 046 if (!"start".equals(args[2]) && !"stop".equals(args[2]) && !"restart".equals(args[2])) { 047 ctx.println("Invalid operation: " + args[2]); 048 return; 049 } 050 String command = ""; 051 int waiting = 0; 052 int executed = 0; 053 054 switch (args[2]) { 055 case "start": 056 command = "start"; 057 waiting = QBean.STARTING; 058 executed = QBean.STARTED; 059 break; 060 case "stop": 061 command = "stop"; 062 waiting = QBean.STOPPING; 063 executed = QBean.STOPPED; 064 break; 065 case "restart": 066 args[2] = "stop"; 067 this.exec(ctx, args); 068 args[2] = "start"; 069 this.exec(ctx, args); 070 return; 071 } 072 073 MBeanServer server = ctx.getCLI().getQ2().getMBeanServer(); 074 075 ObjectName on = new ObjectName(Q2.QBEAN_NAME + args[1]); 076 Set<ObjectInstance> b = server.queryMBeans(on, null); 077 Iterator<ObjectInstance> it = b.iterator(); 078 if (it.hasNext()) { 079 ObjectInstance instance = it.next(); 080 if ((Integer) server.getAttribute(instance.getObjectName(), "State") == executed) { 081 ctx.println(args[2] + ": " + args[1] + " already done."); 082 return; 083 } 084 085 server.invoke(instance.getObjectName(), command, null, null); 086 try { 087 for (int i = 0; i < 100; i++) { 088 if ((Integer) server.getAttribute(instance.getObjectName(), "State") == executed) { 089 ctx.println(args[2] + ": " + args[1] + " done"); 090 return; 091 } 092 Thread.sleep(2000);// wait to stop... 093 ctx.println("waiting for " + args[1] + " to " + args[2]); 094 } 095 ctx.println(args[2] + ": " + args[1] + " is stuck in " + waiting + " state"); 096 return; 097 } catch (Exception e) { 098 ctx.println("Can't get state: " + args[1]); 099 } 100 101 return; 102 103 } else { 104 ctx.println(args[1] + " not found"); 105 } 106 107 } 108}