1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConstantInstanceProvider.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com
.sun
.star
.comp
.connections
;
33 import com
.sun
.star
.bridge
.XInstanceProvider
;
35 import com
.sun
.star
.lang
.XMultiServiceFactory
;
36 import com
.sun
.star
.lang
.XSingleServiceFactory
;
38 import com
.sun
.star
.registry
.XRegistryKey
;
40 import com
.sun
.star
.comp
.loader
.FactoryHelper
;
44 * The <code>ConstantInstanceProvider</code> is a component
45 * that implements the <code>XInstanceProvider</code> Interface.
47 * @version $Revision: 1.3 $ $ $Date: 2008-04-11 11:08:55 $
49 * @see com.sun.star.bridge.XBridge
50 * @see com.sun.star.bridge.XBridgeFactory
51 * @see com.sun.star.bridge.XInstanceProvider
52 * @see com.sun.star.loader.JavaLoader
55 public class ConstantInstanceProvider
implements XInstanceProvider
{
57 * When set to true, enables various debugging output.
59 static public final boolean DEBUG
= false;
62 * The name of the service, the <code>JavaLoader</code> acceses this through reflection.
64 static private final String __serviceName
= "com.sun.star.comp.connection.InstanceProvider";
67 * Gives a factory for creating the service.
68 * This method is called by the <code>JavaLoader</code>
70 * @return returns a <code>XSingleServiceFactory</code> for creating the component
71 * @param implName the name of the implementation for which a service is desired
72 * @param multiFactory the service manager to be uses if needed
73 * @param regKey the registryKey
74 * @see com.sun.star.comp.loader.JavaLoader
76 public static XSingleServiceFactory
__getServiceFactory(String implName
,
77 XMultiServiceFactory multiFactory
,
80 XSingleServiceFactory xSingleServiceFactory
= null;
82 if (implName
.equals(ConstantInstanceProvider
.class.getName()) )
83 xSingleServiceFactory
= FactoryHelper
.getServiceFactory(ConstantInstanceProvider
.class,
88 return xSingleServiceFactory
;
92 * Writes the service information into the given registry key.
93 * This method is called by the <code>JavaLoader</code>
95 * @return returns true if the operation succeeded
96 * @param regKey the registryKey
97 * @see com.sun.star.comp.loader.JavaLoader
99 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey
) {
100 return FactoryHelper
.writeRegistryServiceInfo(ConstantInstanceProvider
.class.getName(), __serviceName
, regKey
);
105 protected XMultiServiceFactory _serviceManager
;
106 protected String _serviceName
;
107 protected Object _instance
;
110 public void setInstance(String serviceName
) throws com
.sun
.star
.uno
.Exception
{
111 _instance
= _serviceManager
.createInstance(serviceName
);
112 _serviceName
= serviceName
;
116 * Constructs a new <code>ConstantInstanceProvider</code>.
117 * Uses the provided ServiceManager as the provided instance.
119 * @param serviceName the provided service manager
121 public ConstantInstanceProvider(XMultiServiceFactory serviceManager
) {
122 _serviceManager
= serviceManager
;
124 _serviceName
= "SERVICEMANAGER";
125 _instance
= serviceManager
;
129 * Gives an object for the passed instance name.
131 * @return the desired instance
132 * @param sInstanceName the name of the desired instance
134 public Object
getInstance(String sInstanceName
) throws com
.sun
.star
.container
.NoSuchElementException
, com
.sun
.star
.uno
.RuntimeException
{
135 Object result
= sInstanceName
.equals(_serviceName
) ? _instance
: null;
137 if(DEBUG
) System
.err
.println("##### " + getClass().getName() + ".getInstance(" + sInstanceName
+ "):" + result
);