KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > systemsunion > LoggingServer > SocketNode2


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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 package com.systemsunion.LoggingServer;
18
19 import java.net.InetAddress JavaDoc;
20 import java.net.Socket JavaDoc;
21 import java.net.ServerSocket JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInputStream JavaDoc;
25
26
27 import org.apache.log4j.Category;
28 import org.apache.log4j.spi.LoggingEvent;
29 import org.apache.log4j.Priority;
30 import org.apache.log4j.NDC;
31
32 // Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu>
33

34 /**
35    Read {@link LoggingEvent} objects sent from a remote client using
36    Sockets (TCP). These logging events are logged according to local
37    policy, as if they were generated locally.
38
39    <p>For example, the socket node might decide to log events to a
40    local file and also resent them to a second socket node.
41
42     @author Ceki G&uuml;lc&uuml;
43
44     @since 0.8.4
45 */

46 public class SocketNode2 implements Runnable JavaDoc {
47
48   Socket JavaDoc socket;
49   ObjectInputStream JavaDoc ois;
50
51   static Category cat = Category.getInstance(SocketNode2.class.getName());
52
53   public
54   SocketNode2(Socket JavaDoc socket) {
55     this.socket = socket;
56     try {
57       ois = new ObjectInputStream JavaDoc(socket.getInputStream());
58     }
59     catch(Exception JavaDoc e) {
60       cat.error("Could not open ObjectInputStream to "+socket, e);
61     }
62   }
63
64   //public
65
//void finalize() {
66
//System.err.println("-------------------------Finalize called");
67
// System.err.flush();
68
//}
69

70   public void run() {
71     LoggingEvent event;
72     Category remoteCategory;
73     String JavaDoc strClientName;
74
75     // Get the client name.
76
InetAddress JavaDoc addr = socket.getInetAddress();
77     strClientName = addr.getHostName();
78     if(strClientName == null || strClientName.length() == 0)
79     {
80         strClientName = addr.getHostAddress();
81     }
82
83     try {
84       while(true) {
85     event = (LoggingEvent) ois.readObject();
86
87     if(event.ndc != null)
88     {
89         event.ndc = strClientName + ":" + event.ndc;
90     }
91     else
92     {
93         event.ndc = strClientName;
94     }
95
96     remoteCategory = Category.getInstance(event.categoryName);
97     remoteCategory.callAppenders(event);
98       }
99     }
100     catch(java.io.EOFException JavaDoc e) {
101       cat.info("Caught java.io.EOFException will close conneciton.", e);
102     }
103     catch(java.net.SocketException JavaDoc e) {
104       cat.info("Caught java.net.SocketException, will close conneciton.", e);
105     }
106     catch(Exception JavaDoc e) {
107       cat.error("Unexpected exception. Closing conneciton.", e);
108     }
109
110     try {
111       ois.close();
112     }
113     catch(Exception JavaDoc e) {
114       cat.info("Could not close connection.", e);
115     }
116   }
117 }
118
Popular Tags