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