summaryrefslogtreecommitdiff
path: root/jurt/com/sun/star/comp/connections
diff options
context:
space:
mode:
Diffstat (limited to 'jurt/com/sun/star/comp/connections')
-rw-r--r--jurt/com/sun/star/comp/connections/Acceptor.java173
-rw-r--r--jurt/com/sun/star/comp/connections/Connector.java154
-rw-r--r--jurt/com/sun/star/comp/connections/ConstantInstanceProvider.java142
-rw-r--r--jurt/com/sun/star/comp/connections/Implementation.java104
-rw-r--r--jurt/com/sun/star/comp/connections/PipedConnection.java286
-rw-r--r--jurt/com/sun/star/comp/connections/makefile.mk47
6 files changed, 906 insertions, 0 deletions
diff --git a/jurt/com/sun/star/comp/connections/Acceptor.java b/jurt/com/sun/star/comp/connections/Acceptor.java
new file mode 100644
index 000000000000..f0cae47aa4f3
--- /dev/null
+++ b/jurt/com/sun/star/comp/connections/Acceptor.java
@@ -0,0 +1,173 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: Acceptor.java,v $
+ * $Revision: 1.5 $
+ *
+ * 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.comp.connections;
+
+import com.sun.star.comp.loader.FactoryHelper;
+import com.sun.star.connection.AlreadyAcceptingException;
+import com.sun.star.connection.ConnectionSetupException;
+import com.sun.star.connection.XAcceptor;
+import com.sun.star.connection.XConnection;
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+import com.sun.star.registry.XRegistryKey;
+
+/**
+ * A component that implements the <code>XAcceptor</code> interface.
+ *
+ * <p>The <code>Acceptor</code> is a general component, that uses less general
+ * components (like <code>com.sun.star.connection.socketAcceptor</code>) to
+ * implement its functionality.</p>
+ *
+ * @see com.sun.star.connections.XAcceptor
+ * @see com.sun.star.connections.XConnection
+ * @see com.sun.star.connections.XConnector
+ * @see com.sun.star.loader.JavaLoader
+ *
+ * @since UDK 1.0
+ */
+public final class Acceptor implements XAcceptor {
+ /**
+ * The name of the service.
+ *
+ * <p>The <code>JavaLoader</code> acceses this through reflection.</p>
+ *
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static final String __serviceName
+ = "com.sun.star.connection.Acceptor";
+
+ /**
+ * Returns a factory for creating the service.
+ *
+ * <p>This method is called by the <code>JavaLoader</code>.</p>
+ *
+ * @param implName the name of the implementation for which a service is
+ * requested.
+ * @param multiFactory the service manager to be used (if needed).
+ * @param regKey the registry key.
+ * @return an <code>XSingleServiceFactory</code> for creating the component.
+ *
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static XSingleServiceFactory __getServiceFactory(
+ String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
+ {
+ return implName.equals(Acceptor.class.getName())
+ ? FactoryHelper.getServiceFactory(Acceptor.class, __serviceName,
+ multiFactory, regKey)
+ : null;
+ }
+
+ /**
+ * Writes the service information into the given registry key.
+ *
+ * <p>This method is called by the <code>JavaLoader</code>.</p>
+ *
+ * @param regKey the registry key.
+ * @return <code>true</code> if the operation succeeded.
+ *
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
+ return FactoryHelper.writeRegistryServiceInfo(Acceptor.class.getName(),
+ __serviceName, regKey);
+ }
+
+ /**
+ * Constructs a new <code>Acceptor</code> that uses the given service
+ * factory to create a specific <code>XAcceptor</code>.
+ *
+ * @param serviceFactory the service factory to use.
+ */
+ public Acceptor(XMultiServiceFactory serviceFactory) {
+ this.serviceFactory = serviceFactory;
+ }
+
+ /**
+ * Accepts a connection request via the given connection type.
+ *
+ * <p>This call blocks until a connection has been established.</p>
+ *
+ * <p>The connection description has the following format:
+ * <code><var>type</var></code><!--
+ * -->*(<code><var>key</var>=<var>value</var></code>).
+ * The specific <code>XAcceptor</code> implementation is instantiated
+ * through the service factory as
+ * <code>com.sun.star.connection.<var>type</var>Acceptor</code> (with
+ * <code><var>type</var></code> in lower case).</p>
+ *
+ * @param connectionDescription the description of the connection.
+ * @return an <code>XConnection</code> to the client.
+ *
+ * @see com.sun.star.connections.XConnection
+ * @see com.sun.star.connections.XConnector
+ */
+ public XConnection accept(String connectionDescription) throws
+ AlreadyAcceptingException, ConnectionSetupException,
+ com.sun.star.lang.IllegalArgumentException
+ {
+ if (DEBUG) {
+ System.err.println("##### " + getClass().getName() + ".accept("
+ + connectionDescription + ")");
+ }
+ XAcceptor acc;
+ synchronized (this) {
+ if (acceptor == null) {
+ acceptor = (XAcceptor) Implementation.getConnectionService(
+ serviceFactory, connectionDescription, XAcceptor.class,
+ "Acceptor");
+ acceptingDescription = connectionDescription;
+ } else if (!connectionDescription.equals(acceptingDescription)) {
+ throw new AlreadyAcceptingException(acceptingDescription
+ + " vs. "
+ + connectionDescription);
+ }
+ acc = acceptor;
+ }
+ return acc.accept(connectionDescription);
+ }
+
+ // see com.sun.star.connection.XAcceptor#stopAccepting
+ public void stopAccepting() {
+ XAcceptor acc;
+ synchronized (this) {
+ acc = acceptor;
+ }
+ acc.stopAccepting();
+ }
+
+ private static final boolean DEBUG = false;
+
+ private final XMultiServiceFactory serviceFactory;
+
+ private XAcceptor acceptor = null;
+ private String acceptingDescription;
+}
diff --git a/jurt/com/sun/star/comp/connections/Connector.java b/jurt/com/sun/star/comp/connections/Connector.java
new file mode 100644
index 000000000000..a74db7d26e5e
--- /dev/null
+++ b/jurt/com/sun/star/comp/connections/Connector.java
@@ -0,0 +1,154 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: Connector.java,v $
+ * $Revision: 1.4 $
+ *
+ * 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.comp.connections;
+
+import com.sun.star.comp.loader.FactoryHelper;
+import com.sun.star.connection.ConnectionSetupException;
+import com.sun.star.connection.NoConnectException;
+import com.sun.star.connection.XConnection;
+import com.sun.star.connection.XConnector;
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+import com.sun.star.registry.XRegistryKey;
+
+/**
+ * A component that implements the <code>XConnector</code> interface.
+ *
+ * <p>The <code>Connector</code> is a general component, that uses less general
+ * components (like <code>com.sun.star.connection.socketConnector</code>) to
+ * implement its functionality.</p>
+ *
+ * @see com.sun.star.connections.XAcceptor
+ * @see com.sun.star.connections.XConnection
+ * @see com.sun.star.connections.XConnector
+ * @see com.sun.star.loader.JavaLoader
+ *
+ * @since UDK 1.0
+ */
+public class Connector implements XConnector {
+ /**
+ * The name of the service.
+ *
+ * <p>The <code>JavaLoader</code> acceses this through reflection.</p>
+ *
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static final String __serviceName
+ = "com.sun.star.connection.Connector";
+
+ /**
+ * Returns a factory for creating the service.
+ *
+ * <p>This method is called by the <code>JavaLoader</code>.</p>
+ *
+ * @param implName the name of the implementation for which a service is
+ * requested.
+ * @param multiFactory the service manager to be used (if needed).
+ * @param regKey the registry key.
+ * @return an <code>XSingleServiceFactory</code> for creating the component.
+ *
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static XSingleServiceFactory __getServiceFactory(
+ String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
+ {
+ return implName.equals(Connector.class.getName())
+ ? FactoryHelper.getServiceFactory(Connector.class, __serviceName,
+ multiFactory, regKey)
+ : null;
+ }
+
+ /**
+ * Writes the service information into the given registry key.
+ *
+ * <p>This method is called by the <code>JavaLoader</code>.</p>
+ *
+ * @param regKey the registry key.
+ * @return <code>true</code> if the operation succeeded.
+ *
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
+ return FactoryHelper.writeRegistryServiceInfo(Connector.class.getName(),
+ __serviceName, regKey);
+ }
+
+ /**
+ * Constructs a new <code>Connector</code> that uses the given service
+ * factory to create a specific <code>XConnector</code>.
+ *
+ * @param serviceFactory the service factory to use.
+ */
+ public Connector(XMultiServiceFactory serviceFactory) {
+ this.serviceFactory = serviceFactory;
+ }
+
+ /**
+ * Connects via the given connection type to a waiting server.
+ *
+ * <p>The connection description has the following format:
+ * <code><var>type</var></code><!--
+ * -->*(<code><var>key</var>=<var>value</var></code>).
+ * The specific <code>XConnector</code> implementation is instantiated
+ * through the service factory as
+ * <code>com.sun.star.connection.<var>type</var>Connector</code> (with
+ * <code><var>type</var></code> in lower case).</p>
+ *
+ * @param connectionDescription the description of the connection.
+ * @return an <code>XConnection</code> to the server.
+ *
+ * @see com.sun.star.connections.XAcceptor
+ * @see com.sun.star.connections.XConnection
+ */
+ public synchronized XConnection connect(String connectionDescription)
+ throws NoConnectException, ConnectionSetupException
+ {
+ if (DEBUG) {
+ System.err.println("##### " + getClass().getName() + ".connect("
+ + connectionDescription + ")");
+ }
+ if (connected) {
+ throw new ConnectionSetupException("already connected");
+ }
+ XConnection con
+ = ((XConnector) Implementation.getConnectionService(
+ serviceFactory, connectionDescription, XConnector.class,
+ "Connector")).connect(connectionDescription);
+ connected = true;
+ return con;
+ }
+
+ private static final boolean DEBUG = false;
+
+ private final XMultiServiceFactory serviceFactory;
+
+ private boolean connected = false;
+}
diff --git a/jurt/com/sun/star/comp/connections/ConstantInstanceProvider.java b/jurt/com/sun/star/comp/connections/ConstantInstanceProvider.java
new file mode 100644
index 000000000000..bf31e731cdf7
--- /dev/null
+++ b/jurt/com/sun/star/comp/connections/ConstantInstanceProvider.java
@@ -0,0 +1,142 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: ConstantInstanceProvider.java,v $
+ * $Revision: 1.3 $
+ *
+ * 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.comp.connections;
+
+import com.sun.star.bridge.XInstanceProvider;
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.registry.XRegistryKey;
+
+import com.sun.star.comp.loader.FactoryHelper;
+
+
+/**
+ * The <code>ConstantInstanceProvider</code> is a component
+ * that implements the <code>XInstanceProvider</code> Interface.
+ * <p>
+ * @version $Revision: 1.3 $ $ $Date: 2008-04-11 11:08:55 $
+ * @author Kay Ramme
+ * @see com.sun.star.bridge.XBridge
+ * @see com.sun.star.bridge.XBridgeFactory
+ * @see com.sun.star.bridge.XInstanceProvider
+ * @see com.sun.star.loader.JavaLoader
+ * @since UDK1.0
+ */
+public class ConstantInstanceProvider implements XInstanceProvider {
+ /**
+ * When set to true, enables various debugging output.
+ */
+ static public final boolean DEBUG = false;
+
+ /**
+ * The name of the service, the <code>JavaLoader</code> acceses this through reflection.
+ */
+ static private final String __serviceName = "com.sun.star.comp.connection.InstanceProvider";
+
+ /**
+ * Gives a factory for creating the service.
+ * This method is called by the <code>JavaLoader</code>
+ * <p>
+ * @return returns a <code>XSingleServiceFactory</code> for creating the component
+ * @param implName the name of the implementation for which a service is desired
+ * @param multiFactory the service manager to be uses if needed
+ * @param regKey the registryKey
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static XSingleServiceFactory __getServiceFactory(String implName,
+ XMultiServiceFactory multiFactory,
+ XRegistryKey regKey)
+ {
+ XSingleServiceFactory xSingleServiceFactory = null;
+
+ if (implName.equals(ConstantInstanceProvider.class.getName()) )
+ xSingleServiceFactory = FactoryHelper.getServiceFactory(ConstantInstanceProvider.class,
+ __serviceName,
+ multiFactory,
+ regKey);
+
+ return xSingleServiceFactory;
+ }
+
+ /**
+ * Writes the service information into the given registry key.
+ * This method is called by the <code>JavaLoader</code>
+ * <p>
+ * @return returns true if the operation succeeded
+ * @param regKey the registryKey
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
+ return FactoryHelper.writeRegistryServiceInfo(ConstantInstanceProvider.class.getName(), __serviceName, regKey);
+ }
+
+
+
+ protected XMultiServiceFactory _serviceManager;
+ protected String _serviceName;
+ protected Object _instance;
+
+
+ public void setInstance(String serviceName) throws com.sun.star.uno.Exception {
+ _instance = _serviceManager.createInstance(serviceName);
+ _serviceName = serviceName;
+ }
+
+ /**
+ * Constructs a new <code>ConstantInstanceProvider</code>.
+ * Uses the provided ServiceManager as the provided instance.
+ * <p>
+ * @param serviceName the provided service manager
+ */
+ public ConstantInstanceProvider(XMultiServiceFactory serviceManager) {
+ _serviceManager = serviceManager;
+
+ _serviceName = "SERVICEMANAGER";
+ _instance = serviceManager;
+ }
+
+ /**
+ * Gives an object for the passed instance name.
+ * <p>
+ * @return the desired instance
+ * @param sInstanceName the name of the desired instance
+ */
+ public Object getInstance(String sInstanceName) throws com.sun.star.container.NoSuchElementException, com.sun.star.uno.RuntimeException {
+ Object result = sInstanceName.equals(_serviceName) ? _instance : null;
+
+ if(DEBUG) System.err.println("##### " + getClass().getName() + ".getInstance(" + sInstanceName + "):" + result);
+
+ return result;
+ }
+}
+
diff --git a/jurt/com/sun/star/comp/connections/Implementation.java b/jurt/com/sun/star/comp/connections/Implementation.java
new file mode 100644
index 000000000000..bbb6e56dd1ff
--- /dev/null
+++ b/jurt/com/sun/star/comp/connections/Implementation.java
@@ -0,0 +1,104 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: Implementation.java,v $
+ * $Revision: 1.3 $
+ *
+ * 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.comp.connections;
+
+import com.sun.star.connection.ConnectionSetupException;
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.uno.UnoRuntime;
+
+/**
+ * Helper class for <code>Acceptor</code> and <code>Connector</code>.
+ */
+final class Implementation {
+ /**
+ * Instantiate a service for a given connection type.
+ *
+ * @param factory the service factory used to instantiate the requested
+ * service.
+ * @param description has the following format:
+ * <code><var>type</var></code><!--
+ * -->*(<code><var>key</var>=<var>value</var></code>).
+ * The specific service implementation is instantiated through the
+ * service factory as
+ * <code>com.sun.star.connection.<var>type</var>service<var></var><!--
+ * --></code>
+ * (with <code><var>type</var></code> in lower case, and
+ * <code><var>service</var></code> either <code>Acceptor</code> or
+ * <code>Connector</code>).</p>
+ * @param serviceClass the IDL interface type for which to query the
+ * requested service.
+ * @param serviceType must be either <code>Acceptor</code> or
+ * <code>Connector</code>.
+ * @return an instance of the requested service. Never returns
+ * <code>null</code>.
+ * @throws ConnectionSetupException if the requested service can not be
+ * found, or cannot be instantiated.
+ */
+ public static Object getConnectionService(XMultiServiceFactory factory,
+ String description,
+ Class serviceClass,
+ String serviceType)
+ throws ConnectionSetupException
+ {
+ int i = description.indexOf(',');
+ String type
+ = (i < 0 ? description : description.substring(0, i)).toLowerCase();
+ Object service = null;
+ try {
+ service = UnoRuntime.queryInterface(
+ serviceClass,
+ factory.createInstance("com.sun.star.connection." + type
+ + serviceType));
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (com.sun.star.uno.Exception e) {
+ }
+ if (service == null) {
+ // As a fallback, also try to instantiate the service from the
+ // com.sun.star.lib.connections package structure:
+ try {
+ service
+ = Class.forName("com.sun.star.lib.connections." + type
+ + "." + type + serviceType).newInstance();
+ } catch (ClassNotFoundException e) {
+ } catch (IllegalAccessException e) {
+ } catch (InstantiationException e) {
+ }
+ }
+ if (service == null) {
+ throw new ConnectionSetupException("no " + serviceType + " for "
+ + type);
+ }
+ return service;
+ }
+
+ private Implementation() {} // do not instantiate
+}
diff --git a/jurt/com/sun/star/comp/connections/PipedConnection.java b/jurt/com/sun/star/comp/connections/PipedConnection.java
new file mode 100644
index 000000000000..1c508206b86b
--- /dev/null
+++ b/jurt/com/sun/star/comp/connections/PipedConnection.java
@@ -0,0 +1,286 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: PipedConnection.java,v $
+ * $Revision: 1.3 $
+ *
+ * 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.comp.connections;
+
+
+import com.sun.star.comp.loader.FactoryHelper;
+
+import com.sun.star.connection.XConnection;
+
+import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.lang.XSingleServiceFactory;
+
+import com.sun.star.registry.XRegistryKey;
+
+/**
+ * The PipedConnection is a component that implements the
+ * <code>XConnection</code> Interface.
+ * It is useful for <code>Thread</code> communication
+ * in one Process.
+ * <p>
+ * @version $Revision: 1.3 $ $ $Date: 2008-04-11 11:09:30 $
+ * @author Kay Ramme
+ * @see com.sun.star.connections.XConnection
+ * @see com.sun.star.loader.JavaLoader
+ * @since UDK1.0
+ */
+public class PipedConnection implements XConnection {
+ /**
+ * When set to true, enables various debugging output.
+ */
+ public static final boolean DEBUG = false;
+
+ /**
+ * The name of the service, the <code>JavaLoader</code> acceses this through reflection.
+ */
+ static private final String __serviceName = "com.sun.star.connection.PipedConnection";
+
+ /**
+ * Gives a factory for creating the service.
+ * This method is called by the <code>JavaLoader</code>
+ * <p>
+ * @return returns a <code>XSingleServiceFactory</code> for creating the component
+ * @param implName the name of the implementation for which a service is desired
+ * @param multiFactory the service manager to be uses if needed
+ * @param regKey the registryKey
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static XSingleServiceFactory __getServiceFactory(String implName,
+ XMultiServiceFactory multiFactory,
+ XRegistryKey regKey)
+ {
+ XSingleServiceFactory xSingleServiceFactory = null;
+
+ if (implName.equals(PipedConnection.class.getName()) )
+ xSingleServiceFactory = FactoryHelper.getServiceFactory(PipedConnection.class,
+ __serviceName,
+ multiFactory,
+ regKey);
+
+ return xSingleServiceFactory;
+ }
+
+ /**
+ * Writes the service information into the given registry key.
+ * This method is called by the <code>JavaLoader</code>
+ * <p>
+ * @return returns true if the operation succeeded
+ * @param regKey the registryKey
+ * @see com.sun.star.comp.loader.JavaLoader
+ */
+ public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
+ return FactoryHelper.writeRegistryServiceInfo(PipedConnection.class.getName(), __serviceName, regKey);
+ }
+
+
+ /**
+ * The amount of time in milliseconds, to wait to
+ * see check the buffers.
+ */
+ protected static final int __waitTime = 10000;
+
+ protected byte _buffer[] = new byte[4096];
+ protected int _in,
+ _out;
+ protected boolean _closed;
+ protected PipedConnection _otherSide;
+
+ /**
+ * Constructs a new <code>PipedConnection</code>, sees if there
+ * is an other side, which it should be connected to.
+ * <p>
+ * @param args Another side could be in index 0.
+ */
+ public PipedConnection(Object args[]) throws com.sun.star.uno.RuntimeException {
+ if (DEBUG) System.err.println("##### " + getClass().getName() + " - instantiated");
+
+ _otherSide = (args.length == 1) ? (PipedConnection)args[0] : null;
+ if(_otherSide != null) {
+ if(_otherSide == this)
+ throw new RuntimeException("can not connect to myself");
+
+ _otherSide._otherSide = this;
+ }
+ }
+
+ /**
+ * This is a private method, used to cummunicate
+ * internal in the pipe.
+ */
+ private synchronized void receive(byte aData[]) throws com.sun.star.io.IOException {
+ int bytesWritten = 0;
+
+ if(DEBUG) System.err.println("##### PipedConnection.receive - bytes:" + aData.length + " at:" + _out);
+
+ while(bytesWritten < aData.length) {
+ // wait until it is not full anymore
+ while(_out == (_in - 1) || (_in == 0 && _out == _buffer.length - 1)) {
+ try {
+ notify(); // the buffer is full, signal it
+
+ wait(__waitTime);
+ }
+ catch(InterruptedException interruptedException) {
+ throw new com.sun.star.io.IOException(interruptedException.toString());
+ }
+ }
+
+ if(_closed) throw new com.sun.star.io.IOException("connection has been closed");
+
+ int bytes = 0;
+
+ if(_out < _in) {
+ bytes = Math.min(aData.length - bytesWritten, _in - _out - 1);
+
+ System.arraycopy(aData, bytesWritten, _buffer, _out, bytes);
+ }
+ else {
+ if(_in > 0){
+ bytes = Math.min(aData.length - bytesWritten, _buffer.length - _out);
+ }
+ else {
+ bytes = Math.min(aData.length - bytesWritten, _buffer.length - _out - 1);
+ }
+
+ System.arraycopy(aData, bytesWritten, _buffer, _out, bytes);
+ }
+
+ bytesWritten += bytes;
+ _out += bytes;
+ if(_out >= _buffer.length)
+ _out = 0;
+ }
+ }
+
+ /**
+ * 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 synchronized int read(/*OUT*/byte[][] aReadBytes, int nBytesToRead) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
+ aReadBytes[0] = new byte[nBytesToRead];
+
+ if(DEBUG) System.err.println("##### PipedConnection.read - bytes:" + nBytesToRead + " at:" + _in);
+
+ // loop while not all bytes read or when closed but there is still data
+ while(nBytesToRead > 0 && (_in != _out || !_closed)) {
+ while(_in == _out && !_closed) {
+ try {
+ notify(); // the buffer is empty, signal it
+
+ wait(__waitTime); // we wait for data or for the pipe to be closed
+ }
+ catch(InterruptedException interruptedException) {
+ throw new com.sun.star.io.IOException(interruptedException.toString());
+ }
+ }
+
+ if(_in < _out) {
+ int bytes = Math.min(nBytesToRead, _out - _in);
+
+ System.arraycopy(_buffer, _in, aReadBytes[0], aReadBytes[0].length - nBytesToRead, bytes);
+
+ nBytesToRead -= bytes;
+ _in += bytes;
+ }
+ else if(_in > _out) {
+ int bytes = Math.min(nBytesToRead, _buffer.length - _in);
+
+ System.arraycopy(_buffer, _in, aReadBytes[0], aReadBytes[0].length - nBytesToRead, bytes);
+
+ nBytesToRead -= bytes;
+ _in += bytes;
+ if(_in >= _buffer.length)
+ _in = 0;
+ }
+ }
+
+ if(nBytesToRead > 0) { // not all bytes read
+ byte tmp[] = new byte[aReadBytes[0].length - nBytesToRead];
+ System.arraycopy(aReadBytes[0], 0, tmp, 0, tmp.length);
+
+ aReadBytes[0] = tmp;
+ }
+
+ return aReadBytes[0].length;
+ }
+
+ /**
+ * 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 {
+ _otherSide.receive(aData);
+ }
+
+ /**
+ * Flushes the buffer, notifies if necessary the other side that new data has arrived.
+ * <p>
+ * @see com.sun.star.connections.XConnection#flush
+ */
+ public void flush() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
+ synchronized(_otherSide) {
+ _otherSide.notify();
+ }
+ }
+
+ /**
+ * Closes the pipe.
+ * <p>
+ * @see com.sun.star.connections.XConnection#closed
+ */
+ public synchronized void close() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
+ if(!_closed) {
+ _closed = true;
+
+ _otherSide.close();
+
+ notify();
+ }
+ }
+
+ /**
+ * Gives a description of this pipe.
+ * <p>
+ * @return the description
+ * @see com.sun.star.connections.XConnection#getDescription
+ */
+ public String getDescription() throws com.sun.star.uno.RuntimeException {
+ return getClass().getName();
+ }
+
+}
+
diff --git a/jurt/com/sun/star/comp/connections/makefile.mk b/jurt/com/sun/star/comp/connections/makefile.mk
new file mode 100644
index 000000000000..02f536af5ae9
--- /dev/null
+++ b/jurt/com/sun/star/comp/connections/makefile.mk
@@ -0,0 +1,47 @@
+#*************************************************************************
+#
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+#
+# Copyright 2008 by Sun Microsystems, Inc.
+#
+# OpenOffice.org - a multi-platform office productivity suite
+#
+# $RCSfile: makefile.mk,v $
+#
+# $Revision: 1.6 $
+#
+# 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.
+#
+#*************************************************************************
+
+PRJ = ..$/..$/..$/..$/..
+PRJNAME = jurt
+TARGET = com_sun_star_comp_connections
+
+PACKAGE = com$/sun$/star$/comp$/connections
+
+.INCLUDE: $(PRJ)$/util$/makefile.pmk
+
+JAVAFILES = \
+ Acceptor.java \
+ Connector.java \
+ ConstantInstanceProvider.java \
+ Implementation.java \
+ PipedConnection.java
+
+.INCLUDE: target.mk