001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.console; 018 019import java.io.ByteArrayOutputStream; 020import java.util.ArrayList; 021import java.util.List; 022import java.util.StringTokenizer; 023 024import javax.jms.TextMessage; 025 026import org.apache.activemq.broker.util.CommandHandler; 027import org.apache.activemq.console.command.ShellCommand; 028import org.apache.activemq.console.formatter.CommandShellOutputFormatter; 029 030/** 031 * A default implementation of the @{link CommandHandler} interface 032 * 033 * 034 */ 035public class ConsoleCommandHandler implements CommandHandler { 036 037 private ShellCommand command = new ShellCommand(true); 038 039 public void processCommand(TextMessage request, TextMessage response) throws Exception { 040 041 ByteArrayOutputStream out = new ByteArrayOutputStream(); 042 CommandContext ctx = new CommandContext(); 043 ctx.setFormatter(new CommandShellOutputFormatter(out)); 044 045 // lets turn the text into a list of arguments 046 String requestText = request.getText(); 047 048 List<String> tokens = tokenize(requestText); 049 command.setCommandContext(ctx); 050 command.execute(tokens); 051 052 out.flush(); 053 byte[] bytes = out.toByteArray(); 054 055 String answer = new String(bytes); 056 057 response.setText(answer); 058 } 059 060 protected List<String> tokenize(String text) { 061 List<String> answer = new ArrayList<String>(); 062 StringTokenizer iter = new StringTokenizer(text); 063 while (iter.hasMoreTokens()) { 064 answer.add(iter.nextToken()); 065 } 066 return answer; 067 } 068}