1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.comp.connections;
25 
26 import com.sun.star.bridge.XInstanceProvider;
27 
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.lang.XSingleServiceFactory;
30 
31 import com.sun.star.registry.XRegistryKey;
32 
33 import com.sun.star.comp.loader.FactoryHelper;
34 
35 
36 /**
37  * The <code>ConstantInstanceProvider</code> is a component
38  * that implements the <code>XInstanceProvider</code> Interface.
39  * <p>
40  * @version 	$Revision: 1.3 $ $ $Date: 2008-04-11 11:08:55 $
41  * @author 	    Kay Ramme
42  * @see         com.sun.star.bridge.XBridge
43  * @see         com.sun.star.bridge.XBridgeFactory
44  * @see         com.sun.star.bridge.XInstanceProvider
45  * @see         com.sun.star.comp.loader.JavaLoader
46  * @since       UDK1.0
47  */
48 public class ConstantInstanceProvider implements XInstanceProvider {
49 	/**
50 	 * When set to true, enables various debugging output.
51 	 */
52 	static public final boolean DEBUG = false;
53 
54 	/**
55 	 * The name of the service, the <code>JavaLoader</code> acceses this through reflection.
56 	 */
57     static private final String __serviceName = "com.sun.star.comp.connection.InstanceProvider";
58 
59 	/**
60 	 * Gives a factory for creating the service.
61 	 * This method is called by the <code>JavaLoader</code>
62 	 * <p>
63 	 * @return  returns a <code>XSingleServiceFactory</code> for creating the component
64 	 * @param   implName     the name of the implementation for which a service is desired
65 	 * @param   multiFactory the service manager to be uses if needed
66 	 * @param   regKey       the registryKey
67 	 * @see                  com.sun.star.comp.loader.JavaLoader
68 	 */
__getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)69 	public static XSingleServiceFactory __getServiceFactory(String implName,
70 															XMultiServiceFactory multiFactory,
71 															XRegistryKey regKey)
72 	{
73 		XSingleServiceFactory xSingleServiceFactory = null;
74 
75 	    if (implName.equals(ConstantInstanceProvider.class.getName()) )
76 	        xSingleServiceFactory = FactoryHelper.getServiceFactory(ConstantInstanceProvider.class,
77 																	__serviceName,
78 																	multiFactory,
79 																	regKey);
80 
81 	    return xSingleServiceFactory;
82 	}
83 
84 	protected XMultiServiceFactory _serviceManager;
85 	protected String _serviceName;
86 	protected Object _instance;
87 
88 
setInstance(String serviceName)89 	public void setInstance(String serviceName) throws com.sun.star.uno.Exception {
90 		_instance = _serviceManager.createInstance(serviceName);
91 		_serviceName = serviceName;
92 	}
93 
94 	/**
95 	 * Constructs a new <code>ConstantInstanceProvider</code>.
96 	 * Uses the provided ServiceManager as the provided instance.
97 	 * <p>
98 	 * @param    serviceName   the provided service manager
99 	 */
ConstantInstanceProvider(XMultiServiceFactory serviceManager)100 	public ConstantInstanceProvider(XMultiServiceFactory serviceManager) {
101 		_serviceManager = serviceManager;
102 
103 		_serviceName = "SERVICEMANAGER";
104 		_instance    = serviceManager;
105 	}
106 
107 	/**
108 	 * Gives an object for the passed instance name.
109 	 * <p>
110 	 * @return  the desired instance
111 	 * @param   sInstanceName   the name of the desired instance
112 	 */
getInstance(String sInstanceName)113     public Object getInstance(String sInstanceName) throws com.sun.star.container.NoSuchElementException, com.sun.star.uno.RuntimeException {
114 		Object result = sInstanceName.equals(_serviceName) ? _instance : null;
115 
116 		if(DEBUG) System.err.println("##### " + getClass().getName() + ".getInstance(" + sInstanceName + "):" + result);
117 
118 		return result;
119 	}
120 }
121 
122