xref: /AOO41X/main/javaunohelper/com/sun/star/lib/uno/helper/Factory.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 package com.sun.star.lib.uno.helper;
28 
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;
34 
35 import com.sun.star.uno.UnoRuntime;
36 
37 
38 /** Factory helper class supporting com.sun.star.lang.XServiceInfo and
39     com.sun.star.lang.XSingleComponentFactory.
40 
41     @attention
42     This factory implementation does not support lang.XSingleServiceFactory.
43 */
44 public class Factory
45     extends ComponentBase
46     implements XSingleComponentFactory, XServiceInfo
47 {
48     private static final boolean DEBUG = false;
49 
50     /** Creates an object factory supporting interfaces
51         com.sun.star.lang.XSingleComponentFactory and
52         com.sun.star.lang.XServiceInfo
53 
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
62 
63         @since UDK 3.2.13
64     */
65     public static XSingleComponentFactory createComponentFactory(
66         Class impl_class, String impl_name, String supported_services [] )
67         throws com.sun.star.uno.RuntimeException
68     {
69         return new Factory( impl_class, impl_name, supported_services );
70     }
71 
72     /** Creates an object factory supporting interfaces
73         com.sun.star.lang.XSingleComponentFactory and
74         com.sun.star.lang.XServiceInfo
75 
76         The implementation name is the name of the implementation class.
77 
78         @param impl_class
79                implementation class
80         @param supported_services
81                services implemented
82         @return
83                 object factory
84     */
85     public static XSingleComponentFactory createComponentFactory(
86         Class impl_class, String supported_services [] )
87         throws com.sun.star.uno.RuntimeException
88     {
89         return createComponentFactory(
90             impl_class, impl_class.getName(), supported_services );
91     }
92     /** Writes component's implementation info to given registry key.
93 
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
102     */
103 	public static boolean writeRegistryServiceInfo(
104         String impl_name, String supported_services [], XRegistryKey xKey )
105     {
106   	    try
107         {
108 	        XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" );
109             for ( int nPos = 0; nPos < supported_services.length; ++nPos )
110             {
111                 xNewKey.createKey( supported_services[ nPos ] );
112             }
113 	        return true;
114   	    }
115   	    catch (com.sun.star.registry.InvalidRegistryException exc)
116         {
117             if (DEBUG)
118             {
119                 System.err.println(
120                     "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " +
121                     exc.toString() );
122             }
123   	    }
124 	    return false;
125     }
126 
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;
133 
134     private Factory(
135         Class impl_class, String impl_name, String supported_services [] )
136     {
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;
142 
143         Class params [] = new Class [] { XComponentContext.class };
144 
145         try
146         {
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 ))
153             {
154                 m_method = null;
155             }
156         }
157         catch (Exception exc)
158         {
159         }
160 
161         if (null == m_method)
162         {
163             try
164             {
165                 // ctor with context
166                 m_ctor = m_impl_class.getConstructor( params );
167             }
168             catch (Exception exc)
169             {
170                 // else take default ctor
171             }
172         }
173     }
174 
175     //______________________________________________________________________________________________
176     private final Object instantiate( XComponentContext xContext )
177         throws com.sun.star.uno.Exception
178     {
179         try
180         {
181             if (DEBUG)
182                 System.out.print( "instantiating " + m_impl_class.toString() + " using " );
183             if (null != m_method)
184             {
185                 if (DEBUG)
186                     System.out.println( "__create( XComponentContext )..." );
187                 return m_method.invoke( null, new Object [] { xContext } );
188             }
189             if (null != m_ctor)
190             {
191                 if (DEBUG)
192                     System.out.println( "ctor( XComponentContext )..." );
193                 return m_ctor.newInstance( new Object [] { xContext } );
194             }
195             if (DEBUG)
196                 System.out.println( "default ctor..." );
197             return m_impl_class.newInstance(); // default ctor
198         }
199         catch (java.lang.reflect.InvocationTargetException exc)
200         {
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 );
210         }
211         catch (IllegalAccessException exc)
212         {
213             throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
214         }
215         catch (InstantiationException exc)
216         {
217             throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
218         }
219     }
220     // XSingleComponentFactory impl
221     //______________________________________________________________________________________________
222     public final Object createInstanceWithContext(
223         XComponentContext xContext )
224         throws com.sun.star.uno.Exception
225     {
226         return instantiate( xContext );
227     }
228     //______________________________________________________________________________________________
229     public final Object createInstanceWithArgumentsAndContext(
230         Object arguments [], XComponentContext xContext )
231         throws com.sun.star.uno.Exception
232     {
233         Object inst = instantiate( xContext );
234         XInitialization xInit = UnoRuntime.queryInterface(
235             XInitialization.class, inst );
236         if (null == xInit)
237         {
238 			throw new com.sun.star.lang.IllegalArgumentException(
239                 "cannot pass arguments to component; no XInitialization implemented!", this,
240                 (short)0 );
241         }
242         xInit.initialize( arguments );
243         return inst;
244     }
245 
246     // XServiceInfo impl
247     //______________________________________________________________________________________________
248     public final String getImplementationName()
249     {
250         return m_impl_name;
251     }
252     //______________________________________________________________________________________________
253     public final boolean supportsService( String service_name )
254     {
255         for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
256         {
257             if (m_supported_services[ nPos ].equals( service_name ))
258                 return true;
259         }
260         return false;
261     }
262     //______________________________________________________________________________________________
263     public final String [] getSupportedServiceNames()
264     {
265         return m_supported_services;
266     }
267 }
268 
269