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 .
18 package com
.sun
.star
.lib
.uno
.helper
;
20 import com
.sun
.star
.uno
.XComponentContext
;
21 import com
.sun
.star
.lang
.XSingleComponentFactory
;
22 import com
.sun
.star
.lang
.XServiceInfo
;
23 import com
.sun
.star
.lang
.XInitialization
;
24 import com
.sun
.star
.registry
.XRegistryKey
;
25 import com
.sun
.star
.uno
.UnoRuntime
;
28 /** Factory helper class supporting com.sun.star.lang.XServiceInfo and
29 com.sun.star.lang.XSingleComponentFactory.
32 Note: This factory implementation does not support lang.XSingleServiceFactory.
37 implements XSingleComponentFactory
, XServiceInfo
39 private static final boolean DEBUG
= false;
41 /** Creates an object factory supporting interfaces
42 com.sun.star.lang.XSingleComponentFactory and
43 com.sun.star.lang.XServiceInfo
49 @param supported_services
56 public static XSingleComponentFactory
createComponentFactory(
57 Class impl_class
, String impl_name
, String supported_services
[] )
58 throws com
.sun
.star
.uno
.RuntimeException
60 return new Factory( impl_class
, impl_name
, supported_services
);
63 /** Creates an object factory supporting interfaces
64 com.sun.star.lang.XSingleComponentFactory and
65 com.sun.star.lang.XServiceInfo
67 The implementation name is the name of the implementation class.
71 @param supported_services
76 public static XSingleComponentFactory
createComponentFactory(
77 Class impl_class
, String supported_services
[] )
78 throws com
.sun
.star
.uno
.RuntimeException
80 return createComponentFactory(
81 impl_class
, impl_class
.getName(), supported_services
);
83 /** Writes component's implementation info to given registry key.
86 name of implementation
87 @param supported_services
88 supported services of implementation
90 registry key to write to
94 public static boolean writeRegistryServiceInfo(
95 String impl_name
, String supported_services
[], XRegistryKey xKey
)
99 XRegistryKey xNewKey
= xKey
.createKey( "/" + impl_name
+ "/UNO/SERVICES" );
100 for ( int nPos
= 0; nPos
< supported_services
.length
; ++nPos
)
102 xNewKey
.createKey( supported_services
[ nPos
] );
106 catch (com
.sun
.star
.registry
.InvalidRegistryException exc
)
111 "##### " + Factory
.class.getName() + ".writeRegistryServiceInfo -- exc: " +
119 private final String m_impl_name
;
120 private final String
[] m_supported_services
;
121 private final Class
<?
> m_impl_class
;
122 private final java
.lang
.reflect
.Method m_method
;
123 private final java
.lang
.reflect
.Constructor m_ctor
;
126 Class impl_class
, String impl_name
, String supported_services
[] )
127 throws com
.sun
.star
.uno
.DeploymentException
129 m_impl_name
= impl_name
;
130 m_supported_services
= supported_services
;
131 m_impl_class
= impl_class
;
133 Class params
[] = new Class
[] { XComponentContext
.class };
135 if (!java
.lang
.reflect
.Modifier
.isPublic( impl_class
.getModifiers() ))
137 throw new com
.sun
.star
.uno
.DeploymentException("class " + impl_class
+ " is not public");
140 java
.lang
.reflect
.Method tmpMethod
= null;
143 // seeking for "public static Object __create( XComponentContext )"
144 tmpMethod
= m_impl_class
.getMethod( "__create", params
);
145 int mod
= tmpMethod
.getModifiers();
146 if (!tmpMethod
.getReturnType().equals( Object
.class ) ||
147 !java
.lang
.reflect
.Modifier
.isStatic( mod
) ||
148 !java
.lang
.reflect
.Modifier
.isPublic( mod
))
153 catch (Exception exc
)
156 m_method
= tmpMethod
;
159 java
.lang
.reflect
.Constructor tmpCtor
= null;
160 if (null == m_method
)
165 tmpCtor
= m_impl_class
.getConstructor( params
);
167 catch (Exception exc
)
172 if (!java
.lang
.reflect
.Modifier
.isPublic( tmpCtor
.getModifiers() ))
174 throw new com
.sun
.star
.uno
.DeploymentException("constructor with XComponentContext param for class " + impl_class
+ " is not public");
179 // else take default ctor
180 java
.lang
.reflect
.Constructor defaultCtor
;
183 defaultCtor
= m_impl_class
.getConstructor(new Class
[0]);
185 catch (Exception exc2
)
187 throw new com
.sun
.star
.uno
.DeploymentException(exc2
, "class " + impl_class
+ " has no means of creating it, cannot find a __create method or a useful constructor.");
189 if (!java
.lang
.reflect
.Modifier
.isPublic( defaultCtor
.getModifiers() ))
191 throw new com
.sun
.star
.uno
.DeploymentException("default constructor for class " + impl_class
+ " is not public");
199 private Object
instantiate( XComponentContext xContext
)
200 throws com
.sun
.star
.uno
.Exception
205 System
.out
.print( "instantiating " + m_impl_class
.toString() + " using " );
206 if (null != m_method
)
209 System
.out
.println( "__create( XComponentContext )..." );
210 return m_method
.invoke( null, new Object
[] { xContext
} );
215 System
.out
.println( "ctor( XComponentContext )..." );
216 return m_ctor
.newInstance( new Object
[] { xContext
} );
219 System
.out
.println( "default ctor..." );
220 return m_impl_class
.newInstance(); // default ctor
222 catch (java
.lang
.reflect
.InvocationTargetException exc
)
224 Throwable targetException
= exc
.getTargetException();
225 if (targetException
instanceof java
.lang
.RuntimeException
)
226 throw (java
.lang
.RuntimeException
)targetException
;
227 else if (targetException
instanceof com
.sun
.star
.uno
.RuntimeException
)
228 throw (com
.sun
.star
.uno
.RuntimeException
)targetException
;
229 else if (targetException
instanceof com
.sun
.star
.uno
.Exception
)
230 throw (com
.sun
.star
.uno
.Exception
)targetException
;
232 throw new com
.sun
.star
.uno
.Exception(targetException
, targetException
.getMessage(), this);
234 catch (IllegalAccessException exc
)
236 throw new com
.sun
.star
.uno
.RuntimeException( exc
, exc
.getMessage(), this);
238 catch (InstantiationException exc
)
240 throw new com
.sun
.star
.uno
.RuntimeException( exc
, exc
.getMessage(), this);
243 // XSingleComponentFactory impl
245 public final Object
createInstanceWithContext(
246 XComponentContext xContext
)
247 throws com
.sun
.star
.uno
.Exception
249 return instantiate( xContext
);
252 public final Object
createInstanceWithArgumentsAndContext(
253 Object arguments
[], XComponentContext xContext
)
254 throws com
.sun
.star
.uno
.Exception
256 Object inst
= instantiate( xContext
);
257 XInitialization xInit
= UnoRuntime
.queryInterface(
258 XInitialization
.class, inst
);
261 throw new com
.sun
.star
.lang
.IllegalArgumentException(
262 "cannot pass arguments to component; no XInitialization implemented!", this,
265 xInit
.initialize( arguments
);
271 public final String
getImplementationName()
276 public final boolean supportsService( String service_name
)
278 for ( int nPos
= 0; nPos
< m_supported_services
.length
; ++nPos
)
280 if (m_supported_services
[ nPos
].equals( service_name
))
286 public final String
[] getSupportedServiceNames()
288 return m_supported_services
;