Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   13    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   14    * License for the specific language governing permissions and limitations under
   15    * the License.
   16    */
   17   package org.apache.sling.maven.projectsupport;
   18   
   19   import java.io.BufferedReader;
   20   import java.io.BufferedWriter;
   21   import java.io.IOException;
   22   import java.io.InputStreamReader;
   23   import java.io.OutputStreamWriter;
   24   import java.net.ConnectException;
   25   import java.net.InetAddress;
   26   import java.net.InetSocketAddress;
   27   import java.net.ServerSocket;
   28   import java.net.Socket;
   29   import java.net.SocketAddress;
   30   import java.net.UnknownHostException;
   31   
   32   import org.apache.maven.plugin.logging.Log;
   33   
   34   /**
   35    * This class is adapted from org.apache.sling.launchpad.app.ControlListener.
   36    */
   37   class ControlListener implements Runnable {
   38       // command sent by the client to cause Sling to shutdown
   39       static final String COMMAND_STOP = "stop";
   40   
   41       // command sent by the client to check for the status of the server
   42       static final String COMMAND_STATUS = "status";
   43   
   44       // the response sent by the server if the command executed successfully
   45       private static final String RESPONSE_OK = "OK";
   46   
   47       // The default port to listen on and to connect to
   48       private static final int DEFAULT_LISTEN_PORT = 63000;
   49   
   50       /** The mojo */
   51       private AbstractLaunchpadStartingPlugin mojo;
   52   
   53       /** The log object */
   54       private final Log log;
   55   
   56       /** The socket address used for control communication */
   57       private final SocketAddress socketAddress;
   58   
   59       ControlListener(AbstractLaunchpadStartingPlugin mojo, Log log, String host, int port) {
   60           this.mojo = mojo;
   61           this.log = log;
   62           this.socketAddress = getSocketAddress(host, port);
   63       }
   64   
   65       /**
   66        * Implements the server side of the control connection starting a thread
   67        * listening on the host and port configured on setup of this instance.
   68        */
   69       void listen() {
   70           if (socketAddress != null) {
   71               Thread listener = new Thread(this);
   72               listener.setDaemon(true);
   73               listener.setName("Sling Control Listener@" + socketAddress);
   74               listener.start();
   75           } else {
   76               log.info("No socket address to listen to");
   77           }
   78       }
   79   
   80       /**
   81        * Implements the client side of the control connection sending the command
   82        * to shutdown Sling.
   83        */
   84       void shutdownServer() {
   85           sendCommand(COMMAND_STOP);
   86       }
   87   
   88       /**
   89        * Implements the client side of the control connection sending the command
   90        * to check whether Sling is active.
   91        */
   92       void statusServer() {
   93           sendCommand(COMMAND_STATUS);
   94       }
   95   
   96       // ---------- Runnable interface
   97   
   98       /**
   99        * Implements the server thread receiving commands from clients and acting
  100        * upon them.
  101        */
  102       public void run() {
  103           ServerSocket server = null;
  104           try {
  105               server = new ServerSocket();
  106               server.bind(socketAddress);
  107               log.info("Sling Control Server started on " + socketAddress.toString());
  108           } catch (IOException ioe) {
  109               log.error("Failed to start Sling Control Server", ioe);
  110               return;
  111           }
  112   
  113           try {
  114               while (true) {
  115   
  116                   Socket s = server.accept();
  117                   try {
  118                       String command = readLine(s);
  119                       log.info(s.getRemoteSocketAddress() + ">" + command);
  120   
  121                       if (COMMAND_STOP.equals(command)) {
  122                           if (mojo != null) {
  123                               mojo.stopSling();
  124                           }
  125                           writeLine(s, RESPONSE_OK);
  126   
  127                           log.info("Sling shut down, stopping Sling.");
  128                           mojo.stopSling();
  129   
  130                       } else if (COMMAND_STATUS.equals(command)) {
  131                           writeLine(s, RESPONSE_OK);
  132   
  133                       } else {
  134                           writeLine(s, "ERR:" + command);
  135   
  136                       }
  137                   } finally {
  138                       try {
  139                           s.close();
  140                       } catch (IOException ignore) {
  141                       }
  142                   }
  143               }
  144           } catch (IOException ioe) {
  145               log.error("Failure reading from client", ioe);
  146           } finally {
  147               try {
  148                   server.close();
  149               } catch (IOException ignore) {
  150               }
  151           }
  152       }
  153   
  154       // ---------- socket support
  155   
  156       private SocketAddress getSocketAddress(String host, int port) {
  157           try {
  158               if (port == -1) {
  159                   port = DEFAULT_LISTEN_PORT;
  160               }
  161   
  162               if (host != null) {
  163                   return new InetSocketAddress(host, port);
  164               } else {
  165   
  166                   return new InetSocketAddress(InetAddress.getLocalHost(), port);
  167               }
  168           } catch (UnknownHostException uhe) {
  169               log.error("Unknown host in '" + host + "': " + uhe.getMessage(), null);
  170           }
  171   
  172           return null;
  173       }
  174   
  175       private void sendCommand(String command) {
  176           if (socketAddress != null) {
  177               Socket socket = null;
  178               try {
  179                   socket = new Socket();
  180                   socket.connect(socketAddress);
  181                   writeLine(socket, command);
  182                   String result = readLine(socket);
  183                   log.info("Sent '" + command + "' to " + socketAddress + ": " + result, null);
  184               } catch (ConnectException ce) {
  185                   log.info("No Sling running at " + socketAddress, null);
  186               } catch (IOException ioe) {
  187                   log.error("Failed sending '" + command + "' to " + socketAddress, ioe);
  188               } finally {
  189                   if (socket != null) {
  190                       try {
  191                           socket.close();
  192                       } catch (IOException ignore) {
  193                       }
  194                   }
  195               }
  196           } else {
  197               log.info("No socket address to send '" + command + "' to", null);
  198           }
  199       }
  200   
  201       private String readLine(Socket socket) throws IOException {
  202           BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
  203           return br.readLine();
  204       }
  205   
  206       private void writeLine(Socket socket, String line) throws IOException {
  207           BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
  208           bw.write(line);
  209           bw.write("\r\n");
  210           bw.flush();
  211       }
  212   }

Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]