KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > ServiceLogger


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ServiceLogger.java 2476 2006-02-21 08:45:52Z dblevins $
44  */

45 package org.openejb.server;
46
47 import java.io.*;
48 import java.net.*;
49 import java.util.*;
50 import org.openejb.*;
51 import org.openejb.util.*;
52
53 /**
54  * The Server will call the following methods.
55  *
56  * newInstance()
57  * init( port, properties)
58  * start()
59  * stop()
60  *
61  * All ServerService implementations must have a no argument
62  * constructor.
63  *
64  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
65  */

66 public class ServiceLogger implements ServerService {
67     
68     Messages messages = new Messages( "org.openejb.server.util.resources" );
69     Logger logger;
70
71     boolean logOnSuccess;
72     boolean logOnFailure;
73
74     ServerService next;
75
76     public ServiceLogger(ServerService next){
77         this.next = next;
78     }
79
80     /**
81      * Pulls out the access log information
82      *
83      * @param props
84      *
85      * @exception ServiceException
86      */

87     public void init(Properties props) throws Exception JavaDoc{
88         // Do our stuff
89
String JavaDoc logCategory = "OpenEJB.server.service."+getName();
90
91         logger = Logger.getInstance( logCategory, "org.openejb.server.util.resources" );
92         // Then call the next guy
93
next.init(props);
94     }
95     
96     public void start() throws ServiceException{
97         // Do our stuff
98

99         // Then call the next guy
100
next.start();
101     }
102     
103     public void stop() throws ServiceException{
104         // Do our stuff
105

106         // Then call the next guy
107
next.stop();
108     }
109
110     public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
111         throw new UnsupportedOperationException JavaDoc("service(in,out)");
112     }
113
114     /**
115      * log_on_success
116      * -----------------
117      * Different information can be logged when a server starts:
118      *
119      * PID : the server's PID (if it's an internal xinetd service, the PID has then a value of 0) ;
120      * HOST : the client address ;
121      * USERID : the identity of the remote user, according to RFC1413 defining identification protocol;
122      * EXIT : the process exit status;
123      * DURATION : the session duration.
124      *
125      * log_on_failure
126      * ------------------
127      * Here again, xinetd can log a lot of information when a server can't start, either by lack of resources or because of access rules:
128      * HOST, USERID : like above mentioned ;
129      * ATTEMPT : logs an access attempt. This an automatic option as soon as another value is provided;
130      * RECORD : logs every information available on the client.
131      *
132      * @param socket
133      *
134      * @exception ServiceException
135      * @exception IOException
136      */

137     public void service(Socket socket) throws ServiceException, IOException{
138         // Fill this in more deeply later.
139
InetAddress client = socket.getInetAddress();
140         org.apache.log4j.MDC.put("HOST", client.getHostName());
141         org.apache.log4j.MDC.put("SERVER", getName());
142
143         try{
144             //logIncoming();
145
// logger.info("[request] "+socket.getPort()+" - "+client.getHostName());
146
next.service(socket);
147 // logSuccess();
148
} catch (Exception JavaDoc e){
149             logger.error("[failure] "+socket.getPort()+" - "+client.getHostName()+": "+e.getMessage());
150             //logFailure(e);
151
e.printStackTrace();
152         }
153     }
154
155     private void logIncoming(){
156         logger.info("incomming request");
157     }
158
159     private void logSuccess(){
160         logger.info("successful request");
161     }
162     
163     private void logFailure(Exception JavaDoc e){
164         logger.error(e.getMessage());
165     }
166
167     /**
168      * Gets the name of the service.
169      * Used for display purposes only
170      */

171     public String JavaDoc getName(){
172         return next.getName();
173     }
174
175     /**
176      * Gets the ip number that the
177      * daemon is listening on.
178      */

179     public String JavaDoc getIP(){
180         return next.getIP();
181     }
182     
183     /**
184      * Gets the port number that the
185      * daemon is listening on.
186      */

187     public int getPort(){
188         return next.getPort();
189     }
190
191 }
192
Popular Tags