Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.example.tcpserver;
- import org.apache.log4j.Logger;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.env.CommandLinePropertySource;
- import org.springframework.core.env.SimpleCommandLinePropertySource;
- import org.springframework.context.support.AbstractApplicationContext;
- /**
- * Defines a tcp server.
- */
- public class ServerRunner {
- /* Local logger. */
- private static final Logger LOG = Logger.getLogger(ServerRunner.class);
- /* Command line parameter holder. */
- /**
- * Famous main function of the tcp server.
- *
- * @param args to be parsed.
- */
- public static void main(String[] args) {
- @SuppressWarnings("rawtypes")
- CommandLinePropertySource clps = processProperties(args);
- /* Spring Integration context used to get desirable beans. */
- AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"server-config.xml"}, false);
- context.getEnvironment().getPropertySources().addFirst(clps);
- context.refresh();
- context.registerShutdownHook();
- }
- @SuppressWarnings("rawtypes")
- private static CommandLinePropertySource processProperties(String[] args) throws IllegalArgumentException {
- CommandLinePropertySource result = new SimpleCommandLinePropertySource(args);
- /* Parses the command line arguments. */
- if(result.containsProperty("file")) {
- LOG.info("File to be sent=" + result.getProperty("file"));
- } else {
- LOG.warn("Please provide --file=<filename>");
- throw new IllegalArgumentException("Please provide --file");
- }
- if(result.containsProperty("port")) {
- LOG.info("Listening to port="+result.getProperty("port"));
- } else {
- LOG.warn("Listening to default port");
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement