| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
package com.sun.star.lib.uno.helper; |
| 19 |
|
| 20 |
import com.sun.star.uno.XComponentContext; |
| 21 |
import com.sun.star.uno.Type; |
| 22 |
import com.sun.star.uno.AnyConverter; |
| 23 |
import com.sun.star.lang.XServiceInfo; |
| 24 |
import com.sun.star.lang.XSingleComponentFactory; |
| 25 |
import com.sun.star.lang.XMultiServiceFactory; |
| 26 |
import com.sun.star.lang.XComponent; |
| 27 |
import com.sun.star.beans.XPropertySet; |
| 28 |
import com.sun.star.registry.XRegistryKey; |
| 29 |
import com.sun.star.registry.XSimpleRegistry; |
| 30 |
import com.sun.star.registry.XImplementationRegistration; |
| 31 |
import com.sun.star.container.XSet; |
| 32 |
|
| 33 |
import com.sun.star.comp.helper.RegistryServiceFactory; |
| 34 |
import com.sun.star.uno.UnoRuntime; |
| 35 |
|
| 36 |
|
| 37 |
@Deprecated |
| 38 |
public class Factory_Test |
| 39 |
extends WeakBase |
| 40 |
implements XServiceInfo |
| 41 |
{ |
| 42 |
static final String m_impl_name = Factory_Test.class.getName(); |
| 43 |
static final String m_supported_services [] = { |
| 44 |
"Factory_Test.Service0", "Factory_Test.Service1" }; |
| 45 |
|
| 46 |
|
| 47 |
public Factory_Test() |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
public Factory_Test( XComponentContext xContext ) |
| 52 |
{ |
| 53 |
if (null == xContext.getValueByName( "/singletons/com.sun.star.lang.theServiceManager" )) |
| 54 |
{ |
| 55 |
throw new com.sun.star.uno.RuntimeException( |
| 56 |
"bad component context given!", this ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public static Object __create( XComponentContext xContext ) |
| 61 |
{ |
| 62 |
return new Factory_Test( xContext ); |
| 63 |
} |
| 64 |
|
| 65 |
// XServiceInfo impl |
| 66 |
|
| 67 |
public final String getImplementationName() |
| 68 |
{ |
| 69 |
return m_impl_name; |
| 70 |
} |
| 71 |
|
| 72 |
public final boolean supportsService( String service_name ) |
| 73 |
{ |
| 74 |
for ( int nPos = 0; nPos < m_supported_services.length; ++nPos ) |
| 75 |
{ |
| 76 |
if (m_supported_services[ nPos ].equals( service_name )) |
| 77 |
return true; |
| 78 |
} |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
public final String [] getSupportedServiceNames() |
| 83 |
{ |
| 84 |
return m_supported_services; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
public static XSingleComponentFactory __getComponentFactory( String implName ) |
| 89 |
{ |
| 90 |
if (implName.equals( m_impl_name )) |
| 91 |
{ |
| 92 |
return Factory.createComponentFactory( |
| 93 |
Factory_Test.class, Factory_Test.m_supported_services ); |
| 94 |
} |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
public static boolean __writeRegistryServiceInfo( XRegistryKey xKey ) |
| 99 |
{ |
| 100 |
return Factory.writeRegistryServiceInfo( |
| 101 |
m_impl_name, Factory_Test.m_supported_services, xKey ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
static void service_info_test( Object inst ) |
| 106 |
{ |
| 107 |
XServiceInfo xInfo = UnoRuntime.queryInterface( XServiceInfo.class, inst ); |
| 108 |
|
| 109 |
if (! xInfo.getImplementationName().equals( m_impl_name )) |
| 110 |
{ |
| 111 |
System.err.println( "Factory_Test: err -- 1" ); |
| 112 |
System.exit( 1 ); |
| 113 |
} |
| 114 |
String supported_services [] = xInfo.getSupportedServiceNames(); |
| 115 |
if (supported_services.length != m_supported_services.length) |
| 116 |
{ |
| 117 |
System.err.println( "Factory_Test: err -- 2" ); |
| 118 |
System.exit( 1 ); |
| 119 |
} |
| 120 |
for ( int nPos = 0; nPos < supported_services.length; ++nPos ) |
| 121 |
{ |
| 122 |
if (! supported_services[ nPos ].equals( m_supported_services[ nPos ] )) |
| 123 |
{ |
| 124 |
System.err.println( "Factory_Test: err -- 3" ); |
| 125 |
System.exit( 1 ); |
| 126 |
} |
| 127 |
if (! xInfo.supportsService( m_supported_services[ nPos ] )) |
| 128 |
{ |
| 129 |
System.err.println( "Factory_Test: err -- 4" ); |
| 130 |
System.exit( 1 ); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public static void main( String args [] ) |
| 136 |
{ |
| 137 |
try |
| 138 |
{ |
| 139 |
String jar = "file://" + new java.io.File( args[ 0 ] ).toURL().getPath(); |
| 140 |
String rdb = "file://" + new java.io.File( args[ 1 ] ).toURL().getPath(); |
| 141 |
System.out.println( "jar file = " + jar ); |
| 142 |
System.out.println( "rdb file = " + rdb ); |
| 143 |
|
| 144 |
// bootstrap service manager |
| 145 |
XMultiServiceFactory xMgr = RegistryServiceFactory.create( rdb ); |
| 146 |
XPropertySet xProps = UnoRuntime.queryInterface( |
| 147 |
XPropertySet.class, xMgr ); |
| 148 |
XComponentContext xContext = (XComponentContext)AnyConverter.toObject( |
| 149 |
new Type( XComponentContext.class ), xProps.getPropertyValue( "DefaultContext" ) ); |
| 150 |
// insert java loader |
| 151 |
XSet xSet = (XSet)AnyConverter.toObject( |
| 152 |
new Type( XSet.class ), xContext.getServiceManager() ); |
| 153 |
xSet.insert( new com.sun.star.comp.loader.JavaLoaderFactory( xMgr ) ); |
| 154 |
// get rdb of smgr |
| 155 |
XSimpleRegistry xRDB = (XSimpleRegistry)AnyConverter.toObject( |
| 156 |
new Type( XSimpleRegistry.class ), xProps.getPropertyValue( "Registry" ) ); |
| 157 |
// register impl |
| 158 |
XImplementationRegistration xImpReg = |
| 159 |
UnoRuntime.queryInterface( |
| 160 |
XImplementationRegistration.class, |
| 161 |
xContext.getServiceManager().createInstanceWithContext( |
| 162 |
"com.sun.star.registry.ImplementationRegistration", xContext ) ); |
| 163 |
xImpReg.registerImplementation( "com.sun.star.loader.Java2", jar, xRDB ); |
| 164 |
|
| 165 |
// tests |
| 166 |
System.out.println( "testing instance" ); |
| 167 |
service_info_test( new Factory_Test() ); |
| 168 |
System.out.println( "testing instance" ); |
| 169 |
service_info_test( new Factory_Test( xContext ) ); |
| 170 |
System.out.println( "testing instance" ); |
| 171 |
service_info_test( Factory_Test.__create( xContext ) ); |
| 172 |
System.out.println( "testing factory __getComponentFactory()" ); |
| 173 |
service_info_test( __getComponentFactory( m_impl_name ) ); |
| 174 |
for ( int nPos = 0; nPos < m_supported_services.length; ++nPos ) |
| 175 |
{ |
| 176 |
System.out.println( "testing factory " + m_supported_services[ nPos ] ); |
| 177 |
service_info_test( |
| 178 |
// create Service |
| 179 |
xContext.getServiceManager().createInstanceWithContext( |
| 180 |
m_supported_services[ nPos ], xContext ) ); |
| 181 |
} |
| 182 |
|
| 183 |
XComponent xComp = UnoRuntime.queryInterface( XComponent.class, xContext ); |
| 184 |
xComp.dispose(); |
| 185 |
} |
| 186 |
catch (Exception exc) |
| 187 |
{ |
| 188 |
System.err.println( ">>>>>>>>>> exc occurred: " + exc.toString() ); |
| 189 |
exc.printStackTrace(); |
| 190 |
} |
| 191 |
System.exit( 0 ); |
| 192 |
} |
| 193 |
} |
| 194 |
|