View Javadoc

1   /**
2    * $Id: ServerSocketConsumerWorker.java 65 2006-11-08 19:42:46Z maldito_orco $
3    *
4    * =========================================================================
5    *
6    * Copyright 2005 Tubo
7    *
8    *  Licensed under the Apache License, Version 2.0 (the "License");
9    *  you may not use this file except in compliance with the License.
10   *  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   *  Unless required by applicable law or agreed to in writing, software
15   *  distributed under the License is distributed on an "AS IS" BASIS,
16   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   *  See the License for the specific language governing permissions and
18   *  limitations under the License.
19   */
20  package org.tubo.resource.consumer.serversocket;
21  
22  //log
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  //tubo
26  import org.tubo.exception.TuboConsumerException;
27  import org.tubo.exception.TuboResourceException;
28  import org.tubo.resource.consumer.ConsumerManager;
29  //io
30  import java.net.Socket;
31  import java.io.IOException;
32  
33  /**
34   * <p></p>
35   * <p/>
36   * Created: Nov 8, 2006, 2:28:11 PM <br>
37   * Last Modification Date: $Date: 2006-11-08 16:42:46 -0300 (Wed, 08 Nov 2006) $
38   * </p>
39   *
40   * @author maldito_orco (maldito_orco@users.sourceforge.net)
41   * @version $Revision: 65 $
42   */
43  public class ServerSocketConsumerWorker  implements Runnable {
44      public static final String RCS_ID = "$Id: ServerSocketConsumerWorker.java 65 2006-11-08 19:42:46Z maldito_orco $";
45      private static Log log = LogFactory.getLog(ServerSocketConsumerWorker.class);
46  
47      private Socket socket = null;
48      private ServerSocketConsumer consumer = null;
49      private ConsumerManager manager = null;
50  
51      /**
52       * TODO
53       * @param socket
54       * @param consumer
55       */
56      public ServerSocketConsumerWorker(Socket socket, ServerSocketConsumer consumer, ConsumerManager manager) {
57          this.socket = socket;
58          this.consumer = consumer;
59          this.manager = manager;
60      }
61  
62      /**
63       * TODO
64       */
65      public void run() {
66          if (log.isDebugEnabled()) log.debug(".run() - Begin Consumer Worker thread="+Thread.currentThread().getName()+" consumer-family="+manager.getId()+" consumer="+consumer.getId());
67          //
68          // process socket
69          try {
70              consumer.process(socket);
71          } catch (TuboConsumerException e) {
72              log.warn("Error procesing client socket",e);
73          } finally {
74              //
75              // when process ends, then close socket and the thread finish
76              if (socket!=null)
77                  try {
78                      socket.close();
79                  } catch (IOException e) {
80                      log.warn("Error closing client socket",e);
81                  }
82              //
83              // restore consumer to pool
84              try {
85                  manager.restore(consumer);
86              } catch (TuboResourceException e) {
87                  log.error("Error tring to restore consumer to manager",e);
88              }
89          }
90          if (log.isDebugEnabled()) log.debug(".run() - Done Consumer Worker thread="+Thread.currentThread().getName());
91      }
92  
93  }