View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.service.server;
14  
15  import org.abstracthorizon.danube.adapter.AdapterFactory;
16  import org.abstracthorizon.danube.adapter.AdapterManager;
17  import org.abstracthorizon.danube.connection.Connection;
18  import org.abstracthorizon.danube.service.ServiceException;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.net.Socket;
24  
25  /**
26   * This is socket connection implementation.
27   *
28   * @see org.abstracthorizon.danube.connection.socket.SocketConnection
29   * @see org.abstracthorizon.danube.connection.Connection
30   *
31   * @author Daniel Sendula
32   */
33  public class SocketConnection implements Connection, AdapterFactory {
34  
35      /** Classes that are available through this object as an {@link AdapterFactory} */
36      protected static final Class<?>[] ADAPTING_CLASSES =
37          new Class[]{
38          SocketConnection.class,
39          InputStream.class,
40          OutputStream.class,
41          Socket.class};
42  
43      /** Socket */
44      protected Socket socket;
45  
46      /** Cached input stream */
47      protected InputStream cachedInputStream;
48  
49      /** Cached output stream */
50      protected OutputStream cachedOutputStream;
51  
52      /**
53       * Constructor. It creates buffered input and output streams.
54       * @param socket socket
55       */
56      public SocketConnection(Socket socket) {
57          this.socket = socket;
58      }
59  
60      /**
61       * Constructor. It creates buffered input and output streams.
62       * @param socket socket
63       */
64      public SocketConnection(AdapterManager adapterManager, Socket socket) {
65          this.socket = socket;
66      }
67  
68      /**
69       * Returns socket
70       * @return socket
71       */
72      public Socket getSocket() {
73          return socket;
74      }
75  
76      /**
77       * Returns input stream
78       * @return input stream
79       */
80      public InputStream getInputStream() {
81          if (cachedInputStream == null) {
82              try {
83                  cachedInputStream = socket.getInputStream();
84              } catch (IOException e) {
85                  throw new ServiceException(e);
86              }
87          }
88          return cachedInputStream;
89      }
90  
91      /**
92       * Returns output stream
93       * @return output stream
94       */
95      public OutputStream getOutputStream() {
96          if (cachedOutputStream == null) {
97              try {
98                  cachedOutputStream = socket.getOutputStream();
99              } catch (IOException e) {
100                 throw new ServiceException(e);
101             }
102         }
103         return cachedOutputStream;
104     }
105 
106     /**
107      * Closes the connection - closes the underlaying socket.
108      */
109     public void close() {
110         if (!socket.isClosed()) {
111             try {
112                 socket.shutdownInput();
113                 socket.shutdownOutput();
114                 socket.setSoTimeout(10);
115                 synchronized (socket) { // TODO
116                     socket.notifyAll();
117                 }
118             } catch (Exception ignore) {
119             } finally {
120                 try {
121                     socket.close();
122                 } catch (Exception ignore) {
123                     ignore.printStackTrace();
124                 }
125             }
126         }
127     }
128 
129     /**
130      * Checks if socket is closed
131      *
132      * @return <code>true</code> when socket is closed
133      */
134     public boolean isClosed() {
135         return socket.isClosed();
136     }
137 
138     /**
139      * Returns connection as a string
140      * @return connection as a string
141      */
142     public String toString() {
143         return "SocketConnection[" + socket + "]";
144     }
145 
146     /**
147      * Adopts given object to the instance of the asked class
148      * @param object object to he adopted
149      * @param cls asked class
150      * @return adopted given object to the instance of the asked class
151      */
152     public <T> T adapt(T object, Class<T> cls) {
153         if (object == this) {
154             return (T)adapt(cls);
155         } else {
156             return null;
157         }
158     }
159 
160     @SuppressWarnings("unchecked")
161     public <T> T adapt(Class<T> cls) {
162         if (cls == SocketConnection.class) {
163             return (T)this;
164         } else if (cls == Socket.class) {
165             return (T)getSocket();
166         } else if (cls == OutputStream.class) {
167             return (T)getOutputStream();
168         } else if (cls == InputStream.class) {
169             return (T)getInputStream();
170         }
171         return null;
172     }
173 
174     /**
175      * Returns list of classes to which given object can be adopted to by this adopter factory
176      * @return list of classes to which given object can be adopted to
177      */
178     @SuppressWarnings("unchecked")
179     public <T> Class<T>[] getAdaptingClasses(T object) {
180         return (Class<T>[])ADAPTING_CLASSES;
181     }
182 }