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