e977f1333a80ceee25086096f5248db3d7c7e985
[jalview.git] / srcjar2 / org / apache / log4j / net / SocketNode.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.log4j.net;
19
20 import java.io.BufferedInputStream;
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
23 import java.io.ObjectInputStream;
24 import java.net.Socket;
25
26 import org.apache.log4j.Logger;
27 import org.apache.log4j.spi.LoggerRepository;
28 import org.apache.log4j.spi.LoggingEvent;
29
30 // Contributors:  Moses Hohman <mmhohman@rainbow.uchicago.edu>
31
32 /**
33    Read {@link LoggingEvent} objects sent from a remote client using
34    Sockets (TCP). These logging events are logged according to local
35    policy, as if they were generated locally.
36
37    <p>For example, the socket node might decide to log events to a
38    local file and also resent them to a second socket node.
39
40     @author  Ceki G&uuml;lc&uuml;
41
42     @since 0.8.4
43 */
44 public class SocketNode implements Runnable {
45
46   Socket socket;
47   LoggerRepository hierarchy;
48   ObjectInputStream ois;
49
50   static Logger logger = Logger.getLogger(SocketNode.class);
51
52   public SocketNode(Socket socket, LoggerRepository hierarchy) {
53     this.socket = socket;
54     this.hierarchy = hierarchy;
55     try {
56       ois = new ObjectInputStream(
57                          new BufferedInputStream(socket.getInputStream()));
58     } catch(InterruptedIOException e) {
59       Thread.currentThread().interrupt();
60       logger.error("Could not open ObjectInputStream to "+socket, e);
61     } catch(IOException e) {
62       logger.error("Could not open ObjectInputStream to "+socket, e);
63     } catch(RuntimeException e) {
64       logger.error("Could not open ObjectInputStream to "+socket, e);
65     }
66   }
67
68   //public
69   //void finalize() {
70   //System.err.println("-------------------------Finalize called");
71   // System.err.flush();
72   //}
73
74   public void run() {
75     LoggingEvent event;
76     Logger remoteLogger;
77
78     try {
79       if (ois != null) {
80           while(true) {
81                 // read an event from the wire
82                 event = (LoggingEvent) ois.readObject();
83                 // get a logger from the hierarchy. The name of the logger is taken to be the name contained in the event.
84                 remoteLogger = hierarchy.getLogger(event.getLoggerName());
85                 //event.logger = remoteLogger;
86                 // apply the logger-level filter
87                 if(event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
88                 // finally log the event as if was generated locally
89                 remoteLogger.callAppenders(event);
90               }
91         }
92       }
93     } catch(java.io.EOFException e) {
94       logger.info("Caught java.io.EOFException closing conneciton.");
95     } catch(java.net.SocketException e) {
96       logger.info("Caught java.net.SocketException closing conneciton.");
97     } catch(InterruptedIOException e) {
98       Thread.currentThread().interrupt();
99       logger.info("Caught java.io.InterruptedIOException: "+e);
100       logger.info("Closing connection.");
101     } catch(IOException e) {
102       logger.info("Caught java.io.IOException: "+e);
103       logger.info("Closing connection.");
104     } catch(Exception e) {
105       logger.error("Unexpected exception. Closing conneciton.", e);
106     } finally {
107       if (ois != null) {
108          try {
109             ois.close();
110          } catch(Exception e) {
111             logger.info("Could not close connection.", e);
112          }
113       }
114       if (socket != null) {
115         try {
116           socket.close();
117         } catch(InterruptedIOException e) {
118             Thread.currentThread().interrupt();
119         } catch(IOException ex) {
120         }
121       }
122     }
123   }
124 }