Files
libreoffice/jurt/com/sun/star/comp/servicemanager/ServiceManager.java

660 lines
24 KiB
Java
Raw Normal View History

2012-06-29 14:02:24 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 14:29:57 +00:00
package com.sun.star.comp.servicemanager;
import java.util.ArrayList;
import java.util.Collections;
2000-09-18 14:29:57 +00:00
import com.sun.star.container.XContentEnumerationAccess;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XSet;
2000-09-18 14:29:57 +00:00
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XEventListener;
import com.sun.star.lang.XMultiComponentFactory;
2000-09-18 14:29:57 +00:00
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XServiceInfo;
2001-06-14 10:54:57 +00:00
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
2000-09-18 14:29:57 +00:00
/**
* 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>
*
2000-09-18 14:29:57 +00:00
* @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
* @since UDK1.0
*/
public class ServiceManager implements XMultiServiceFactory,
2001-06-14 10:54:57 +00:00
XMultiComponentFactory,
2000-09-18 14:29:57 +00:00
XSet,
XContentEnumerationAccess,
XComponent,
XServiceInfo
2000-09-18 14:29:57 +00:00
{
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;
static String[] supportedServiceNames = {
"com.sun.star.lang.MultiServiceFactory",
"com.sun.star.lang.ServiceManager"
};
ArrayList<XEventListener> eventListener;
java.util.HashMap<String, Object> factoriesByImplNames;
java.util.HashMap<String, ArrayList<Object>> factoriesByServiceNames; // keys:
2000-09-18 14:29:57 +00:00
2001-06-14 10:54:57 +00:00
private com.sun.star.uno.XComponentContext m_xDefaultContext;
2000-09-18 14:29:57 +00:00
/**
* Creates a new instance of the <code>ServiceManager</code>.
*/
public ServiceManager() {
eventListener = new ArrayList<XEventListener>();
factoriesByImplNames = new java.util.HashMap<String, Object>();
factoriesByServiceNames = new java.util.HashMap<String, ArrayList<Object>>();
2001-06-14 10:54:57 +00:00
m_xDefaultContext = null;
}
2000-09-18 14:29:57 +00:00
public void setDefaultContext(XComponentContext context) {
m_xDefaultContext = context;
2000-09-18 14:29:57 +00:00
}
/**
* Creates a new instance of a specified service.
*
* <p>Therefore the associated factory of the service is looked up and used
* to instanciate a new component. </p>
*
* @param serviceSpecifier indicates the service or component name.
* @return newly created component.
*
2000-09-18 14:29:57 +00:00
* @see com.sun.star.lang.XMultiServiceFactory
*/
public java.lang.Object createInstance( String serviceSpecifier )
throws com.sun.star.uno.Exception,
com.sun.star.uno.RuntimeException
{
2001-06-14 10:54:57 +00:00
return createInstanceWithContext( serviceSpecifier, m_xDefaultContext );
2000-09-18 14:29:57 +00:00
}
/**
* Creates a new instance of a specified service with the given parameters.
*
* <p>Therefore 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.
2000-09-18 14:29:57 +00:00
* @see com.sun.star.lang.XMultiServiceFactory
*/
2001-06-14 10:54:57 +00:00
public java.lang.Object createInstanceWithArguments(
String serviceSpecifier, Object[] args )
throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException
2000-09-18 14:29:57 +00:00
{
if (DEBUG) {
System.err.println("createInstanceWithArguments:" );
for (Object arg : args) {
System.err.print(" " + arg);
}
2000-09-18 14:29:57 +00:00
System.err.println();
}
2001-06-14 10:54:57 +00:00
return createInstanceWithArgumentsAndContext( serviceSpecifier, args, m_xDefaultContext );
2000-09-18 14:29:57 +00:00
}
/**
* Look up the factory for a given service or implementation name.
*
* <p>First the requested service name is search in the list of available
* services. If it can not be found the name is looked up in the implementation
* list.</p>
*
* @param serviceName indicates the service or implementation name.
* @return the factory of the service / implementation.
*
2000-09-18 14:29:57 +00:00
* @see com.sun.star.lang.XMultiServiceFactory
*/
2001-06-14 10:54:57 +00:00
private Object queryServiceFactory(String serviceName)
2000-09-18 14:29:57 +00:00
throws com.sun.star.uno.Exception,
com.sun.star.uno.RuntimeException
{
DEBUG("queryServiceFactory for name " + serviceName );
Object factory = null;
if ( factoriesByServiceNames.containsKey( serviceName ) ) {
ArrayList<Object> availableFact = factoriesByServiceNames.get( serviceName );
2000-09-18 14:29:57 +00:00
DEBUG("");
DEBUG("aviable factories for " + serviceName +" "+ availableFact);
2000-09-18 14:29:57 +00:00
DEBUG("");
if ( !availableFact.isEmpty() )
factory = availableFact.get(availableFact.size()-1);
2000-09-18 14:29:57 +00:00
} 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.");
2001-06-14 10:54:57 +00:00
return factory;
2000-09-18 14:29:57 +00:00
}
/**
* Supplies a list of all avialable services names.
*
* @return list of Strings of all service names.
2000-09-18 14:29:57 +00:00
* @see com.sun.star.container.XContentEnumerationAccess
*/
public String[] getAvailableServiceNames()
throws com.sun.star.uno.RuntimeException
{
try{
return factoriesByServiceNames.keySet().toArray(
new String[ factoriesByServiceNames.size() ] );
} catch(Exception ex) {
throw new com.sun.star.uno.RuntimeException(ex);
}
2000-09-18 14:29:57 +00:00
}
2001-06-14 10:54:57 +00:00
// XMultiComponentFactory implementation
/** Create a service instance with given context.
*
* @param rServiceSpecifier service name.
* @param xContext context.
* @return service instance.
*/
2001-06-14 10:54:57 +00:00
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)
{
CWS-TOOLING: integrate CWS sb113 2009-09-01 sb #i76393# second attempt at properly #ifdef-ing previous HG commit d598efdbf012 2009-08-28 sb #i102469# change back <T extends XInterface> to just <T> on queryInterface, to avoid binary incompatibility (method changing its signature from (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; to (Ljava/lang/Class;Ljava/lang/Object;)Lcom/sun/star/uno/XInterface;) 2009-08-28 sb #i76393# properly #ifdef previous HG commit d598efdbf012 2009-08-27 sb #i94421# work around compiler error (based on a patch supplied by cloph) 2009-08-26 sb merged in DEV300_m56 2009-08-26 sb #i76393# on Linux, include dynamic section offset in crash report so as to be able to map "prelinked" callstacks back to original (patch by cmc) 2009-08-26 sb #i88162# remove unnecessary whitespace lines from per-locale xcu files (patch by tora) 2009-08-17 Juergen Schmidt #i104292# set context classloader after create new custom UNO loader 2009-08-17 Juergen Schmidt #i103749# integrate patch 2009-08-14 sb #i103269# cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r 5124ebd5edd1 ("#i101955# changed encoding of XML file content from erroneous ISO-8859-1 to UTF-8") 2009-08-12 sb #i102469# fixed mis-applications of UnoRuntime.queryInterface (detected via the simplified UnoRuntime.queryInterface, the HG changeset 29de35fc9554) to use AnyConverter instead; changed qadevOOo's lib.MultiMethodTest.before to allow throwing arbitrary exceptions, to cater for IllegalArgumentException thrown by AnyConverter 2009-08-12 sb #i104178# drop extra libxml2-config script from libxmlsec 2009-08-10 sb #i101754# simplified osl_getProcessInfo for LINUX (patch by cmc) 2009-08-10 sb #i95018# avoid closing -1 fds (patch supplied by cmc) 2009-08-10 sb #i103585# removed (apparently unnecessary) zlib support from libxml2; in turn, removed zlib dependencies from libxmlsec, libxslt, and redland (assuming those were transitive dependencies brought in by direct dependencies on libxml2) 2009-08-10 sb #i102469# simplified UnoRuntime.queryInterface using Java 5 generics; adapted URE-related modules accordingly 2009-08-10 sb #i101213# adapted setsolar env (solenv/config/) to set PYTHONPATH (and not set PYTHONHOME) in accordance with configure env (set_soenv.in); fixed testtools/source/bridgetest/pyuno (which now should work everywhere out of the box, thanks to the fixed setsolar PYTHONPATH) 2009-08-10 sb cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r ea8de6d9396b ("#i101955# work in progress for a .hgignore file, continued")
2009-09-16 14:37:52 +00:00
XSingleComponentFactory xCompFac = UnoRuntime.queryInterface(
2001-06-14 10:54:57 +00:00
XSingleComponentFactory.class, fac );
if (xCompFac != null)
{
return xCompFac.createInstanceWithContext( xContext );
}
else
{
CWS-TOOLING: integrate CWS sb113 2009-09-01 sb #i76393# second attempt at properly #ifdef-ing previous HG commit d598efdbf012 2009-08-28 sb #i102469# change back <T extends XInterface> to just <T> on queryInterface, to avoid binary incompatibility (method changing its signature from (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; to (Ljava/lang/Class;Ljava/lang/Object;)Lcom/sun/star/uno/XInterface;) 2009-08-28 sb #i76393# properly #ifdef previous HG commit d598efdbf012 2009-08-27 sb #i94421# work around compiler error (based on a patch supplied by cloph) 2009-08-26 sb merged in DEV300_m56 2009-08-26 sb #i76393# on Linux, include dynamic section offset in crash report so as to be able to map "prelinked" callstacks back to original (patch by cmc) 2009-08-26 sb #i88162# remove unnecessary whitespace lines from per-locale xcu files (patch by tora) 2009-08-17 Juergen Schmidt #i104292# set context classloader after create new custom UNO loader 2009-08-17 Juergen Schmidt #i103749# integrate patch 2009-08-14 sb #i103269# cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r 5124ebd5edd1 ("#i101955# changed encoding of XML file content from erroneous ISO-8859-1 to UTF-8") 2009-08-12 sb #i102469# fixed mis-applications of UnoRuntime.queryInterface (detected via the simplified UnoRuntime.queryInterface, the HG changeset 29de35fc9554) to use AnyConverter instead; changed qadevOOo's lib.MultiMethodTest.before to allow throwing arbitrary exceptions, to cater for IllegalArgumentException thrown by AnyConverter 2009-08-12 sb #i104178# drop extra libxml2-config script from libxmlsec 2009-08-10 sb #i101754# simplified osl_getProcessInfo for LINUX (patch by cmc) 2009-08-10 sb #i95018# avoid closing -1 fds (patch supplied by cmc) 2009-08-10 sb #i103585# removed (apparently unnecessary) zlib support from libxml2; in turn, removed zlib dependencies from libxmlsec, libxslt, and redland (assuming those were transitive dependencies brought in by direct dependencies on libxml2) 2009-08-10 sb #i102469# simplified UnoRuntime.queryInterface using Java 5 generics; adapted URE-related modules accordingly 2009-08-10 sb #i101213# adapted setsolar env (solenv/config/) to set PYTHONPATH (and not set PYTHONHOME) in accordance with configure env (set_soenv.in); fixed testtools/source/bridgetest/pyuno (which now should work everywhere out of the box, thanks to the fixed setsolar PYTHONPATH) 2009-08-10 sb cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r ea8de6d9396b ("#i101955# work in progress for a .hgignore file, continued")
2009-09-16 14:37:52 +00:00
XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface(
2001-06-14 10:54:57 +00:00
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.
*/
2001-06-14 10:54:57 +00:00
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)
{
CWS-TOOLING: integrate CWS sb113 2009-09-01 sb #i76393# second attempt at properly #ifdef-ing previous HG commit d598efdbf012 2009-08-28 sb #i102469# change back <T extends XInterface> to just <T> on queryInterface, to avoid binary incompatibility (method changing its signature from (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; to (Ljava/lang/Class;Ljava/lang/Object;)Lcom/sun/star/uno/XInterface;) 2009-08-28 sb #i76393# properly #ifdef previous HG commit d598efdbf012 2009-08-27 sb #i94421# work around compiler error (based on a patch supplied by cloph) 2009-08-26 sb merged in DEV300_m56 2009-08-26 sb #i76393# on Linux, include dynamic section offset in crash report so as to be able to map "prelinked" callstacks back to original (patch by cmc) 2009-08-26 sb #i88162# remove unnecessary whitespace lines from per-locale xcu files (patch by tora) 2009-08-17 Juergen Schmidt #i104292# set context classloader after create new custom UNO loader 2009-08-17 Juergen Schmidt #i103749# integrate patch 2009-08-14 sb #i103269# cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r 5124ebd5edd1 ("#i101955# changed encoding of XML file content from erroneous ISO-8859-1 to UTF-8") 2009-08-12 sb #i102469# fixed mis-applications of UnoRuntime.queryInterface (detected via the simplified UnoRuntime.queryInterface, the HG changeset 29de35fc9554) to use AnyConverter instead; changed qadevOOo's lib.MultiMethodTest.before to allow throwing arbitrary exceptions, to cater for IllegalArgumentException thrown by AnyConverter 2009-08-12 sb #i104178# drop extra libxml2-config script from libxmlsec 2009-08-10 sb #i101754# simplified osl_getProcessInfo for LINUX (patch by cmc) 2009-08-10 sb #i95018# avoid closing -1 fds (patch supplied by cmc) 2009-08-10 sb #i103585# removed (apparently unnecessary) zlib support from libxml2; in turn, removed zlib dependencies from libxmlsec, libxslt, and redland (assuming those were transitive dependencies brought in by direct dependencies on libxml2) 2009-08-10 sb #i102469# simplified UnoRuntime.queryInterface using Java 5 generics; adapted URE-related modules accordingly 2009-08-10 sb #i101213# adapted setsolar env (solenv/config/) to set PYTHONPATH (and not set PYTHONHOME) in accordance with configure env (set_soenv.in); fixed testtools/source/bridgetest/pyuno (which now should work everywhere out of the box, thanks to the fixed setsolar PYTHONPATH) 2009-08-10 sb cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r ea8de6d9396b ("#i101955# work in progress for a .hgignore file, continued")
2009-09-16 14:37:52 +00:00
XSingleComponentFactory xCompFac = UnoRuntime.queryInterface(
2001-06-14 10:54:57 +00:00
XSingleComponentFactory.class, fac );
if (xCompFac != null)
{
return xCompFac.createInstanceWithArgumentsAndContext( rArguments, xContext );
}
else
{
CWS-TOOLING: integrate CWS sb113 2009-09-01 sb #i76393# second attempt at properly #ifdef-ing previous HG commit d598efdbf012 2009-08-28 sb #i102469# change back <T extends XInterface> to just <T> on queryInterface, to avoid binary incompatibility (method changing its signature from (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; to (Ljava/lang/Class;Ljava/lang/Object;)Lcom/sun/star/uno/XInterface;) 2009-08-28 sb #i76393# properly #ifdef previous HG commit d598efdbf012 2009-08-27 sb #i94421# work around compiler error (based on a patch supplied by cloph) 2009-08-26 sb merged in DEV300_m56 2009-08-26 sb #i76393# on Linux, include dynamic section offset in crash report so as to be able to map "prelinked" callstacks back to original (patch by cmc) 2009-08-26 sb #i88162# remove unnecessary whitespace lines from per-locale xcu files (patch by tora) 2009-08-17 Juergen Schmidt #i104292# set context classloader after create new custom UNO loader 2009-08-17 Juergen Schmidt #i103749# integrate patch 2009-08-14 sb #i103269# cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r 5124ebd5edd1 ("#i101955# changed encoding of XML file content from erroneous ISO-8859-1 to UTF-8") 2009-08-12 sb #i102469# fixed mis-applications of UnoRuntime.queryInterface (detected via the simplified UnoRuntime.queryInterface, the HG changeset 29de35fc9554) to use AnyConverter instead; changed qadevOOo's lib.MultiMethodTest.before to allow throwing arbitrary exceptions, to cater for IllegalArgumentException thrown by AnyConverter 2009-08-12 sb #i104178# drop extra libxml2-config script from libxmlsec 2009-08-10 sb #i101754# simplified osl_getProcessInfo for LINUX (patch by cmc) 2009-08-10 sb #i95018# avoid closing -1 fds (patch supplied by cmc) 2009-08-10 sb #i103585# removed (apparently unnecessary) zlib support from libxml2; in turn, removed zlib dependencies from libxmlsec, libxslt, and redland (assuming those were transitive dependencies brought in by direct dependencies on libxml2) 2009-08-10 sb #i102469# simplified UnoRuntime.queryInterface using Java 5 generics; adapted URE-related modules accordingly 2009-08-10 sb #i101213# adapted setsolar env (solenv/config/) to set PYTHONPATH (and not set PYTHONHOME) in accordance with configure env (set_soenv.in); fixed testtools/source/bridgetest/pyuno (which now should work everywhere out of the box, thanks to the fixed setsolar PYTHONPATH) 2009-08-10 sb cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r ea8de6d9396b ("#i101955# work in progress for a .hgignore file, continued")
2009-09-16 14:37:52 +00:00
XSingleServiceFactory xServiceFac = UnoRuntime.queryInterface(
2001-06-14 10:54:57 +00:00
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;
}
2000-09-18 14:29:57 +00:00
/**
* Removes all listeners from the <code>ServiceManager</code> and clears the
* list of the services.
*
2000-09-18 14:29:57 +00:00
* @see com.sun.star.lang.XComponent
*/
public void dispose()
throws com.sun.star.uno.RuntimeException
{
if (eventListener != null) {
for (XEventListener listener : eventListener) {
2000-09-18 14:29:57 +00:00
listener.disposing(new com.sun.star.lang.EventObject(this));
}
eventListener.clear();
2000-09-18 14:29:57 +00:00
}
factoriesByServiceNames.clear();
factoriesByImplNames.clear();
}
/**
* Adds a new <code>EventListener</code>.
*
* <p>The listener is notified when a service is added (removed) to (from)
* the <code>ServiceManager</code>.</p>
*
* <p>If the listener is already registred a
* <code>com.sun.star.uno.RuntimeException</code> will be thrown.</p>
*
2000-09-18 14:29:57 +00:00
* @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.add(xListener);
2000-09-18 14:29:57 +00:00
}
/**
* Removes a <code>EventListener</code> from the <code>ServiceManager</code>.
*
* <p>If the listener is not registered a <code>com.sun.star.uno.RuntimeException</code>
* will be thrown.</p>
*
2000-09-18 14:29:57 +00:00
* @param xListener the new listener which should been removed.
* @see com.sun.star.lang.XComponent
2000-09-18 14:29:57 +00:00
*/
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.remove(xListener);
2000-09-18 14:29:57 +00:00
}
/**
* Checks if a component is registered at the <code>ServiceManager</code>.
*
* <p>The given object argument must provide a <code>XServiceInfo</code>
* interface.</p>
*
2000-09-18 14:29:57 +00:00
* @param object object which provides a <code>XServiceInfo</code> interface.
* @return true if the component is registred otherwise false.
*
2000-09-18 14:29:57 +00:00
* @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");
2000-09-18 14:29:57 +00:00
XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, object);
2000-09-18 14:29:57 +00:00
2011-03-10 12:58:23 +01:00
return xServiceInfo != null && UnoRuntime.areSame(factoriesByImplNames.get(xServiceInfo.getImplementationName()), object);
}
2000-09-18 14:29:57 +00:00
/**
* Adds a <code>SingleServiceFactory</code> to the <code>ServiceManager</code>.
*
2000-09-18 14:29:57 +00:00
* @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 =
CWS-TOOLING: integrate CWS sb113 2009-09-01 sb #i76393# second attempt at properly #ifdef-ing previous HG commit d598efdbf012 2009-08-28 sb #i102469# change back <T extends XInterface> to just <T> on queryInterface, to avoid binary incompatibility (method changing its signature from (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; to (Ljava/lang/Class;Ljava/lang/Object;)Lcom/sun/star/uno/XInterface;) 2009-08-28 sb #i76393# properly #ifdef previous HG commit d598efdbf012 2009-08-27 sb #i94421# work around compiler error (based on a patch supplied by cloph) 2009-08-26 sb merged in DEV300_m56 2009-08-26 sb #i76393# on Linux, include dynamic section offset in crash report so as to be able to map "prelinked" callstacks back to original (patch by cmc) 2009-08-26 sb #i88162# remove unnecessary whitespace lines from per-locale xcu files (patch by tora) 2009-08-17 Juergen Schmidt #i104292# set context classloader after create new custom UNO loader 2009-08-17 Juergen Schmidt #i103749# integrate patch 2009-08-14 sb #i103269# cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r 5124ebd5edd1 ("#i101955# changed encoding of XML file content from erroneous ISO-8859-1 to UTF-8") 2009-08-12 sb #i102469# fixed mis-applications of UnoRuntime.queryInterface (detected via the simplified UnoRuntime.queryInterface, the HG changeset 29de35fc9554) to use AnyConverter instead; changed qadevOOo's lib.MultiMethodTest.before to allow throwing arbitrary exceptions, to cater for IllegalArgumentException thrown by AnyConverter 2009-08-12 sb #i104178# drop extra libxml2-config script from libxmlsec 2009-08-10 sb #i101754# simplified osl_getProcessInfo for LINUX (patch by cmc) 2009-08-10 sb #i95018# avoid closing -1 fds (patch supplied by cmc) 2009-08-10 sb #i103585# removed (apparently unnecessary) zlib support from libxml2; in turn, removed zlib dependencies from libxmlsec, libxslt, and redland (assuming those were transitive dependencies brought in by direct dependencies on libxml2) 2009-08-10 sb #i102469# simplified UnoRuntime.queryInterface using Java 5 generics; adapted URE-related modules accordingly 2009-08-10 sb #i101213# adapted setsolar env (solenv/config/) to set PYTHONPATH (and not set PYTHONHOME) in accordance with configure env (set_soenv.in); fixed testtools/source/bridgetest/pyuno (which now should work everywhere out of the box, thanks to the fixed setsolar PYTHONPATH) 2009-08-10 sb cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r ea8de6d9396b ("#i101955# work in progress for a .hgignore file, continued")
2009-09-16 14:37:52 +00:00
UnoRuntime.queryInterface(XServiceInfo.class, object);
2000-09-18 14:29:57 +00:00
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();
ArrayList<Object> vec ;
2000-09-18 14:29:57 +00:00
for (String serviceName : serviceNames) {
if (!factoriesByServiceNames.containsKey(serviceName)) {
DEBUG("> no registered services found under " + serviceName + ": adding...");
factoriesByServiceNames.put(serviceName, new ArrayList<Object>());
2000-09-18 14:29:57 +00:00
}
vec = factoriesByServiceNames.get(serviceName);
if (vec.contains( object )) {
2000-09-18 14:29:57 +00:00
System.err.println("The implementation " + xServiceInfo.getImplementationName() +
" already registered for the service " + serviceName + " - ignoring!");
} else {
vec.add(object);
}
2000-09-18 14:29:57 +00:00
}
}
/**
* Removes a <code>SingleServiceFactory</code> from the <code>ServiceManager</code>.
*
2000-09-18 14:29:57 +00:00
* @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 =
CWS-TOOLING: integrate CWS sb113 2009-09-01 sb #i76393# second attempt at properly #ifdef-ing previous HG commit d598efdbf012 2009-08-28 sb #i102469# change back <T extends XInterface> to just <T> on queryInterface, to avoid binary incompatibility (method changing its signature from (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; to (Ljava/lang/Class;Ljava/lang/Object;)Lcom/sun/star/uno/XInterface;) 2009-08-28 sb #i76393# properly #ifdef previous HG commit d598efdbf012 2009-08-27 sb #i94421# work around compiler error (based on a patch supplied by cloph) 2009-08-26 sb merged in DEV300_m56 2009-08-26 sb #i76393# on Linux, include dynamic section offset in crash report so as to be able to map "prelinked" callstacks back to original (patch by cmc) 2009-08-26 sb #i88162# remove unnecessary whitespace lines from per-locale xcu files (patch by tora) 2009-08-17 Juergen Schmidt #i104292# set context classloader after create new custom UNO loader 2009-08-17 Juergen Schmidt #i103749# integrate patch 2009-08-14 sb #i103269# cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r 5124ebd5edd1 ("#i101955# changed encoding of XML file content from erroneous ISO-8859-1 to UTF-8") 2009-08-12 sb #i102469# fixed mis-applications of UnoRuntime.queryInterface (detected via the simplified UnoRuntime.queryInterface, the HG changeset 29de35fc9554) to use AnyConverter instead; changed qadevOOo's lib.MultiMethodTest.before to allow throwing arbitrary exceptions, to cater for IllegalArgumentException thrown by AnyConverter 2009-08-12 sb #i104178# drop extra libxml2-config script from libxmlsec 2009-08-10 sb #i101754# simplified osl_getProcessInfo for LINUX (patch by cmc) 2009-08-10 sb #i95018# avoid closing -1 fds (patch supplied by cmc) 2009-08-10 sb #i103585# removed (apparently unnecessary) zlib support from libxml2; in turn, removed zlib dependencies from libxmlsec, libxslt, and redland (assuming those were transitive dependencies brought in by direct dependencies on libxml2) 2009-08-10 sb #i102469# simplified UnoRuntime.queryInterface using Java 5 generics; adapted URE-related modules accordingly 2009-08-10 sb #i101213# adapted setsolar env (solenv/config/) to set PYTHONPATH (and not set PYTHONHOME) in accordance with configure env (set_soenv.in); fixed testtools/source/bridgetest/pyuno (which now should work everywhere out of the box, thanks to the fixed setsolar PYTHONPATH) 2009-08-10 sb cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r ea8de6d9396b ("#i101955# work in progress for a .hgignore file, continued")
2009-09-16 14:37:52 +00:00
UnoRuntime.queryInterface(XServiceInfo.class, object);
2000-09-18 14:29:57 +00:00
if (xServiceInfo == null)
throw new com.sun.star.lang.IllegalArgumentException(
"The given object does not implement the XServiceInfo interface."
);
XSingleServiceFactory xSingleServiceFactory =
CWS-TOOLING: integrate CWS sb113 2009-09-01 sb #i76393# second attempt at properly #ifdef-ing previous HG commit d598efdbf012 2009-08-28 sb #i102469# change back <T extends XInterface> to just <T> on queryInterface, to avoid binary incompatibility (method changing its signature from (Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; to (Ljava/lang/Class;Ljava/lang/Object;)Lcom/sun/star/uno/XInterface;) 2009-08-28 sb #i76393# properly #ifdef previous HG commit d598efdbf012 2009-08-27 sb #i94421# work around compiler error (based on a patch supplied by cloph) 2009-08-26 sb merged in DEV300_m56 2009-08-26 sb #i76393# on Linux, include dynamic section offset in crash report so as to be able to map "prelinked" callstacks back to original (patch by cmc) 2009-08-26 sb #i88162# remove unnecessary whitespace lines from per-locale xcu files (patch by tora) 2009-08-17 Juergen Schmidt #i104292# set context classloader after create new custom UNO loader 2009-08-17 Juergen Schmidt #i103749# integrate patch 2009-08-14 sb #i103269# cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r 5124ebd5edd1 ("#i101955# changed encoding of XML file content from erroneous ISO-8859-1 to UTF-8") 2009-08-12 sb #i102469# fixed mis-applications of UnoRuntime.queryInterface (detected via the simplified UnoRuntime.queryInterface, the HG changeset 29de35fc9554) to use AnyConverter instead; changed qadevOOo's lib.MultiMethodTest.before to allow throwing arbitrary exceptions, to cater for IllegalArgumentException thrown by AnyConverter 2009-08-12 sb #i104178# drop extra libxml2-config script from libxmlsec 2009-08-10 sb #i101754# simplified osl_getProcessInfo for LINUX (patch by cmc) 2009-08-10 sb #i95018# avoid closing -1 fds (patch supplied by cmc) 2009-08-10 sb #i103585# removed (apparently unnecessary) zlib support from libxml2; in turn, removed zlib dependencies from libxmlsec, libxslt, and redland (assuming those were transitive dependencies brought in by direct dependencies on libxml2) 2009-08-10 sb #i102469# simplified UnoRuntime.queryInterface using Java 5 generics; adapted URE-related modules accordingly 2009-08-10 sb #i101213# adapted setsolar env (solenv/config/) to set PYTHONPATH (and not set PYTHONHOME) in accordance with configure env (set_soenv.in); fixed testtools/source/bridgetest/pyuno (which now should work everywhere out of the box, thanks to the fixed setsolar PYTHONPATH) 2009-08-10 sb cherry-picked ssh://hg@hg.services.openoffice.org/cws/sb111 -r ea8de6d9396b ("#i101955# work in progress for a .hgignore file, continued")
2009-09-16 14:37:52 +00:00
UnoRuntime.queryInterface(XSingleServiceFactory.class, object);
2000-09-18 14:29:57 +00:00
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 (String serviceName : serviceNames) {
if (factoriesByServiceNames.containsKey(serviceName)) {
ArrayList<Object> vec = factoriesByServiceNames.get(serviceName);
if (!vec.remove(object)) {
2000-09-18 14:29:57 +00:00
System.err.println("The implementation " + xServiceInfo.getImplementationName() +
" is not registered for the service " + serviceName + " - ignoring!");
}
// remove the vector if no implementations aviable for the service
if (vec.isEmpty()) {
factoriesByServiceNames.remove(serviceName);
}
2000-09-18 14:29:57 +00:00
}
}
}
/**
* Provides an enumeration of all registered services.
*
* @return an enumeration of all available services.
* @see com.sun.star.container.XEnumerationAccess
2000-09-18 14:29:57 +00:00
*/
public XEnumeration createEnumeration()
throws com.sun.star.uno.RuntimeException
{
return new ServiceEnumerationImpl( factoriesByImplNames.values().iterator() );
2000-09-18 14:29:57 +00:00
}
/**
* Provides the UNO type of the <code>ServiceManager</code>
*
2000-09-18 14:29:57 +00:00
* @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 )
2001-01-16 17:07:14 +00:00
UNO_TYPE = new com.sun.star.uno.Type(ServiceManager.class);
2000-09-18 14:29:57 +00:00
return UNO_TYPE;
}
/**
* Checks if the any componets are registered.
*
2000-09-18 14:29:57 +00:00
* @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.
*
* @param serviceName name of the requested service.
2000-09-18 14:29:57 +00:00
* @return an enumeration for service name.
* @see com.sun.star.container.XContentEnumerationAccess
*/
public XEnumeration createContentEnumeration( String serviceName )
throws com.sun.star.uno.RuntimeException
{
2011-03-10 12:58:23 +01:00
XEnumeration enumer ;
2000-09-18 14:29:57 +00:00
ArrayList<Object> serviceList = factoriesByServiceNames.get(serviceName);
2000-09-18 14:29:57 +00:00
if (serviceList != null)
enumer = new ServiceEnumerationImpl( serviceList.iterator() );
2000-09-18 14:29:57 +00:00
else
enumer = new ServiceEnumerationImpl();
2000-09-18 14:29:57 +00:00
return enumer;
2000-09-18 14:29:57 +00:00
}
/**
* Returns the implementation name of the <code>ServiceManager</code> component.
*
2000-09-18 14:29:57 +00:00
* @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.
*
2000-09-18 14:29:57 +00:00
* @param serviceName service name which should be checked.
* @return true if the service is supported - otherwise false.
*
2000-09-18 14:29:57 +00:00
* @see com.sun.star.lang.XServiceInfo
*/
public boolean supportsService( String serviceName )
throws com.sun.star.uno.RuntimeException
{
for (String supportedServiceName : supportedServiceNames) {
if (supportedServiceName.equals(serviceName)) {
return true;
}
}
2000-09-18 14:29:57 +00:00
2011-03-10 12:58:23 +01:00
return getImplementationName().equals(serviceName);
}
2000-09-18 14:29:57 +00:00
/**
* Supplies list of all supported services.
*
2000-09-18 14:29:57 +00:00
* @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.
*
* <p>It is a inner wrapper for a java.util.Enumeration object.</p>
*
2000-09-18 14:29:57 +00:00
* @see com.sun.star.lang.XSingleServiceFactory
* @see com.sun.star.lang.XServiceInfo
* @since UDK1.0
*/
class ServiceEnumerationImpl implements XEnumeration {
java.util.Iterator<Object> enumeration = null;
2000-09-18 14:29:57 +00:00
/**
* Constructs a new empty instance.
*/
public ServiceEnumerationImpl() {
}
/**
* Constructs a new instance with a given enumeration.
*
* @param enumer is the enumeration which should been wrapped.
2000-09-18 14:29:57 +00:00
* @see com.sun.star.container.XEnumeration
*/
public ServiceEnumerationImpl(java.util.Enumeration<Object> enumer) {
enumeration = Collections.list(enumer).iterator();
}
/**
* Constructs a new instance with a given enumeration.
*
* @param enumer is the enumeration which should been wrapped.
* @see com.sun.star.container.XEnumeration
*/
public ServiceEnumerationImpl(java.util.Iterator<Object> enumer) {
enumeration = enumer;
2000-09-18 14:29:57 +00:00
}
/**
* Checks if the enumeration contains more elements.
*
2000-09-18 14:29:57 +00:00
* @return true if more elements are available - otherwise false.
* @see com.sun.star.container.XEnumeration
*/
public boolean hasMoreElements()
throws com.sun.star.uno.RuntimeException
{
return enumeration != null && enumeration.hasNext();
2000-09-18 14:29:57 +00:00
2011-03-10 12:58:23 +01:00
}
2000-09-18 14:29:57 +00:00
/**
* Returns the next element of the enumeration.
*
* <p>If no further elements available a com.sun.star.container.NoSuchElementException
* exception will be thrown.</p>
*
2000-09-18 14:29:57 +00:00
* @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.next();
2000-09-18 14:29:57 +00:00
} catch (java.util.NoSuchElementException e) {
throw new com.sun.star.container.NoSuchElementException(e, e.toString());
2000-09-18 14:29:57 +00:00
}
}
}
}