KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > BaseConnection


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.remote;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.cayenne.CayenneRuntimeException;
25
26 /**
27  * A common base class for concrete ClientConnection implementations. Provides message
28  * logging functionality via commons-logging.
29  *
30  * @since 1.2
31  * @author Andrus Adamchik
32  */

33 public abstract class BaseConnection implements ClientConnection {
34
35     protected Log logger;
36     protected long messageId;
37
38     /**
39      * Default constructor that initializes logging and a single threaded EventManager.
40      */

41     protected BaseConnection() {
42         this.logger = LogFactory.getLog(getClass());
43     }
44
45     /**
46      * Invokes 'beforeSendMessage' on self, then invokes 'doSendMessage'. Implements basic
47      * logging functionality. Do not override this method unless absolutely necessary.
48      * Override 'beforeSendMessage' and 'doSendMessage' instead.
49      */

50     public Object JavaDoc sendMessage(ClientMessage message) throws CayenneRuntimeException {
51         if (message == null) {
52             throw new NullPointerException JavaDoc("Null message");
53         }
54
55         beforeSendMessage(message);
56
57         // log start...
58
long t0 = 0;
59         String JavaDoc messageLabel = "";
60
61         // using sequential number for message id ... it can be useful for some basic
62
// connector stats.
63
long messageId = this.messageId++;
64
65         if (logger.isInfoEnabled()) {
66             t0 = System.currentTimeMillis();
67             messageLabel = message.toString();
68             logger.info("--- Message " + messageId + ": " + messageLabel);
69         }
70
71         Object JavaDoc response;
72         try {
73             response = doSendMessage(message);
74         }
75         catch (CayenneRuntimeException e) {
76
77             // log error
78
if (logger.isInfoEnabled()) {
79                 long time = System.currentTimeMillis() - t0;
80                 logger.info("*** Message error for "
81                         + messageId
82                         + ": "
83                         + messageLabel
84                         + " - took "
85                         + time
86                         + " ms.");
87             }
88
89             throw e;
90         }
91
92         // log success...
93
if (logger.isInfoEnabled()) {
94             long time = System.currentTimeMillis() - t0;
95             logger.info("=== Message "
96                     + messageId
97                     + ": "
98                     + messageLabel
99                     + " done - took "
100                     + time
101                     + " ms.");
102         }
103
104         return response;
105     }
106
107     /**
108      * Returns a count of processed messages since the beginning of life of this
109      * connector.
110      */

111     public long getProcessedMessagesCount() {
112         return messageId + 1;
113     }
114
115     /**
116      * Called before logging the beginning of message processing.
117      */

118     protected abstract void beforeSendMessage(ClientMessage message)
119             throws CayenneRuntimeException;
120
121     /**
122      * The worker method invoked to process message.
123      */

124     protected abstract Object JavaDoc doSendMessage(ClientMessage message)
125             throws CayenneRuntimeException;
126 }
127
Popular Tags