001    /*
002     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003     *
004     * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
005     *
006     * The contents of this file are subject to the terms of either the GNU
007     * General Public License Version 2 only ("GPL") or the Common Development
008     * and Distribution License("CDDL") (collectively, the "License").  You
009     * may not use this file except in compliance with the License.  You can
010     * obtain a copy of the License at
011     * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
012     * or packager/legal/LICENSE.txt.  See the License for the specific
013     * language governing permissions and limitations under the License.
014     *
015     * When distributing the software, include this License Header Notice in each
016     * file and include the License file at packager/legal/LICENSE.txt.
017     *
018     * GPL Classpath Exception:
019     * Oracle designates this particular file as subject to the "Classpath"
020     * exception as provided by Oracle in the GPL Version 2 section of the License
021     * file that accompanied this code.
022     *
023     * Modifications:
024     * If applicable, add the following below the License Header, with the fields
025     * enclosed by brackets [] replaced by your own identifying information:
026     * "Portions Copyright [year] [name of copyright owner]"
027     *
028     * Contributor(s):
029     * If you wish your version of this file to be governed by only the CDDL or
030     * only the GPL Version 2, indicate your decision by adding "[Contributor]
031     * elects to include this software in this distribution under the [CDDL or GPL
032     * Version 2] license."  If you don't indicate a single choice of license, a
033     * recipient has the option to distribute your version of this file under
034     * either the CDDL, the GPL Version 2 or to extend the choice of license to
035     * its licensees as provided above.  However, if you add GPL Version 2 code
036     * and therefore, elected the GPL Version 2 license, then the option applies
037     * only if the new code is made subject to such option by the copyright
038     * holder.
039     */
040    
041    package com.sun.enterprise.admin.cli;
042    
043    import java.io.*;
044    import java.util.*;
045    import java.util.logging.*;
046    import org.jvnet.hk2.annotations.*;
047    import org.jvnet.hk2.component.*;
048    import org.glassfish.api.Param;
049    import org.glassfish.api.admin.*;
050    import com.sun.enterprise.admin.cli.remote.RemoteCommand;
051    import com.sun.enterprise.universal.i18n.LocalStringsImpl;
052    
053    /**
054     * A local Monitor Command (this will call the remote 'monitor' command).
055     * The reason for having to implement this as local is to interpret the options
056     * --interval and --filename(TBD) options.
057     * 
058     * @author Prashanth
059     * @author Bill Shannon
060     */
061    @Service(name = "monitor")
062    @Scoped(PerLookup.class)
063    public class MonitorCommand extends CLICommand {
064    
065        @Param(optional = true, defaultValue = "30")
066        private int interval = 30;  // default 30 seconds
067    
068        @Param
069        private String type;
070    
071        @Param(optional = true)
072        private String filter;
073    
074        @Param(optional = true)
075        private File fileName;
076    
077        @Param(primary = true, optional = true)
078        private String target;      // XXX - not currently used
079    
080        private static final LocalStringsImpl strings =
081                new LocalStringsImpl(MonitorCommand.class);
082    
083        @Override
084        protected int executeCommand() 
085                throws CommandException, CommandValidationException {
086            // Based on interval, loop the subject to print the output
087            Timer timer = new Timer();
088            try {
089                MonitorTask monitorTask = new MonitorTask(timer, getRemoteArgs(),
090                                programOpts, env, type, filter, fileName);
091                timer.scheduleAtFixedRate(monitorTask, 0, interval * 1000);
092    
093                boolean done = false;
094                // detect if a q or Q key is entered
095                while (!done) {
096                    // The native doesn't exist yet
097                    //final char c = new CliUtil().getKeyboardInput();
098                    //final char c = 'p';
099                    final String str = new BufferedReader(
100                                    new InputStreamReader(System.in)).readLine(); 
101                    if (str.equals("q") || str.equals("Q")) {
102                        timer.cancel();
103                        done = true;
104                        String exceptionMessage = monitorTask.getExceptionMessage();
105                        if (exceptionMessage != null) {
106                            throw new CommandException(exceptionMessage);
107                        }
108                    } else if (str.equals("h") || str.equals("H")) {
109                        monitorTask.displayDetails();
110                    }
111                }
112            } catch (Exception e) {
113                timer.cancel();
114                throw new CommandException(
115                    strings.get("monitorCommand.errorRemote", e.getMessage()));
116            }
117            return 0;
118        }
119    
120        private String[] getRemoteArgs() {
121            List<String> list = new ArrayList<String>(5);
122            list.add("monitor");
123     
124            if (ok(type)) {
125                list.add("--type");
126                list.add(type);
127            }
128            if (ok(filter)) {
129                list.add("--filter");
130                list.add(filter);
131            }
132            return list.toArray(new String[list.size()]);
133        }
134    }