1*2be43276SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2be43276SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2be43276SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2be43276SAndrew Rist  * distributed with this work for additional information
6*2be43276SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2be43276SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2be43276SAndrew Rist  * "License"); you may not use this file except in compliance
9*2be43276SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2be43276SAndrew Rist  *
11*2be43276SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2be43276SAndrew Rist  *
13*2be43276SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2be43276SAndrew Rist  * software distributed under the License is distributed on an
15*2be43276SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2be43276SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2be43276SAndrew Rist  * specific language governing permissions and limitations
18*2be43276SAndrew Rist  * under the License.
19*2be43276SAndrew Rist  *
20*2be43276SAndrew Rist  *************************************************************/
21*2be43276SAndrew Rist 
22*2be43276SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.comp.loader;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.lang.reflect.Method;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import java.lang.reflect.InvocationTargetException;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import java.net.URLDecoder;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.loader.CannotActivateFactoryException;
33cdf0e10cSrcweir import com.sun.star.loader.XImplementationLoader;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.registry.CannotRegisterImplementationException;
36cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
39cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
40cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
41cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
42cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
45cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
46cdf0e10cSrcweir import com.sun.star.util.XMacroExpander;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir import com.sun.star.uno.Type;
49cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir import com.sun.star.lib.util.StringHelper;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /**
57cdf0e10cSrcweir  * The <code>JavaLoader</code> class provides the functionality of the <code>com.sun.star.loader.Java</code>
58cdf0e10cSrcweir  * service. Therefor the <code>JavaLoader</code> activates external UNO components which are implemented in Java.
59cdf0e10cSrcweir  * The loader is used by the <code>ServiceManger</code>.
60cdf0e10cSrcweir  * <p>
61cdf0e10cSrcweir  * @version 	$Revision: 1.16 $ $ $Date: 2008-04-11 11:10:31 $
62cdf0e10cSrcweir  * @author 	    Markus Herzog
63cdf0e10cSrcweir  * @see         com.sun.star.loader.XImplementationLoader
64cdf0e10cSrcweir  * @see         com.sun.star.loader.Java
65cdf0e10cSrcweir  * @see         com.sun.star.comp.servicemanager.ServiceManager
66cdf0e10cSrcweir  * @see			com.sun.star.lang.ServiceManager
67cdf0e10cSrcweir  * @since       UDK1.0
68cdf0e10cSrcweir  */
69cdf0e10cSrcweir public class JavaLoader implements XImplementationLoader,
70cdf0e10cSrcweir                                    XServiceInfo,
71cdf0e10cSrcweir                                    XInitialization
72cdf0e10cSrcweir {
73cdf0e10cSrcweir 	private static final boolean DEBUG = false;
74cdf0e10cSrcweir 
DEBUG(String dbg)75cdf0e10cSrcweir 	private static final void DEBUG(String dbg) {
76cdf0e10cSrcweir 	    if (DEBUG) System.err.println( dbg );
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     private static String[] supportedServices = {
80cdf0e10cSrcweir         "com.sun.star.loader.Java"
81cdf0e10cSrcweir     };
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	protected XMultiServiceFactory multiServiceFactory = null;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	private XMacroExpander m_xMacroExpander = null;
86cdf0e10cSrcweir     private static final String EXPAND_PROTOCOL_PREFIX = "vnd.sun.star.expand:";
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /** Expands macrofied url using the macro expander singleton.
89cdf0e10cSrcweir      */
expand_url( String url )90cdf0e10cSrcweir     private String expand_url( String url ) throws RuntimeException
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         if (url != null && url.startsWith( EXPAND_PROTOCOL_PREFIX ))
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             try
95cdf0e10cSrcweir             {
96cdf0e10cSrcweir                 if (m_xMacroExpander == null)
97cdf0e10cSrcweir                 {
98cdf0e10cSrcweir                     XPropertySet xProps =
99cdf0e10cSrcweir                         UnoRuntime.queryInterface(
100cdf0e10cSrcweir                             XPropertySet.class, multiServiceFactory );
101cdf0e10cSrcweir                     if (xProps == null)
102cdf0e10cSrcweir                     {
103cdf0e10cSrcweir                         throw new com.sun.star.uno.RuntimeException(
104cdf0e10cSrcweir                             "service manager does not support XPropertySet!",
105cdf0e10cSrcweir                             this );
106cdf0e10cSrcweir                     }
107cdf0e10cSrcweir                     XComponentContext xContext = (XComponentContext)
108cdf0e10cSrcweir                         AnyConverter.toObject(
109cdf0e10cSrcweir                             new Type( XComponentContext.class ),
110cdf0e10cSrcweir                             xProps.getPropertyValue( "DefaultContext" ) );
111cdf0e10cSrcweir                     m_xMacroExpander = (XMacroExpander)AnyConverter.toObject(
112cdf0e10cSrcweir                         new Type( XMacroExpander.class ),
113cdf0e10cSrcweir                         xContext.getValueByName(
114cdf0e10cSrcweir                             "/singletons/com.sun.star.util.theMacroExpander" )
115cdf0e10cSrcweir                         );
116cdf0e10cSrcweir                 }
117cdf0e10cSrcweir                 // decode uric class chars
118cdf0e10cSrcweir                 String macro = URLDecoder.decode(
119cdf0e10cSrcweir                     StringHelper.replace(
120cdf0e10cSrcweir                         url.substring( EXPAND_PROTOCOL_PREFIX.length() ),
121cdf0e10cSrcweir                         '+', "%2B" ) );
122cdf0e10cSrcweir                 // expand macro string
123cdf0e10cSrcweir                 String ret = m_xMacroExpander.expandMacros( macro );
124cdf0e10cSrcweir                 if (DEBUG)
125cdf0e10cSrcweir                 {
126cdf0e10cSrcweir                     System.err.println(
127cdf0e10cSrcweir                         "JavaLoader.expand_url(): " + url + " => " +
128cdf0e10cSrcweir                         macro + " => " + ret );
129cdf0e10cSrcweir                 }
130cdf0e10cSrcweir                 return ret;
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir             catch (com.sun.star.uno.Exception exc)
133cdf0e10cSrcweir             {
134cdf0e10cSrcweir                 throw new com.sun.star.uno.RuntimeException(
135cdf0e10cSrcweir                     exc.getMessage(), this );
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir             catch (java.lang.Exception exc)
138cdf0e10cSrcweir             {
139cdf0e10cSrcweir                 throw new com.sun.star.uno.RuntimeException(
140cdf0e10cSrcweir                     exc.getMessage(), this );
141cdf0e10cSrcweir             }
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir         return url;
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	/** default constructor
147cdf0e10cSrcweir 	 */
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	/**
150cdf0e10cSrcweir 	 * Creates a new instance of the <code>JavaLoader</code> class.
151cdf0e10cSrcweir 	 * <p>
152cdf0e10cSrcweir 	 * @return	new instance
153cdf0e10cSrcweir 	 */
JavaLoader()154cdf0e10cSrcweir 	public JavaLoader() {}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	/**
157cdf0e10cSrcweir 	 * Creates a new <code>JavaLoader</code> object. The specified <code>com.sun.star.lang.XMultiServiceFactory</code>
158cdf0e10cSrcweir 	 * is the <code>ServiceManager</code> service which can be deliviert to all components the <code>JavaLoader</code> is
159cdf0e10cSrcweir 	 * loading.
160cdf0e10cSrcweir 	 * To set the <code>MultiServiceFactory</code> you can use the <code>com.sun.star.lang.XInitialization</code> interface, either.
161cdf0e10cSrcweir 	 * <p>
162cdf0e10cSrcweir 	 * @return	new instance
163cdf0e10cSrcweir 	 * @param	factory		the <code>ServiceManager</code>
164cdf0e10cSrcweir 	 * @see		com.sun.star.lang.ServiceManager
165cdf0e10cSrcweir 	 * @see		com.sun.star.lang.ServiceManager
166cdf0e10cSrcweir 	 * @see		com.sun.star.lang.XInitialization
167cdf0e10cSrcweir 	 */
JavaLoader(XMultiServiceFactory factory)168cdf0e10cSrcweir 	public JavaLoader(XMultiServiceFactory factory) {
169cdf0e10cSrcweir 	    multiServiceFactory = factory;
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	/**
173cdf0e10cSrcweir      * Unlike the original intention, the method could be called every time a new
174cdf0e10cSrcweir      * <code>com.sun.star.lang.XMultiServiceFactory</code> should be set at the loader.
175cdf0e10cSrcweir 	 * <p>
176cdf0e10cSrcweir 	 * @param		args - the first parameter (args[0]) specifices the <code>ServiceManager</code>
177cdf0e10cSrcweir 	 * @see		    com.sun.star.lang.XInitialization
178cdf0e10cSrcweir 	 * @see		    com.sun.star.lang.ServiceManager
179cdf0e10cSrcweir 	 */
initialize( java.lang.Object[] args )180cdf0e10cSrcweir     public void initialize( java.lang.Object[] args )
181cdf0e10cSrcweir             throws com.sun.star.uno.Exception,
182cdf0e10cSrcweir                    com.sun.star.uno.RuntimeException
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         if (args.length == 0) throw new com.sun.star.lang.IllegalArgumentException("No arguments specified");
185cdf0e10cSrcweir 
186cdf0e10cSrcweir         try {
187cdf0e10cSrcweir             multiServiceFactory = (XMultiServiceFactory) AnyConverter.toObject(
188cdf0e10cSrcweir                 new Type(XMultiServiceFactory.class), args[0]);
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir         catch (ClassCastException castEx) {
191cdf0e10cSrcweir             throw new com.sun.star.lang.IllegalArgumentException(
192cdf0e10cSrcweir                 "The argument must be an instance of XMultiServiceFactory");
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 	/**
197cdf0e10cSrcweir 	 * Supplies the implementation name of the component.
198cdf0e10cSrcweir 	 * <p>
199cdf0e10cSrcweir 	 * @return      the implementation name - here the class name
200cdf0e10cSrcweir 	 * @see			com.sun.star.lang.XServiceInfo
201cdf0e10cSrcweir 	 */
getImplementationName()202cdf0e10cSrcweir 	public String getImplementationName()
203cdf0e10cSrcweir             throws com.sun.star.uno.RuntimeException
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir 		return getClass().getName();
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	/**
209cdf0e10cSrcweir 	 * Verifies if a given service is supported by the component.
210cdf0e10cSrcweir 	 * <p>
211cdf0e10cSrcweir 	 * @return		true,if service is suported - otherwise false
212cdf0e10cSrcweir 	 * @param		serviceName		the name of the service that should be checked
213cdf0e10cSrcweir 	 * @see			com.sun.star.lang.XServiceInfo
214cdf0e10cSrcweir 	 */
supportsService(String serviceName)215cdf0e10cSrcweir 	public boolean supportsService(String serviceName)
216cdf0e10cSrcweir 	        throws com.sun.star.uno.RuntimeException
217cdf0e10cSrcweir 	{
218cdf0e10cSrcweir 		for ( int i = 0; i < supportedServices.length; i++ ) {
219cdf0e10cSrcweir 			if ( supportedServices[i].equals(serviceName) )
220cdf0e10cSrcweir 				return true;
221cdf0e10cSrcweir 		}
222cdf0e10cSrcweir 		return false;
223cdf0e10cSrcweir 	}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	/**
226cdf0e10cSrcweir 	 * Supplies a list of all service names supported by the component
227cdf0e10cSrcweir 	 * <p>
228cdf0e10cSrcweir 	 * @return		a String array with all supported services
229cdf0e10cSrcweir 	 * @see			com.sun.star.lang.XServiceInfo
230cdf0e10cSrcweir 	 */
getSupportedServiceNames()231cdf0e10cSrcweir 	public String[] getSupportedServiceNames()
232cdf0e10cSrcweir 	        throws com.sun.star.uno.RuntimeException
233cdf0e10cSrcweir 	{
234cdf0e10cSrcweir 		return supportedServices;
235cdf0e10cSrcweir 	}
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	/**
238cdf0e10cSrcweir 	 * Provides a components factory.
239cdf0e10cSrcweir 	 * The <code>JavaLoader</code> tries to load the class first. If a loacation URL is given the
240cdf0e10cSrcweir 	 * RegistrationClassFinder is used to load the class. Otherwise the class is loaded thru the Class.forName
241cdf0e10cSrcweir 	 * method.
242cdf0e10cSrcweir 	 * To get the factory the inspects the class for the optional static member functions __getServiceFactory resp.
243cdf0e10cSrcweir 	 * getServiceFactory (DEPRECATED).
244cdf0e10cSrcweir 	 * If the function can not be found a default factory @see ComponentFactoryWrapper will be created.
245cdf0e10cSrcweir 	 * <p>
246cdf0e10cSrcweir 	 * @return		the factory for the component (@see com.sun.star.lang.XSingleServiceFactory)
247cdf0e10cSrcweir 	 * @param      	implementationName			the implementation (class) name of the component
248cdf0e10cSrcweir 	 * @param      	implementationLoaderUrl		the URL of the implementation loader. Not used.
249cdf0e10cSrcweir 	 * @param      	locationUrl					points to an archive (JAR file) which contains a component
250cdf0e10cSrcweir 	 * @param      	xKey
251cdf0e10cSrcweir 	 * @see		   	com.sun.star.lang.XImplementationLoader
252cdf0e10cSrcweir 	 * @see			com.sun.star.com.loader.RegistrationClassFinder
253cdf0e10cSrcweir 	 */
activate( String implementationName, String implementationLoaderUrl, String locationUrl, XRegistryKey xKey )254cdf0e10cSrcweir 	public java.lang.Object activate( String implementationName,
255cdf0e10cSrcweir 	                                  String implementationLoaderUrl,
256cdf0e10cSrcweir 	                                  String locationUrl,
257cdf0e10cSrcweir 	                                  XRegistryKey xKey )
258cdf0e10cSrcweir         throws CannotActivateFactoryException,
259cdf0e10cSrcweir                com.sun.star.uno.RuntimeException
260cdf0e10cSrcweir     {
261cdf0e10cSrcweir         locationUrl = expand_url( locationUrl );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 		Object returnObject  = null;
264cdf0e10cSrcweir 	    Class clazz  ;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 	    DEBUG("try to get factory for " + implementationName);
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 	    // first we must get the class of the implementation
269cdf0e10cSrcweir 	    // 1. If a location URL is given it is assumed that this points to a JAR file.
270cdf0e10cSrcweir 	    //    The components class name is stored in the manifest file.
271cdf0e10cSrcweir 	    // 2. If only the implementation name is given, the class is loaded with the
272cdf0e10cSrcweir 	    //    Class.forName() method. This is a hack to load bootstrap components.
273cdf0e10cSrcweir         //    Normally a string must no be null.
274cdf0e10cSrcweir 	    try {
275cdf0e10cSrcweir 		    if ( locationUrl != null ) {
276cdf0e10cSrcweir 		    	// 1.
277cdf0e10cSrcweir 	    		clazz = RegistrationClassFinder.find( locationUrl );
278cdf0e10cSrcweir 		    }
279cdf0e10cSrcweir 		    else {
280cdf0e10cSrcweir 		    	// 2.
281cdf0e10cSrcweir 	    		clazz = Class.forName( implementationName );
282cdf0e10cSrcweir 			}
283cdf0e10cSrcweir 		}
284cdf0e10cSrcweir 		catch (java.net.MalformedURLException e) {
285cdf0e10cSrcweir 			CannotActivateFactoryException cae = new CannotActivateFactoryException(
286cdf0e10cSrcweir 					"Can not activate factory because " + e.toString() );
287cdf0e10cSrcweir 			cae.fillInStackTrace();
288cdf0e10cSrcweir 			throw cae;
289cdf0e10cSrcweir 		}
290cdf0e10cSrcweir 		catch (java.io.IOException e) {
291cdf0e10cSrcweir 			CannotActivateFactoryException cae = new CannotActivateFactoryException(
292cdf0e10cSrcweir 					"Can not activate factory because " + e.toString() );
293cdf0e10cSrcweir 			cae.fillInStackTrace();
294cdf0e10cSrcweir 			throw cae;
295cdf0e10cSrcweir 		}
296cdf0e10cSrcweir 		catch (java.lang.ClassNotFoundException e) {
297cdf0e10cSrcweir 			CannotActivateFactoryException cae = new CannotActivateFactoryException(
298cdf0e10cSrcweir 					"Can not activate factory because " + e.toString() );
299cdf0e10cSrcweir 			cae.fillInStackTrace();
300cdf0e10cSrcweir 			throw cae;
301cdf0e10cSrcweir 		}
302cdf0e10cSrcweir 
303cdf0e10cSrcweir         if (null == clazz)
304cdf0e10cSrcweir         {
305cdf0e10cSrcweir             CannotActivateFactoryException cae =
306cdf0e10cSrcweir                 new CannotActivateFactoryException(
307cdf0e10cSrcweir                     "Cannot determine activation class!" );
308cdf0e10cSrcweir 			cae.fillInStackTrace();
309cdf0e10cSrcweir 			throw cae;
310cdf0e10cSrcweir         }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 		Class[] paramTypes = {String.class, XMultiServiceFactory.class, XRegistryKey.class};
313cdf0e10cSrcweir 		Object[] params = { implementationName, multiServiceFactory, xKey };
314cdf0e10cSrcweir 
315cdf0e10cSrcweir         // try to get factory from implemetation class
316cdf0e10cSrcweir         // latest style: use the public static method __getComponentFactory
317cdf0e10cSrcweir         // - new style: use the public static method __getServiceFactory
318cdf0e10cSrcweir         // - old style: use the public static method getServiceFactory ( DEPRECATED )
319cdf0e10cSrcweir 
320cdf0e10cSrcweir         Method compfac_method = null;
321cdf0e10cSrcweir 		try
322cdf0e10cSrcweir         {
323cdf0e10cSrcweir 		 	compfac_method = clazz.getMethod(
324cdf0e10cSrcweir                 "__getComponentFactory", new Class [] { String.class } );
325cdf0e10cSrcweir 		}
326cdf0e10cSrcweir 		catch ( NoSuchMethodException noSuchMethodEx) {}
327cdf0e10cSrcweir 		catch ( SecurityException secEx) {}
328cdf0e10cSrcweir 
329cdf0e10cSrcweir         Method method = null;
330cdf0e10cSrcweir         if (null == compfac_method)
331cdf0e10cSrcweir         {
332cdf0e10cSrcweir             try {
333cdf0e10cSrcweir                 method = clazz.getMethod("__getServiceFactory", paramTypes);
334cdf0e10cSrcweir             }
335cdf0e10cSrcweir             catch ( NoSuchMethodException noSuchMethodEx) {
336cdf0e10cSrcweir                 method = null;
337cdf0e10cSrcweir             }
338cdf0e10cSrcweir             catch ( SecurityException secEx) {
339cdf0e10cSrcweir                 method = null;
340cdf0e10cSrcweir             }
341cdf0e10cSrcweir         }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 		try {
344cdf0e10cSrcweir             if (null != compfac_method)
345cdf0e10cSrcweir             {
346cdf0e10cSrcweir                 Object ret = compfac_method.invoke( clazz, new Object [] { implementationName } );
347cdf0e10cSrcweir                 if (null == ret || !(ret instanceof XSingleComponentFactory))
348cdf0e10cSrcweir                 {
349cdf0e10cSrcweir                     throw new CannotActivateFactoryException(
350cdf0e10cSrcweir                         "No factory object for " + implementationName );
351cdf0e10cSrcweir                 }
352cdf0e10cSrcweir                 return (XSingleComponentFactory)ret;
353cdf0e10cSrcweir             }
354cdf0e10cSrcweir             else
355cdf0e10cSrcweir             {
356cdf0e10cSrcweir                 if ( method == null ) {
357cdf0e10cSrcweir                     method = clazz.getMethod("getServiceFactory", paramTypes);
358cdf0e10cSrcweir                 }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir                 Object oRet = method.invoke(clazz, params);
361cdf0e10cSrcweir 
362cdf0e10cSrcweir                 if ( (oRet != null) && (oRet instanceof XSingleServiceFactory) ) {
363cdf0e10cSrcweir                     returnObject = (XSingleServiceFactory) oRet;
364cdf0e10cSrcweir                 }
365cdf0e10cSrcweir             }
366cdf0e10cSrcweir     	}
367cdf0e10cSrcweir 		catch ( NoSuchMethodException e) {
368cdf0e10cSrcweir             throw new CannotActivateFactoryException("Can not activate the factory for "
369cdf0e10cSrcweir                         + implementationName + " because " + e.toString() );
370cdf0e10cSrcweir 		}
371cdf0e10cSrcweir 		catch ( SecurityException e) {
372cdf0e10cSrcweir             throw new CannotActivateFactoryException("Can not activate the factory for "
373cdf0e10cSrcweir 						+ implementationName + " because " + e.toString() );
374cdf0e10cSrcweir 		}
375cdf0e10cSrcweir 		catch ( IllegalAccessException e ) {
376cdf0e10cSrcweir 			throw new CannotActivateFactoryException("Can not activate the factory for "
377cdf0e10cSrcweir 						+ implementationName + " because " + e.toString() );
378cdf0e10cSrcweir 		}
379cdf0e10cSrcweir 		catch ( IllegalArgumentException e ) {
380cdf0e10cSrcweir 			throw new CannotActivateFactoryException("Can not activate the factory for "
381cdf0e10cSrcweir 						+ implementationName + " because " + e.toString() );
382cdf0e10cSrcweir 		}
383cdf0e10cSrcweir 		catch ( InvocationTargetException e ) {
384cdf0e10cSrcweir 			throw new CannotActivateFactoryException("Can not activate the factory for "
385cdf0e10cSrcweir 						+ implementationName + " because " + e.getTargetException().toString() );
386cdf0e10cSrcweir 		}
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 		return returnObject;
389cdf0e10cSrcweir 	}
390cdf0e10cSrcweir 
391cdf0e10cSrcweir 	/**
392cdf0e10cSrcweir 	 * Registers the component in a registry under a given root key. If the component supports the optional
393cdf0e10cSrcweir 	 * methods __writeRegistryServiceInfo, writeRegistryServiceInfo (DEPRECATED), the call is delegated to that
394cdf0e10cSrcweir 	 * method. Otherwise a default registration will be accomplished.
395cdf0e10cSrcweir 	 * <p>
396cdf0e10cSrcweir 	 * @return		true if registration is successfully - otherwise false
397cdf0e10cSrcweir 	 * @param		regKey					the root key under that the component should be registred.
398cdf0e10cSrcweir 	 * @param		implementationLoaderUrl	specifies the loader, the component is loaded by.
399cdf0e10cSrcweir 	 * @param		locationUrl				points to an archive (JAR file) which contains a component
400cdf0e10cSrcweir 	 * @see 		ComponentFactoryWrapper
401cdf0e10cSrcweir 	 */
writeRegistryInfo( XRegistryKey regKey, String implementationLoaderUrl, String locationUrl )402cdf0e10cSrcweir 	public boolean writeRegistryInfo( XRegistryKey regKey,
403cdf0e10cSrcweir 	                                  String implementationLoaderUrl,
404cdf0e10cSrcweir 	                                  String locationUrl )
405cdf0e10cSrcweir 	        throws CannotRegisterImplementationException,
406cdf0e10cSrcweir 	               com.sun.star.uno.RuntimeException
407cdf0e10cSrcweir 	{
408cdf0e10cSrcweir         locationUrl = expand_url( locationUrl );
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 		boolean success = false;
411cdf0e10cSrcweir 
412cdf0e10cSrcweir         try {
413cdf0e10cSrcweir 
414cdf0e10cSrcweir     		Class clazz = RegistrationClassFinder.find(locationUrl);
415cdf0e10cSrcweir             if (null == clazz)
416cdf0e10cSrcweir             {
417cdf0e10cSrcweir                 throw new CannotRegisterImplementationException(
418cdf0e10cSrcweir                     "Cannot determine registration class!" );
419cdf0e10cSrcweir             }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 			Class[] paramTypes = { XRegistryKey.class };
422cdf0e10cSrcweir 			Object[] params = { regKey };
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 			Method method  = clazz.getMethod("__writeRegistryServiceInfo", paramTypes);
425cdf0e10cSrcweir 			Object oRet = method.invoke(clazz, params);
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 			if ( (oRet != null) && (oRet instanceof Boolean) )
428cdf0e10cSrcweir 				success = ((Boolean) oRet).booleanValue();
429cdf0e10cSrcweir         }
430cdf0e10cSrcweir 		catch (Exception e) {
431cdf0e10cSrcweir             throw new CannotRegisterImplementationException( e.getMessage());
432cdf0e10cSrcweir  		}
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 		return success;
435cdf0e10cSrcweir 	}
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 	/**
438cdf0e10cSrcweir 	 * Supplies the factory for the <code>JavaLoader</code>
439cdf0e10cSrcweir 	 * <p>
440cdf0e10cSrcweir 	 * @return	the factory for the <code>JavaLoader</code>
441cdf0e10cSrcweir 	 * @param	implName		the name of the desired component
442cdf0e10cSrcweir 	 * @param   multiFactory	the <code>ServiceManager</code> is delivered to the factory
443cdf0e10cSrcweir 	 * @param   regKey			not used - can be null
444cdf0e10cSrcweir 	 */
getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)445cdf0e10cSrcweir 	public static XSingleServiceFactory getServiceFactory( String implName,
446cdf0e10cSrcweir 	                                                       XMultiServiceFactory multiFactory,
447cdf0e10cSrcweir 	                                                       XRegistryKey regKey)
448cdf0e10cSrcweir 	{
449cdf0e10cSrcweir 	    if ( implName.equals(JavaLoader.class.getName()) )
450cdf0e10cSrcweir 	        return new JavaLoaderFactory( multiFactory );
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 	    return null;
453cdf0e10cSrcweir 	}
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 	/**
456cdf0e10cSrcweir 	 * Registers the <code>JavaLoader</code> at the registry.
457cdf0e10cSrcweir 	 * <p>
458cdf0e10cSrcweir 	 * @return     true if registration succseeded - otherwise false
459cdf0e10cSrcweir 	 * @param      regKey	root key under which the <code>JavaLoader</code> should be regidstered
460cdf0e10cSrcweir 	 */
writeRegistryServiceInfo(XRegistryKey regKey)461cdf0e10cSrcweir 	public static boolean writeRegistryServiceInfo(XRegistryKey regKey) {
462cdf0e10cSrcweir 	    boolean result = false;
463cdf0e10cSrcweir 
464cdf0e10cSrcweir 	    try {
465cdf0e10cSrcweir 	        XRegistryKey newKey = regKey.createKey("/" + JavaLoader.class.getName() + "/UNO/SERVICE");
466cdf0e10cSrcweir 
467cdf0e10cSrcweir 	        for (int i=0; i<supportedServices.length; i++)
468cdf0e10cSrcweir 	            newKey.createKey(supportedServices[i]);
469cdf0e10cSrcweir 
470cdf0e10cSrcweir 	        result = true;
471cdf0e10cSrcweir 	    }
472cdf0e10cSrcweir 	    catch (Exception ex) {
473cdf0e10cSrcweir 	        if (DEBUG) System.err.println(">>>JavaLoader.writeRegistryServiceInfo " + ex);
474cdf0e10cSrcweir 	    }
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 	    return result;
477cdf0e10cSrcweir     }
478cdf0e10cSrcweir }
479cdf0e10cSrcweir 
480