1a5b190bfSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3a5b190bfSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4a5b190bfSAndrew Rist * or more contributor license agreements. See the NOTICE file 5a5b190bfSAndrew Rist * distributed with this work for additional information 6a5b190bfSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7a5b190bfSAndrew Rist * to you under the Apache License, Version 2.0 (the 8a5b190bfSAndrew Rist * "License"); you may not use this file except in compliance 9a5b190bfSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11a5b190bfSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13a5b190bfSAndrew Rist * Unless required by applicable law or agreed to in writing, 14a5b190bfSAndrew Rist * software distributed under the License is distributed on an 15a5b190bfSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16a5b190bfSAndrew Rist * KIND, either express or implied. See the License for the 17a5b190bfSAndrew Rist * specific language governing permissions and limitations 18a5b190bfSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20a5b190bfSAndrew Rist *************************************************************/ 21a5b190bfSAndrew Rist 22a5b190bfSAndrew Rist 23cdf0e10cSrcweir package com.sun.star.lib.uno.helper; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 26cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory; 27cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 28cdf0e10cSrcweir import com.sun.star.lang.XInitialization; 29cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 30cdf0e10cSrcweir 31cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 32cdf0e10cSrcweir 33cdf0e10cSrcweir 34cdf0e10cSrcweir /** Factory helper class supporting com.sun.star.lang.XServiceInfo and 35cdf0e10cSrcweir com.sun.star.lang.XSingleComponentFactory. 36cdf0e10cSrcweir 37*5fdea833SDamjan Jovanovic Attention: 38*5fdea833SDamjan Jovanovic <br> 39cdf0e10cSrcweir This factory implementation does not support lang.XSingleServiceFactory. 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir public class Factory 42cdf0e10cSrcweir extends ComponentBase 43cdf0e10cSrcweir implements XSingleComponentFactory, XServiceInfo 44cdf0e10cSrcweir { 45cdf0e10cSrcweir private static final boolean DEBUG = false; 46cdf0e10cSrcweir 47cdf0e10cSrcweir /** Creates an object factory supporting interfaces 48cdf0e10cSrcweir com.sun.star.lang.XSingleComponentFactory and 49cdf0e10cSrcweir com.sun.star.lang.XServiceInfo 50cdf0e10cSrcweir 51cdf0e10cSrcweir @param impl_class 52cdf0e10cSrcweir implementation class 53cdf0e10cSrcweir @param impl_name 54cdf0e10cSrcweir implementation name 55cdf0e10cSrcweir @param supported_services 56cdf0e10cSrcweir services implemented 57cdf0e10cSrcweir @return 58cdf0e10cSrcweir object factory 59cdf0e10cSrcweir 60cdf0e10cSrcweir @since UDK 3.2.13 61cdf0e10cSrcweir */ createComponentFactory( Class impl_class, String impl_name, String supported_services [] )62cdf0e10cSrcweir public static XSingleComponentFactory createComponentFactory( 63cdf0e10cSrcweir Class impl_class, String impl_name, String supported_services [] ) 64cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 65cdf0e10cSrcweir { 66cdf0e10cSrcweir return new Factory( impl_class, impl_name, supported_services ); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir /** Creates an object factory supporting interfaces 70cdf0e10cSrcweir com.sun.star.lang.XSingleComponentFactory and 71cdf0e10cSrcweir com.sun.star.lang.XServiceInfo 72cdf0e10cSrcweir 73cdf0e10cSrcweir The implementation name is the name of the implementation class. 74cdf0e10cSrcweir 75cdf0e10cSrcweir @param impl_class 76cdf0e10cSrcweir implementation class 77cdf0e10cSrcweir @param supported_services 78cdf0e10cSrcweir services implemented 79cdf0e10cSrcweir @return 80cdf0e10cSrcweir object factory 81cdf0e10cSrcweir */ createComponentFactory( Class impl_class, String supported_services [] )82cdf0e10cSrcweir public static XSingleComponentFactory createComponentFactory( 83cdf0e10cSrcweir Class impl_class, String supported_services [] ) 84cdf0e10cSrcweir throws com.sun.star.uno.RuntimeException 85cdf0e10cSrcweir { 86cdf0e10cSrcweir return createComponentFactory( 87cdf0e10cSrcweir impl_class, impl_class.getName(), supported_services ); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir /** Writes component's implementation info to given registry key. 90cdf0e10cSrcweir 91cdf0e10cSrcweir @param impl_name 92cdf0e10cSrcweir name of implementation 93cdf0e10cSrcweir @param supported_services 94cdf0e10cSrcweir supported services of implementation 95cdf0e10cSrcweir @param xKey 96cdf0e10cSrcweir registry key to write to 97cdf0e10cSrcweir @return 98cdf0e10cSrcweir success 99cdf0e10cSrcweir */ writeRegistryServiceInfo( String impl_name, String supported_services [], XRegistryKey xKey )100cdf0e10cSrcweir public static boolean writeRegistryServiceInfo( 101cdf0e10cSrcweir String impl_name, String supported_services [], XRegistryKey xKey ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir try 104cdf0e10cSrcweir { 105cdf0e10cSrcweir XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" ); 106cdf0e10cSrcweir for ( int nPos = 0; nPos < supported_services.length; ++nPos ) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir xNewKey.createKey( supported_services[ nPos ] ); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir return true; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir catch (com.sun.star.registry.InvalidRegistryException exc) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir if (DEBUG) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir System.err.println( 117cdf0e10cSrcweir "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " + 118cdf0e10cSrcweir exc.toString() ); 119cdf0e10cSrcweir } 120cdf0e10cSrcweir } 121cdf0e10cSrcweir return false; 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir //============================================================================================== 125cdf0e10cSrcweir private String m_impl_name; 126cdf0e10cSrcweir private String [] m_supported_services; 127cdf0e10cSrcweir private Class m_impl_class; 128cdf0e10cSrcweir private java.lang.reflect.Method m_method; 129cdf0e10cSrcweir private java.lang.reflect.Constructor m_ctor; 130cdf0e10cSrcweir Factory( Class impl_class, String impl_name, String supported_services [] )131cdf0e10cSrcweir private Factory( 132cdf0e10cSrcweir Class impl_class, String impl_name, String supported_services [] ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir m_impl_name = impl_name; 135cdf0e10cSrcweir m_supported_services = supported_services; 136cdf0e10cSrcweir m_impl_class = impl_class; 137cdf0e10cSrcweir m_method = null; 138cdf0e10cSrcweir m_ctor = null; 139cdf0e10cSrcweir 140cdf0e10cSrcweir Class params [] = new Class [] { XComponentContext.class }; 141cdf0e10cSrcweir 142cdf0e10cSrcweir try 143cdf0e10cSrcweir { 144cdf0e10cSrcweir // seeking for "public static Object __create( XComponentContext )" 145cdf0e10cSrcweir m_method = m_impl_class.getMethod( "__create", params ); 146cdf0e10cSrcweir int mod = m_method.getModifiers(); 147cdf0e10cSrcweir if (!m_method.getReturnType().equals( Object.class ) || 148cdf0e10cSrcweir !java.lang.reflect.Modifier.isStatic( mod ) || 149cdf0e10cSrcweir !java.lang.reflect.Modifier.isPublic( mod )) 150cdf0e10cSrcweir { 151cdf0e10cSrcweir m_method = null; 152cdf0e10cSrcweir } 153cdf0e10cSrcweir } 154cdf0e10cSrcweir catch (Exception exc) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir if (null == m_method) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir try 161cdf0e10cSrcweir { 162cdf0e10cSrcweir // ctor with context 163cdf0e10cSrcweir m_ctor = m_impl_class.getConstructor( params ); 164cdf0e10cSrcweir } 165cdf0e10cSrcweir catch (Exception exc) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir // else take default ctor 168cdf0e10cSrcweir } 169cdf0e10cSrcweir } 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir //______________________________________________________________________________________________ instantiate( XComponentContext xContext )173cdf0e10cSrcweir private final Object instantiate( XComponentContext xContext ) 174cdf0e10cSrcweir throws com.sun.star.uno.Exception 175cdf0e10cSrcweir { 176cdf0e10cSrcweir try 177cdf0e10cSrcweir { 178cdf0e10cSrcweir if (DEBUG) 179cdf0e10cSrcweir System.out.print( "instantiating " + m_impl_class.toString() + " using " ); 180cdf0e10cSrcweir if (null != m_method) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir if (DEBUG) 183cdf0e10cSrcweir System.out.println( "__create( XComponentContext )..." ); 184cdf0e10cSrcweir return m_method.invoke( null, new Object [] { xContext } ); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir if (null != m_ctor) 187cdf0e10cSrcweir { 188cdf0e10cSrcweir if (DEBUG) 189cdf0e10cSrcweir System.out.println( "ctor( XComponentContext )..." ); 190cdf0e10cSrcweir return m_ctor.newInstance( new Object [] { xContext } ); 191cdf0e10cSrcweir } 192cdf0e10cSrcweir if (DEBUG) 193cdf0e10cSrcweir System.out.println( "default ctor..." ); 194cdf0e10cSrcweir return m_impl_class.newInstance(); // default ctor 195cdf0e10cSrcweir } 196cdf0e10cSrcweir catch (java.lang.reflect.InvocationTargetException exc) 197cdf0e10cSrcweir { 198cdf0e10cSrcweir Throwable targetException = exc.getTargetException(); 199cdf0e10cSrcweir if (targetException instanceof java.lang.RuntimeException) 200cdf0e10cSrcweir throw (java.lang.RuntimeException)targetException; 201cdf0e10cSrcweir else if (targetException instanceof com.sun.star.uno.RuntimeException) 202cdf0e10cSrcweir throw (com.sun.star.uno.RuntimeException)targetException; 203cdf0e10cSrcweir else if (targetException instanceof com.sun.star.uno.Exception) 204cdf0e10cSrcweir throw (com.sun.star.uno.Exception)targetException; 205cdf0e10cSrcweir else 206cdf0e10cSrcweir throw new com.sun.star.uno.Exception( targetException.toString(), this ); 207cdf0e10cSrcweir } 208cdf0e10cSrcweir catch (IllegalAccessException exc) 209cdf0e10cSrcweir { 210cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException( exc.toString(), this ); 211cdf0e10cSrcweir } 212cdf0e10cSrcweir catch (InstantiationException exc) 213cdf0e10cSrcweir { 214cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException( exc.toString(), this ); 215cdf0e10cSrcweir } 216cdf0e10cSrcweir } 217cdf0e10cSrcweir // XSingleComponentFactory impl 218cdf0e10cSrcweir //______________________________________________________________________________________________ createInstanceWithContext( XComponentContext xContext )219cdf0e10cSrcweir public final Object createInstanceWithContext( 220cdf0e10cSrcweir XComponentContext xContext ) 221cdf0e10cSrcweir throws com.sun.star.uno.Exception 222cdf0e10cSrcweir { 223cdf0e10cSrcweir return instantiate( xContext ); 224cdf0e10cSrcweir } 225cdf0e10cSrcweir //______________________________________________________________________________________________ createInstanceWithArgumentsAndContext( Object arguments [], XComponentContext xContext )226cdf0e10cSrcweir public final Object createInstanceWithArgumentsAndContext( 227cdf0e10cSrcweir Object arguments [], XComponentContext xContext ) 228cdf0e10cSrcweir throws com.sun.star.uno.Exception 229cdf0e10cSrcweir { 230cdf0e10cSrcweir Object inst = instantiate( xContext ); 231cdf0e10cSrcweir XInitialization xInit = UnoRuntime.queryInterface( 232cdf0e10cSrcweir XInitialization.class, inst ); 233cdf0e10cSrcweir if (null == xInit) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( 236cdf0e10cSrcweir "cannot pass arguments to component; no XInitialization implemented!", this, 237cdf0e10cSrcweir (short)0 ); 238cdf0e10cSrcweir } 239cdf0e10cSrcweir xInit.initialize( arguments ); 240cdf0e10cSrcweir return inst; 241cdf0e10cSrcweir } 242cdf0e10cSrcweir 243cdf0e10cSrcweir // XServiceInfo impl 244cdf0e10cSrcweir //______________________________________________________________________________________________ getImplementationName()245cdf0e10cSrcweir public final String getImplementationName() 246cdf0e10cSrcweir { 247cdf0e10cSrcweir return m_impl_name; 248cdf0e10cSrcweir } 249cdf0e10cSrcweir //______________________________________________________________________________________________ supportsService( String service_name )250cdf0e10cSrcweir public final boolean supportsService( String service_name ) 251cdf0e10cSrcweir { 252cdf0e10cSrcweir for ( int nPos = 0; nPos < m_supported_services.length; ++nPos ) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir if (m_supported_services[ nPos ].equals( service_name )) 255cdf0e10cSrcweir return true; 256cdf0e10cSrcweir } 257cdf0e10cSrcweir return false; 258cdf0e10cSrcweir } 259cdf0e10cSrcweir //______________________________________________________________________________________________ getSupportedServiceNames()260cdf0e10cSrcweir public final String [] getSupportedServiceNames() 261cdf0e10cSrcweir { 262cdf0e10cSrcweir return m_supported_services; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir } 265cdf0e10cSrcweir 266