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