1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.transport;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * <a href="MulticastClientTool.java.html"><b><i>View Source</i></b></a>
33   *
34   * <p>
35   * A client that listens for multicast messages at a designated port. You may
36   * use this to for potential multicast issues when tuning distributed caches.
37   * </p>
38   *
39   * @author Michael C. Han
40   * @author Raymond Augé
41   */
42  public class MulticastClientTool {
43  
44      public static void main(String[] args) {
45          try {
46              new MulticastClientTool(args);
47          }
48          catch (Exception e) {
49              _log.error(e);
50  
51              StringBuilder sb = new StringBuilder();
52  
53              sb.append("Usage: java -classpath ");
54              sb.append("commons-logging.jar:util-java.jar ");
55              sb.append("com.liferay.util.transport.MulticastClientTool [-g] ");
56              sb.append("[-s] -h [multicastAddress] -p [port]");
57  
58              System.err.println(sb.toString());
59  
60              System.exit(1);
61          }
62      }
63  
64      private MulticastClientTool(String[] args) throws Exception {
65          Map<String, Object> argsMap = _getArgsMap(args);
66  
67          Integer port = (Integer)argsMap.get("port");
68          String host = (String)argsMap.get("host");
69          Boolean gzipData = (Boolean)argsMap.get("gzip");
70          Boolean shortData = (Boolean)argsMap.get("short");
71  
72          DatagramHandler handler = new MulticastDatagramHandler(
73              gzipData.booleanValue(), shortData.booleanValue());
74  
75          MulticastTransport transport = new MulticastTransport(
76              handler, host, port);
77  
78          if (_log.isInfoEnabled()) {
79              if (shortData.booleanValue()) {
80                  _log.info("Truncating to 96 bytes.");
81              }
82  
83              _log.info("Started up and waiting...");
84          }
85  
86          transport.connect();
87  
88          synchronized (transport) {
89              transport.wait();
90          }
91      }
92  
93      private Map<String, Object> _getArgsMap(String[] args)
94          throws Exception {
95  
96          Map<String, Object> argsMap = new HashMap<String, Object>();
97  
98          for (int i = 0; i < args.length; i++) {
99              if (args[i].equals("-g")) {
100                 argsMap.put("gzip", Boolean.TRUE);
101             }
102             else if (args[i].equals("-s")) {
103                 argsMap.put("short", Boolean.TRUE);
104             }
105             else if (args[i].equals("-h")) {
106                 argsMap.put("host", args[i + 1]);
107 
108                 i++;
109             }
110             else if (args[i].equals("-p")) {
111                 argsMap.put("port", new Integer(args[i + 1]));
112 
113                 i++;
114             }
115         }
116 
117         if (!argsMap.containsKey("gzip")) {
118             argsMap.put("gzip", Boolean.FALSE);
119         }
120 
121         if (!argsMap.containsKey("short")) {
122             argsMap.put("short", Boolean.FALSE);
123         }
124 
125         return argsMap;
126     }
127 
128     private static final Log _log = LogFactoryUtil.getLog(
129         MulticastClientTool.class);
130 
131 }