1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
package net.sf.infrared.web; |
24 |
|
|
25 |
|
import java.io.IOException; |
26 |
|
import java.net.BindException; |
27 |
|
|
28 |
|
import javax.servlet.ServletConfig; |
29 |
|
import javax.servlet.ServletContext; |
30 |
|
import javax.servlet.ServletException; |
31 |
|
import javax.servlet.http.HttpServlet; |
32 |
|
import javax.servlet.http.HttpServletRequest; |
33 |
|
import javax.servlet.http.HttpServletResponse; |
34 |
|
|
35 |
|
import net.sf.infrared.base.util.LoggingFactory; |
36 |
|
import net.sf.infrared.collector.Collector; |
37 |
|
import net.sf.infrared.collector.CollectorConfig; |
38 |
|
import net.sf.infrared.collector.impl.CollectorImpl; |
39 |
|
import net.sf.infrared.web.util.DataFetchUtil; |
40 |
|
import net.sf.infrared.web.util.WebConfig; |
41 |
|
|
42 |
|
import org.apache.log4j.Logger; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public class InfraRedListenerServlet extends HttpServlet { |
50 |
|
public static final String PORT_KEY = "port"; |
51 |
|
|
52 |
|
public static final int DEFAULT_PORT = 9001; |
53 |
|
|
54 |
|
private static final Logger logger = |
55 |
|
LoggingFactory.getLogger(InfraRedListenerServlet.class.getName()); |
56 |
|
|
57 |
|
private int port = DEFAULT_PORT; |
58 |
|
|
59 |
|
private Collector collector; |
60 |
|
|
61 |
|
public void init(ServletConfig config) throws ServletException { |
62 |
|
super.init(config); |
63 |
|
initCollector(config); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
public void destroy() { |
71 |
|
if (collector != null) { |
72 |
|
collector.shutdown(); |
73 |
|
} |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
public void doGet(HttpServletRequest req, HttpServletResponse res) |
81 |
|
throws ServletException,IOException { |
82 |
|
|
83 |
|
|
84 |
|
} |
85 |
|
|
86 |
|
void initCollector(ServletConfig config) throws ServletException { |
87 |
|
|
88 |
|
|
89 |
|
String portVal = config.getInitParameter(PORT_KEY); |
90 |
|
try { |
91 |
|
if (portVal != null) { |
92 |
|
port = Integer.parseInt(portVal); |
93 |
|
} |
94 |
|
} |
95 |
|
catch (NumberFormatException numex) { |
96 |
|
String msg = "The value of 'port' init parameter, '" + portVal |
97 |
|
+ "', is not valid one. " + "Legal values are non-zero positive integers"; |
98 |
|
logger.error(msg, numex); |
99 |
|
throw new ServletException(msg, numex); |
100 |
|
} |
101 |
|
if (port <= 0) { |
102 |
|
String msg = "The value of 'port' init parameter, '" + portVal |
103 |
|
+ "', is not valid one. " + "Legal values are non-zero positive integers"; |
104 |
|
logger.error(msg); |
105 |
|
throw new ServletException(msg); |
106 |
|
} |
107 |
|
try { |
108 |
|
startListening(); |
109 |
|
if (logger.isDebugEnabled()) { |
110 |
|
logger.debug("AgentListener thread spawned to listen on port " + port); |
111 |
|
} |
112 |
|
} |
113 |
|
catch (BindException bindex) { |
114 |
|
String msg = "Error while attempting to start listening; port specified is " + port |
115 |
|
+ ". " + "Check if this port is in use by another application"; |
116 |
|
logger.error(msg, bindex); |
117 |
|
throw new ServletException(msg, bindex); |
118 |
|
} |
119 |
|
catch (IOException e) { |
120 |
|
String msg = "Error while attempting to start listening"; |
121 |
|
logger.error(msg, e); |
122 |
|
throw new ServletException(msg, e); |
123 |
|
} |
124 |
|
|
125 |
|
} |
126 |
|
|
127 |
|
void startListening() throws IOException { |
128 |
|
Collector collector = new CollectorImpl(); |
129 |
|
collector.start(new CollectorConfig(){ |
130 |
|
public int getAgentListenPort(){ |
131 |
0 |
return port; |
132 |
|
} |
133 |
0 |
public long getPersistInterval(){ |
134 |
0 |
return WebConfig.getPersistInterval(); |
135 |
|
} |
136 |
|
}); |
137 |
|
DataFetchUtil.setCollector(collector); |
138 |
|
} |
139 |
|
|
140 |
|
int getPort() { |
141 |
|
return this.port; |
142 |
|
} |
143 |
|
|
144 |
|
} |