View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.ftp.core.command.internal;
22  
23  import goldengate.common.command.ReplyCode;
24  import goldengate.common.command.exception.Reply500Exception;
25  import goldengate.common.command.exception.Reply501Exception;
26  import goldengate.common.logging.GgInternalLogger;
27  import goldengate.common.logging.GgInternalLoggerFactory;
28  import goldengate.ftp.core.command.AbstractCommand;
29  import goldengate.ftp.core.config.FtpConfiguration;
30  import goldengate.ftp.core.utils.FtpChannelUtils;
31  
32  import org.jboss.netty.channel.ChannelFuture;
33  import org.jboss.netty.channel.ChannelFutureListener;
34  import org.jboss.netty.channel.Channels;
35  
36  /**
37   * Internal shutdown command that will shutdown the FTP service with a password
38   *
39   * @author Frederic Bregier
40   *
41   */
42  public class INTERNALSHUTDOWN extends AbstractCommand {
43      /**
44       * Internal Logger
45       */
46      private static final GgInternalLogger logger = GgInternalLoggerFactory
47              .getLogger(INTERNALSHUTDOWN.class);
48  
49      /**
50       *
51       * @author Frederic Bregier
52       *
53       */
54      private class ShutdownChannelFutureListener implements
55              ChannelFutureListener {
56  
57          private final FtpConfiguration configuration;
58  
59          protected ShutdownChannelFutureListener(FtpConfiguration configuration) {
60              this.configuration = configuration;
61          }
62  
63          /*
64           * (non-Javadoc)
65           *
66           * @see
67           * org.jboss.netty.channel.ChannelFutureListener#operationComplete(org
68           * .jboss.netty.channel.ChannelFuture)
69           */
70          public void operationComplete(ChannelFuture arg0) throws Exception {
71              Channels.close(arg0.getChannel());
72              FtpChannelUtils.teminateServer(configuration);
73  
74          }
75  
76      }
77  
78      /*
79       * (non-Javadoc)
80       *
81       * @see goldengate.ftp.core.command.AbstractCommand#exec()
82       */
83      public void exec() throws Reply501Exception, Reply500Exception {
84          if (!getSession().getAuth().isAdmin()) {
85              // not admin
86              throw new Reply500Exception("Command Not Allowed");
87          }
88          if (!hasArg()) {
89              throw new Reply501Exception("Shutdown Need password");
90          }
91          String password = getArg();
92          if (!getConfiguration().checkPassword(password)) {
93              throw new Reply501Exception("Shutdown Need a correct password");
94          }
95          logger.warn("Shutdown...");
96          getSession().setReplyCode(
97                  ReplyCode.REPLY_221_CLOSING_CONTROL_CONNECTION,
98                  "System shutdown");
99          getSession().getNetworkHandler().writeIntermediateAnswer().addListener(
100                 new ShutdownChannelFutureListener(getConfiguration()));
101     }
102 
103 }