1*2be43276SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2be43276SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2be43276SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2be43276SAndrew Rist * distributed with this work for additional information 6*2be43276SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2be43276SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2be43276SAndrew Rist * "License"); you may not use this file except in compliance 9*2be43276SAndrew Rist * with the License. You may obtain a copy of the License at 10*2be43276SAndrew Rist * 11*2be43276SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*2be43276SAndrew Rist * 13*2be43276SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2be43276SAndrew Rist * software distributed under the License is distributed on an 15*2be43276SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2be43276SAndrew Rist * KIND, either express or implied. See the License for the 17*2be43276SAndrew Rist * specific language governing permissions and limitations 18*2be43276SAndrew Rist * under the License. 19*2be43276SAndrew Rist * 20*2be43276SAndrew Rist *************************************************************/ 21*2be43276SAndrew Rist 22*2be43276SAndrew Rist 23cdf0e10cSrcweir package com.sun.star.lib.connections.pipe; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import java.io.IOException; 26cdf0e10cSrcweir 27cdf0e10cSrcweir import java.util.StringTokenizer; 28cdf0e10cSrcweir import java.util.Enumeration; 29cdf0e10cSrcweir import java.util.Vector; 30cdf0e10cSrcweir 31cdf0e10cSrcweir import com.sun.star.lib.util.NativeLibraryLoader; 32cdf0e10cSrcweir 33cdf0e10cSrcweir import com.sun.star.io.XStreamListener; 34cdf0e10cSrcweir 35cdf0e10cSrcweir import com.sun.star.connection.XConnection; 36cdf0e10cSrcweir import com.sun.star.connection.XConnectionBroadcaster; 37cdf0e10cSrcweir 38cdf0e10cSrcweir /** 39cdf0e10cSrcweir * The PipeConnection implements the <code>XConnection</code> interface 40cdf0e10cSrcweir * and is uses by the <code>PipeConnector</code> and the <code>PipeAcceptor</code>. 41cdf0e10cSrcweir * This class is not part of the provided <code>api</code>. 42cdf0e10cSrcweir * <p> 43cdf0e10cSrcweir * @version $Revision: 1.7 $ $ $Date: 2008-04-11 11:13:00 $ 44cdf0e10cSrcweir * @author Kay Ramme 45cdf0e10cSrcweir * @see com.sun.star.comp.connections.PipeAcceptor 46cdf0e10cSrcweir * @see com.sun.star.comp.connections.PipeConnector 47cdf0e10cSrcweir * @see com.sun.star.connections.XConnection 48cdf0e10cSrcweir * @since UDK1.0 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir public class PipeConnection implements XConnection, XConnectionBroadcaster { 51cdf0e10cSrcweir /** 52cdf0e10cSrcweir * When set to true, enables various debugging output. 53cdf0e10cSrcweir */ 54cdf0e10cSrcweir static public final boolean DEBUG = false; 55cdf0e10cSrcweir 56cdf0e10cSrcweir static { 57cdf0e10cSrcweir // load shared library for JNI code PipeConnection.class.getClassLoader()58cdf0e10cSrcweir NativeLibraryLoader.loadLibrary(PipeConnection.class.getClassLoader(), "jpipe"); 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61cdf0e10cSrcweir protected String _aDescription; 62cdf0e10cSrcweir protected long _nPipeHandle; 63cdf0e10cSrcweir protected Vector _aListeners; 64cdf0e10cSrcweir protected boolean _bFirstRead; 65cdf0e10cSrcweir 66cdf0e10cSrcweir /** 67cdf0e10cSrcweir * Constructs a new <code>PipeConnection</code>. 68cdf0e10cSrcweir * <p> 69cdf0e10cSrcweir * @param description the description of the connection 70cdf0e10cSrcweir * @param pipe the pipe of the connection 71cdf0e10cSrcweir */ PipeConnection(String description)72cdf0e10cSrcweir public PipeConnection(String description) 73cdf0e10cSrcweir throws IOException 74cdf0e10cSrcweir { 75cdf0e10cSrcweir if (DEBUG) System.err.println("##### " + getClass().getName() + " - instantiated " + description ); 76cdf0e10cSrcweir 77cdf0e10cSrcweir _aListeners = new Vector(); 78cdf0e10cSrcweir _bFirstRead = true; 79cdf0e10cSrcweir 80cdf0e10cSrcweir // get pipe name from pipe descriptor 81cdf0e10cSrcweir String aPipeName ; 82cdf0e10cSrcweir StringTokenizer aTokenizer = new StringTokenizer( description, "," ); 83cdf0e10cSrcweir if ( aTokenizer.hasMoreTokens() ) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir String aConnType = aTokenizer.nextToken(); 86cdf0e10cSrcweir if ( !aConnType.equals( "pipe" ) ) 87cdf0e10cSrcweir throw new RuntimeException( "invalid pipe descriptor: does not start with 'pipe,'" ); 88cdf0e10cSrcweir 89cdf0e10cSrcweir String aPipeNameParam = aTokenizer.nextToken(); 90cdf0e10cSrcweir if ( !aPipeNameParam.substring( 0, 5 ).equals( "name=" ) ) 91cdf0e10cSrcweir throw new RuntimeException( "invalid pipe descriptor: no 'name=' parameter found" ); 92cdf0e10cSrcweir aPipeName = aPipeNameParam.substring( 5 ); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir else 95cdf0e10cSrcweir throw new RuntimeException( "invalid or empty pipe descriptor" ); 96cdf0e10cSrcweir 97cdf0e10cSrcweir // create the pipe 98cdf0e10cSrcweir try 99cdf0e10cSrcweir { createJNI( aPipeName ); } 100cdf0e10cSrcweir catch ( java.lang.NullPointerException aNPE ) 101cdf0e10cSrcweir { throw new IOException( aNPE.getMessage() ); } 102cdf0e10cSrcweir catch ( com.sun.star.io.IOException aIOE ) 103cdf0e10cSrcweir { throw new IOException( aIOE.getMessage() ); } 104cdf0e10cSrcweir catch ( java.lang.Exception aE ) 105cdf0e10cSrcweir { throw new IOException( aE.getMessage() ); } 106cdf0e10cSrcweir } 107cdf0e10cSrcweir addStreamListener(XStreamListener aListener )108cdf0e10cSrcweir public void addStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException { 109cdf0e10cSrcweir _aListeners.addElement(aListener); 110cdf0e10cSrcweir } 111cdf0e10cSrcweir removeStreamListener(XStreamListener aListener )112cdf0e10cSrcweir public void removeStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException { 113cdf0e10cSrcweir _aListeners.removeElement(aListener); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir notifyListeners_open()116cdf0e10cSrcweir private void notifyListeners_open() { 117cdf0e10cSrcweir Enumeration elements = _aListeners.elements(); 118cdf0e10cSrcweir while(elements.hasMoreElements()) { 119cdf0e10cSrcweir XStreamListener xStreamListener = (XStreamListener)elements.nextElement(); 120cdf0e10cSrcweir xStreamListener.started(); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir } 123cdf0e10cSrcweir notifyListeners_close()124cdf0e10cSrcweir private void notifyListeners_close() { 125cdf0e10cSrcweir Enumeration elements = _aListeners.elements(); 126cdf0e10cSrcweir while(elements.hasMoreElements()) { 127cdf0e10cSrcweir XStreamListener xStreamListener = (XStreamListener)elements.nextElement(); 128cdf0e10cSrcweir xStreamListener.closed(); 129cdf0e10cSrcweir } 130cdf0e10cSrcweir } 131cdf0e10cSrcweir notifyListeners_error(com.sun.star.uno.Exception exception)132cdf0e10cSrcweir private void notifyListeners_error(com.sun.star.uno.Exception exception) { 133cdf0e10cSrcweir Enumeration elements = _aListeners.elements(); 134cdf0e10cSrcweir while(elements.hasMoreElements()) { 135cdf0e10cSrcweir XStreamListener xStreamListener = (XStreamListener)elements.nextElement(); 136cdf0e10cSrcweir xStreamListener.error(exception); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir // JNI implementation to create the pipe createJNI( String name )141cdf0e10cSrcweir private native int createJNI( String name ) 142cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException; 143cdf0e10cSrcweir 144cdf0e10cSrcweir // JNI implementation to read from the pipe readJNI( byte[][] bytes, int nBytesToRead)145cdf0e10cSrcweir private native int readJNI(/*OUT*/byte[][] bytes, int nBytesToRead) 146cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException; 147cdf0e10cSrcweir 148cdf0e10cSrcweir // JNI implementation to write to the pipe writeJNI(byte aData[])149cdf0e10cSrcweir private native void writeJNI(byte aData[]) 150cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException; 151cdf0e10cSrcweir 152cdf0e10cSrcweir // JNI implementation to flush the pipe flushJNI()153cdf0e10cSrcweir private native void flushJNI() 154cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException; 155cdf0e10cSrcweir 156cdf0e10cSrcweir // JNI implementation to close the pipe closeJNI()157cdf0e10cSrcweir private native void closeJNI() 158cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException; 159cdf0e10cSrcweir 160cdf0e10cSrcweir /** 161cdf0e10cSrcweir * Read the required number of bytes. 162cdf0e10cSrcweir * <p> 163cdf0e10cSrcweir * @return the number of bytes read 164cdf0e10cSrcweir * @param aReadBytes the outparameter, where the bytes have to be placed 165cdf0e10cSrcweir * @param nBytesToRead the number of bytes to read 166cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#read 167cdf0e10cSrcweir */ read( byte[][] bytes, int nBytesToRead)168cdf0e10cSrcweir public int read(/*OUT*/byte[][] bytes, int nBytesToRead) 169cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException 170cdf0e10cSrcweir { 171cdf0e10cSrcweir if(_bFirstRead) { 172cdf0e10cSrcweir _bFirstRead = false; 173cdf0e10cSrcweir 174cdf0e10cSrcweir notifyListeners_open(); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir 177cdf0e10cSrcweir return readJNI( bytes, nBytesToRead ); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir /** 181cdf0e10cSrcweir * Write bytes. 182cdf0e10cSrcweir * <p> 183cdf0e10cSrcweir * @param aData the bytes to write 184cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#write 185cdf0e10cSrcweir */ write(byte aData[])186cdf0e10cSrcweir public void write(byte aData[]) 187cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException 188cdf0e10cSrcweir { 189cdf0e10cSrcweir writeJNI( aData ); 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir /** 193cdf0e10cSrcweir * Flushes the buffer. 194cdf0e10cSrcweir * <p> 195cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#flush 196cdf0e10cSrcweir */ flush()197cdf0e10cSrcweir public void flush() 198cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException 199cdf0e10cSrcweir { 200cdf0e10cSrcweir flushJNI(); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir 203cdf0e10cSrcweir /** 204cdf0e10cSrcweir * Closes the connection. 205cdf0e10cSrcweir * <p> 206cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#close 207cdf0e10cSrcweir */ close()208cdf0e10cSrcweir public void close() 209cdf0e10cSrcweir throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException 210cdf0e10cSrcweir { 211cdf0e10cSrcweir if (DEBUG) System.out.print( "PipeConnection::close() " ); 212cdf0e10cSrcweir closeJNI(); 213cdf0e10cSrcweir notifyListeners_close(); 214cdf0e10cSrcweir if (DEBUG) System.out.println( "done" ); 215cdf0e10cSrcweir } 216cdf0e10cSrcweir 217cdf0e10cSrcweir /** 218cdf0e10cSrcweir * Gives a description of the connection. 219cdf0e10cSrcweir * <p> 220cdf0e10cSrcweir * @return the description 221cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#getDescription 222cdf0e10cSrcweir */ getDescription()223cdf0e10cSrcweir public String getDescription() throws com.sun.star.uno.RuntimeException { 224cdf0e10cSrcweir return _aDescription; 225cdf0e10cSrcweir } 226cdf0e10cSrcweir 227cdf0e10cSrcweir } 228cdf0e10cSrcweir 229