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