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.uno.Type;
27 import com.sun.star.uno.AnyConverter;
28 import com.sun.star.lang.XServiceInfo;
29 import com.sun.star.lang.XSingleComponentFactory;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.registry.XRegistryKey;
34 import com.sun.star.registry.XSimpleRegistry;
35 import com.sun.star.registry.XImplementationRegistration;
36 import com.sun.star.container.XSet;
37 
38 import com.sun.star.lib.uno.helper.Factory;
39 import com.sun.star.comp.helper.Bootstrap;
40 import com.sun.star.comp.helper.RegistryServiceFactory;
41 import com.sun.star.uno.UnoRuntime;
42 
43 
44 //==================================================================================================
45 public class Factory_Test
46     extends WeakBase
47     implements XServiceInfo
48 {
49     static final String m_impl_name = Factory_Test.class.getName();
50     static final String m_supported_services [] = {
51         "Factory_Test.Service0", "Factory_Test.Service1" };
52 
53     //______________________________________________________________________________________________
Factory_Test()54     public Factory_Test()
55     {
56     }
57     //______________________________________________________________________________________________
Factory_Test( XComponentContext xContext )58     public Factory_Test( XComponentContext xContext )
59         throws com.sun.star.uno.Exception
60     {
61         if (null == xContext.getValueByName( "/singletons/com.sun.star.lang.theServiceManager" ))
62         {
63             throw new com.sun.star.uno.RuntimeException(
64                 "bad component context given!", this );
65         }
66     }
67     //______________________________________________________________________________________________
__create( XComponentContext xContext )68     public static Object __create( XComponentContext xContext )
69         throws com.sun.star.uno.Exception
70     {
71         return new Factory_Test( xContext );
72     }
73 
74     // XServiceInfo impl
75     //______________________________________________________________________________________________
getImplementationName()76     public final String getImplementationName()
77     {
78         return m_impl_name;
79     }
80     //______________________________________________________________________________________________
supportsService( String service_name )81     public final boolean supportsService( String service_name )
82     {
83         for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
84         {
85             if (m_supported_services[ nPos ].equals( service_name ))
86                 return true;
87         }
88         return false;
89     }
90     //______________________________________________________________________________________________
getSupportedServiceNames()91     public final String [] getSupportedServiceNames()
92     {
93         return m_supported_services;
94     }
95 
96     //==============================================================================================
__getComponentFactory( String implName )97 	public static XSingleComponentFactory __getComponentFactory( String implName )
98 	{
99   	    if (implName.equals( m_impl_name ))
100         {
101 	        return Factory.createComponentFactory(
102                 Factory_Test.class, Factory_Test.m_supported_services );
103 	    }
104         return null;
105 	}
106     //==============================================================================================
__writeRegistryServiceInfo( XRegistryKey xKey )107 	public static boolean __writeRegistryServiceInfo( XRegistryKey xKey )
108     {
109 		return Factory.writeRegistryServiceInfo(
110             m_impl_name, Factory_Test.m_supported_services, xKey );
111     }
112 
113     //==============================================================================================
service_info_test( Object inst )114     static void service_info_test( Object inst )
115     {
116         XServiceInfo xInfo = UnoRuntime.queryInterface( XServiceInfo.class, inst );
117 
118         if (! xInfo.getImplementationName().equals( m_impl_name ))
119         {
120             System.err.println( "Factory_Test: err -- 1" );
121             System.exit( 1 );
122         }
123         String supported_services [] = xInfo.getSupportedServiceNames();
124         if (supported_services.length != m_supported_services.length)
125         {
126             System.err.println( "Factory_Test: err -- 2" );
127             System.exit( 1 );
128         }
129         for ( int nPos = 0; nPos < supported_services.length; ++nPos )
130         {
131             if (! supported_services[ nPos ].equals( m_supported_services[ nPos ] ))
132             {
133                 System.err.println( "Factory_Test: err -- 3" );
134                 System.exit( 1 );
135             }
136             if (! xInfo.supportsService( m_supported_services[ nPos ] ))
137             {
138                 System.err.println( "Factory_Test: err -- 4" );
139                 System.exit( 1 );
140             }
141         }
142     }
143     //==============================================================================================
main( String args [] )144     public static void main( String args [] )
145     {
146         try
147         {
148             String jar = "file://" + new java.io.File( args[ 0 ] ).toURL().getPath();
149             String rdb = "file://" + new java.io.File( args[ 1 ] ).toURL().getPath();
150             System.out.println( "jar file = " + jar );
151             System.out.println( "rdb file = " + rdb );
152 
153             // bootstrap service manager
154             XMultiServiceFactory xMgr = RegistryServiceFactory.create( rdb );
155             XPropertySet xProps = UnoRuntime.queryInterface(
156                 XPropertySet.class, xMgr );
157             XComponentContext xContext = (XComponentContext)AnyConverter.toObject(
158                 new Type( XComponentContext.class ), xProps.getPropertyValue( "DefaultContext" ) );
159             // insert java loader
160             XSet xSet = (XSet)AnyConverter.toObject(
161                 new Type( XSet.class ), xContext.getServiceManager() );
162             xSet.insert( new com.sun.star.comp.loader.JavaLoaderFactory( xMgr ) );
163             // get rdb of smgr
164             XSimpleRegistry xRDB = (XSimpleRegistry)AnyConverter.toObject(
165                 new Type( XSimpleRegistry.class ), xProps.getPropertyValue( "Registry" ) );
166             // register impl
167             XImplementationRegistration xImpReg =
168                 UnoRuntime.queryInterface(
169                     XImplementationRegistration.class,
170                     xContext.getServiceManager().createInstanceWithContext(
171                         "com.sun.star.registry.ImplementationRegistration", xContext ) );
172             xImpReg.registerImplementation( "com.sun.star.loader.Java2", jar, xRDB );
173 
174             // tests
175             System.out.println( "testing instance" );
176             service_info_test( new Factory_Test() );
177             System.out.println( "testing instance" );
178             service_info_test( new Factory_Test( xContext ) );
179             System.out.println( "testing instance" );
180             service_info_test( Factory_Test.__create( xContext ) );
181             System.out.println( "testing factory __getComponentFactory()" );
182             service_info_test( __getComponentFactory( m_impl_name ) );
183             for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
184             {
185                 System.out.println( "testing factory " + m_supported_services[ nPos ] );
186                 service_info_test(
187                     // create Service
188                     xContext.getServiceManager().createInstanceWithContext(
189                         m_supported_services[ nPos ], xContext ) );
190             }
191 
192             XComponent xComp = UnoRuntime.queryInterface( XComponent.class, xContext );
193             xComp.dispose();
194         }
195         catch (Exception exc)
196         {
197             System.err.println( ">>>>>>>>>> exc occured: " + exc.toString() );
198             exc.printStackTrace();
199         }
200         System.exit( 0 );
201     }
202 }
203 
204