merge the formfield patch from ooo-build
[ooovba.git] / javaunohelper / com / sun / star / lib / uno / helper / Factory.java
blob8f2cd60b00a7d2b59bc46af0363bc8c67d734ea4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Factory.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package com.sun.star.lib.uno.helper;
32 import com.sun.star.uno.XComponentContext;
33 import com.sun.star.lang.XSingleComponentFactory;
34 import com.sun.star.lang.XServiceInfo;
35 import com.sun.star.lang.XInitialization;
36 import com.sun.star.registry.XRegistryKey;
38 import com.sun.star.uno.UnoRuntime;
41 /** Factory helper class supporting com.sun.star.lang.XServiceInfo and
42 com.sun.star.lang.XSingleComponentFactory.
44 @attention
45 This factory implementation does not support lang.XSingleServiceFactory.
47 public class Factory
48 extends ComponentBase
49 implements XSingleComponentFactory, XServiceInfo
51 private static final boolean DEBUG = false;
53 /** Creates an object factory supporting interfaces
54 com.sun.star.lang.XSingleComponentFactory and
55 com.sun.star.lang.XServiceInfo
57 @param impl_class
58 implementation class
59 @param supported_services
60 services implemented
61 @return
62 object factory
64 public static XSingleComponentFactory createComponentFactory(
65 Class impl_class, String supported_services [] )
66 throws com.sun.star.uno.RuntimeException
68 return new Factory( impl_class, supported_services );
70 /** Writes component's implementation info to given registry key.
72 @param impl_name
73 name of implementation
74 @param supported_services
75 supported services of implementation
76 @param xKey
77 registry key to write to
78 @return
79 success
81 public static boolean writeRegistryServiceInfo(
82 String impl_name, String supported_services [], XRegistryKey xKey )
84 try
86 XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" );
87 for ( int nPos = 0; nPos < supported_services.length; ++nPos )
89 xNewKey.createKey( supported_services[ nPos ] );
91 return true;
93 catch (com.sun.star.registry.InvalidRegistryException exc)
95 if (DEBUG)
97 System.err.println(
98 "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " +
99 exc.toString() );
102 return false;
105 //==============================================================================================
106 private String m_impl_name;
107 private String [] m_supported_services;
108 private Class m_impl_class;
109 private java.lang.reflect.Method m_method;
110 private java.lang.reflect.Constructor m_ctor;
112 // ctor
113 private Factory( Class impl_class, String supported_services [] )
114 throws com.sun.star.uno.RuntimeException
116 m_impl_name = impl_class.getName();
117 m_supported_services = supported_services;
118 m_impl_class = impl_class;
119 m_method = null;
120 m_ctor = null;
122 Class params [] = new Class [] { XComponentContext.class };
126 // seeking for "public static Object __create( XComponentContext )"
127 m_method = m_impl_class.getMethod( "__create", params );
128 int mod = m_method.getModifiers();
129 if (!m_method.getReturnType().equals( Object.class ) ||
130 !java.lang.reflect.Modifier.isStatic( mod ) ||
131 !java.lang.reflect.Modifier.isPublic( mod ))
133 m_method = null;
136 catch (Exception exc)
140 if (null == m_method)
144 // ctor with context
145 m_ctor = m_impl_class.getConstructor( params );
147 catch (Exception exc)
149 // else take default ctor
154 //______________________________________________________________________________________________
155 private final Object instantiate( XComponentContext xContext )
156 throws com.sun.star.uno.Exception
160 if (DEBUG)
161 System.out.print( "instantiating " + m_impl_class.toString() + " using " );
162 if (null != m_method)
164 if (DEBUG)
165 System.out.println( "__create( XComponentContext )..." );
166 return m_method.invoke( null, new Object [] { xContext } );
168 if (null != m_ctor)
170 if (DEBUG)
171 System.out.println( "ctor( XComponentContext )..." );
172 return m_ctor.newInstance( new Object [] { xContext } );
174 if (DEBUG)
175 System.out.println( "default ctor..." );
176 return m_impl_class.newInstance(); // default ctor
178 catch (java.lang.reflect.InvocationTargetException exc)
180 Throwable targetException = exc.getTargetException();
181 if (targetException instanceof java.lang.RuntimeException)
182 throw (java.lang.RuntimeException)targetException;
183 else if (targetException instanceof com.sun.star.uno.RuntimeException)
184 throw (com.sun.star.uno.RuntimeException)targetException;
185 else if (targetException instanceof com.sun.star.uno.Exception)
186 throw (com.sun.star.uno.Exception)targetException;
187 else
188 throw new com.sun.star.uno.Exception( targetException.toString(), this );
190 catch (IllegalAccessException exc)
192 throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
194 catch (InstantiationException exc)
196 throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
199 // XSingleComponentFactory impl
200 //______________________________________________________________________________________________
201 public final Object createInstanceWithContext(
202 XComponentContext xContext )
203 throws com.sun.star.uno.Exception
205 return instantiate( xContext );
207 //______________________________________________________________________________________________
208 public final Object createInstanceWithArgumentsAndContext(
209 Object arguments [], XComponentContext xContext )
210 throws com.sun.star.uno.Exception
212 Object inst = instantiate( xContext );
213 XInitialization xInit = UnoRuntime.queryInterface(
214 XInitialization.class, inst );
215 if (null == xInit)
217 throw new com.sun.star.lang.IllegalArgumentException(
218 "cannot pass arguments to component; no XInitialization implemented!", this,
219 (short)0 );
221 xInit.initialize( arguments );
222 return inst;
225 // XServiceInfo impl
226 //______________________________________________________________________________________________
227 public final String getImplementationName()
229 return m_impl_name;
231 //______________________________________________________________________________________________
232 public final boolean supportsService( String service_name )
234 for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
236 if (m_supported_services[ nPos ].equals( service_name ))
237 return true;
239 return false;
241 //______________________________________________________________________________________________
242 public final String [] getSupportedServiceNames()
244 return m_supported_services;