bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / comp / loader / FactoryHelper.java
blob3fdbac4871803a63ba8a0448d81d3fcade04dfee
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.comp.loader;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Field;
23 import java.lang.reflect.InvocationTargetException;
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.lang.XInitialization;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.lang.XServiceInfo;
29 import com.sun.star.lang.XSingleServiceFactory;
30 import com.sun.star.lang.XSingleComponentFactory;
31 import com.sun.star.lang.XTypeProvider;
32 import com.sun.star.registry.XRegistryKey;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.Type;
37 /**
38 * The purpose of this class to help component implementation.
40 * <p>This class has default implementations for <code>getServiceFactory</code>
41 * and <code>writeRegistryServiceInfo</code>.</p>
43 * @see com.sun.star.lang.XMultiServiceFactory
44 * @see com.sun.star.lang.XServiceInfo
45 * @see com.sun.star.lang.XSingleServiceFactory
46 * @see com.sun.star.registry.XRegistryKey
47 * @since UDK1.0
49 public class FactoryHelper {
51 private static final boolean DEBUG = false;
52 // the factory
53 static protected class Factory
54 implements XSingleServiceFactory, XSingleComponentFactory, XServiceInfo,
55 XTypeProvider {
57 protected XMultiServiceFactory _xMultiServiceFactory;
58 protected XRegistryKey _xRegistryKey;
59 protected int _nCode;
60 protected Constructor<?> _constructor;
61 protected String _implName;
62 protected String _serviceName;
64 protected Factory(Class<?> implClass,
65 String serviceName,
66 XMultiServiceFactory xMultiServiceFactory,
67 XRegistryKey xRegistryKey)
69 _xMultiServiceFactory = xMultiServiceFactory;
70 _xRegistryKey = xRegistryKey;
71 _implName = implClass.getName();
72 _serviceName = serviceName;
74 Constructor<?> constructors[] = implClass.getConstructors();
75 for(int i = 0; i < constructors.length && _constructor == null; ++i) {
76 Class<?> parameters[] = constructors[i].getParameterTypes();
78 if(parameters.length == 3
79 && parameters[0].equals(XComponentContext.class)
80 && parameters[1].equals(XRegistryKey.class)
81 && parameters[2].equals(Object[].class)) {
82 _nCode = 0;
83 _constructor = constructors[i];
85 else if(parameters.length == 2
86 && parameters[0].equals(XComponentContext.class)
87 && parameters[1].equals(XRegistryKey.class)) {
88 _nCode = 1;
89 _constructor = constructors[i];
91 else if(parameters.length == 2
92 && parameters[0].equals(XComponentContext.class)
93 && parameters[1].equals(Object[].class)) {
94 _nCode = 2;
95 _constructor = constructors[i];
97 else if(parameters.length == 1
98 && parameters[0].equals(XComponentContext.class)) {
99 _nCode = 3;
100 _constructor = constructors[i];
102 // depr
103 else if(parameters.length == 3
104 && parameters[0].equals(XMultiServiceFactory.class)
105 && parameters[1].equals(XRegistryKey.class)
106 && parameters[2].equals(Object[].class)) {
107 _nCode = 4;
108 _constructor = constructors[i];
110 else if(parameters.length == 2
111 && parameters[0].equals(XMultiServiceFactory.class)
112 && parameters[1].equals(XRegistryKey.class)) {
113 _nCode = 5;
114 _constructor = constructors[i];
116 else if(parameters.length == 2
117 && parameters[0].equals(XMultiServiceFactory.class)
118 && parameters[1].equals(Object[].class)) {
119 _nCode = 6;
120 _constructor = constructors[i];
122 else if(parameters.length == 1
123 && parameters[0].equals(XMultiServiceFactory.class)) {
124 _nCode = 7;
125 _constructor = constructors[i];
127 else if(parameters.length == 1
128 && parameters[0].equals(Object[].class)) {
129 _nCode = 8;
130 _constructor = constructors[i];
132 else if(parameters.length == 0) {
133 _nCode = 9;
134 _constructor = constructors[i];
138 if(_constructor == null) // have not found a useable constructor
139 throw new com.sun.star.uno.RuntimeException(getClass().getName() + " can not find a useable constructor");
142 private final XMultiServiceFactory getSMgr( XComponentContext xContext )
144 if (xContext != null)
146 return UnoRuntime.queryInterface(
147 XMultiServiceFactory.class, xContext.getServiceManager() );
149 else
151 return _xMultiServiceFactory;
155 // XComponentContext impl
157 public Object createInstanceWithContext(
158 XComponentContext xContext )
159 throws com.sun.star.uno.Exception
161 Object args[];
162 switch (_nCode)
164 case 0:
165 args = new Object [] { xContext, _xRegistryKey, new Object[ 0 ] };
166 break;
167 case 1:
168 args = new Object [] { xContext, _xRegistryKey };
169 break;
170 case 2:
171 args = new Object [] { xContext, new Object[ 0 ] };
172 break;
173 case 3:
174 args = new Object [] { xContext };
175 break;
176 case 4:
177 args = new Object [] { getSMgr( xContext ), _xRegistryKey, new Object[ 0 ] };
178 break;
179 case 5:
180 args = new Object [] { getSMgr( xContext ), _xRegistryKey };
181 break;
182 case 6:
183 args = new Object [] { getSMgr( xContext ), new Object[ 0 ] };
184 break;
185 case 7:
186 args = new Object [] { getSMgr( xContext ) };
187 break;
188 case 8:
189 args = new Object [] { new Object[ 0 ] };
190 break;
191 default:
192 args = new Object [ 0 ];
193 break;
196 try {
197 return _constructor.newInstance( args );
198 } catch (InvocationTargetException invocationTargetException) {
199 Throwable targetException = invocationTargetException.getCause();
201 if (targetException instanceof java.lang.RuntimeException)
202 throw (java.lang.RuntimeException)targetException;
203 else if (targetException instanceof com.sun.star.uno.Exception)
204 throw (com.sun.star.uno.Exception)targetException;
205 else if (targetException instanceof com.sun.star.uno.RuntimeException)
206 throw (com.sun.star.uno.RuntimeException)targetException;
207 else
208 throw new com.sun.star.uno.Exception( targetException );
209 } catch (IllegalAccessException illegalAccessException) {
210 throw new com.sun.star.uno.Exception( illegalAccessException );
211 } catch (InstantiationException instantiationException) {
212 throw new com.sun.star.uno.Exception( instantiationException );
216 public Object createInstanceWithArgumentsAndContext(
217 Object rArguments[], XComponentContext xContext )
218 throws com.sun.star.uno.Exception
220 Object args[];
222 boolean bInitCall = true;
223 switch (_nCode)
225 case 0:
226 args = new Object [] { xContext, _xRegistryKey, rArguments };
227 bInitCall = false;
228 break;
229 case 1:
230 args = new Object [] { xContext, _xRegistryKey };
231 break;
232 case 2:
233 args = new Object [] { xContext, rArguments };
234 bInitCall = false;
235 break;
236 case 3:
237 args = new Object [] { xContext };
238 break;
239 case 4:
240 args = new Object [] { getSMgr( xContext ), _xRegistryKey, rArguments };
241 bInitCall = false;
242 break;
243 case 5:
244 args = new Object [] { getSMgr( xContext ), _xRegistryKey };
245 break;
246 case 6:
247 args = new Object [] { getSMgr( xContext ), rArguments };
248 bInitCall = false;
249 break;
250 case 7:
251 args = new Object [] { getSMgr( xContext ) };
252 break;
253 case 8:
254 args = new Object [] { rArguments };
255 bInitCall = false;
256 break;
257 default:
258 args = new Object [ 0 ];
259 break;
262 try {
263 Object instance = _constructor.newInstance( args );
264 if (bInitCall)
266 XInitialization xInitialization = UnoRuntime.queryInterface(
267 XInitialization.class, instance );
268 if (xInitialization != null)
270 xInitialization.initialize( rArguments );
273 return instance;
274 } catch (InvocationTargetException invocationTargetException) {
275 Throwable targetException = invocationTargetException.getCause();
277 if (targetException instanceof java.lang.RuntimeException)
278 throw (java.lang.RuntimeException)targetException;
279 else if (targetException instanceof com.sun.star.uno.Exception)
280 throw (com.sun.star.uno.Exception)targetException;
281 else if (targetException instanceof com.sun.star.uno.RuntimeException)
282 throw (com.sun.star.uno.RuntimeException)targetException;
283 else
284 throw new com.sun.star.uno.Exception( targetException );
285 } catch (IllegalAccessException illegalAccessException) {
286 throw new com.sun.star.uno.Exception( illegalAccessException );
287 } catch (InstantiationException instantiationException) {
288 throw new com.sun.star.uno.Exception( instantiationException );
293 * Creates an instance of the desired service.
295 * @return returns an instance of the desired service.
296 * @see com.sun.star.lang.XSingleServiceFactory
298 public Object createInstance()
299 throws com.sun.star.uno.Exception,
300 com.sun.star.uno.RuntimeException
302 return createInstanceWithContext( null );
306 * Creates an instance of the desired service.
308 * @param args the args given to the constructor of the service.
309 * @return returns an instance of the desired service.
311 * @see com.sun.star.lang.XSingleServiceFactory
313 public Object createInstanceWithArguments(Object[] args)
314 throws com.sun.star.uno.Exception,
315 com.sun.star.uno.RuntimeException
317 return createInstanceWithArgumentsAndContext( args, null );
321 * Gives the supported services.
323 * @return returns an array of supported services.
324 * @see com.sun.star.lang.XServiceInfo
326 public String[] getSupportedServiceNames() throws com.sun.star.uno.RuntimeException {
327 return new String[]{_serviceName};
331 * Gives the implementation name.
333 * @return returns the implementation name.
334 * @see com.sun.star.lang.XServiceInfo
336 public String getImplementationName() throws com.sun.star.uno.RuntimeException {
337 return _implName;
341 * Indicates if the given service is supported.
343 * @return returns true if the given service is supported.
344 * @see com.sun.star.lang.XServiceInfo
346 public boolean supportsService(String serviceName) throws com.sun.star.uno.RuntimeException {
347 String services[] = getSupportedServiceNames();
349 boolean found = false;
351 for(int i = 0; i < services.length && !found; ++i)
352 found = services[i].equals(serviceName);
354 return found;
357 //XTypeProvider
358 public byte[] getImplementationId()
360 return new byte[0];
362 //XTypeProvider
363 public com.sun.star.uno.Type[] getTypes()
365 Type[] t = new Type[] {
366 new Type(XSingleServiceFactory.class),
367 new Type(XSingleComponentFactory.class),
368 new Type(XServiceInfo.class),
369 new Type(XTypeProvider.class)
371 return t;
377 * Creates a factory for the given class.
379 * @param implClass the implementing class.
380 * @param multiFactory the given multi service factory (service manager).
381 * @param regKey the given registry key.
382 * @return returns a factory.
384 * @see com.sun.star.lang.XServiceInfo
385 * @deprecated as of UDK 1.0
387 static public XSingleServiceFactory getServiceFactory(Class<?> implClass,
388 XMultiServiceFactory multiFactory,
389 XRegistryKey regKey)
391 XSingleServiceFactory xSingleServiceFactory = null;
393 try {
394 Field serviceName ;
396 try {
397 serviceName = implClass.getField("__serviceName");
398 } catch(NoSuchFieldException noSuchFieldExceptio) {
399 serviceName = implClass.getField("serviceName"); // old style
402 xSingleServiceFactory = new Factory(implClass, (String)serviceName.get(null), multiFactory, regKey);
403 } catch(NoSuchFieldException noSuchFieldException) {
404 System.err.println("##### FactoryHelper.getServiceFactory - exception:" + noSuchFieldException);
405 } catch(IllegalAccessException illegalAccessException) {
406 System.err.println("##### FactoryHelper.getServiceFactory - exception:" + illegalAccessException);
409 return xSingleServiceFactory;
413 * Creates a factory for the given class.
415 * @param implClass the implementing class.
416 * @param serviceName the service name of the implementing class.
417 * @param multiFactory the given multi service factory (service manager).
418 * @param regKey the given registry key.
420 * @return returns a factory.
421 * @see com.sun.star.lang.XServiceInfo
423 static public XSingleServiceFactory getServiceFactory(Class<?> implClass,
424 String serviceName,
425 XMultiServiceFactory multiFactory,
426 XRegistryKey regKey)
428 return new Factory(implClass, serviceName, multiFactory, regKey);
432 * Creates a factory for the given class.
434 * @param implClass the implementing class.
435 * @return returns a factory object.
437 static public Object createComponentFactory( Class<?> implClass, String serviceName )
439 return new Factory( implClass, serviceName, null, null );
443 * Writes the registration data into the registry key.
445 * @param implName the name of the implementing class.
446 * @param serviceName the service name.
447 * @param regKey the given registry key.
448 * @return success.
450 * @see com.sun.star.lang.XServiceInfo
452 static public boolean writeRegistryServiceInfo(String implName, String serviceName, XRegistryKey regKey) {
453 boolean result = false;
455 try {
456 XRegistryKey newKey = regKey.createKey("/" + implName + "/UNO/SERVICES");
458 newKey.createKey(serviceName);
460 result = true;
462 catch (Exception ex) {
463 System.err.println(">>>Connection_Impl.writeRegistryServiceInfo " + ex);
466 return result;
470 * Writes the registration data into the registry key.
472 * <p>Several services are supported.</p>
474 * @param impl_name name of implementation.
475 * @param supported_services supported services of implementation.
476 * @param xKey registry key to write to.
477 * @return success.
479 public static boolean writeRegistryServiceInfo(
480 String impl_name, String supported_services [], XRegistryKey xKey )
482 try {
483 XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" );
484 for ( int nPos = 0; nPos < supported_services.length; ++nPos ) {
485 xNewKey.createKey( supported_services[ nPos ] );
487 return true;
488 } catch (com.sun.star.registry.InvalidRegistryException exc) {
489 if (DEBUG) {
490 System.err.println(
491 "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " +
492 exc.toString() );
495 return false;