1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 35 import com.sun.star.lib.uno.helper.Factory; 36 import com.sun.star.lang.XMultiComponentFactory; 37 import com.sun.star.lang.XSingleComponentFactory; 38 import com.sun.star.lib.uno.helper.WeakBase; 39 import com.sun.star.uno.UnoRuntime; 40 import com.sun.star.uno.XComponentContext; 41 import com.sun.star.registry.XRegistryKey; 42 import com.sun.star.lang.XInitialization; 43 import com.sun.star.lang.XTypeProvider; 44 import com.sun.star.lang.XServiceInfo; 45 import com.sun.star.uno.Type; 46 47 /** This class capsulates the class, that implements the minimal component, a 48 * factory for creating the service (<CODE>__getComponentFactory</CODE>) and a 49 * method, that writes the information into the given registry key 50 * (<CODE>__writeRegistryServiceInfo</CODE>). 51 */ 52 public class MinimalComponent { 53 /** This class implements the component. At least the interfaces XServiceInfo, 54 * XTypeProvider, and XInitialization should be provided by the service. 55 */ 56 public static class _MinimalComponent extends WeakBase 57 implements XInitialization, XServiceInfo { 58 /** The service name, that must be used to get an instance of this service. 59 */ 60 static private final String __serviceName = 61 "org.openoffice.MinimalComponent"; 62 63 /** The initial component contextr, that gives access to 64 * the service manager, supported singletons, ... 65 * It's often later used 66 */ 67 private XComponentContext m_cmpCtx; 68 69 /** The service manager, that gives access to all registered services. 70 * It's often later used 71 */ 72 private XMultiComponentFactory m_xMCF; 73 74 /** The constructor of the inner class has a XMultiServiceFactory parameter. 75 * @param xmultiservicefactoryInitialization A special service factory 76 * could be introduced while initializing. 77 */ 78 public _MinimalComponent(XComponentContext xCompContext) { 79 try { 80 m_cmpCtx = xCompContext; 81 m_xMCF = m_cmpCtx.getServiceManager(); 82 } 83 catch( Exception e ) { 84 e.printStackTrace(); 85 } 86 } 87 88 /** This method is a member of the interface for initializing an object 89 * directly after its creation. 90 * @param object This array of arbitrary objects will be passed to the 91 * component after its creation. 92 * @throws Exception Every exception will not be handled, but will be 93 * passed to the caller. 94 */ 95 public void initialize( Object[] object ) 96 throws com.sun.star.uno.Exception { 97 /* The component describes what arguments its expected and in which 98 * order!At this point you can read the objects and can intialize 99 * your component using these objects. 100 */ 101 } 102 103 /** This method returns an array of all supported service names. 104 * @return Array of supported service names. 105 */ 106 public String[] getSupportedServiceNames() { 107 return getServiceNames(); 108 } 109 110 /** This method is a simple helper function to used in the 111 * static component initialisation functions as well as in 112 * getSupportedServiceNames. 113 */ 114 public static String[] getServiceNames() { 115 String[] sSupportedServiceNames = { __serviceName }; 116 return sSupportedServiceNames; 117 } 118 119 /** This method returns true, if the given service will be 120 * supported by the component. 121 * @param sServiceName Service name. 122 * @return True, if the given service name will be supported. 123 */ 124 public boolean supportsService( String sServiceName ) { 125 return sServiceName.equals( __serviceName ); 126 } 127 128 /** Return the class name of the component. 129 * @return Class name of the component. 130 */ 131 public String getImplementationName() { 132 return _MinimalComponent.class.getName(); 133 } 134 } 135 136 137 /** 138 * Gives a factory for creating the service. 139 * This method is called by the <code>JavaLoader</code> 140 * <p> 141 * @return returns a <code>XSingleComponentFactory</code> for creating 142 * the component 143 * @param sImplName the name of the implementation for which a 144 * service is desired 145 * @see com.sun.star.comp.loader.JavaLoader 146 */ 147 public static XSingleComponentFactory __getComponentFactory(String sImplName) 148 { 149 XSingleComponentFactory xFactory = null; 150 151 if ( sImplName.equals( _MinimalComponent.class.getName() ) ) 152 xFactory = Factory.createComponentFactory(_MinimalComponent.class, 153 _MinimalComponent.getServiceNames()); 154 155 return xFactory; 156 } 157 158 /** 159 * Writes the service information into the given registry key. 160 * This method is called by the <code>JavaLoader</code> 161 * <p> 162 * @return returns true if the operation succeeded 163 * @param regKey the registryKey 164 * @see com.sun.star.comp.loader.JavaLoader 165 */ 166 // This method not longer necessary since OOo 3.4 where the component registration 167 // was changed to passive component registration. For more details see 168 // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration 169 170 // public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) { 171 // return Factory.writeRegistryServiceInfo(_MinimalComponent.class.getName(), 172 // _MinimalComponent.getServiceNames(), 173 // regKey); 174 // } 175 } 176