1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.lib.uno.helper;
24 
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.lang.XSingleComponentFactory;
27 import com.sun.star.lang.XServiceInfo;
28 import com.sun.star.lang.XInitialization;
29 import com.sun.star.registry.XRegistryKey;
30 
31 import com.sun.star.uno.UnoRuntime;
32 
33 
34 /** Factory helper class supporting com.sun.star.lang.XServiceInfo and
35     com.sun.star.lang.XSingleComponentFactory.
36 
37     Attention:
38     <br>
39     This factory implementation does not support lang.XSingleServiceFactory.
40 */
41 public class Factory
42     extends ComponentBase
43     implements XSingleComponentFactory, XServiceInfo
44 {
45     private static final boolean DEBUG = false;
46 
47     /** Creates an object factory supporting interfaces
48         com.sun.star.lang.XSingleComponentFactory and
49         com.sun.star.lang.XServiceInfo
50 
51         @param impl_class
52                implementation class
53         @param impl_name
54                implementation name
55         @param supported_services
56                services implemented
57         @return
58                object factory
59 
60         @since UDK 3.2.13
61     */
createComponentFactory( Class impl_class, String impl_name, String supported_services [] )62     public static XSingleComponentFactory createComponentFactory(
63         Class impl_class, String impl_name, String supported_services [] )
64         throws com.sun.star.uno.RuntimeException
65     {
66         return new Factory( impl_class, impl_name, supported_services );
67     }
68 
69     /** Creates an object factory supporting interfaces
70         com.sun.star.lang.XSingleComponentFactory and
71         com.sun.star.lang.XServiceInfo
72 
73         The implementation name is the name of the implementation class.
74 
75         @param impl_class
76                implementation class
77         @param supported_services
78                services implemented
79         @return
80                 object factory
81     */
createComponentFactory( Class impl_class, String supported_services [] )82     public static XSingleComponentFactory createComponentFactory(
83         Class impl_class, String supported_services [] )
84         throws com.sun.star.uno.RuntimeException
85     {
86         return createComponentFactory(
87             impl_class, impl_class.getName(), supported_services );
88     }
89     /** Writes component's implementation info to given registry key.
90 
91         @param impl_name
92                name of implementation
93         @param supported_services
94                supported services of implementation
95         @param xKey
96                registry key to write to
97         @return
98                 success
99     */
writeRegistryServiceInfo( String impl_name, String supported_services [], XRegistryKey xKey )100 	public static boolean writeRegistryServiceInfo(
101         String impl_name, String supported_services [], XRegistryKey xKey )
102     {
103   	    try
104         {
105 	        XRegistryKey xNewKey = xKey.createKey( "/" + impl_name + "/UNO/SERVICES" );
106             for ( int nPos = 0; nPos < supported_services.length; ++nPos )
107             {
108                 xNewKey.createKey( supported_services[ nPos ] );
109             }
110 	        return true;
111   	    }
112   	    catch (com.sun.star.registry.InvalidRegistryException exc)
113         {
114             if (DEBUG)
115             {
116                 System.err.println(
117                     "##### " + Factory.class.getName() + ".writeRegistryServiceInfo -- exc: " +
118                     exc.toString() );
119             }
120   	    }
121 	    return false;
122     }
123 
124     //==============================================================================================
125     private String m_impl_name;
126     private String [] m_supported_services;
127     private Class m_impl_class;
128     private java.lang.reflect.Method m_method;
129     private java.lang.reflect.Constructor m_ctor;
130 
Factory( Class impl_class, String impl_name, String supported_services [] )131     private Factory(
132         Class impl_class, String impl_name, String supported_services [] )
133     {
134         m_impl_name = impl_name;
135         m_supported_services = supported_services;
136         m_impl_class = impl_class;
137         m_method = null;
138         m_ctor = null;
139 
140         Class params [] = new Class [] { XComponentContext.class };
141 
142         try
143         {
144             // seeking for "public static Object __create( XComponentContext )"
145             m_method = m_impl_class.getMethod( "__create", params );
146             int mod = m_method.getModifiers();
147             if (!m_method.getReturnType().equals( Object.class ) ||
148                 !java.lang.reflect.Modifier.isStatic( mod ) ||
149                 !java.lang.reflect.Modifier.isPublic( mod ))
150             {
151                 m_method = null;
152             }
153         }
154         catch (Exception exc)
155         {
156         }
157 
158         if (null == m_method)
159         {
160             try
161             {
162                 // ctor with context
163                 m_ctor = m_impl_class.getConstructor( params );
164             }
165             catch (Exception exc)
166             {
167                 // else take default ctor
168             }
169         }
170     }
171 
172     //______________________________________________________________________________________________
instantiate( XComponentContext xContext )173     private final Object instantiate( XComponentContext xContext )
174         throws com.sun.star.uno.Exception
175     {
176         try
177         {
178             if (DEBUG)
179                 System.out.print( "instantiating " + m_impl_class.toString() + " using " );
180             if (null != m_method)
181             {
182                 if (DEBUG)
183                     System.out.println( "__create( XComponentContext )..." );
184                 return m_method.invoke( null, new Object [] { xContext } );
185             }
186             if (null != m_ctor)
187             {
188                 if (DEBUG)
189                     System.out.println( "ctor( XComponentContext )..." );
190                 return m_ctor.newInstance( new Object [] { xContext } );
191             }
192             if (DEBUG)
193                 System.out.println( "default ctor..." );
194             return m_impl_class.newInstance(); // default ctor
195         }
196         catch (java.lang.reflect.InvocationTargetException exc)
197         {
198             Throwable targetException = exc.getTargetException();
199             if (targetException instanceof java.lang.RuntimeException)
200                 throw (java.lang.RuntimeException)targetException;
201             else if (targetException instanceof com.sun.star.uno.RuntimeException)
202                 throw (com.sun.star.uno.RuntimeException)targetException;
203             else if (targetException instanceof com.sun.star.uno.Exception)
204                 throw (com.sun.star.uno.Exception)targetException;
205             else
206                 throw new com.sun.star.uno.Exception( targetException.toString(), this );
207         }
208         catch (IllegalAccessException exc)
209         {
210             throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
211         }
212         catch (InstantiationException exc)
213         {
214             throw new com.sun.star.uno.RuntimeException( exc.toString(), this );
215         }
216     }
217     // XSingleComponentFactory impl
218     //______________________________________________________________________________________________
createInstanceWithContext( XComponentContext xContext )219     public final Object createInstanceWithContext(
220         XComponentContext xContext )
221         throws com.sun.star.uno.Exception
222     {
223         return instantiate( xContext );
224     }
225     //______________________________________________________________________________________________
createInstanceWithArgumentsAndContext( Object arguments [], XComponentContext xContext )226     public final Object createInstanceWithArgumentsAndContext(
227         Object arguments [], XComponentContext xContext )
228         throws com.sun.star.uno.Exception
229     {
230         Object inst = instantiate( xContext );
231         XInitialization xInit = UnoRuntime.queryInterface(
232             XInitialization.class, inst );
233         if (null == xInit)
234         {
235 			throw new com.sun.star.lang.IllegalArgumentException(
236                 "cannot pass arguments to component; no XInitialization implemented!", this,
237                 (short)0 );
238         }
239         xInit.initialize( arguments );
240         return inst;
241     }
242 
243     // XServiceInfo impl
244     //______________________________________________________________________________________________
getImplementationName()245     public final String getImplementationName()
246     {
247         return m_impl_name;
248     }
249     //______________________________________________________________________________________________
supportsService( String service_name )250     public final boolean supportsService( String service_name )
251     {
252         for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
253         {
254             if (m_supported_services[ nPos ].equals( service_name ))
255                 return true;
256         }
257         return false;
258     }
259     //______________________________________________________________________________________________
getSupportedServiceNames()260     public final String [] getSupportedServiceNames()
261     {
262         return m_supported_services;
263     }
264 }
265 
266