SHARE
TWEET

ServerRunner.java

olegtikhonov Apr 1st, 2013 341 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Licensed under the Apache License, Version 2.0 (the "License");
  3.  *  you may not use this file except in compliance with the License.
  4.  *  You may obtain a copy of the License at
  5.  *
  6.  *       http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  *  Unless required by applicable law or agreed to in writing, software
  9.  *  distributed under the License is distributed on an "AS IS" BASIS,
  10.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  *  See the License for the specific language governing permissions and
  12.  *  limitations under the License.
  13.  */
  14. package org.example.tcpserver;
  15.  
  16. import org.apache.log4j.Logger;
  17. import org.springframework.context.support.ClassPathXmlApplicationContext;
  18. import org.springframework.core.env.CommandLinePropertySource;
  19. import org.springframework.core.env.SimpleCommandLinePropertySource;
  20. import org.springframework.context.support.AbstractApplicationContext;
  21.  
  22. /**
  23.  * Defines a tcp server.
  24.  */
  25. public class ServerRunner {
  26.     /* Local logger. */
  27.     private static final Logger LOG = Logger.getLogger(ServerRunner.class);
  28.     /* Command line parameter holder. */
  29.  
  30.     /**
  31.      * Famous main function of the tcp server.
  32.      *
  33.      * @param args to be parsed.
  34.      */
  35.     public static void main(String[] args) {
  36.         @SuppressWarnings("rawtypes")
  37.                 CommandLinePropertySource clps = processProperties(args);
  38.         /* Spring Integration context used to get desirable beans. */
  39.         AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"server-config.xml"}, false);
  40.         context.getEnvironment().getPropertySources().addFirst(clps);
  41.         context.refresh();
  42.         context.registerShutdownHook();
  43.     }
  44.  
  45.  
  46.     @SuppressWarnings("rawtypes")
  47.         private static CommandLinePropertySource processProperties(String[] args) throws IllegalArgumentException {
  48.         CommandLinePropertySource result = new SimpleCommandLinePropertySource(args);
  49.         /* Parses the command line arguments. */
  50.         if(result.containsProperty("file")) {
  51.             LOG.info("File to be sent=" + result.getProperty("file"));
  52.         } else {
  53.             LOG.warn("Please provide --file=<filename>");
  54.             throw new IllegalArgumentException("Please provide --file");
  55.         }
  56.         if(result.containsProperty("port")) {
  57.             LOG.info("Listening to port="+result.getProperty("port"));
  58.         } else {
  59.             LOG.warn("Listening to default port");
  60.         }
  61.         return result;
  62.     }
  63. }
RAW Paste Data
Top