KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > channel > AbstractAcceptSocketImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream.channel;
26
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 import org.objectweb.dream.AbstractComponent;
32 import org.objectweb.dream.control.activity.Util;
33 import org.objectweb.dream.control.activity.task.AbstractTask;
34 import org.objectweb.dream.control.activity.task.Task;
35 import org.objectweb.fractal.api.Component;
36 import org.objectweb.fractal.api.NoSuchInterfaceException;
37 import org.objectweb.fractal.api.control.IllegalBindingException;
38 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
39 import org.objectweb.util.monolog.api.BasicLevel;
40
41 /**
42  * Abstract Accept socket component. This component creates new incoming socket
43  * using the {@link #acceptSocket() }method, and pass it through its
44  * {@link OpenedSocket }client interface.
45  */

46 public abstract class AbstractAcceptSocketImpl extends AbstractComponent
47 {
48
49   protected Task acceptTask = new AcceptTask();
50
51   // ---------------------------------------------------------------------------
52
// Client interfaces
53
// ---------------------------------------------------------------------------
54

55   protected OpenedSocket openedSocketItf;
56
57   // ---------------------------------------------------------------------------
58
// Abstract method
59
// ---------------------------------------------------------------------------
60

61   /**
62    * Abstract method that return a newly accepted socket.
63    */

64   protected abstract SocketState acceptSocket() throws IOException JavaDoc;
65
66   // ---------------------------------------------------------------------------
67
// Inner task class implementation
68
// ---------------------------------------------------------------------------
69

70   protected class AcceptTask extends AbstractTask
71   {
72
73     /** Constructor */
74     public AcceptTask()
75     {
76       super("ChannelInTask");
77     }
78
79     /**
80      * @see Task#execute(Object)
81      */

82     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
83     {
84       SocketState socketState;
85       ObjectInputStream JavaDoc ois = null;
86       try
87       {
88         logger.log(BasicLevel.DEBUG, "accept socket.");
89         socketState = acceptSocket();
90         logger.log(BasicLevel.DEBUG, "connected");
91       }
92       catch (IOException JavaDoc e)
93       {
94         logger.log(BasicLevel.ERROR,
95             "An error occrued while accepting incoming connections", e);
96         return EXECUTE_AGAIN;
97       }
98       try
99       {
100         openedSocketItf.openedSocket(socketState);
101       }
102       catch (IOException JavaDoc e)
103       {
104         logger.log(BasicLevel.WARN, "Input/Output socket error", e);
105         socketState.close();
106       }
107
108       return EXECUTE_AGAIN;
109     }
110   }
111
112   // -------------------------------------------------------------------------
113
// Overriden methods
114
// -------------------------------------------------------------------------
115

116   /**
117    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
118    */

119   protected void beforeFirstStart(Component componentItf)
120       throws IllegalLifeCycleException
121   {
122     try
123     {
124       Util.addTask(componentItf, acceptTask, Collections.EMPTY_MAP);
125     }
126     catch (Exception JavaDoc e)
127     {
128       throw new IllegalLifeCycleException("Can't add task");
129     }
130   }
131
132   // ---------------------------------------------------------------------------
133
// Implementation of BindingController interface
134
// ---------------------------------------------------------------------------
135

136   /**
137    * @see org.objectweb.fractal.api.control.BindingController#listFc()
138    */

139   public String JavaDoc[] listFc()
140   {
141     return new String JavaDoc[]{OpenedSocket.ITF_NAME};
142   }
143
144   /**
145    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
146    * Object)
147    */

148   public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
149       throws NoSuchInterfaceException, IllegalBindingException,
150       IllegalLifeCycleException
151   {
152     super.bindFc(clientItfName, serverItf);
153     if (clientItfName.equals(OpenedSocket.ITF_NAME))
154     {
155       openedSocketItf = (OpenedSocket) serverItf;
156     }
157   }
158 }
Popular Tags