View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  // Checkstyle: UseLogger off
19  package ch.elca.el4j.services.tcpforwarder;
20  
21  import java.net.InetSocketAddress;
22  
23  import org.springframework.util.StringUtils;
24  
25  // Checkstyle: UncommentedMain off
26  /**
27   * Used to start the tcp forwarder from command line.
28   *
29   * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/tcp_forwarder/src/main/java/ch/elca/el4j/services/tcpforwarder/TcpForwarderRunner.java $
30   *
31   * @author Martin Zeltner (MZE)
32   */
33  public class TcpForwarderRunner {
34  	/**
35  	 * Hide default constructor.
36  	 */
37  	protected TcpForwarderRunner() { }
38  	
39  	/**
40  	 * Runs a tcp forwarder on a given port and forwards the tcp messages to
41  	 * the given target address.
42  	 *
43  	 * @param args
44  	 *            The first argument is the local listening port and the second
45  	 *            argument is the local port or remote target with its port to
46  	 *            forward to.
47  	 */
48  	public static void main(String[] args) {
49  		int listeningPort = 0;
50  		InetSocketAddress targetAddress = null;
51  		if (args.length != 2) {
52  			printUsage();
53  		} else {
54  			try {
55  				listeningPort = Integer.parseInt(args[0]);
56  				
57  				String target = args[1];
58  				String targetHost = null;
59  				int targetPort = 0;
60  				if (StringUtils.hasText(target)) {
61  					int i = target.indexOf(':');
62  					if (i > 0) {
63  						targetHost = target.substring(0, i);
64  					}
65  					if (i < target.length() - 1) {
66  						targetPort = Integer.parseInt(target.substring(i + 1));
67  					}
68  				}
69  				if (targetHost == null) {
70  					targetAddress = new InetSocketAddress(targetPort);
71  				} else {
72  					targetAddress
73  						= new InetSocketAddress(targetHost, targetPort);
74  				}
75  			} catch (Exception e) {
76  				e.printStackTrace();
77  				printUsage();
78  			}
79  		}
80  		
81  		if (listeningPort <= 0 || targetAddress == null) {
82  			return;
83  		}
84  		
85  		new TcpForwarder(listeningPort, targetAddress);
86  	}
87  
88  	/**
89  	 * Prints the usage of this main class.
90  	 */
91  	protected static void printUsage() {
92  		System.out.println(
93  			"Usage: java TcpForwarderRunner listeningPort targetAddress");
94  		System.out.println(
95  			"   Example: java TcpForwarderRunner 6786 tulipe.elca.ch:1521");
96  	}
97  }
98  // Checkstyle: UseLogger on
99  // Checkstyle: UncommentedMain on