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 org.jvnet.hk2.annotations.*;
046    import org.jvnet.hk2.component.*;
047    import org.glassfish.api.Param;
048    import org.glassfish.api.admin.*;
049    import com.sun.enterprise.universal.i18n.LocalStringsImpl;
050    
051    /**
052     * The help command will display the help text for all the commands and their
053     * options
054     */
055    @Service(name = "help")
056    @Scoped(PerLookup.class)
057    public class HelpCommand extends CLICommand {
058        @Inject
059        private Habitat habitat;
060    
061        private static final int DEFAULT_PAGE_LENGTH = 50;
062        private static final int NO_PAGE_LENGTH = -1;
063        private static final String DEFAULT_HELP_PAGE = "help";
064    
065        private static final LocalStringsImpl strings =
066                new LocalStringsImpl(HelpCommand.class);
067    
068        @Param(name = "command-name", primary = true, optional = true)
069        private String cmd;
070    
071        @Override
072        protected int executeCommand()
073                throws CommandException, CommandValidationException {
074            try {
075                new More(getPageLength(),
076                    getSource(),
077                    getDestination(),
078                    getUserInput(),
079                    getUserOutput(),
080                    getQuitChar(),
081                    getPrompt());
082            } catch (IOException ioe) {
083                throw new CommandException(ioe);
084            }
085            return 0;
086        }
087    
088        private String getCommandName() {
089            return cmd != null ? cmd : DEFAULT_HELP_PAGE;
090        }
091    
092        private Writer getDestination() {
093            return new OutputStreamWriter(System.out);
094        }
095    
096        private int getPageLength() {
097          if (programOpts.isInteractive())
098              return DEFAULT_PAGE_LENGTH;
099          else
100              return NO_PAGE_LENGTH;
101        }
102    
103        private String getPrompt() {
104            return strings.get("ManpagePrompt");
105        }
106     
107        private String getQuitChar() {
108            return strings.get("ManpageQuit");
109        }
110    
111        private Reader getSource()
112                throws CommandException, CommandValidationException {
113            CLICommand cmd = CLICommand.getCommand(habitat, getCommandName());
114            Reader r = cmd.getManPage();
115            if (r == null)
116                throw new CommandException(
117                            strings.get("ManpageMissing", getCommandName()));
118            return r;
119        }
120    
121        private Reader getUserInput() {
122            return new InputStreamReader(System.in);
123        }
124    
125        private Writer getUserOutput() {
126            return new OutputStreamWriter(System.err);
127        }
128    }