* gnu/classpath/jdwp/transport/ITransport.java: New file.
[official-gcc.git] / libjava / gnu / classpath / jdwp / transport / SocketTransport.java
blob3be193a052813117a76828bbd1c4d560a7476db8
1 /* SocketTransport.java -- a socket transport
2 Copyright (C) 2005 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package gnu.classpath.jdwp.transport;
42 import gnu.classpath.jdwp.transport.ITransport;
43 import gnu.classpath.jdwp.transport.TransportException;
45 import java.io.InputStream;
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.net.ServerSocket;
49 import java.net.Socket;
50 import java.util.HashMap;
52 import javax.net.ServerSocketFactory;
53 import javax.net.SocketFactory;
55 /**
56 * A socket-based transport. This transport uses
57 * configury string that looks like "name=dt_socket,
58 * address=localhost:1234,server=y".
60 * @author Keith Seitz <keiths@redhat.com>
62 class SocketTransport
63 implements ITransport
65 /**
66 * Name of this transport
68 public static final String NAME = "dt_socket";
70 // Configure properties
71 private static final String _PROPERTY_ADDRESS = "address";
72 private static final String _PROPERTY_SERVER = "server";
74 // Port number
75 private int _port;
77 // Host name
78 private String _host;
80 // Are we acting as a server?
81 private boolean _server = false;
83 // Socket
84 private Socket _socket;
86 /**
87 * Setup the connection configuration from the given properties
89 * @param properties the properties of the JDWP session
90 * @throws TransportException for any configury errors
92 public void configure (HashMap properties)
93 throws TransportException
95 // Get address [form: "hostname:port"]
96 String p = (String) properties.get (_PROPERTY_ADDRESS);
97 if (p != null)
99 String[] s = p.split (":");
100 if (s.length == 2)
102 _host = s[0];
103 _port = Integer.parseInt (s[1]);
107 // Get server [form: "y" or "n"]
108 p = (String) properties.get (_PROPERTY_SERVER);
109 if (p != null)
111 if (p.toLowerCase().equals ("y"))
112 _server = true;
117 * Initialize this socket connection. This includes
118 * connecting to the host (or listening for it).
120 * @throws TransportException if a transport-related error occurs
122 public void initialize ()
123 throws TransportException
127 if (_server)
129 // Get a server socket
130 ServerSocketFactory ssf = ServerSocketFactory.getDefault ();
131 ServerSocket ss = ssf.createServerSocket (_port, 1);
132 _socket = ss.accept ();
134 else
136 // Get a client socket (the factory will connect it)
137 SocketFactory sf = SocketFactory.getDefault ();
138 _socket = sf.createSocket (_host, _port);
141 catch (IOException ioe)
143 // This will grab UnknownHostException, too.
144 throw new TransportException (ioe);
149 * Returns an <code>InputStream</code> for the transport
151 * @throws IOException if an I/O error occurs creating the stream
152 * or the socket is not connected
154 public InputStream getInputStream ()
155 throws IOException
157 return _socket.getInputStream ();
161 * Returns an <code>OutputStream</code> for the transport
163 * @throws IOException if an I/O error occurs creating the stream
164 * or the socket is not connected
166 public OutputStream getOutputStream ()
167 throws IOException
169 return _socket.getOutputStream ();