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
;
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
49 public class FactoryHelper
{
51 private static final boolean DEBUG
= false;
53 static protected class Factory
54 implements XSingleServiceFactory
, XSingleComponentFactory
, XServiceInfo
,
57 protected XMultiServiceFactory _xMultiServiceFactory
;
58 protected XRegistryKey _xRegistryKey
;
60 protected Constructor
<?
> _constructor
;
61 protected String _implName
;
62 protected String _serviceName
;
64 protected Factory(Class
<?
> implClass
,
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)) {
83 _constructor
= constructors
[i
];
85 else if(parameters
.length
== 2
86 && parameters
[0].equals(XComponentContext
.class)
87 && parameters
[1].equals(XRegistryKey
.class)) {
89 _constructor
= constructors
[i
];
91 else if(parameters
.length
== 2
92 && parameters
[0].equals(XComponentContext
.class)
93 && parameters
[1].equals(Object
[].class)) {
95 _constructor
= constructors
[i
];
97 else if(parameters
.length
== 1
98 && parameters
[0].equals(XComponentContext
.class)) {
100 _constructor
= constructors
[i
];
103 else if(parameters
.length
== 3
104 && parameters
[0].equals(XMultiServiceFactory
.class)
105 && parameters
[1].equals(XRegistryKey
.class)
106 && parameters
[2].equals(Object
[].class)) {
108 _constructor
= constructors
[i
];
110 else if(parameters
.length
== 2
111 && parameters
[0].equals(XMultiServiceFactory
.class)
112 && parameters
[1].equals(XRegistryKey
.class)) {
114 _constructor
= constructors
[i
];
116 else if(parameters
.length
== 2
117 && parameters
[0].equals(XMultiServiceFactory
.class)
118 && parameters
[1].equals(Object
[].class)) {
120 _constructor
= constructors
[i
];
122 else if(parameters
.length
== 1
123 && parameters
[0].equals(XMultiServiceFactory
.class)) {
125 _constructor
= constructors
[i
];
127 else if(parameters
.length
== 1
128 && parameters
[0].equals(Object
[].class)) {
130 _constructor
= constructors
[i
];
132 else if(parameters
.length
== 0) {
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() );
151 return _xMultiServiceFactory
;
155 // XComponentContext impl
157 public Object
createInstanceWithContext(
158 XComponentContext xContext
)
159 throws com
.sun
.star
.uno
.Exception
165 args
= new Object
[] { xContext
, _xRegistryKey
, new Object
[ 0 ] };
168 args
= new Object
[] { xContext
, _xRegistryKey
};
171 args
= new Object
[] { xContext
, new Object
[ 0 ] };
174 args
= new Object
[] { xContext
};
177 args
= new Object
[] { getSMgr( xContext
), _xRegistryKey
, new Object
[ 0 ] };
180 args
= new Object
[] { getSMgr( xContext
), _xRegistryKey
};
183 args
= new Object
[] { getSMgr( xContext
), new Object
[ 0 ] };
186 args
= new Object
[] { getSMgr( xContext
) };
189 args
= new Object
[] { new Object
[ 0 ] };
192 args
= new Object
[ 0 ];
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
;
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
222 boolean bInitCall
= true;
226 args
= new Object
[] { xContext
, _xRegistryKey
, rArguments
};
230 args
= new Object
[] { xContext
, _xRegistryKey
};
233 args
= new Object
[] { xContext
, rArguments
};
237 args
= new Object
[] { xContext
};
240 args
= new Object
[] { getSMgr( xContext
), _xRegistryKey
, rArguments
};
244 args
= new Object
[] { getSMgr( xContext
), _xRegistryKey
};
247 args
= new Object
[] { getSMgr( xContext
), rArguments
};
251 args
= new Object
[] { getSMgr( xContext
) };
254 args
= new Object
[] { rArguments
};
258 args
= new Object
[ 0 ];
263 Object instance
= _constructor
.newInstance( args
);
266 XInitialization xInitialization
= UnoRuntime
.queryInterface(
267 XInitialization
.class, instance
);
268 if (xInitialization
!= null)
270 xInitialization
.initialize( rArguments
);
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
;
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
{
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
);
358 public byte[] getImplementationId()
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)
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
,
391 XSingleServiceFactory xSingleServiceFactory
= null;
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
,
425 XMultiServiceFactory multiFactory
,
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.
450 * @see com.sun.star.lang.XServiceInfo
452 static public boolean writeRegistryServiceInfo(String implName
, String serviceName
, XRegistryKey regKey
) {
453 boolean result
= false;
456 XRegistryKey newKey
= regKey
.createKey("/" + implName
+ "/UNO/SERVICES");
458 newKey
.createKey(serviceName
);
462 catch (Exception ex
) {
463 System
.err
.println(">>>Connection_Impl.writeRegistryServiceInfo " + ex
);
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.
479 public static boolean writeRegistryServiceInfo(
480 String impl_name
, String supported_services
[], XRegistryKey xKey
)
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
] );
488 } catch (com
.sun
.star
.registry
.InvalidRegistryException exc
) {
491 "##### " + Factory
.class.getName() + ".writeRegistryServiceInfo -- exc: " +