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