001    /*
002     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003     *
004     * Copyright (c) 1997-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 java.io.*;
044    import java.util.*;
045    import java.util.logging.Level;
046    import org.glassfish.api.Param;
047    import org.jvnet.hk2.annotations.*;
048    import org.jvnet.hk2.component.*;
049    import org.glassfish.api.admin.*;
050    import com.sun.enterprise.admin.cli.remote.*;
051    import com.sun.enterprise.universal.i18n.LocalStringsImpl;
052    
053    /**
054     * THe restart-domain command.
055     * The local portion of this command is only used to block until:
056     * <ul><li>the old server dies
057     * <li>the new server starts
058     * </ul>
059     * Tactics:
060     * <ul>
061     * <li>Get the uptime for the current server
062     * <li>start the remote Restart command
063     * <li>Call uptime in a loop until the uptime number is less than
064     * the original uptime
065     *
066     * @author bnevins
067     * @author Bill Shannon
068     */
069    @Service(name = "restart-domain")
070    @Scoped(PerLookup.class)
071    public class RestartDomainCommand extends StopDomainCommand {
072    
073        @Param(name = "debug", optional = true)
074        private Boolean debug;
075    
076        @Inject
077        private Habitat habitat;
078        private static final LocalStringsImpl strings =
079                new LocalStringsImpl(RestartDomainCommand.class);
080    
081        /**
082         * Execute the restart-domain command.
083         */
084        @Override
085        protected void doCommand()
086                throws CommandException {
087            
088            if(!isRestartable())
089                throw new CommandException(Strings.get("restartDomain.notRestartable"));
090    
091            // find out how long the server has been up
092            long uptimeOldServer = getUptime();  // may throw CommandException
093    
094            // run the remote restart-domain command and throw away the output
095            RemoteCommand cmd =
096                    new RemoteCommand("restart-domain", programOpts, env);
097            String oldpw = programOpts.getPassword();
098    
099    
100            File pwFile = getServerDirs().getLocalPasswordFile();
101            long stamp = -1;
102    
103            if (pwFile != null)
104                stamp = pwFile.lastModified();
105    
106            if (debug != null)
107                cmd.executeAndReturnOutput("restart-domain", "--debug", debug.toString());
108            else
109                cmd.executeAndReturnOutput("restart-domain");
110    
111            waitForRestart(pwFile, stamp, uptimeOldServer);
112    
113            logger.info(strings.get("restartDomain.success"));
114        }
115    
116        /**
117         * If the server isn't running, try to start it.
118         */
119        @Override
120        protected int dasNotRunning(boolean local) throws CommandException {
121            if (!local)
122                throw new CommandException(
123                    Strings.get("restart.dasNotRunningNoRestart"));
124            logger.warning(strings.get("restart.dasNotRunning"));
125            CLICommand cmd = habitat.getComponent(CLICommand.class, "start-domain");
126            /*
127             * Collect the arguments that also apply to start-domain.
128             * The start-domain CLICommand object will already have the
129             * ProgramOptions injected into it so we don't need to worry
130             * about them here.
131             *
132             * Usage: asadmin [asadmin-utility-options] start-domain
133             *      [-v|--verbose[=<verbose(default:false)>]]
134             *      [--upgrade[=<upgrade(default:false)>]]
135             *      [--debug[=<debug(default:false)>]] [--domaindir <domaindir>]
136             *      [-?|--help[=<help(default:false)>]] [domain_name]
137             *
138             * Only --debug, --domaindir, and the operand apply here.
139             */
140            List<String> opts = new ArrayList<String>();
141            opts.add("start-domain");
142            if (debug != null) {
143                opts.add("--debug");
144                opts.add(debug.toString());
145            }
146            if (domainDirParam != null) {
147                opts.add("--domaindir");
148                opts.add(domainDirParam);
149                // XXX - would this be better?
150                //opts.add(getDomainRootDir().toString());
151            }
152            if (getDomainName() != null)
153                opts.add(getDomainName());
154    
155            return cmd.execute(opts.toArray(new String[opts.size()]));
156        }
157    }