View Javadoc

1   /**
2    * $Id: ServerSocketConsumerLoader.java 67 2006-11-09 05:26:18Z maldito_orco $
3    * $Revision: 67 $
4    * $Date: 2006-11-09 02:26:18 -0300 (Thu, 09 Nov 2006) $
5    *
6    * =========================================================================
7    *
8    * Copyright 2005 Tubo
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   */
22  package org.tubo.resource.consumer.serversocket;
23  
24  //log
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  //tubo
28  import org.tubo.resource.consumer.ConsumerLoader;
29  import org.tubo.resource.consumer.ConsumerManager;
30  import org.tubo.exception.TuboConsumerException;
31  //util
32  import java.util.StringTokenizer;
33  //concurrent
34  import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
35  import edu.emory.mathcs.backport.java.util.concurrent.Executors;
36  
37  
38  /**
39   * Created: Sep 6, 2006, 1:10:48 AM
40   * Last Modification Date: $Date: 2006-11-09 02:26:18 -0300 (Thu, 09 Nov 2006) $
41   *
42   * @author maldito_orco (maldito_orco@users.sourceforge.net)
43   * @version $Revision: 67 $
44   */
45  public class ServerSocketConsumerLoader implements ConsumerLoader {
46      public static final String RCS_ID = "$Id: ServerSocketConsumerLoader.java 67 2006-11-09 05:26:18Z maldito_orco $";
47      private static Log log = LogFactory.getLog(ServerSocketConsumerLoader.class);
48  
49      public void load(ConsumerManager manager) throws TuboConsumerException {
50          //
51          // get ports to listen
52          String ports = manager.getProperty("ports");
53          //
54          // check ports null
55          if (ports==null || "".equals(ports.trim()))
56                  throw new TuboConsumerException("port property is empty or not configured");
57          //
58          // create tokenizer
59          StringTokenizer tokenizer = new StringTokenizer(ports,",",false);
60          //
61          // check number of tokens
62          if (tokenizer.countTokens()==0)
63              throw new TuboConsumerException("Ports not found");
64          //
65          // create Workers ejecutor
66          ExecutorService executor = Executors.newCachedThreadPool();
67          while (tokenizer.hasMoreElements()) {
68              //
69              // get port
70              String sport = tokenizer.nextToken();
71              //
72              // convert String port to int (FIXME: ver el error)
73              int port = 0;
74              try {
75                  port = Integer.parseInt(sport);
76              } catch (NumberFormatException e) {
77                  throw new TuboConsumerException("Malformat port number port="+sport);
78              }
79              //
80              // create worker for server socket accept
81              ServerSocketWorker worker = new ServerSocketWorker(manager,port);
82              //
83              // execute worker
84              try {
85                  executor.execute(worker);
86              } catch (Exception e) {
87                  //FIXME: Add a exception management at this point
88              }
89          }
90      }
91  }