xref: /AOO41X/main/jurt/com/sun/star/comp/connections/ConstantInstanceProvider.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package com.sun.star.comp.connections;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
33*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir /**
41*cdf0e10cSrcweir  * The <code>ConstantInstanceProvider</code> is a component
42*cdf0e10cSrcweir  * that implements the <code>XInstanceProvider</code> Interface.
43*cdf0e10cSrcweir  * <p>
44*cdf0e10cSrcweir  * @version 	$Revision: 1.3 $ $ $Date: 2008-04-11 11:08:55 $
45*cdf0e10cSrcweir  * @author 	    Kay Ramme
46*cdf0e10cSrcweir  * @see         com.sun.star.bridge.XBridge
47*cdf0e10cSrcweir  * @see         com.sun.star.bridge.XBridgeFactory
48*cdf0e10cSrcweir  * @see         com.sun.star.bridge.XInstanceProvider
49*cdf0e10cSrcweir  * @see         com.sun.star.loader.JavaLoader
50*cdf0e10cSrcweir  * @since       UDK1.0
51*cdf0e10cSrcweir  */
52*cdf0e10cSrcweir public class ConstantInstanceProvider implements XInstanceProvider {
53*cdf0e10cSrcweir 	/**
54*cdf0e10cSrcweir 	 * When set to true, enables various debugging output.
55*cdf0e10cSrcweir 	 */
56*cdf0e10cSrcweir 	static public final boolean DEBUG = false;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	/**
59*cdf0e10cSrcweir 	 * The name of the service, the <code>JavaLoader</code> acceses this through reflection.
60*cdf0e10cSrcweir 	 */
61*cdf0e10cSrcweir     static private final String __serviceName = "com.sun.star.comp.connection.InstanceProvider";
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	/**
64*cdf0e10cSrcweir 	 * Gives a factory for creating the service.
65*cdf0e10cSrcweir 	 * This method is called by the <code>JavaLoader</code>
66*cdf0e10cSrcweir 	 * <p>
67*cdf0e10cSrcweir 	 * @return  returns a <code>XSingleServiceFactory</code> for creating the component
68*cdf0e10cSrcweir 	 * @param   implName     the name of the implementation for which a service is desired
69*cdf0e10cSrcweir 	 * @param   multiFactory the service manager to be uses if needed
70*cdf0e10cSrcweir 	 * @param   regKey       the registryKey
71*cdf0e10cSrcweir 	 * @see                  com.sun.star.comp.loader.JavaLoader
72*cdf0e10cSrcweir 	 */
73*cdf0e10cSrcweir 	public static XSingleServiceFactory __getServiceFactory(String implName,
74*cdf0e10cSrcweir 															XMultiServiceFactory multiFactory,
75*cdf0e10cSrcweir 															XRegistryKey regKey)
76*cdf0e10cSrcweir 	{
77*cdf0e10cSrcweir 		XSingleServiceFactory xSingleServiceFactory = null;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	    if (implName.equals(ConstantInstanceProvider.class.getName()) )
80*cdf0e10cSrcweir 	        xSingleServiceFactory = FactoryHelper.getServiceFactory(ConstantInstanceProvider.class,
81*cdf0e10cSrcweir 																	__serviceName,
82*cdf0e10cSrcweir 																	multiFactory,
83*cdf0e10cSrcweir 																	regKey);
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	    return xSingleServiceFactory;
86*cdf0e10cSrcweir 	}
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	protected XMultiServiceFactory _serviceManager;
89*cdf0e10cSrcweir 	protected String _serviceName;
90*cdf0e10cSrcweir 	protected Object _instance;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	public void setInstance(String serviceName) throws com.sun.star.uno.Exception {
94*cdf0e10cSrcweir 		_instance = _serviceManager.createInstance(serviceName);
95*cdf0e10cSrcweir 		_serviceName = serviceName;
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	/**
99*cdf0e10cSrcweir 	 * Constructs a new <code>ConstantInstanceProvider</code>.
100*cdf0e10cSrcweir 	 * Uses the provided ServiceManager as the provided instance.
101*cdf0e10cSrcweir 	 * <p>
102*cdf0e10cSrcweir 	 * @param    serviceName   the provided service manager
103*cdf0e10cSrcweir 	 */
104*cdf0e10cSrcweir 	public ConstantInstanceProvider(XMultiServiceFactory serviceManager) {
105*cdf0e10cSrcweir 		_serviceManager = serviceManager;
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 		_serviceName = "SERVICEMANAGER";
108*cdf0e10cSrcweir 		_instance    = serviceManager;
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	/**
112*cdf0e10cSrcweir 	 * Gives an object for the passed instance name.
113*cdf0e10cSrcweir 	 * <p>
114*cdf0e10cSrcweir 	 * @return  the desired instance
115*cdf0e10cSrcweir 	 * @param   sInstanceName   the name of the desired instance
116*cdf0e10cSrcweir 	 */
117*cdf0e10cSrcweir     public Object getInstance(String sInstanceName) throws com.sun.star.container.NoSuchElementException, com.sun.star.uno.RuntimeException {
118*cdf0e10cSrcweir 		Object result = sInstanceName.equals(_serviceName) ? _instance : null;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 		if(DEBUG) System.err.println("##### " + getClass().getName() + ".getInstance(" + sInstanceName + "):" + result);
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 		return result;
123*cdf0e10cSrcweir 	}
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126