Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / javaunohelper / com / sun / star / lib / uno / helper / Factory.java
blob0780f26486f2025e8a356f7837cd8a03418683e8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package com.sun.star.lib.uno.helper;
29 import com.sun.star.uno.XComponentContext;
30 import com.sun.star.lang.XSingleComponentFactory;
31 import com.sun.star.lang.XServiceInfo;
32 import com.sun.star.lang.XInitialization;
33 import com.sun.star.registry.XRegistryKey;
35 import com.sun.star.uno.UnoRuntime;
38 /** Factory helper class supporting com.sun.star.lang.XServiceInfo and
39 com.sun.star.lang.XSingleComponentFactory.
41 @attention
42 This factory implementation does not support lang.XSingleServiceFactory.
44 public class Factory
45 extends ComponentBase
46 implements XSingleComponentFactory, XServiceInfo
48 private static final boolean DEBUG = false;
50 /** Creates an object factory supporting interfaces
51 com.sun.star.lang.XSingleComponentFactory and
52 com.sun.star.lang.XServiceInfo
54 @param impl_class
55 implementation class
56 @param impl_name
57 implementation name
58 @param supported_services
59 services implemented
60 @return
61 object factory
63 @since UDK 3.2.13
65 public static XSingleComponentFactory createComponentFactory(
66 Class impl_class, String impl_name, String supported_services [] )
67 throws com.sun.star.uno.RuntimeException
69 return new Factory( impl_class, impl_name, supported_services );
72 /** Creates an object factory supporting interfaces
73 com.sun.star.lang.XSingleComponentFactory and
74 com.sun.star.lang.XServiceInfo
76 The implementation name is the name of the implementation class.
78 @param impl_class
79 implementation class
80 @param supported_services
81 services implemented
82 @return
83 object factory
85 public static XSingleComponentFactory createComponentFactory(
86 Class impl_class, String supported_services [] )
87 throws com.sun.star.uno.RuntimeException
89 return createComponentFactory(
90 impl_class, impl_class.getName(), supported_services );
92 /** Writes component's implementation info to given registry key.
94 @param impl_name
95 name of implementation
96 @param supported_services
97 supported services of implementation
98 @param xKey
99 registry key to write to
100 @return
101 success
103 public static boolean writeRegistryServiceInfo(
104 String impl_name, String supported_services [], XRegistryKey xKey )
108 XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" );
109 for ( int nPos = 0; nPos < supported_services.length; ++nPos )
111 xNewKey.createKey( supported_services[ nPos ] );
113 return true;
115 catch (com.sun.star.registry.InvalidRegistryException exc)
117 if (DEBUG)
119 System.err.println(
120 "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " +
121 exc.toString() );
124 return false;
127 //==============================================================================================
128 private String m_impl_name;
129 private String [] m_supported_services;
130 private Class m_impl_class;
131 private java.lang.reflect.Method m_method;
132 private java.lang.reflect.Constructor m_ctor;
134 private Factory(
135 Class impl_class, String impl_name, String supported_services [] )
137 m_impl_name = impl_name;
138 m_supported_services = supported_services;
139 m_impl_class = impl_class;
140 m_method = null;
141 m_ctor = null;
143 Class params [] = new Class [] { XComponentContext.class };
147 // seeking for "public static Object __create( XComponentContext )"
148 m_method = m_impl_class.getMethod( "__create", params );
149 int mod = m_method.getModifiers();
150 if (!m_method.getReturnType().equals( Object.class ) ||
151 !java.lang.reflect.Modifier.isStatic( mod ) ||
152 !java.lang.reflect.Modifier.isPublic( mod ))
154 m_method = null;
157 catch (Exception exc)
161 if (null == m_method)
165 // ctor with context
166 m_ctor = m_impl_class.getConstructor( params );
168 catch (Exception exc)
170 // else take default ctor
175 //______________________________________________________________________________________________
176 private final Object instantiate( XComponentContext xContext )
177 throws com.sun.star.uno.Exception
181 if (DEBUG)
182 System.out.print( "instantiating " + m_impl_class.toString() + " using " );
183 if (null != m_method)
185 if (DEBUG)
186 System.out.println( "__create( XComponentContext )..." );
187 return m_method.invoke( null, new Object [] { xContext } );
189 if (null != m_ctor)
191 if (DEBUG)
192 System.out.println( "ctor( XComponentContext )..." );
193 return m_ctor.newInstance( new Object [] { xContext } );
195 if (DEBUG)
196 System.out.println( "default ctor..." );
197 return m_impl_class.newInstance(); // default ctor
199 catch (java.lang.reflect.InvocationTargetException exc)
201 Throwable targetException = exc.getTargetException();
202 if (targetException instanceof java.lang.RuntimeException)
203 throw (java.lang.RuntimeException)targetException;
204 else if (targetException instanceof com.sun.star.uno.RuntimeException)
205 throw (com.sun.star.uno.RuntimeException)targetException;
206 else if (targetException instanceof com.sun.star.uno.Exception)
207 throw (com.sun.star.uno.Exception)targetException;
208 else
209 throw new com.sun.star.uno.Exception( targetException.toString(), this );
211 catch (IllegalAccessException exc)
213 throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
215 catch (InstantiationException exc)
217 throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
220 // XSingleComponentFactory impl
221 //______________________________________________________________________________________________
222 public final Object createInstanceWithContext(
223 XComponentContext xContext )
224 throws com.sun.star.uno.Exception
226 return instantiate( xContext );
228 //______________________________________________________________________________________________
229 public final Object createInstanceWithArgumentsAndContext(
230 Object arguments [], XComponentContext xContext )
231 throws com.sun.star.uno.Exception
233 Object inst = instantiate( xContext );
234 XInitialization xInit = UnoRuntime.queryInterface(
235 XInitialization.class, inst );
236 if (null == xInit)
238 throw new com.sun.star.lang.IllegalArgumentException(
239 "cannot pass arguments to component; no XInitialization implemented!", this,
240 (short)0 );
242 xInit.initialize( arguments );
243 return inst;
246 // XServiceInfo impl
247 //______________________________________________________________________________________________
248 public final String getImplementationName()
250 return m_impl_name;
252 //______________________________________________________________________________________________
253 public final boolean supportsService( String service_name )
255 for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
257 if (m_supported_services[ nPos ].equals( service_name ))
258 return true;
260 return false;
262 //______________________________________________________________________________________________
263 public final String [] getSupportedServiceNames()
265 return m_supported_services;