KickJava   Java API By Example, From Geeks To Geeks.

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


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):
23  */

24
25 package org.objectweb.dream.channel;
26
27 import java.io.IOException JavaDoc;
28 import java.net.Socket JavaDoc;
29
30 /**
31  * Basic implementation of {@link SocketState}.
32  */

33 public class SocketStateImpl implements SocketState
34 {
35   protected Socket JavaDoc socket;
36   protected Object JavaDoc input;
37   protected Object JavaDoc output;
38
39   // ---------------------------------------------------------------------------
40
// Implementation of the CodecInputOutput interface
41
// ---------------------------------------------------------------------------
42

43   /**
44    * @see SocketState#setSocket(Socket)
45    */

46   public void setSocket(Socket JavaDoc socket)
47   {
48     this.socket = socket;
49   }
50
51   /**
52    * @see org.objectweb.dream.message.codec.CodecInputOutput#getInput()
53    */

54   public Object JavaDoc getInput() throws IOException JavaDoc
55   {
56     if (input == null)
57     {
58       input = socket.getInputStream();
59     }
60     return input;
61   }
62
63   /**
64    * @see org.objectweb.dream.message.codec.CodecInputOutput#setInput(Object)
65    */

66   public void setInput(Object JavaDoc input)
67   {
68     this.input = input;
69   }
70
71   /**
72    * @see org.objectweb.dream.message.codec.CodecInputOutput#getOutput()
73    */

74   public Object JavaDoc getOutput() throws IOException JavaDoc
75   {
76     if (output == null)
77     {
78       output = socket.getOutputStream();
79     }
80     return output;
81   }
82
83   /**
84    * @see org.objectweb.dream.message.codec.CodecInputOutput#setOutput(Object)
85    */

86   public void setOutput(Object JavaDoc output)
87   {
88     this.output = output;
89   }
90
91   // ---------------------------------------------------------------------------
92
// Implementation of the SocketState interface
93
// ---------------------------------------------------------------------------
94

95   /**
96    * @see SocketState#isClosed()
97    */

98   public boolean isClosed()
99   {
100     return socket.isClosed() || !socket.isConnected();
101   }
102
103   /**
104    * @see SocketState#close()
105    */

106   public void close()
107   {
108     if (!socket.isClosed())
109     {
110       try
111       {
112         socket.close();
113       }
114       catch (IOException JavaDoc e)
115       {
116         // ignore
117
}
118     }
119   }
120
121   // ---------------------------------------------------------------------------
122
// Implementation of the Recyclable interface
123
// ---------------------------------------------------------------------------
124

125   /**
126    * WARNING : The socket state must be previously closed.
127    *
128    * @see org.objectweb.dream.pool.Recyclable#recycle()
129    */

130   public void recycle()
131   {
132     socket = null;
133     input = null;
134     output = null;
135   }
136
137 }
Popular Tags