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