%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.collector.impl.transport.AgentThread |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | * |
|
16 | * |
|
17 | * |
|
18 | * Original Author: Ceki Gülcü |
|
19 | * Contributor(s): -; Moses Hohman <mmhohman@rainbow.uchicago.edu>, binil.thomas (Tavant Technologies) |
|
20 | * |
|
21 | */ |
|
22 | package net.sf.infrared.collector.impl.transport; |
|
23 | ||
24 | import java.io.BufferedInputStream; |
|
25 | import java.io.IOException; |
|
26 | import java.io.ObjectInputStream; |
|
27 | import java.net.Socket; |
|
28 | ||
29 | import net.sf.infrared.base.model.ApplicationStatistics; |
|
30 | import net.sf.infrared.base.util.LoggingFactory; |
|
31 | import net.sf.infrared.collector.StatisticsRepository; |
|
32 | ||
33 | import org.apache.log4j.Logger; |
|
34 | import org.apache.log4j.spi.LoggingEvent; |
|
35 | ||
36 | /** |
|
37 | * Read {@link LoggingEvent} objects sent from a remote client using Sockets |
|
38 | * (TCP). These logging events are logged according to local policy, as if they |
|
39 | * were generated locally. |
|
40 | * |
|
41 | * <p> |
|
42 | * For example, the socket node might decide to log events to a local file and |
|
43 | * also resent them to a second socket node. |
|
44 | * |
|
45 | */ |
|
46 | public class AgentThread extends Thread { |
|
47 | private Socket socket; |
|
48 | ||
49 | /* The listener thread that spawned this thread */ |
|
50 | private AgentListener parentListener; |
|
51 | ||
52 | private ObjectInputStream ois; |
|
53 | ||
54 | private StatisticsRepository statsRepository; |
|
55 | ||
56 | 0 | private static Logger logger = LoggingFactory.getLogger(AgentThread.class.getName()); |
57 | ||
58 | 0 | public AgentThread(Socket socket, AgentListener listener, StatisticsRepository statsRepository) { |
59 | 0 | this.statsRepository = statsRepository; |
60 | 0 | this.socket = socket; |
61 | 0 | this.parentListener = listener; |
62 | 0 | setName("InfraRED-Agent-Communication:" + socket); |
63 | try { |
|
64 | 0 | ois = new ObjectInputStream(class="keyword">new BufferedInputStream(socket.getInputStream())); |
65 | 0 | } catch (IOException e) { |
66 | 0 | logger.error("Could not open stream to read from " + socket, e); |
67 | 0 | } |
68 | 0 | } |
69 | ||
70 | public void run() { |
|
71 | try { |
|
72 | while (true) { |
|
73 | 0 | ApplicationStatistics newStats = (ApplicationStatistics) ois.readObject(); |
74 | 0 | statsRepository.addStatistics(newStats); |
75 | 0 | if (logger.isDebugEnabled()) { |
76 | 0 | logger.debug("Received statistics from " + newStats.getApplicationName() + "#" |
77 | + newStats.getInstanceId()); |
|
78 | } |
|
79 | } |
|
80 | 0 | } catch (Exception e) { |
81 | 0 | logger.error("Unexpected exception while reading from socket; closing connection", e); |
82 | } finally { |
|
83 | 0 | shutdown(); |
84 | 0 | } |
85 | 0 | } |
86 | ||
87 | public boolean isOpen() { |
|
88 | 0 | return !socket.isClosed(); |
89 | } |
|
90 | ||
91 | public void shutdown() { |
|
92 | try { |
|
93 | 0 | ois.close(); |
94 | 0 | } catch (Exception e) { |
95 | 0 | logger.info("Could not close connection", e); |
96 | 0 | } |
97 | try { |
|
98 | 0 | socket.close(); |
99 | 0 | } catch (Exception e) { |
100 | 0 | logger.info("Could not close connection", e); |
101 | 0 | } |
102 | 0 | parentListener.removeAgent(this); |
103 | 0 | } |
104 | ||
105 | public String toString() { |
|
106 | 0 | return "AgentThread[" + socket.getLocalPort() + "] --> " + socket.getInetAddress() |
107 | + ":" + socket.getPort(); |
|
108 | } |
|
109 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |