001    /*
002     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003     *
004     * Copyright (c) 2008-2011 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 org.jvnet.hk2.annotations.*;
044    import org.jvnet.hk2.component.*;
045    import org.glassfish.api.Param;
046    import org.glassfish.api.admin.*;
047    import com.sun.appserv.server.util.Version;
048    import com.sun.enterprise.admin.cli.remote.*;
049    import com.sun.enterprise.universal.i18n.LocalStringsImpl;
050    import org.glassfish.branding.GlassFishBranding;
051    
052    /**
053     * A local version command.
054     * Prints the version of the server, if running. Prints the version from locally
055     * available Version class if server is not running, if the --local flag is passed
056     * or if the version could not
057     * be obtained from a server for some reason. The idea is to get the version
058     * of server software, the server process need not be running. This command
059     * does not return the version of local server installation if its
060     * options (host, port, user, passwordfile) identify a running server.
061     * 
062     * @author km@dev.java.net
063     * @author Bill Shannon
064     */
065    @Service(name = "version")
066    @Scoped(PerLookup.class)
067    public class VersionCommand extends CLICommand {
068    
069        @Param(optional = true, shortName = "v")
070        private boolean verbose;
071    
072        @Param(optional = true)
073        private boolean local;
074    
075        @Param(optional = true)
076        private boolean terse;
077    
078        private static final LocalStringsImpl strings =
079                new LocalStringsImpl(VersionCommand.class);
080    
081        @Override
082        protected int executeCommand() throws CommandException {
083            if (local) {
084                invokeLocal();
085                return 0;
086            }
087            try {
088                RemoteCommand cmd = new RemoteCommand("version", programOpts, env);
089                String version;
090                if (verbose)
091                    version = cmd.executeAndReturnOutput("version", "--verbose");
092                else
093                    version = cmd.executeAndReturnOutput("version");
094                version = version.trim();   // get rid of gratuitous newlines
095                logger.info(terse ? version : strings.get("version.remote", version));
096            } catch (Exception e) {
097                // suppress all output and infer that the server is not running
098                printRemoteException(e);
099                invokeLocal();
100            }
101            return 0;       // always succeeds
102        }
103    
104        private void invokeLocal() {
105    
106            GlassFishBranding br = new GlassFishBranding();
107            br.postConstruct();
108            Version version = new Version();
109            version.setBranding(br);
110            version.postConstruct();
111    
112            String fv = Version.getFullVersion();
113    
114            logger.info(terse ? fv : strings.get("version.local", fv));
115            if (verbose)
116                logger.info(strings.get("version.local.java",
117                                        System.getProperty("java.version")));
118        }
119    
120        private void printRemoteException(Exception e) {
121            if (CLIConstants.debug())
122                logger.info(strings.get("remote.version.failed.debug",
123                    programOpts.getHost(), programOpts.getPort() + ""));
124            else
125                logger.info(strings.get("remote.version.failed.non-debug", 
126                    programOpts.getHost(), programOpts.getPort() + ""));
127            logger.finer(e.getMessage());
128        }
129    }