1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Factory_Test.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package com
.sun
.star
.lib
.uno
.helper
;
32 import com
.sun
.star
.uno
.XComponentContext
;
33 import com
.sun
.star
.uno
.Type
;
34 import com
.sun
.star
.uno
.AnyConverter
;
35 import com
.sun
.star
.lang
.XServiceInfo
;
36 import com
.sun
.star
.lang
.XSingleComponentFactory
;
37 import com
.sun
.star
.lang
.XMultiServiceFactory
;
38 import com
.sun
.star
.lang
.XComponent
;
39 import com
.sun
.star
.beans
.XPropertySet
;
40 import com
.sun
.star
.registry
.XRegistryKey
;
41 import com
.sun
.star
.registry
.XSimpleRegistry
;
42 import com
.sun
.star
.registry
.XImplementationRegistration
;
43 import com
.sun
.star
.container
.XSet
;
45 import com
.sun
.star
.lib
.uno
.helper
.Factory
;
46 import com
.sun
.star
.comp
.helper
.Bootstrap
;
47 import com
.sun
.star
.comp
.helper
.RegistryServiceFactory
;
48 import com
.sun
.star
.uno
.UnoRuntime
;
51 //==================================================================================================
52 public class Factory_Test
54 implements XServiceInfo
56 static final String m_impl_name
= Factory_Test
.class.getName();
57 static final String m_supported_services
[] = {
58 "Factory_Test.Service0", "Factory_Test.Service1" };
60 //______________________________________________________________________________________________
64 //______________________________________________________________________________________________
65 public Factory_Test( XComponentContext xContext
)
66 throws com
.sun
.star
.uno
.Exception
68 if (null == xContext
.getValueByName( "/singletons/com.sun.star.lang.theServiceManager" ))
70 throw new com
.sun
.star
.uno
.RuntimeException(
71 "bad component context given!", this );
74 //______________________________________________________________________________________________
75 public static Object
__create( XComponentContext xContext
)
76 throws com
.sun
.star
.uno
.Exception
78 return new Factory_Test( xContext
);
82 //______________________________________________________________________________________________
83 public final String
getImplementationName()
87 //______________________________________________________________________________________________
88 public final boolean supportsService( String service_name
)
90 for ( int nPos
= 0; nPos
< m_supported_services
.length
; ++nPos
)
92 if (m_supported_services
[ nPos
].equals( service_name
))
97 //______________________________________________________________________________________________
98 public final String
[] getSupportedServiceNames()
100 return m_supported_services
;
103 //==============================================================================================
104 public static XSingleComponentFactory
__getComponentFactory( String implName
)
106 if (implName
.equals( m_impl_name
))
108 return Factory
.createComponentFactory(
109 Factory_Test
.class, Factory_Test
.m_supported_services
);
113 //==============================================================================================
114 public static boolean __writeRegistryServiceInfo( XRegistryKey xKey
)
116 return Factory
.writeRegistryServiceInfo(
117 m_impl_name
, Factory_Test
.m_supported_services
, xKey
);
120 //==============================================================================================
121 static void service_info_test( Object inst
)
123 XServiceInfo xInfo
= UnoRuntime
.queryInterface( XServiceInfo
.class, inst
);
125 if (! xInfo
.getImplementationName().equals( m_impl_name
))
127 System
.err
.println( "Factory_Test: err -- 1" );
130 String supported_services
[] = xInfo
.getSupportedServiceNames();
131 if (supported_services
.length
!= m_supported_services
.length
)
133 System
.err
.println( "Factory_Test: err -- 2" );
136 for ( int nPos
= 0; nPos
< supported_services
.length
; ++nPos
)
138 if (! supported_services
[ nPos
].equals( m_supported_services
[ nPos
] ))
140 System
.err
.println( "Factory_Test: err -- 3" );
143 if (! xInfo
.supportsService( m_supported_services
[ nPos
] ))
145 System
.err
.println( "Factory_Test: err -- 4" );
150 //==============================================================================================
151 public static void main( String args
[] )
155 String jar
= "file://" + new java
.io
.File( args
[ 0 ] ).toURL().getPath();
156 String rdb
= "file://" + new java
.io
.File( args
[ 1 ] ).toURL().getPath();
157 System
.out
.println( "jar file = " + jar
);
158 System
.out
.println( "rdb file = " + rdb
);
160 // bootstrap service manager
161 XMultiServiceFactory xMgr
= RegistryServiceFactory
.create( rdb
);
162 XPropertySet xProps
= UnoRuntime
.queryInterface(
163 XPropertySet
.class, xMgr
);
164 XComponentContext xContext
= (XComponentContext
)AnyConverter
.toObject(
165 new Type( XComponentContext
.class ), xProps
.getPropertyValue( "DefaultContext" ) );
166 // insert java loader
167 XSet xSet
= (XSet
)AnyConverter
.toObject(
168 new Type( XSet
.class ), xContext
.getServiceManager() );
169 xSet
.insert( new com
.sun
.star
.comp
.loader
.JavaLoaderFactory( xMgr
) );
171 XSimpleRegistry xRDB
= (XSimpleRegistry
)AnyConverter
.toObject(
172 new Type( XSimpleRegistry
.class ), xProps
.getPropertyValue( "Registry" ) );
174 XImplementationRegistration xImpReg
=
175 UnoRuntime
.queryInterface(
176 XImplementationRegistration
.class,
177 xContext
.getServiceManager().createInstanceWithContext(
178 "com.sun.star.registry.ImplementationRegistration", xContext
) );
179 xImpReg
.registerImplementation( "com.sun.star.loader.Java2", jar
, xRDB
);
182 System
.out
.println( "testing instance" );
183 service_info_test( new Factory_Test() );
184 System
.out
.println( "testing instance" );
185 service_info_test( new Factory_Test( xContext
) );
186 System
.out
.println( "testing instance" );
187 service_info_test( Factory_Test
.__create( xContext
) );
188 System
.out
.println( "testing factory __getComponentFactory()" );
189 service_info_test( __getComponentFactory( m_impl_name
) );
190 for ( int nPos
= 0; nPos
< m_supported_services
.length
; ++nPos
)
192 System
.out
.println( "testing factory " + m_supported_services
[ nPos
] );
195 xContext
.getServiceManager().createInstanceWithContext(
196 m_supported_services
[ nPos
], xContext
) );
199 XComponent xComp
= UnoRuntime
.queryInterface( XComponent
.class, xContext
);
202 catch (Exception exc
)
204 System
.err
.println( ">>>>>>>>>> exc occured: " + exc
.toString() );
205 exc
.printStackTrace();