diff options
Diffstat (limited to 'jurt/com/sun/star/comp')
17 files changed, 3672 insertions, 0 deletions
diff --git a/jurt/com/sun/star/comp/bridgefactory/BridgeFactory.java b/jurt/com/sun/star/comp/bridgefactory/BridgeFactory.java new file mode 100644 index 000000000000..a444f1813c3d --- /dev/null +++ b/jurt/com/sun/star/comp/bridgefactory/BridgeFactory.java @@ -0,0 +1,228 @@ +/************************************************************************* + * + * 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.comp.bridgefactory; + +import java.math.BigInteger; +import java.util.Vector; + + +import com.sun.star.bridge.BridgeExistsException; +import com.sun.star.bridge.XBridge; +import com.sun.star.bridge.XBridgeFactory; +import com.sun.star.bridge.XInstanceProvider; + +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; + +import com.sun.star.uno.IBridge; +import com.sun.star.uno.UnoRuntime; + + +/** + * The BridgeFactory class implements the <code>XBridgeFactory</code> Interface. + * It wrapps the <code>UnoRuntime#getBridgeByName</code>method and delivers a + * XBridge component. + * <p> + * This component is only usable for remote bridges. + * <p> + * @version $Revision: 1.11 $ $ $Date: 2008-04-11 11:07:51 $ + * @author Kay Ramme + * @see com.sun.star.uno.UnoRuntime + * @since UDK1.0 + */ +public class BridgeFactory implements XBridgeFactory/*, XEventListener*/ { + static private final boolean DEBUG = false; + + /** + * The name of the service, the <code>JavaLoader</code> acceses this through reflection. + */ + public final static String __serviceName = "com.sun.star.bridge.BridgeFactory"; + + /** + * 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(BridgeFactory.class.getName()) ) + xSingleServiceFactory = FactoryHelper.getServiceFactory(BridgeFactory.class, + 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(BridgeFactory.class.getName(), __serviceName, regKey); + } + + /** + * Creates a remote bridge and memorizes it under <code>sName</code>. + * <p> + * @return the bridge + * @param sName the name to memorize the bridge + * @param sProtocol the protocol the bridge should use + * @param anInstanceProvider the instance provider + * @see com.sun.star.bridge.XBridgeFactory + */ + public XBridge createBridge(String sName, String sProtocol, XConnection aConnection, XInstanceProvider anInstanceProvider) throws + BridgeExistsException, + com.sun.star.lang.IllegalArgumentException, + com.sun.star.uno.RuntimeException + { + boolean hasName = sName.length() != 0; + Object context = hasName ? (Object) sName : (Object) new UniqueToken(); + // UnoRuntime.getBridgeByName internally uses context.toString() to + // distinguish bridges, so the result of + // new UniqueToken().toString() might clash with an explicit + // sName.toString(), but the UnoRuntime bridge management is + // obsolete anyway and should be removed + + // do not create a new bridge, if one already exists + if (hasName) { + IBridge iBridges[] = UnoRuntime.getBridges(); + for(int i = 0; i < iBridges.length; ++ i) { + XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]); + + if(xBridge != null) { + if(xBridge.getName().equals(sName)) + throw new BridgeExistsException(sName + " already exists"); + } + } + } + + XBridge xBridge = null; + + try { + IBridge iBridge = UnoRuntime.getBridgeByName("java", context, "remote", context, hasName ? new Object[]{sProtocol, aConnection, anInstanceProvider, sName} : new Object[]{sProtocol, aConnection, anInstanceProvider}); + + xBridge = UnoRuntime.queryInterface(XBridge.class, iBridge); + } + catch(Exception exception) { + throw new com.sun.star.lang.IllegalArgumentException(exception.getMessage()); + } + + if(DEBUG) System.err.println("##### " + getClass().getName() + ".createBridge:" + sName + " " + sProtocol + " " + aConnection + " " + anInstanceProvider + " " + xBridge); + + return xBridge; + } + + /** + * Gets a remote bridge which must already exist. + * <p> + * @return the bridge + * @param sName the name of the bridge + * @see com.sun.star.bridge.XBridgeFactory + */ + public XBridge getBridge(String sName) throws com.sun.star.uno.RuntimeException { + XBridge xBridge = null; + + IBridge iBridges[] = UnoRuntime.getBridges(); + for(int i = 0; i < iBridges.length; ++ i) { + xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]); + + if(xBridge != null) { + if(xBridge.getName().equals(sName)) + break; + + else + xBridge = null; + } + } + + + if(DEBUG) System.err.println("##### " + getClass().getName() + ".getBridge:" + sName + " " + xBridge); + + return xBridge; + } + + /** + * Gives all created bridges + * <p> + * @return the bridges + * @see com.sun.star.bridge.XBridgeFactory + */ + public synchronized XBridge[] getExistingBridges() throws com.sun.star.uno.RuntimeException { + Vector vector = new Vector(); + + IBridge iBridges[] = UnoRuntime.getBridges(); + for(int i = 0; i < iBridges.length; ++ i) { + XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]); + + if(xBridge != null) + vector.addElement(xBridge); + } + + XBridge xBridges[]= new XBridge[vector.size()]; + for(int i = 0; i < vector.size(); ++ i) + xBridges[i] = (XBridge)vector.elementAt(i); + + return xBridges; + } + + private static final class UniqueToken { + public UniqueToken() { + synchronized (UniqueToken.class) { + token = counter.toString(); + counter = counter.add(BigInteger.ONE); + } + } + + public String toString() { + return token; + } + + private final String token; + private static BigInteger counter = BigInteger.ZERO; + } +} + diff --git a/jurt/com/sun/star/comp/bridgefactory/makefile.mk b/jurt/com/sun/star/comp/bridgefactory/makefile.mk new file mode 100644 index 000000000000..fa1c7345e9a1 --- /dev/null +++ b/jurt/com/sun/star/comp/bridgefactory/makefile.mk @@ -0,0 +1,43 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* + +PRJ = ..$/..$/..$/..$/.. +PRJNAME = jurt +PACKAGE = com$/sun$/star$/comp$/bridgefactory +TARGET = com_sun_star_comp_bridgefactory + +# --- Settings ----------------------------------------------------- + +.INCLUDE : $(PRJ)$/util$/makefile.pmk + +# --- Files -------------------------------------------------------- + +JAVAFILES = BridgeFactory.java + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk 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..3df51e735a1c --- /dev/null +++ b/jurt/com/sun/star/comp/connections/Acceptor.java @@ -0,0 +1,170 @@ +/************************************************************************* + * + * 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.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..c02db5c52b8b --- /dev/null +++ b/jurt/com/sun/star/comp/connections/Connector.java @@ -0,0 +1,151 @@ +/************************************************************************* + * + * 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.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..f63eeadf10d8 --- /dev/null +++ b/jurt/com/sun/star/comp/connections/ConstantInstanceProvider.java @@ -0,0 +1,139 @@ +/************************************************************************* + * + * 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.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..466ea9741766 --- /dev/null +++ b/jurt/com/sun/star/comp/connections/Implementation.java @@ -0,0 +1,101 @@ +/************************************************************************* + * + * 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.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..630adf4e223b --- /dev/null +++ b/jurt/com/sun/star/comp/connections/PipedConnection.java @@ -0,0 +1,283 @@ +/************************************************************************* + * + * 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.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..99141108c7d9 --- /dev/null +++ b/jurt/com/sun/star/comp/connections/makefile.mk @@ -0,0 +1,43 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* + +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 diff --git a/jurt/com/sun/star/comp/loader/FactoryHelper.java b/jurt/com/sun/star/comp/loader/FactoryHelper.java new file mode 100644 index 000000000000..29b484631670 --- /dev/null +++ b/jurt/com/sun/star/comp/loader/FactoryHelper.java @@ -0,0 +1,564 @@ +/************************************************************************* + * + * 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.comp.loader; + + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; + +import com.sun.star.uno.XComponentContext; +import com.sun.star.lang.XInitialization; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.lang.XSingleServiceFactory; +import com.sun.star.lang.XSingleComponentFactory; +import com.sun.star.lang.XTypeProvider; + +import com.sun.star.registry.XRegistryKey; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.Type; + + +/** + * The purpose of this class to help component implementation. + * This class has default implementations for <code>getServiceFactory</code> + * and <code>writeRegistryServiceInfo</code>. + * <p> + * @version $Revision: 1.9 $ $ $Date: 2008-04-11 11:10:09 $ + * @author Kay Ramme + * @see com.sun.star.lang.XMultiServiceFactory + * @see com.sun.star.lang.XServiceInfo + * @see com.sun.star.lang.XSingleServiceFactory + * @see com.sun.star.registry.XRegistryKey + * @since UDK1.0 + */ +public class FactoryHelper { + + private static final boolean DEBUG = false; + // the factory + static protected class Factory + implements XSingleServiceFactory, XSingleComponentFactory, XServiceInfo, + XTypeProvider { + protected static Class __objectArray; + + static { + try { + __objectArray = Class.forName("[Ljava.lang.Object;"); + } + catch(ClassNotFoundException classNotFoundException) { + System.err.println(FactoryHelper.class.getName() + " exception occurred - " + classNotFoundException); + } + } + +// private static final boolean DEBUG = false; + + protected XMultiServiceFactory _xMultiServiceFactory; + protected XRegistryKey _xRegistryKey; + protected int _nCode; + protected Constructor _constructor; + protected String _implName; + protected String _serviceName; + // keeps the Id for XTypeProvider + protected static Object _mutex= new Object(); + private static byte[] _implementationId; + + protected Factory(Class implClass, + String serviceName, + XMultiServiceFactory xMultiServiceFactory, + XRegistryKey xRegistryKey) + { + _xMultiServiceFactory = xMultiServiceFactory; + _xRegistryKey = xRegistryKey; + _implName = implClass.getName(); + _serviceName = serviceName; + + Constructor constructors[] = implClass.getConstructors(); + for(int i = 0; i < constructors.length && _constructor == null; ++i) { + Class parameters[] = constructors[i].getParameterTypes(); + + if(parameters.length == 3 + && parameters[0].equals(XComponentContext.class) + && parameters[1].equals(XRegistryKey.class) + && parameters[2].equals(__objectArray)) { + _nCode = 0; + _constructor = constructors[i]; + } + else if(parameters.length == 2 + && parameters[0].equals(XComponentContext.class) + && parameters[1].equals(XRegistryKey.class)) { + _nCode = 1; + _constructor = constructors[i]; + } + else if(parameters.length == 2 + && parameters[0].equals(XComponentContext.class) + && parameters[1].equals(__objectArray)) { + _nCode = 2; + _constructor = constructors[i]; + } + else if(parameters.length == 1 + && parameters[0].equals(XComponentContext.class)) { + _nCode = 3; + _constructor = constructors[i]; + } + // depr + else if(parameters.length == 3 + && parameters[0].equals(XMultiServiceFactory.class) + && parameters[1].equals(XRegistryKey.class) + && parameters[2].equals(__objectArray)) { + _nCode = 4; + _constructor = constructors[i]; + } + else if(parameters.length == 2 + && parameters[0].equals(XMultiServiceFactory.class) + && parameters[1].equals(XRegistryKey.class)) { + _nCode = 5; + _constructor = constructors[i]; + } + else if(parameters.length == 2 + && parameters[0].equals(XMultiServiceFactory.class) + && parameters[1].equals(__objectArray)) { + _nCode = 6; + _constructor = constructors[i]; + } + else if(parameters.length == 1 + && parameters[0].equals(XMultiServiceFactory.class)) { + _nCode = 7; + _constructor = constructors[i]; + } + else if(parameters.length == 1 + && parameters[0].equals(__objectArray)) { + _nCode = 8; + _constructor = constructors[i]; + } + else if(parameters.length == 0) { + _nCode = 9; + _constructor = constructors[i]; + } + } + + if(_constructor == null) // have not found a useable constructor + throw new com.sun.star.uno.RuntimeException(getClass().getName() + " can not find a useable constructor"); + } + + private final XMultiServiceFactory getSMgr( XComponentContext xContext ) + { + if (xContext != null) + { + return UnoRuntime.queryInterface( + XMultiServiceFactory.class, xContext.getServiceManager() ); + } + else + { + return _xMultiServiceFactory; + } + } + + // XComponentContext impl + //______________________________________________________________________________________________ + public Object createInstanceWithContext( + XComponentContext xContext ) + throws com.sun.star.uno.Exception + { + Object args[]; + switch (_nCode) + { + case 0: + args = new Object [] { xContext, _xRegistryKey, new Object[ 0 ] }; + break; + case 1: + args = new Object [] { xContext, _xRegistryKey }; + break; + case 2: + args = new Object [] { xContext, new Object[ 0 ] }; + break; + case 3: + args = new Object [] { xContext }; + break; + case 4: + args = new Object [] { getSMgr( xContext ), _xRegistryKey, new Object[ 0 ] }; + break; + case 5: + args = new Object [] { getSMgr( xContext ), _xRegistryKey }; + break; + case 6: + args = new Object [] { getSMgr( xContext ), new Object[ 0 ] }; + break; + case 7: + args = new Object [] { getSMgr( xContext ) }; + break; + case 8: + args = new Object [] { new Object[ 0 ] }; + break; + default: + args = new Object [ 0 ]; + break; + } + + try + { + return _constructor.newInstance( args ); + } + catch (InvocationTargetException invocationTargetException) + { + Throwable targetException = invocationTargetException.getTargetException(); + + if (targetException instanceof java.lang.RuntimeException) + throw (java.lang.RuntimeException)targetException; + else if (targetException instanceof com.sun.star.uno.Exception) + throw (com.sun.star.uno.Exception)targetException; + else if (targetException instanceof com.sun.star.uno.RuntimeException) + throw (com.sun.star.uno.RuntimeException)targetException; + else + throw new com.sun.star.uno.Exception( targetException.toString() ); + } + catch (IllegalAccessException illegalAccessException) + { + throw new com.sun.star.uno.Exception( illegalAccessException.toString() ); + } + catch (InstantiationException instantiationException) + { + throw new com.sun.star.uno.Exception( instantiationException.toString() ); + } + } + //______________________________________________________________________________________________ + public Object createInstanceWithArgumentsAndContext( + Object rArguments[], XComponentContext xContext ) + throws com.sun.star.uno.Exception + { + Object args[]; + + boolean bInitCall = true; + switch (_nCode) + { + case 0: + args = new Object [] { xContext, _xRegistryKey, rArguments }; + bInitCall = false; + break; + case 1: + args = new Object [] { xContext, _xRegistryKey }; + break; + case 2: + args = new Object [] { xContext, rArguments }; + bInitCall = false; + break; + case 3: + args = new Object [] { xContext }; + break; + case 4: + args = new Object [] { getSMgr( xContext ), _xRegistryKey, rArguments }; + bInitCall = false; + break; + case 5: + args = new Object [] { getSMgr( xContext ), _xRegistryKey }; + break; + case 6: + args = new Object [] { getSMgr( xContext ), rArguments }; + bInitCall = false; + break; + case 7: + args = new Object [] { getSMgr( xContext ) }; + break; + case 8: + args = new Object [] { rArguments }; + bInitCall = false; + break; + default: + args = new Object [ 0 ]; + break; + } + + try + { + Object instance = _constructor.newInstance( args ); + if (bInitCall) + { + XInitialization xInitialization = UnoRuntime.queryInterface( + XInitialization.class, instance ); + if (xInitialization != null) + { + xInitialization.initialize( rArguments ); + } + } + return instance; + } + catch (InvocationTargetException invocationTargetException) + { + Throwable targetException = invocationTargetException.getTargetException(); + + if (targetException instanceof java.lang.RuntimeException) + throw (java.lang.RuntimeException)targetException; + else if (targetException instanceof com.sun.star.uno.Exception) + throw (com.sun.star.uno.Exception)targetException; + else if (targetException instanceof com.sun.star.uno.RuntimeException) + throw (com.sun.star.uno.RuntimeException)targetException; + else + throw new com.sun.star.uno.Exception( targetException.toString() ); + } + catch (IllegalAccessException illegalAccessException) + { + throw new com.sun.star.uno.Exception( illegalAccessException.toString() ); + } + catch (InstantiationException instantiationException) + { + throw new com.sun.star.uno.Exception( instantiationException.toString() ); + } + } + + /** + * Creates an instance of the desired service. + * <p> + * @return returns an instance of the desired service + * @see com.sun.star.lang.XSingleServiceFactory + */ + public Object createInstance() + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + return createInstanceWithContext( null ); + } + + /** + * Creates an instance of the desired service. + * <p> + * @return returns an instance of the desired service + * @param args the args given to the constructor of the service + * @see com.sun.star.lang.XSingleServiceFactory + */ + public Object createInstanceWithArguments(Object[] args) + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + return createInstanceWithArgumentsAndContext( args, null ); + } + + /** + * Gives the supported services + * <p> + * @return returns an array of supported services + * @see com.sun.star.lang.XServiceInfo + */ + public String[] getSupportedServiceNames() throws com.sun.star.uno.RuntimeException { + return new String[]{_serviceName}; + } + + /** + * Gives the implementation name + * <p> + * @return returns the implementation name + * @see com.sun.star.lang.XServiceInfo + */ + public String getImplementationName() throws com.sun.star.uno.RuntimeException { + return _implName; + } + + /** + * Indicates if the given service is supported. + * <p> + * @return returns true if the given service is supported + * @see com.sun.star.lang.XServiceInfo + */ + public boolean supportsService(String serviceName) throws com.sun.star.uno.RuntimeException { + String services[] = getSupportedServiceNames(); + + boolean found = false; + + for(int i = 0; i < services.length && !found; ++i) + found = services[i].equals(serviceName); + + return found; + } + + //XTypeProvider + public byte[] getImplementationId() + { + synchronized (_mutex) + { + if (_implementationId == null) + { + int hash = hashCode(); + String sName= getClass().getName(); + byte[] arName= sName.getBytes(); + int nNameLength= arName.length; + + _implementationId= new byte[ 4 + nNameLength]; + _implementationId[0]= (byte)(hash & 0xff); + _implementationId[1]= (byte)((hash >>> 8) & 0xff); + _implementationId[2]= (byte)((hash >>> 16) & 0xff); + _implementationId[3]= (byte)((hash >>>24) & 0xff); + + for (int i= 0; i < nNameLength; i++) + { + _implementationId[4 + i]= arName[i]; + } + } + } + return _implementationId; + } + //XTypeProvider + public com.sun.star.uno.Type[] getTypes() + { + Type[] t = new Type[] { + new Type(XSingleServiceFactory.class), + new Type(XSingleComponentFactory.class), + new Type(XServiceInfo.class), + new Type(XTypeProvider.class) + }; + return t; + } + + } + + /** + * Creates a factory for the given class. + * <p> + * @deprecated as of UDK 1.0 + * <p> + * @return returns a factory + * @param implClass the implementing class + * @param multiFactory the given multi service factory (service manager) + * @param regKey the given registry key + * @see com.sun.star.lang.XServiceInfo + */ + static public XSingleServiceFactory getServiceFactory(Class implClass, + XMultiServiceFactory multiFactory, + XRegistryKey regKey) + { + XSingleServiceFactory xSingleServiceFactory = null; + + try { + Field serviceName = null; + + try { + serviceName = implClass.getField("__serviceName"); + } + catch(NoSuchFieldException noSuchFieldExceptio) { + serviceName = implClass.getField("serviceName"); // old style + } + + xSingleServiceFactory = new Factory(implClass, (String)serviceName.get(null), multiFactory, regKey); + } + catch(NoSuchFieldException noSuchFieldException) { + System.err.println("##### FactoryHelper.getServiceFactory - exception:" + noSuchFieldException); + } + catch(IllegalAccessException illegalAccessException) { + System.err.println("##### FactoryHelper.getServiceFactory - exception:" + illegalAccessException); + } + + return xSingleServiceFactory; + } + + /** + * Creates a factory for the given class. + * <p> + * @return returns a factory + * @param implClass the implementing class + * @param serviceName the service name of the implementing class + * @param multiFactory the given multi service factory (service manager) + * @param regKey the given registry key + * @see com.sun.star.lang.XServiceInfo + */ + static public XSingleServiceFactory getServiceFactory(Class implClass, + String serviceName, + XMultiServiceFactory multiFactory, + XRegistryKey regKey) + { + return new Factory(implClass, serviceName, multiFactory, regKey); + } + + /** Creates a factory for the given class. + + @return returns a factory object + @param implClass the implementing class + */ + static public Object createComponentFactory( Class implClass, String serviceName ) + { + return new Factory( implClass, serviceName, null, null ); + } + + /** + * Writes the registration data into the registry key + * <p> + * @return success + * @param implName the name of the implementing class + * @param serviceName the service name + * @param regKey the given registry key + * @see com.sun.star.lang.XServiceInfo + */ + static public boolean writeRegistryServiceInfo(String implName, String serviceName, XRegistryKey regKey) { + boolean result = false; + + try { + XRegistryKey newKey = regKey.createKey("/" + implName + "/UNO/SERVICES"); + + newKey.createKey(serviceName); + + result = true; + } + catch (Exception ex) { + System.err.println(">>>Connection_Impl.writeRegistryServiceInfo " + ex); + } + + return result; + } + + /** Writes the registration data into the registry key. + * Several services are supported. + * + * @param impl_name name of implementation + * @param supported_services supported services of implementation + * @param xKey registry key to write to + * @return success + */ + public static boolean writeRegistryServiceInfo( + String impl_name, String supported_services [], XRegistryKey xKey ) + { + try + { + XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" ); + for ( int nPos = 0; nPos < supported_services.length; ++nPos ) + { + xNewKey.createKey( supported_services[ nPos ] ); + } + return true; + } + catch (com.sun.star.registry.InvalidRegistryException exc) + { + if (DEBUG) + { + System.err.println( + "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " + + exc.toString() ); + } + } + return false; + } + +} + diff --git a/jurt/com/sun/star/comp/loader/JavaLoader.java b/jurt/com/sun/star/comp/loader/JavaLoader.java new file mode 100644 index 000000000000..47723b208497 --- /dev/null +++ b/jurt/com/sun/star/comp/loader/JavaLoader.java @@ -0,0 +1,483 @@ +/************************************************************************* + * + * 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.comp.loader; + +import java.lang.reflect.Method; + +import java.lang.reflect.InvocationTargetException; + +import java.net.URLDecoder; + +import com.sun.star.loader.CannotActivateFactoryException; +import com.sun.star.loader.XImplementationLoader; + +import com.sun.star.registry.CannotRegisterImplementationException; +import com.sun.star.registry.XRegistryKey; + +import com.sun.star.lang.XSingleComponentFactory; +import com.sun.star.lang.XSingleServiceFactory; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.lang.XInitialization; + +import com.sun.star.uno.XComponentContext; +import com.sun.star.beans.XPropertySet; +import com.sun.star.util.XMacroExpander; + +import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; + +import com.sun.star.lib.util.StringHelper; + +import com.sun.star.uno.AnyConverter; + + +/** + * The <code>JavaLoader</code> class provides the functionality of the <code>com.sun.star.loader.Java</code> + * service. Therefor the <code>JavaLoader</code> activates external UNO components which are implemented in Java. + * The loader is used by the <code>ServiceManger</code>. + * <p> + * @version $Revision: 1.16 $ $ $Date: 2008-04-11 11:10:31 $ + * @author Markus Herzog + * @see com.sun.star.loader.XImplementationLoader + * @see com.sun.star.loader.Java + * @see com.sun.star.comp.servicemanager.ServiceManager + * @see com.sun.star.lang.ServiceManager + * @since UDK1.0 + */ +public class JavaLoader implements XImplementationLoader, + XServiceInfo, + XInitialization +{ + private static final boolean DEBUG = false; + + private static final void DEBUG(String dbg) { + if (DEBUG) System.err.println( dbg ); + } + + private static String[] supportedServices = { + "com.sun.star.loader.Java" + }; + + protected XMultiServiceFactory multiServiceFactory = null; + + private XMacroExpander m_xMacroExpander = null; + private static final String EXPAND_PROTOCOL_PREFIX = "vnd.sun.star.expand:"; + + /** Expands macrofied url using the macro expander singleton. + */ + private String expand_url( String url ) throws RuntimeException + { + if (url != null && url.startsWith( EXPAND_PROTOCOL_PREFIX )) + { + try + { + if (m_xMacroExpander == null) + { + XPropertySet xProps = + UnoRuntime.queryInterface( + XPropertySet.class, multiServiceFactory ); + if (xProps == null) + { + throw new com.sun.star.uno.RuntimeException( + "service manager does not support XPropertySet!", + this ); + } + XComponentContext xContext = (XComponentContext) + AnyConverter.toObject( + new Type( XComponentContext.class ), + xProps.getPropertyValue( "DefaultContext" ) ); + m_xMacroExpander = (XMacroExpander)AnyConverter.toObject( + new Type( XMacroExpander.class ), + xContext.getValueByName( + "/singletons/com.sun.star.util.theMacroExpander" ) + ); + } + // decode uric class chars + String macro = URLDecoder.decode( + StringHelper.replace( + url.substring( EXPAND_PROTOCOL_PREFIX.length() ), + '+', "%2B" ) ); + // expand macro string + String ret = m_xMacroExpander.expandMacros( macro ); + if (DEBUG) + { + System.err.println( + "JavaLoader.expand_url(): " + url + " => " + + macro + " => " + ret ); + } + return ret; + } + catch (com.sun.star.uno.Exception exc) + { + throw new com.sun.star.uno.RuntimeException( + exc.getMessage(), this ); + } + catch (java.lang.Exception exc) + { + throw new com.sun.star.uno.RuntimeException( + exc.getMessage(), this ); + } + } + return url; + } + + /** default constructor + */ + + /** + * Creates a new instance of the <code>JavaLoader</code> class. + * <p> + * @return new instance + */ + public JavaLoader() {} + + /** + * Creates a new <code>JavaLoader</code> object. The specified <code>com.sun.star.lang.XMultiServiceFactory</code> + * is the <code>ServiceManager</code> service which can be deliviert to all components the <code>JavaLoader</code> is + * loading. + * To set the <code>MultiServiceFactory</code> you can use the <code>com.sun.star.lang.XInitialization</code> interface, either. + * <p> + * @return new instance + * @param factory the <code>ServiceManager</code> + * @see com.sun.star.lang.ServiceManager + * @see com.sun.star.lang.ServiceManager + * @see com.sun.star.lang.XInitialization + */ + public JavaLoader(XMultiServiceFactory factory) { + multiServiceFactory = factory; + } + + /** + * Unlike the original intention, the method could be called every time a new + * <code>com.sun.star.lang.XMultiServiceFactory</code> should be set at the loader. + * <p> + * @param args - the first parameter (args[0]) specifices the <code>ServiceManager</code> + * @see com.sun.star.lang.XInitialization + * @see com.sun.star.lang.ServiceManager + */ + public void initialize( java.lang.Object[] args ) + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + if (args.length == 0) throw new com.sun.star.lang.IllegalArgumentException("No arguments specified"); + + try { + multiServiceFactory = (XMultiServiceFactory) AnyConverter.toObject( + new Type(XMultiServiceFactory.class), args[0]); + } + catch (ClassCastException castEx) { + throw new com.sun.star.lang.IllegalArgumentException( + "The argument must be an instance of XMultiServiceFactory"); + } + } + + /** + * Supplies the implementation name of the component. + * <p> + * @return the implementation name - here the class name + * @see com.sun.star.lang.XServiceInfo + */ + public String getImplementationName() + throws com.sun.star.uno.RuntimeException + { + return getClass().getName(); + } + + /** + * Verifies if a given service is supported by the component. + * <p> + * @return true,if service is suported - otherwise false + * @param serviceName the name of the service that should be checked + * @see com.sun.star.lang.XServiceInfo + */ + public boolean supportsService(String serviceName) + throws com.sun.star.uno.RuntimeException + { + for ( int i = 0; i < supportedServices.length; i++ ) { + if ( supportedServices[i].equals(serviceName) ) + return true; + } + return false; + } + + /** + * Supplies a list of all service names supported by the component + * <p> + * @return a String array with all supported services + * @see com.sun.star.lang.XServiceInfo + */ + public String[] getSupportedServiceNames() + throws com.sun.star.uno.RuntimeException + { + return supportedServices; + } + + /** + * Provides a components factory. + * The <code>JavaLoader</code> tries to load the class first. If a loacation URL is given the + * RegistrationClassFinder is used to load the class. Otherwise the class is loaded thru the Class.forName + * method. + * To get the factory the inspects the class for the optional static member functions __getServiceFactory resp. + * getServiceFactory (DEPRECATED). + * If the function can not be found a default factory @see ComponentFactoryWrapper will be created. + * <p> + * @return the factory for the component (@see com.sun.star.lang.XSingleServiceFactory) + * @param implementationName the implementation (class) name of the component + * @param implementationLoaderUrl the URL of the implementation loader. Not used. + * @param locationUrl points to an archive (JAR file) which contains a component + * @param xKey + * @see com.sun.star.lang.XImplementationLoader + * @see com.sun.star.com.loader.RegistrationClassFinder + */ + public java.lang.Object activate( String implementationName, + String implementationLoaderUrl, + String locationUrl, + XRegistryKey xKey ) + throws CannotActivateFactoryException, + com.sun.star.uno.RuntimeException + { + locationUrl = expand_url( locationUrl ); + + Object returnObject = null; + Class clazz = null; + + DEBUG("try to get factory for " + implementationName); + + // first we must get the class of the implementation + // 1. If a location URL is given it is assumed that this points to a JAR file. + // The components class name is stored in the manifest file. + // 2. If only the implementation name is given, the class is loaded with the + // Class.forName() method. This is a hack to load bootstrap components. + // Normally a string must no be null. + try { + if ( locationUrl != null ) { + // 1. + clazz = RegistrationClassFinder.find( locationUrl ); + } + else { + // 2. + clazz = Class.forName( implementationName ); + } + } + catch (java.net.MalformedURLException e) { + CannotActivateFactoryException cae = new CannotActivateFactoryException( + "Can not activate factory because " + e.toString() ); + cae.fillInStackTrace(); + throw cae; + } + catch (java.io.IOException e) { + CannotActivateFactoryException cae = new CannotActivateFactoryException( + "Can not activate factory because " + e.toString() ); + cae.fillInStackTrace(); + throw cae; + } + catch (java.lang.ClassNotFoundException e) { + CannotActivateFactoryException cae = new CannotActivateFactoryException( + "Can not activate factory because " + e.toString() ); + cae.fillInStackTrace(); + throw cae; + } + + if (null == clazz) + { + CannotActivateFactoryException cae = + new CannotActivateFactoryException( + "Cannot determine activation class!" ); + cae.fillInStackTrace(); + throw cae; + } + + Class[] paramTypes = {String.class, XMultiServiceFactory.class, XRegistryKey.class}; + Object[] params = { implementationName, multiServiceFactory, xKey }; + + // try to get factory from implemetation class + // latest style: use the public static method __getComponentFactory + // - new style: use the public static method __getServiceFactory + // - old style: use the public static method getServiceFactory ( DEPRECATED ) + + Method compfac_method = null; + try + { + compfac_method = clazz.getMethod( + "__getComponentFactory", new Class [] { String.class } ); + } + catch ( NoSuchMethodException noSuchMethodEx) {} + catch ( SecurityException secEx) {} + + Method method = null; + if (null == compfac_method) + { + try { + method = clazz.getMethod("__getServiceFactory", paramTypes); + } + catch ( NoSuchMethodException noSuchMethodEx) { + method = null; + } + catch ( SecurityException secEx) { + method = null; + } + } + + try { + if (null != compfac_method) + { + Object ret = compfac_method.invoke( clazz, new Object [] { implementationName } ); + if (null == ret || !(ret instanceof XSingleComponentFactory)) + { + throw new CannotActivateFactoryException( + "No factory object for " + implementationName ); + } + return (XSingleComponentFactory)ret; + } + else + { + if ( method == null ) { + method = clazz.getMethod("getServiceFactory", paramTypes); + } + + Object oRet = method.invoke(clazz, params); + + if ( (oRet != null) && (oRet instanceof XSingleServiceFactory) ) { + returnObject = (XSingleServiceFactory) oRet; + } + } + } + catch ( NoSuchMethodException e) { + throw new CannotActivateFactoryException("Can not activate the factory for " + + implementationName + " because " + e.toString() ); + } + catch ( SecurityException e) { + throw new CannotActivateFactoryException("Can not activate the factory for " + + implementationName + " because " + e.toString() ); + } + catch ( IllegalAccessException e ) { + throw new CannotActivateFactoryException("Can not activate the factory for " + + implementationName + " because " + e.toString() ); + } + catch ( IllegalArgumentException e ) { + throw new CannotActivateFactoryException("Can not activate the factory for " + + implementationName + " because " + e.toString() ); + } + catch ( InvocationTargetException e ) { + throw new CannotActivateFactoryException("Can not activate the factory for " + + implementationName + " because " + e.getTargetException().toString() ); + } + + return returnObject; + } + + /** + * Registers the component in a registry under a given root key. If the component supports the optional + * methods __writeRegistryServiceInfo, writeRegistryServiceInfo (DEPRECATED), the call is delegated to that + * method. Otherwise a default registration will be accomplished. + * <p> + * @return true if registration is successfully - otherwise false + * @param regKey the root key under that the component should be registred. + * @param implementationLoaderUrl specifies the loader, the component is loaded by. + * @param locationUrl points to an archive (JAR file) which contains a component + * @see ComponentFactoryWrapper + */ + public boolean writeRegistryInfo( XRegistryKey regKey, + String implementationLoaderUrl, + String locationUrl ) + throws CannotRegisterImplementationException, + com.sun.star.uno.RuntimeException + { + locationUrl = expand_url( locationUrl ); + + boolean success = false; + + try { + + Class clazz = RegistrationClassFinder.find(locationUrl); + if (null == clazz) + { + throw new CannotRegisterImplementationException( + "Cannot determine registration class!" ); + } + + Class[] paramTypes = { XRegistryKey.class }; + Object[] params = { regKey }; + + Method method = clazz.getMethod("__writeRegistryServiceInfo", paramTypes); + Object oRet = method.invoke(clazz, params); + + if ( (oRet != null) && (oRet instanceof Boolean) ) + success = ((Boolean) oRet).booleanValue(); + } + catch (Exception e) { + throw new CannotRegisterImplementationException( e.getMessage()); + } + + return success; + } + + /** + * Supplies the factory for the <code>JavaLoader</code> + * <p> + * @return the factory for the <code>JavaLoader</code> + * @param implName the name of the desired component + * @param multiFactory the <code>ServiceManager</code> is delivered to the factory + * @param regKey not used - can be null + */ + public static XSingleServiceFactory getServiceFactory( String implName, + XMultiServiceFactory multiFactory, + XRegistryKey regKey) + { + if ( implName.equals(JavaLoader.class.getName()) ) + return new JavaLoaderFactory( multiFactory ); + + return null; + } + + /** + * Registers the <code>JavaLoader</code> at the registry. + * <p> + * @return true if registration succseeded - otherwise false + * @param regKey root key under which the <code>JavaLoader</code> should be regidstered + */ + public static boolean writeRegistryServiceInfo(XRegistryKey regKey) { + boolean result = false; + + try { + XRegistryKey newKey = regKey.createKey("/" + JavaLoader.class.getName() + "/UNO/SERVICE"); + + for (int i=0; i<supportedServices.length; i++) + newKey.createKey(supportedServices[i]); + + result = true; + } + catch (Exception ex) { + if (DEBUG) System.err.println(">>>JavaLoader.writeRegistryServiceInfo " + ex); + } + + return result; + } +} + diff --git a/jurt/com/sun/star/comp/loader/JavaLoaderFactory.java b/jurt/com/sun/star/comp/loader/JavaLoaderFactory.java new file mode 100644 index 000000000000..22981f2081e3 --- /dev/null +++ b/jurt/com/sun/star/comp/loader/JavaLoaderFactory.java @@ -0,0 +1,104 @@ +/************************************************************************* + * + * 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.comp.loader; + +import com.sun.star.lang.XSingleServiceFactory; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; + + +public class JavaLoaderFactory implements XSingleServiceFactory, XServiceInfo { + + private static String[] supportedServices = { + "com.sun.star.loader.Java", + "com.sun.star.loader.Java2" + }; + + private static final boolean DEBUG = false; + + private static final void DEBUG(String dbg) { + if (DEBUG) + System.err.println(" >>> JavaLoaderFactory - " + dbg); + } + + protected XMultiServiceFactory multiServiceFactory = null; + + /** default constructor + */ +// public JavaLoaderFactory() {} + + public JavaLoaderFactory(XMultiServiceFactory factory) { + multiServiceFactory = factory; + } + + public java.lang.Object createInstance() + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + return new JavaLoader(multiServiceFactory); + } + + public java.lang.Object createInstanceWithArguments( java.lang.Object[] args ) + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + JavaLoader loader = new JavaLoader(); + loader.initialize(args); + + return loader; + } + + /** implements the XServiceInfo interface + */ + public String getImplementationName() + throws com.sun.star.uno.RuntimeException + { + return JavaLoader.class.getName(); + } + + /** implements the XServiceInfo interface + */ + public boolean supportsService(String serviceName) + throws com.sun.star.uno.RuntimeException + { + for ( int i = 0; i < supportedServices.length; i++ ) { + if ( supportedServices[i].equals(serviceName) ) + return true; + } + return false; + } + + /** implements the XServiceInfo interface + */ + public String[] getSupportedServiceNames() + throws com.sun.star.uno.RuntimeException + { + return supportedServices; + } +} + diff --git a/jurt/com/sun/star/comp/loader/RegistrationClassFinder.java b/jurt/com/sun/star/comp/loader/RegistrationClassFinder.java new file mode 100644 index 000000000000..3a40daab866b --- /dev/null +++ b/jurt/com/sun/star/comp/loader/RegistrationClassFinder.java @@ -0,0 +1,133 @@ +/************************************************************************* + * + * 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.comp.loader; + +import com.sun.star.lib.unoloader.UnoClassLoader; +import com.sun.star.lib.util.WeakMap; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.StringTokenizer; +import java.util.jar.Attributes; + +final class RegistrationClassFinder { + public static Class find(String locationUrl) + throws ClassNotFoundException, IOException + { + synchronized (map) { + Class c = (Class) WeakMap.getValue(map.get(locationUrl)); + if (c != null) { + return c; + } + } + URL url = new URL(locationUrl); + checkAccess(url); + Attributes attr = UnoClassLoader.getJarMainAttributes(url); + String name = attr == null + ? null : attr.getValue("RegistrationClassName"); + if (name == null) { + return null; + } + ClassLoader cl1 = RegistrationClassFinder.class.getClassLoader(); + ClassLoader cl2; + if (cl1 instanceof UnoClassLoader) { + cl2 = ((UnoClassLoader) cl1).getClassLoader(url, attr); + } else { + cl2 = URLClassLoader.newInstance(new URL[] { url }, cl1); + } + Class c = cl2.loadClass(name); + synchronized (map) { + Class c2 = (Class) WeakMap.getValue(map.get(locationUrl)); + if (c2 != null) { + return c2; + } + map.put(locationUrl, c); + } + return c; + } + + private RegistrationClassFinder() {} // do not instantiate + + private static void checkAccess(URL url) throws ClassNotFoundException { + // The system property com.sun.star.comp.loader.CPLD_ACCESSPATH was + // introduced as a hack to restrict which UNO components can be + // instantiated. It seems to be unused nowadays, and should probably be + // replaced by the native Java security features, anyway. + if (accessPath != null) { + if (!url.getProtocol().equals("file")) { + throw new ClassNotFoundException( + "Access restriction: <" + url + "> is not a file URL"); + } + String p; + try { + p = new File(url.getFile()).getCanonicalPath(); + } catch (IOException e) { + throw new ClassNotFoundException( + "Access restriction: <" + url + "> is bad: " + e); + } + for (int i = 0; i < accessPath.length; ++i) { + String p2 = accessPath[i]; + if (p.startsWith(p2) && p.length() > p2.length() + && (p2.charAt(p2.length() - 1) == File.separatorChar + || p.charAt(p2.length()) == File.separatorChar)) + { + return; + } + } + throw new ClassNotFoundException( + "Access restriction: <" + url + "> is restricted"); + } + } + + private static final WeakMap map = new WeakMap(); + + private static final String[] accessPath; + static { + String[] ap = null; + String p = System.getProperty( + "com.sun.star.comp.loader.CPLD_ACCESSPATH"); + if (p != null) { + StringTokenizer t = new StringTokenizer(p, ";"); + ap = new String[t.countTokens()]; + int i = 0; + while (t.hasMoreTokens()) { + try { + ap[i] = new File(t.nextToken()).getCanonicalPath(); + ++i; + } catch (IOException e) {} + } + if (i != ap.length) { + String[] ap2 = new String[i]; + System.arraycopy(ap, 0, ap2, 0, i); + ap = ap2; + } + } + accessPath = ap; + } +} diff --git a/jurt/com/sun/star/comp/loader/makefile.mk b/jurt/com/sun/star/comp/loader/makefile.mk new file mode 100644 index 000000000000..849509995ab5 --- /dev/null +++ b/jurt/com/sun/star/comp/loader/makefile.mk @@ -0,0 +1,47 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* + +PRJ = ..$/..$/..$/..$/.. +PRJNAME = jurt +PACKAGE = com$/sun$/star$/comp$/loader +TARGET = com_sun_star_comp_loader + +# --- Settings ----------------------------------------------------- + +.INCLUDE : $(PRJ)$/util$/makefile.pmk + +# --- Files -------------------------------------------------------- + +JAVAFILES = \ + FactoryHelper.java \ + JavaLoader.java \ + JavaLoaderFactory.java \ + RegistrationClassFinder.java + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/jurt/com/sun/star/comp/servicemanager/ServiceManager.java b/jurt/com/sun/star/comp/servicemanager/ServiceManager.java new file mode 100644 index 000000000000..3d672c6dfddf --- /dev/null +++ b/jurt/com/sun/star/comp/servicemanager/ServiceManager.java @@ -0,0 +1,926 @@ +/************************************************************************* + * + * 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.comp.servicemanager; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +import com.sun.star.container.XSet; +import com.sun.star.container.XContentEnumerationAccess; +import com.sun.star.container.XEnumeration; + +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XEventListener; +import com.sun.star.lang.XInitialization; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.lang.XSingleServiceFactory; +import com.sun.star.lang.XSingleComponentFactory; +import com.sun.star.lang.XMultiComponentFactory; + +import com.sun.star.registry.XRegistryKey; +import com.sun.star.registry.XSimpleRegistry; + +import com.sun.star.loader.XImplementationLoader; + +import java.lang.reflect.InvocationTargetException; + +/** + * The <code>ServiceManager</code> class is an implmentation of the <code>ServiceManager</code>the central class needed for + * implementing or using UNO components in Java. + * <p> + * The Methods <code>queryInterface</code> and <code>isSame</code> delegate + * calls to the implementing objects and are used instead of casts + * and identity comparisons. + * <p> + * @version $Revision: 1.10 $ $ $Date: 2008-04-11 11:11:46 $ + * @author Markus Herzog + * @see com.sun.star.lang.XMultiServiceFactory + * @see com.sun.star.container.XSet + * @see com.sun.star.container.XContentEnumerationAccess + * @see com.sun.star.lang.XComponent + * @see com.sun.star.lang.XServiceInfo + * @see com.sun.star.lang.XInitialization + * @since UDK1.0 + */ +public class ServiceManager implements XMultiServiceFactory, + XMultiComponentFactory, + XSet, + XContentEnumerationAccess, + XComponent, + XServiceInfo, + XInitialization +{ + private static final boolean DEBUG = false; + + private static final void DEBUG (String dbg) { + if (DEBUG) System.err.println( dbg ); + } + + private static com.sun.star.uno.Type UNO_TYPE = null; + + XImplementationLoader loader = null; + + static String[] supportedServiceNames = { + "com.sun.star.lang.MultiServiceFactory", + "com.sun.star.lang.ServiceManager" + }; + + java.util.Vector eventListener; + java.util.Hashtable factoriesByImplNames; + java.util.Hashtable factoriesByServiceNames; // keys: + + private com.sun.star.uno.XComponentContext m_xDefaultContext; + + /** + * Creates a new instance of the <code>ServiceManager</code>. + */ + public ServiceManager() { + eventListener = new java.util.Vector(); + factoriesByImplNames = new java.util.Hashtable(); + factoriesByServiceNames = new java.util.Hashtable(); + m_xDefaultContext = null; + } + /** + * Creates a new instance of the <code>ServiceManager</code>. + */ + public ServiceManager( XComponentContext xContext ) { + eventListener = new java.util.Vector(); + factoriesByImplNames = new java.util.Hashtable(); + factoriesByServiceNames = new java.util.Hashtable(); + m_xDefaultContext = xContext; + } + + /** + * Returns the service factory for the <code>ServiceManager</code>. If the given implementation name + * does not equal to the <code>ServiceManagers</code> class name null will be returned. + * <p> + * @return the factory for the <code>ServiceManager</code>. + * @param implName the implementation name of the of the service. + * Must be equal to <code>com.sun.star.comp.servicemanager.ServicManager</code> + * @param multiFactory refernce of the <code>MultiServiceFactory</code>. This parameter will be ignored. + * @param regKey the root key of the registry. This parameter will be ignored. + */ + public static XSingleServiceFactory getServiceFactory( String implName, + XMultiServiceFactory multiFactory, + XRegistryKey regKey) + { + if ( implName.equals(ServiceManager.class.getName()) ) + return new ServiceManagerFactory(); + + return null; + } + + + /** + * Supplies a Java component loader. The loader component must be enlisted at the <code>ServiceManager</code> before. + * <p> + * @return a new instance of the Java component loader + * @see com.sun.star.loader.Java + */ + private XImplementationLoader getLoader() + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + Object[] param = { this }; + DEBUG("make loader"); + Object loaderObj = createInstanceWithArgumentsAndContext( + "com.sun.star.loader.Java", param, m_xDefaultContext ); + + if (loaderObj == null) + throw new com.sun.star.uno.Exception("Can get an instance of com.sun.star.loader.Java"); + + return UnoRuntime.queryInterface( XImplementationLoader.class, loaderObj ); + } + + /** + * Registers a list of components given by their class names. + * <p> + * @param newImpls list of the components that should be registered, given by their class names. + * If any exception occured during the registration, the process will be canceled. + * @see com.sun.star.container.XSet + */ + private void xaddFactories( String[] newImpls ) + throws com.sun.star.uno.Exception + { + for (int i=0; i<newImpls.length; i++) { + DEBUG ("try to add " + newImpls[i] ); + Object newFactory = null; + + try { + if (loader == null) + loader = getLoader(); + + newFactory = loader.activate( newImpls[i], null, null, null ); + } + catch (com.sun.star.uno.Exception e) { + +//****************************** BEGIN DEPRECATED ****************************************** + + try { + // try to get the class of the implementation + Class clazz = Class.forName( newImpls[i] ); + + Class[] methodClassParam = { String.class, XMultiServiceFactory.class, XRegistryKey.class }; + java.lang.reflect.Method getFactoryMeth = null; + try { + getFactoryMeth = clazz.getMethod("__getServiceFactory", methodClassParam); + } + catch (NoSuchMethodException noSuchMethodEx) { + getFactoryMeth = null; + } + catch (SecurityException securityExc) { + getFactoryMeth = null; + } + + if (getFactoryMeth == null) + getFactoryMeth = clazz.getMethod("getServiceFactory", methodClassParam); + + Object[] methodParams = { newImpls[i], this, null }; + newFactory = getFactoryMeth.invoke( clazz, methodParams ); + } + catch (NoSuchMethodException ex) {} + catch (SecurityException ex) {} + catch (ClassNotFoundException ex) {} + catch (IllegalAccessException ex) {} + catch (IllegalArgumentException ex) {} + catch (InvocationTargetException ex) {} + +//****************************** END DEPRECATED ****************************************** + } + + if ( newFactory == null ) + throw new com.sun.star.loader.CannotActivateFactoryException("Can not get factory for " + newImpls[i]); + + insert( newFactory ); + } // end of for ... + } + + /** + * The method is used to add components to the <code>ServiceManager</code>. The first argument indicates a <code>SimpleRegistry</code>. + * The components which should be added will be searched under the <i>Implementations</i> key in the registry. + * <p> + * @param args the first argument ( args[0] ) specifices the SimpleRegistry object + * @see com.sun.star.lang.XInitialization + * @see com.sun.star.lang.RegistryServiceManager + * @see com.sun.star.lang.XSimpleRegistry + */ + public void initialize( Object args[] ) + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException { + XSimpleRegistry xSimpleRegistry = null; + try { + xSimpleRegistry = (XSimpleRegistry) args[0]; + if (xSimpleRegistry != null) + { + XRegistryKey rootkey = xSimpleRegistry.getRootKey(); + + XRegistryKey implkey_xRegistryKey = rootkey.openKey("Implementations"); + if(implkey_xRegistryKey != null) { + XRegistryKey xRegistryKeys[] = implkey_xRegistryKey.openKeys(); + + for(int i = 0; i < xRegistryKeys.length; ++ i) { + xaddFactories(new String[]{xRegistryKeys[i].getStringValue()}); + } + } + } + + if (args.length > 1) + { + m_xDefaultContext = (XComponentContext)args[ 1 ]; + } + } + catch (ArrayIndexOutOfBoundsException e) + { + throw new com.sun.star.lang.IllegalArgumentException("Argument must not be null."); + } + } + + /** + * Creates a new instance of a specified service. Therefor the associated factory of the service is + * looked up and used to instanciate a new component. + * <p> + * @return newly created component + * @param serviceSpecifier indicates the service or component name + * @see com.sun.star.lang.XMultiServiceFactory + */ + public java.lang.Object createInstance( String serviceSpecifier ) + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + return createInstanceWithContext( serviceSpecifier, m_xDefaultContext ); + } + + /** + * Creates a new instance of a specified service with the given parameters. + * Therefor the associated factory of the service is looked up and used to instanciate a new component. + * <p> + * @return newly created component + * @param serviceSpecifier indicates the service or component name + * @see com.sun.star.lang.XMultiServiceFactory + */ + public java.lang.Object createInstanceWithArguments( + String serviceSpecifier, Object[] args ) + throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException + { + if (DEBUG) { + System.err.println("createInstanceWithArguments:" ); + + for (int i=0; i<args.length; i++) + System.err.print(" "+ args[i]); + + System.err.println(); + } + + return createInstanceWithArgumentsAndContext( serviceSpecifier, args, m_xDefaultContext ); + } + + /** + * Look up the factory for a given service or implementation name. + * First the requested service name is search in the list of avaible services. If it can not be found + * the name is looked up in the the implementation list. + * <p> + * @return the factory of the service / implementation + * @param serviceSpecifier indicates the service or implementation name + * @see com.sun.star.lang.XMultiServiceFactory + */ + private Object queryServiceFactory(String serviceName) + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + DEBUG("queryServiceFactory for name " + serviceName ); + Object factory = null; + + if ( factoriesByServiceNames.containsKey( serviceName ) ) { + java.util.Vector aviableFact = (java.util.Vector) factoriesByServiceNames.get( serviceName ); + + DEBUG(""); + DEBUG("aviable factories for " + serviceName +" "+ aviableFact); + DEBUG(""); + + if ( !aviableFact.isEmpty() ) + factory = aviableFact.lastElement(); + + } else // not found in list of services - now try the implementations + factory = factoriesByImplNames.get( serviceName ); // return null if none is aviable + + if (DEBUG) { + if (factory == null) System.err.println("service not registered"); + else + System.err.println("service found:" + factory + " " + UnoRuntime.queryInterface(XSingleServiceFactory.class, factory)); + } + + if (factory == null) + throw new com.sun.star.uno.Exception("Query for service factory for " + serviceName + " failed."); + + return factory; + } + + /** + * Supplies a list of all avialable services names. + * <p> + * @return list of Strings of all service names + * @see com.sun.star.container.XContentEnumerationAccess + */ + public String[] getAvailableServiceNames() + throws com.sun.star.uno.RuntimeException + { + int i = 0; + String[] availableServiceNames = new String[factoriesByServiceNames.size()]; + + java.util.Enumeration keys = factoriesByServiceNames.keys(); + + while (keys.hasMoreElements()) + availableServiceNames[i++] = (String) keys.nextElement(); + + return availableServiceNames; + } + + // XMultiComponentFactory implementation + + /** Create a service instance with given context. + + @param rServiceSpecifier service name + @param xContext context + @return service instance + */ + public java.lang.Object createInstanceWithContext( + String rServiceSpecifier, + com.sun.star.uno.XComponentContext xContext ) + throws com.sun.star.uno.Exception + { + Object fac = queryServiceFactory( rServiceSpecifier ); + if (fac != null) + { + XSingleComponentFactory xCompFac = UnoRuntime.queryInterface( + XSingleComponentFactory.class, fac ); + if (xCompFac != null) + { + return xCompFac.createInstanceWithContext( xContext ); + } + else + { + XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface( + XSingleServiceFactory.class, fac ); + if (xServiceFac != null) + { + if (DEBUG) + System.err.println( "### ignoring context raising service \"" + rServiceSpecifier + "\"!" ); + return xServiceFac.createInstance(); + } + else + { + throw new com.sun.star.uno.Exception( + "retrieved service factory object for \"" + rServiceSpecifier + + "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" ); + } + } + } + return null; + } + /** Create a service instance with given context and arguments. + + @param rServiceSpecifier service name + @param rArguments arguments + @param xContext context + @return service instance + */ + public java.lang.Object createInstanceWithArgumentsAndContext( + String rServiceSpecifier, + java.lang.Object[] rArguments, + com.sun.star.uno.XComponentContext xContext ) + throws com.sun.star.uno.Exception + { + Object fac = queryServiceFactory( rServiceSpecifier ); + if (fac != null) + { + XSingleComponentFactory xCompFac = UnoRuntime.queryInterface( + XSingleComponentFactory.class, fac ); + if (xCompFac != null) + { + return xCompFac.createInstanceWithArgumentsAndContext( rArguments, xContext ); + } + else + { + XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface( + XSingleServiceFactory.class, fac ); + if (xServiceFac != null) + { + if (DEBUG) + System.err.println( "### ignoring context raising service \"" + rServiceSpecifier + "\"!" ); + return xServiceFac.createInstanceWithArguments( rArguments ); + } + else + { + throw new com.sun.star.uno.Exception( + "retrieved service factory object for \"" + rServiceSpecifier + + "\" does not export XSingleComponentFactory nor XSingleServiceFactory!" ); + } + } + } + return null; + } +// public String[] getAvailableServiceNames(); + + /** + * Removes all listeners from the <code>ServiceManager</code> and clears the list of the services. + * <p> + * @see com.sun.star.lang.XComponent + */ + public void dispose() + throws com.sun.star.uno.RuntimeException + { + if (eventListener != null) { + java.util.Enumeration enumer = eventListener.elements(); + + while (enumer.hasMoreElements()) { + XEventListener listener = (XEventListener) enumer.nextElement(); + listener.disposing(new com.sun.star.lang.EventObject(this)); + } + } + + eventListener.removeAllElements(); + factoriesByServiceNames.clear(); + factoriesByImplNames.clear(); + } + + /** + * Adds a new <code>EventListener</code>. The listener is notified when a + * service is added (removed) to (from) the <code>ServiceManager</code>. + * If the listener is already registred a + * <code>com.sun.star.uno.RuntimeException</code> will be thrown. + * <p> + * @param xListener the new listener which should been added. + * @see com.sun.star.lang.XComponent + */ + public void addEventListener( XEventListener xListener ) + throws com.sun.star.uno.RuntimeException + { + if (xListener == null) + throw new com.sun.star.uno.RuntimeException("Listener must not be null"); + + if ( eventListener.contains(xListener) ) + throw new com.sun.star.uno.RuntimeException("Listener already registred."); + + eventListener.addElement(xListener); + } + + /** + * Removes a <code>EventListener</code> from the <code>ServiceManager</code>. + * If the listener is not registered a <code>com.sun.star.uno.RuntimeException</code> + * will be thrown. + * <p> + * @param xListener the new listener which should been removed. + * @see com.sun.star.lang.XComponent + */ + public void removeEventListener( XEventListener xListener ) + throws com.sun.star.uno.RuntimeException + { + if (xListener == null) + throw new com.sun.star.uno.RuntimeException("Listener must not be null"); + + if ( !eventListener.contains(xListener) ) + throw new com.sun.star.uno.RuntimeException("Listener is not registered."); + + eventListener.removeElement(xListener); + } + + /** + * Checks if a component is registered at the <code>ServiceManager</code>. The given object argument must + * provide a <code>XServiceInfo</code> interface. + * <p> + * @return true if the component is registred otherwise false. + * @param object object which provides a <code>XServiceInfo</code> interface. + * @see com.sun.star.container.XSet + * @see com.sun.star.lang.XServiceInfo + */ + public boolean has( Object object ) + throws com.sun.star.uno.RuntimeException + { + if (object == null) + throw new com.sun.star.uno.RuntimeException("The parameter must not been null"); + + XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, object); + + if (xServiceInfo != null) { + return UnoRuntime.areSame(factoriesByImplNames.get(xServiceInfo.getImplementationName()), object); + } + + return false; + } + + /** + * Adds a <code>SingleServiceFactory</code> to the <code>ServiceManager</code>. + * <p> + * @param object factory which should be added. + * @see com.sun.star.container.XSet + * @see com.sun.star.lang.XSingleServiceFactory + */ + public void insert( Object object ) + throws com.sun.star.lang.IllegalArgumentException, + com.sun.star.container.ElementExistException, + com.sun.star.uno.RuntimeException + { + if (object == null) throw new com.sun.star.lang.IllegalArgumentException(); + + XServiceInfo xServiceInfo = + UnoRuntime.queryInterface(XServiceInfo.class, object); + + if (xServiceInfo == null) + throw new com.sun.star.lang.IllegalArgumentException( + "The given object does not implement the XServiceInfo interface." + ); + + if ( factoriesByImplNames.containsKey( xServiceInfo.getImplementationName() ) ) { + throw new com.sun.star.container.ElementExistException( + xServiceInfo.getImplementationName() + " already registred" + ); + } + + DEBUG("add factory " + object.toString() + " for " + xServiceInfo.getImplementationName()); + factoriesByImplNames.put( xServiceInfo.getImplementationName(), object ); + + + String[] serviceNames = xServiceInfo.getSupportedServiceNames(); + java.util.Vector vec = null; + + for (int i=0; i<serviceNames.length; i++) { + if ( !factoriesByServiceNames.containsKey( serviceNames[i] ) ) { + DEBUG("> no registered services found under " + serviceNames[i] + ": adding..." ); + factoriesByServiceNames.put(serviceNames[i], new java.util.Vector()); + } + + vec = (java.util.Vector) factoriesByServiceNames.get( serviceNames[i] ); + + if ( vec.contains( object ) ) + System.err.println("The implementation " + xServiceInfo.getImplementationName() + + " already registered for the service " + serviceNames[i] + " - ignoring!"); + else + vec.addElement(object); + } + } + + /** + * Removes a <code>SingleServiceFactory</code> from the <code>ServiceManager</code>. + * <p> + * @param object factory which should be removed. + * @see com.sun.star.container.XSet + * @see com.sun.star.lang.XSingleServiceFactory + */ + public void remove( Object object ) + throws com.sun.star.lang.IllegalArgumentException, + com.sun.star.container.NoSuchElementException, + com.sun.star.uno.RuntimeException + { + if (object == null) + throw new com.sun.star.lang.IllegalArgumentException( + "The given object must not be null." + ); + + XServiceInfo xServiceInfo = + UnoRuntime.queryInterface(XServiceInfo.class, object); + + if (xServiceInfo == null) + throw new com.sun.star.lang.IllegalArgumentException( + "The given object does not implement the XServiceInfo interface." + ); + + XSingleServiceFactory xSingleServiceFactory = + UnoRuntime.queryInterface(XSingleServiceFactory.class, object); + + if (xSingleServiceFactory == null) + throw new com.sun.star.lang.IllegalArgumentException( + "The given object does not implement the XSingleServiceFactory interface." + ); + + if ( factoriesByImplNames.remove( xServiceInfo.getImplementationName() ) == null ) + throw new com.sun.star.container.NoSuchElementException( + xServiceInfo.getImplementationName() + + " is not registered as an implementation." + ); + + String[] serviceNames = xServiceInfo.getSupportedServiceNames(); + + for ( int i=0; i<serviceNames.length; i++ ) { + if ( factoriesByServiceNames.containsKey( serviceNames[i] ) ) { + java.util.Vector vec = (java.util.Vector) factoriesByServiceNames.get(serviceNames[i]); + + if ( !vec.removeElement(object) ) + System.err.println("The implementation " + xServiceInfo.getImplementationName() + + " is not registered for the service " + serviceNames[i] + " - ignoring!"); + + if ( vec.isEmpty() ) // remove the vector if no implementations aviable for the service + factoriesByServiceNames.remove( serviceNames[i] ); + } + } + } + + /** + * Provides an enumeration of all registred services. + * <p> + * @return an enumeration of all avialable services. + * @see com.sun.star.conatiner.XEnumerationAccess + */ + public XEnumeration createEnumeration() + throws com.sun.star.uno.RuntimeException + { + return new ServiceEnumerationImpl( factoriesByImplNames.elements() ); + } + + /** + * Provides the UNO type of the <code>ServiceManager</code> + * <p> + * @return the UNO type of the <code>ServiceManager</code>. + * @see com.sun.star.container.XElementAccess + * @see com.sun.star.uno.TypeClass + */ + public com.sun.star.uno.Type getElementType() + throws com.sun.star.uno.RuntimeException + { + if ( UNO_TYPE == null ) + UNO_TYPE = new com.sun.star.uno.Type(ServiceManager.class); + + return UNO_TYPE; + } + + /** + * Checks if the any componets are registered. + * <p> + * @return true - if the list of the registred components is not empty - otherwise false. + * @see com.sun.star.container.XElementAccess + */ + public boolean hasElements() { + return ! factoriesByImplNames.isEmpty(); + } + + /** + * Provides an enumeration of of all factorys for a specified service. + * <p> + * @return an enumeration for service name. + * @param serviceName name of the requested service + * @see com.sun.star.container.XContentEnumerationAccess + */ + public XEnumeration createContentEnumeration( String serviceName ) + throws com.sun.star.uno.RuntimeException + { + XEnumeration enumer = null; + + java.util.Vector serviceList = (java.util.Vector) factoriesByServiceNames.get(serviceName); + + if (serviceList != null) + enumer = new ServiceEnumerationImpl( serviceList.elements() ); + else + enumer = new ServiceEnumerationImpl(); + + return enumer; + } + + /** + * Returns the implementation name of the <code>ServiceManager</code> component. + * <p> + * @return the class name of the <code>ServiceManager</code>. + * @see com.sun.star.lang.XServiceInfo + */ + public String getImplementationName() + throws com.sun.star.uno.RuntimeException + { + return getClass().getName(); + } + + /** + * Checks if the <code>ServiceManager</code> supports a service. + * <p> + * @return true if the service is supported - otherwise false. + * @param serviceName service name which should be checked. + * @see com.sun.star.lang.XServiceInfo + */ + public boolean supportsService( String serviceName ) + throws com.sun.star.uno.RuntimeException + { + for (int i=0; i<supportedServiceNames.length; i++) + if (supportedServiceNames[i].equals( serviceName )) return true; + + if (getImplementationName().equals( serviceName )) return true; + + return false; + } + + /** + * Supplies list of all supported services. + * <p> + * @return a list of all supported service names. + * @see com.sun.star.lang.XServiceInfo + */ + public String[] getSupportedServiceNames() + throws com.sun.star.uno.RuntimeException + { + return supportedServiceNames; + } + + /** + * The <code>ServiceEnumerationImpl</code> class provides an + * implementation of the @see com.sun.star.container.XEnumeration interface. + * It is a inner wrapper for a java.util.Enumeration object. + * <p> + * @version $Revision: 1.10 $ $ $Date: 2008-04-11 11:11:46 $ + * @author Markus Herzog + * @see com.sun.star.lang.XSingleServiceFactory + * @see com.sun.star.lang.XServiceInfo + * @since UDK1.0 + */ + class ServiceEnumerationImpl implements XEnumeration { + java.util.Enumeration enumeration = null; + + /** + * Constructs a new empty instance. + */ + public ServiceEnumerationImpl() { + } + + /** + * Constructs a new instance with a given enumeration. + * <p> + * @param enumer is the enumeration which should been wrapped. + * @see com.sun.star.container.XEnumeration + */ + public ServiceEnumerationImpl(java.util.Enumeration enumer) { + enumeration = enumer; + } + + /** + * Checks if the enumeration contains more elements. + * <p> + * @return true if more elements are available - otherwise false. + * @see com.sun.star.container.XEnumeration + */ + public boolean hasMoreElements() + throws com.sun.star.uno.RuntimeException + { + if (enumeration != null) + return enumeration.hasMoreElements(); + + return false; + } + + /** + * Returns the next element of the enumeration. If no further elements + * available a com.sun.star.container.NoSuchElementException exception will be thrown. + * <p> + * @return the next element. + * @see com.sun.star.container.XEnumeration + */ + public Object nextElement() + throws com.sun.star.container.NoSuchElementException, + com.sun.star.lang.WrappedTargetException, + com.sun.star.uno.RuntimeException + { + if (enumeration == null) + throw new com.sun.star.container.NoSuchElementException(); + + try { + return enumeration.nextElement(); + } catch (java.util.NoSuchElementException e) { + com.sun.star.container.NoSuchElementException ex = + new com.sun.star.container.NoSuchElementException(); + ex.fillInStackTrace(); + + throw ex; + } + } + } +} +/** + * The <code>ServiceManagerFactory</code> is the factory class for the + * <code>ServiceManager</code>. As all factories it implments the + * com.sun.star.lang.XSingleServiceFactory and the com.sun.star.lang.XServiceInfo + * interfaces. + * <p> + * @version $Revision: 1.10 $ $ $Date: 2008-04-11 11:11:46 $ + * @author Markus Herzog + * @see com.sun.star.lang.XSingleServiceFactory + * @see com.sun.star.lang.XServiceInfo + * @since UDK1.0 +*/ +class ServiceManagerFactory implements XServiceInfo, XSingleComponentFactory, XSingleServiceFactory +{ + /** + * Creates a new instance of the <code>ServiceManagerFactory</code>. + */ + public ServiceManagerFactory() { + } + + /** + * Supplies the implementation name of the <code>ServiceManager</code>. + * <p> + * @return <code>ServiceManager</code> class name. + * @see com.sun.star.lang.XServiceInfo + */ + public String getImplementationName() + throws com.sun.star.uno.RuntimeException + { + return ServiceManager.class.getName(); + } + + /** + * Checks wether or not a service is supported. + * <p> + * @return true - if the service is supported, otherwise false. + * @param serviceName the name of the service that should be checked. + * @see com.sun.star.lang.XServiceInfo + */ + public boolean supportsService( String serviceName ) + throws com.sun.star.uno.RuntimeException + { + for ( int i=0; i<ServiceManager.supportedServiceNames.length; i++ ) + if ( ServiceManager.supportedServiceNames[i].equals(serviceName) ) return true; + + if ( getImplementationName().equals(serviceName) ) return true; + + return false; + } + + /** + * Returns all service names which are supported by <code>ServiceManager</code>. + * <p> + * @return a list aof all supported service names. + * @see com.sun.star.lang.XServiceInfo + */ + public String[] getSupportedServiceNames() + throws com.sun.star.uno.RuntimeException + { + return ServiceManager.supportedServiceNames; + } + + /** + * Creates a new instance of the <code>ServiceManager</code>. + * <p> + * @return newly created <code>ServiceManager</code> object. + * @see com.sun.star.lang.XSingleServiceFactory + */ + public java.lang.Object createInstance() + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + return new ServiceManager(); + } + + /** + * Creates a new instance of the <code>ServiceManager</code> with arguments. + * At this time it always throws a com.sun.star.lang.NoSuchMethodException + * because there is no the <code>ServiceManager</code> has no constructor with + * arguments. + * <p> + * @return null - allways throws an exception + * @param aArguments arguments for new instance. + * @see com.sun.star.lang.XSingleServiceFactory + */ + public java.lang.Object createInstanceWithArguments( java.lang.Object[] aArguments ) + throws com.sun.star.uno.Exception, + com.sun.star.uno.RuntimeException + { + throw new com.sun.star.lang.NoSuchMethodException("Constructor with arguments is not supported."); + } + + // XSingleComponentFactory impl + //______________________________________________________________________________________________ + public Object createInstanceWithContext( XComponentContext xContext ) + throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException + { + return new ServiceManager( xContext ); + } + //______________________________________________________________________________________________ + public Object createInstanceWithArgumentsAndContext( + Object aArguments [], XComponentContext xContext ) + throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException + { + throw new com.sun.star.lang.NoSuchMethodException( + "ServiceManagerFactory.createInstanceWithArgumentsAndContext() not impl!" ); + } +} + + diff --git a/jurt/com/sun/star/comp/servicemanager/makefile.mk b/jurt/com/sun/star/comp/servicemanager/makefile.mk new file mode 100644 index 000000000000..46fae35b5902 --- /dev/null +++ b/jurt/com/sun/star/comp/servicemanager/makefile.mk @@ -0,0 +1,43 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* + +PRJ = ..$/..$/..$/..$/.. +PRJNAME = jurt +PACKAGE = com$/sun$/star$/comp$/servicemanager +TARGET = com_sun_star_comp_servicemanager + +# --- Settings ----------------------------------------------------- + +.INCLUDE : $(PRJ)$/util$/makefile.pmk + +# --- Files -------------------------------------------------------- + +JAVAFILES = ServiceManager.java + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/jurt/com/sun/star/comp/urlresolver/UrlResolver.java b/jurt/com/sun/star/comp/urlresolver/UrlResolver.java new file mode 100644 index 000000000000..3509dfb303f2 --- /dev/null +++ b/jurt/com/sun/star/comp/urlresolver/UrlResolver.java @@ -0,0 +1,171 @@ +/************************************************************************* + * + * 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.comp.urlresolver; + + +import com.sun.star.bridge.XBridge; +import com.sun.star.bridge.XBridgeFactory; +import com.sun.star.bridge.XUnoUrlResolver; + +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.IllegalArgumentException; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XSingleServiceFactory; + +import com.sun.star.registry.XRegistryKey; + +import com.sun.star.uno.UnoRuntime; + + +/** + * This component gives a factory for an <code>UnoUrlResolver</code> service. + * <p> + * @version $Revision: 1.6 $ $ $Date: 2008-04-11 11:12:25 $ + * @author Kay Ramme + * @see com.sun.star.brige.XBrideFactory + * @see com.sun.star.connection.Connector + * @since UDK1.0 + */ +public class UrlResolver { + static private final boolean DEBUG = false; + + + static public class _UrlResolver implements XUnoUrlResolver { + static private final String __serviceName = "com.sun.star.bridge.UnoUrlResolver"; + + private XMultiServiceFactory _xMultiServiceFactory; + + public _UrlResolver(XMultiServiceFactory xMultiServiceFactory) { + _xMultiServiceFactory = xMultiServiceFactory; + } + + public Object resolve(/*IN*/String dcp) throws NoConnectException, ConnectionSetupException, IllegalArgumentException, com.sun.star.uno.RuntimeException { + String conDcp = null; + String protDcp = null; + String rootOid = null; + + if(dcp.indexOf(';') == -1) {// use old style + conDcp = dcp; + protDcp = "iiop"; + rootOid = "classic_uno"; + } + else { // new style + int index = dcp.indexOf(':'); + String url = dcp.substring(0, index).trim(); + dcp = dcp.substring(index + 1).trim(); + + index = dcp.indexOf(';'); + conDcp = dcp.substring(0, index).trim(); + dcp = dcp.substring(index + 1).trim(); + + index = dcp.indexOf(';'); + protDcp = dcp.substring(0, index).trim(); + dcp = dcp.substring(index + 1).trim(); + + rootOid = dcp.trim().trim(); + } + + Object rootObject = null; + XBridgeFactory xBridgeFactory= null; + try { + xBridgeFactory = UnoRuntime.queryInterface(XBridgeFactory.class, + _xMultiServiceFactory.createInstance("com.sun.star.bridge.BridgeFactory")); + } catch (com.sun.star.uno.Exception e) { + throw new com.sun.star.uno.RuntimeException(e.getMessage()); + } + XBridge xBridge = xBridgeFactory.getBridge(conDcp + ";" + protDcp); + + if(xBridge == null) { + Object connector= null; + try { + connector = _xMultiServiceFactory.createInstance("com.sun.star.connection.Connector"); + } catch (com.sun.star.uno.Exception e) { + throw new com.sun.star.uno.RuntimeException(e.getMessage()); + } + + XConnector connector_xConnector = UnoRuntime.queryInterface(XConnector.class, connector); + + // connect to the server + XConnection xConnection = connector_xConnector.connect(conDcp); + try { + xBridge = xBridgeFactory.createBridge(conDcp + ";" + protDcp, protDcp, xConnection, null); + } catch (com.sun.star.bridge.BridgeExistsException e) { + throw new com.sun.star.uno.RuntimeException(e.getMessage()); + } + } + rootObject = xBridge.getInstance(rootOid); + return rootObject; + } + } + + + /** + * 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(UrlResolver.class.getName()) ) + xSingleServiceFactory = FactoryHelper.getServiceFactory(_UrlResolver.class, + _UrlResolver.__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(_UrlResolver.class.getName(), _UrlResolver.__serviceName, regKey); + } + +} + diff --git a/jurt/com/sun/star/comp/urlresolver/makefile.mk b/jurt/com/sun/star/comp/urlresolver/makefile.mk new file mode 100644 index 000000000000..20f4dd78c33e --- /dev/null +++ b/jurt/com/sun/star/comp/urlresolver/makefile.mk @@ -0,0 +1,43 @@ +#************************************************************************* +# +# 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. +# +#************************************************************************* + +PRJ = ..$/..$/..$/..$/.. +PRJNAME = jurt +PACKAGE = com$/sun$/star$/comp$/urlresolver +TARGET = com_sun_star_comp_urlresolver + +# --- Settings ----------------------------------------------------- + +.INCLUDE : $(PRJ)$/util$/makefile.pmk + +# Files -------------------------------------------------------- + +JAVAFILES = UrlResolver.java + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk |