Update ooo320-m1
[ooovba.git] / jurt / com / sun / star / lib / connections / pipe / PipeConnection.java
blobe92c02dddef48c084debaff65e8163155de71365
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PipeConnection.java,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package com.sun.star.lib.connections.pipe;
32 import java.io.IOException;
34 import java.util.StringTokenizer;
35 import java.util.Enumeration;
36 import java.util.Vector;
38 import com.sun.star.lib.util.NativeLibraryLoader;
40 import com.sun.star.io.XStreamListener;
42 import com.sun.star.connection.XConnection;
43 import com.sun.star.connection.XConnectionBroadcaster;
45 /**
46 * The PipeConnection implements the <code>XConnection</code> interface
47 * and is uses by the <code>PipeConnector</code> and the <code>PipeAcceptor</code>.
48 * This class is not part of the provided <code>api</code>.
49 * <p>
50 * @version $Revision: 1.7 $ $ $Date: 2008-04-11 11:13:00 $
51 * @author Kay Ramme
52 * @see com.sun.star.comp.connections.PipeAcceptor
53 * @see com.sun.star.comp.connections.PipeConnector
54 * @see com.sun.star.connections.XConnection
55 * @since UDK1.0
57 public class PipeConnection implements XConnection, XConnectionBroadcaster {
58 /**
59 * When set to true, enables various debugging output.
61 static public final boolean DEBUG = false;
63 static {
64 // preload shared libraries whichs import lips are linked to jpipe
65 if ( System.getProperty( "os.name" ).startsWith( "Windows" ) )
67 try {
68 NativeLibraryLoader.loadLibrary(PipeConnection.class.getClassLoader(), "msvcr71");
69 } catch (Throwable e){} // loading twice would fail
71 try {
72 NativeLibraryLoader.loadLibrary(PipeConnection.class.getClassLoader(), "msvcr70");
73 } catch (Throwable e){} // loading twice would fail
75 try {
76 NativeLibraryLoader.loadLibrary(PipeConnection.class.getClassLoader(), "uwinapi");
77 } catch (Throwable e){} // loading twice would fail
79 try {
80 NativeLibraryLoader.loadLibrary(PipeConnection.class.getClassLoader(), "sal3");
81 } catch (Throwable e){} // loading twice would fail
84 // load shared library for JNI code
85 try {
86 NativeLibraryLoader.loadLibrary(PipeConnection.class.getClassLoader(), "jpipe");
87 } catch (Throwable e){} // loading twice would fail
90 protected String _aDescription;
91 protected long _nPipeHandle;
92 protected Vector _aListeners;
93 protected boolean _bFirstRead;
95 /**
96 * Constructs a new <code>PipeConnection</code>.
97 * <p>
98 * @param description the description of the connection
99 * @param pipe the pipe of the connection
101 public PipeConnection(String description)
102 throws IOException
104 if (DEBUG) System.err.println("##### " + getClass().getName() + " - instantiated " + description );
106 _aListeners = new Vector();
107 _bFirstRead = true;
109 // get pipe name from pipe descriptor
110 String aPipeName = null;
111 StringTokenizer aTokenizer = new StringTokenizer( description, "," );
112 if ( aTokenizer.hasMoreTokens() )
114 String aConnType = aTokenizer.nextToken();
115 if ( !aConnType.equals( "pipe" ) )
116 throw new RuntimeException( "invalid pipe descriptor: does not start with 'pipe,'" );
118 String aPipeNameParam = aTokenizer.nextToken();
119 if ( !aPipeNameParam.substring( 0, 5 ).equals( "name=" ) )
120 throw new RuntimeException( "invalid pipe descriptor: no 'name=' parameter found" );
121 aPipeName = aPipeNameParam.substring( 5 );
123 else
124 throw new RuntimeException( "invalid or empty pipe descriptor" );
126 // create the pipe
127 try
128 { createJNI( aPipeName ); }
129 catch ( java.lang.NullPointerException aNPE )
130 { throw new IOException( aNPE.getMessage() ); }
131 catch ( com.sun.star.io.IOException aIOE )
132 { throw new IOException( aIOE.getMessage() ); }
133 catch ( java.lang.Exception aE )
134 { throw new IOException( aE.getMessage() ); }
137 public void addStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException {
138 _aListeners.addElement(aListener);
141 public void removeStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException {
142 _aListeners.removeElement(aListener);
145 private void notifyListeners_open() {
146 Enumeration elements = _aListeners.elements();
147 while(elements.hasMoreElements()) {
148 XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
149 xStreamListener.started();
153 private void notifyListeners_close() {
154 Enumeration elements = _aListeners.elements();
155 while(elements.hasMoreElements()) {
156 XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
157 xStreamListener.closed();
161 private void notifyListeners_error(com.sun.star.uno.Exception exception) {
162 Enumeration elements = _aListeners.elements();
163 while(elements.hasMoreElements()) {
164 XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
165 xStreamListener.error(exception);
169 // JNI implementation to create the pipe
170 private native int createJNI( String name )
171 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException;
173 // JNI implementation to read from the pipe
174 private native int readJNI(/*OUT*/byte[][] bytes, int nBytesToRead)
175 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException;
177 // JNI implementation to write to the pipe
178 private native void writeJNI(byte aData[])
179 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException;
181 // JNI implementation to flush the pipe
182 private native void flushJNI()
183 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException;
185 // JNI implementation to close the pipe
186 private native void closeJNI()
187 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException;
190 * Read the required number of bytes.
191 * <p>
192 * @return the number of bytes read
193 * @param aReadBytes the outparameter, where the bytes have to be placed
194 * @param nBytesToRead the number of bytes to read
195 * @see com.sun.star.connections.XConnection#read
197 public int read(/*OUT*/byte[][] bytes, int nBytesToRead)
198 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
200 if(_bFirstRead) {
201 _bFirstRead = false;
203 notifyListeners_open();
206 return readJNI( bytes, nBytesToRead );
210 * Write bytes.
211 * <p>
212 * @param aData the bytes to write
213 * @see com.sun.star.connections.XConnection#write
215 public void write(byte aData[])
216 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
218 writeJNI( aData );
222 * Flushes the buffer.
223 * <p>
224 * @see com.sun.star.connections.XConnection#flush
226 public void flush()
227 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
229 flushJNI();
233 * Closes the connection.
234 * <p>
235 * @see com.sun.star.connections.XConnection#close
237 public void close()
238 throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException
240 if (DEBUG) System.out.print( "PipeConnection::close() " );
241 closeJNI();
242 notifyListeners_close();
243 if (DEBUG) System.out.println( "done" );
247 * Gives a description of the connection.
248 * <p>
249 * @return the description
250 * @see com.sun.star.connections.XConnection#getDescription
252 public String getDescription() throws com.sun.star.uno.RuntimeException {
253 return _aDescription;