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.util.TimerTask;
044    import java.util.Timer;
045    import java.util.logging.Logger;
046    import java.io.File;
047    
048    import org.glassfish.api.admin.*;
049    import com.sun.enterprise.admin.cli.remote.RemoteCommand;
050    import com.sun.enterprise.universal.i18n.LocalStringsImpl;
051    
052    import java.awt.Robot;
053    import java.awt.event.KeyEvent;
054    
055    public class MonitorTask extends TimerTask {
056        private String type = null;
057        private String filter = null;
058        private Timer timer = null;
059        private File fileName = null;
060        private String[] remoteArgs;
061        private String exceptionMessage = null;
062        private RemoteCommand cmd;
063        private static final int NUM_ROWS = 25;
064        private int counter = 0;
065    
066        private static final Logger logger =
067            Logger.getLogger(MonitorTask.class.getPackage().getName());
068        private final static LocalStringsImpl strings =
069                                new LocalStringsImpl(MonitorTask.class);
070    
071        public MonitorTask(final Timer timer, final String[] remoteArgs,
072                ProgramOptions programOpts, Environment env,
073                final String type, final String filter, final File fileName)
074                throws CommandException, CommandValidationException {
075            this.timer = timer;
076            if ((type != null) && (type.length() > 0))
077                this.type = type;
078            if ((filter != null) && (filter.length() > 0))
079                this.filter = filter;
080            this.fileName = fileName;
081            this.remoteArgs = remoteArgs;
082            cmd = new RemoteCommand(remoteArgs[0], programOpts, env);
083            displayHeader(type);
084    
085        }
086    
087        void displayHeader(String type) {
088            // print title
089            String title = "";
090            if ("servlet".equals(type)) {
091                title = String.format("%1$-10s %2$-10s %3$-10s",
092                "aslc", "mslc", "tslc");
093            } else if ("httplistener".equals(type)) {
094                title = String.format("%1$-4s %2$-4s %3$-6s %4$-4s",
095                "ec", "mt", "pt", "rc");
096            } else if ("jvm".equals(type)) {
097                title = String.format("%1$45s", strings.get("monitor.jvm.title"));
098                logger.info(title);
099                // row title
100                title = null;
101                if (filter != null) {
102                    if (("heapmemory".equals(filter)) ||
103                            ("nonheapmemory".equals(filter))) {
104                        title = String.format("%1$-10s %2$-10s %3$-10s %4$-10s",
105                            "init", "used", "committed", "max");
106                    }
107                }
108                if (title == null) {
109                    // default jvm stats
110                    title = String.format("%1$-35s %2$-40s", strings.get("monitor.jvm.uptime.title"),
111                                            strings.get("monitor.jvm.memory.title"));
112                    logger.info(title);
113                    title = String.format(
114                        "%1$-25s %2$-10s %3$-10s %4$-10s %5$-10s %6$-10s",
115                        strings.get("monitor.jvm.current"), strings.get("monitor.jvm.min"), 
116                        strings.get("monitor.jvm.max"), strings.get("monitor.jvm.low"),
117                        strings.get("monitor.jvm.high"), strings.get("monitor.jvm.count"));
118                }
119            } else if ("webmodule".equals(type)) {
120                title = String.format(
121        "%1$-5s %2$-5s %3$-5s %4$-5s %5$-5s %6$-5s %7$-5s %8$-8s %9$-10s %10$-5s",
122        "asc", "ast", "rst", "st", "ajlc", "mjlc", "tjlc", "aslc", "mslc", "tslc");
123            }
124            logger.info(title);
125        }
126    
127        void cancelMonitorTask() {
128            timer.cancel();
129            try {
130                Robot robot = new Robot();
131                robot.keyPress(KeyEvent.VK_Q);
132                robot.keyRelease(KeyEvent.VK_Q);
133                robot.keyPress(KeyEvent.VK_ENTER);
134                robot.keyRelease(KeyEvent.VK_ENTER);
135            } catch (java.awt.AWTException e) {
136                logger.severe(strings.get("awt.error", e.getMessage()));
137            }
138        }
139    
140        public void run() {
141            try {
142                cmd.execute(remoteArgs);
143                if (counter == NUM_ROWS) {
144                        displayHeader(type);
145                        counter = 0;
146                    }
147               counter++;
148            } catch (Exception e) {
149                //logger.severe(
150                        //strings.get("monitorCommand.errorRemote", e.getMessage()));
151                cancelMonitorTask();
152                exceptionMessage = e.getMessage();
153            }
154        }
155    
156        public String getExceptionMessage() {
157            return exceptionMessage;
158        }
159     
160        public void displayDetails(){
161            String details = "";
162            if ("servlet".equals(type)) {
163                details = strings.get("commands.monitor.servlet_detail");
164            } else if ("httplistener".equals(type)) {
165                details = strings.get("commands.monitor.httplistener_detail");
166            } else if ("jvm".equals(type)) {
167                //no details
168            } else if ("webmodule".equals(type)) {
169                details = strings.get("commands.monitor.webmodule_virtual_server_detail");
170            }
171            logger.info(details);
172        }
173     
174    /*    
175        synchronized void writeToFile(final String text) {
176            try {
177                BufferedWriter out =
178                            new BufferedWriter(new FileWriter(fileName, true));
179                out.append(text);
180                out.newLine();
181                out.close();
182            } catch (IOException ioe) {
183                final String unableToWriteFile =
184                    strings.getString("commands.monitor.unable_to_write_to_file",
185                    fileName.getName());
186                logger.info(unableToWriteFile);
187                //if (verbose) {
188                    //ioe.printStackTrace();
189                //}
190            }
191        }
192     */
193    }