1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.lib.uno.helper;
28 
29 import com.sun.star.uno.XComponentContext;
30 import com.sun.star.uno.Type;
31 import com.sun.star.uno.AnyConverter;
32 import com.sun.star.lang.XServiceInfo;
33 import com.sun.star.lang.XSingleComponentFactory;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.beans.XPropertySet;
37 import com.sun.star.registry.XRegistryKey;
38 import com.sun.star.registry.XSimpleRegistry;
39 import com.sun.star.registry.XImplementationRegistration;
40 import com.sun.star.container.XSet;
41 
42 import com.sun.star.lib.uno.helper.Factory;
43 import com.sun.star.comp.helper.Bootstrap;
44 import com.sun.star.comp.helper.RegistryServiceFactory;
45 import com.sun.star.uno.UnoRuntime;
46 
47 
48 //==================================================================================================
49 public class Factory_Test
50     extends WeakBase
51     implements XServiceInfo
52 {
53     static final String m_impl_name = Factory_Test.class.getName();
54     static final String m_supported_services [] = {
55         "Factory_Test.Service0", "Factory_Test.Service1" };
56 
57     //______________________________________________________________________________________________
58     public Factory_Test()
59     {
60     }
61     //______________________________________________________________________________________________
62     public Factory_Test( XComponentContext xContext )
63         throws com.sun.star.uno.Exception
64     {
65         if (null == xContext.getValueByName( "/singletons/com.sun.star.lang.theServiceManager" ))
66         {
67             throw new com.sun.star.uno.RuntimeException(
68                 "bad component context given!", this );
69         }
70     }
71     //______________________________________________________________________________________________
72     public static Object __create( XComponentContext xContext )
73         throws com.sun.star.uno.Exception
74     {
75         return new Factory_Test( xContext );
76     }
77 
78     // XServiceInfo impl
79     //______________________________________________________________________________________________
80     public final String getImplementationName()
81     {
82         return m_impl_name;
83     }
84     //______________________________________________________________________________________________
85     public final boolean supportsService( String service_name )
86     {
87         for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
88         {
89             if (m_supported_services[ nPos ].equals( service_name ))
90                 return true;
91         }
92         return false;
93     }
94     //______________________________________________________________________________________________
95     public final String [] getSupportedServiceNames()
96     {
97         return m_supported_services;
98     }
99 
100     //==============================================================================================
101 	public static XSingleComponentFactory __getComponentFactory( String implName )
102 	{
103   	    if (implName.equals( m_impl_name ))
104         {
105 	        return Factory.createComponentFactory(
106                 Factory_Test.class, Factory_Test.m_supported_services );
107 	    }
108         return null;
109 	}
110     //==============================================================================================
111 	public static boolean __writeRegistryServiceInfo( XRegistryKey xKey )
112     {
113 		return Factory.writeRegistryServiceInfo(
114             m_impl_name, Factory_Test.m_supported_services, xKey );
115     }
116 
117     //==============================================================================================
118     static void service_info_test( Object inst )
119     {
120         XServiceInfo xInfo = UnoRuntime.queryInterface( XServiceInfo.class, inst );
121 
122         if (! xInfo.getImplementationName().equals( m_impl_name ))
123         {
124             System.err.println( "Factory_Test: err -- 1" );
125             System.exit( 1 );
126         }
127         String supported_services [] = xInfo.getSupportedServiceNames();
128         if (supported_services.length != m_supported_services.length)
129         {
130             System.err.println( "Factory_Test: err -- 2" );
131             System.exit( 1 );
132         }
133         for ( int nPos = 0; nPos < supported_services.length; ++nPos )
134         {
135             if (! supported_services[ nPos ].equals( m_supported_services[ nPos ] ))
136             {
137                 System.err.println( "Factory_Test: err -- 3" );
138                 System.exit( 1 );
139             }
140             if (! xInfo.supportsService( m_supported_services[ nPos ] ))
141             {
142                 System.err.println( "Factory_Test: err -- 4" );
143                 System.exit( 1 );
144             }
145         }
146     }
147     //==============================================================================================
148     public static void main( String args [] )
149     {
150         try
151         {
152             String jar = "file://" + new java.io.File( args[ 0 ] ).toURL().getPath();
153             String rdb = "file://" + new java.io.File( args[ 1 ] ).toURL().getPath();
154             System.out.println( "jar file = " + jar );
155             System.out.println( "rdb file = " + rdb );
156 
157             // bootstrap service manager
158             XMultiServiceFactory xMgr = RegistryServiceFactory.create( rdb );
159             XPropertySet xProps = UnoRuntime.queryInterface(
160                 XPropertySet.class, xMgr );
161             XComponentContext xContext = (XComponentContext)AnyConverter.toObject(
162                 new Type( XComponentContext.class ), xProps.getPropertyValue( "DefaultContext" ) );
163             // insert java loader
164             XSet xSet = (XSet)AnyConverter.toObject(
165                 new Type( XSet.class ), xContext.getServiceManager() );
166             xSet.insert( new com.sun.star.comp.loader.JavaLoaderFactory( xMgr ) );
167             // get rdb of smgr
168             XSimpleRegistry xRDB = (XSimpleRegistry)AnyConverter.toObject(
169                 new Type( XSimpleRegistry.class ), xProps.getPropertyValue( "Registry" ) );
170             // register impl
171             XImplementationRegistration xImpReg =
172                 UnoRuntime.queryInterface(
173                     XImplementationRegistration.class,
174                     xContext.getServiceManager().createInstanceWithContext(
175                         "com.sun.star.registry.ImplementationRegistration", xContext ) );
176             xImpReg.registerImplementation( "com.sun.star.loader.Java2", jar, xRDB );
177 
178             // tests
179             System.out.println( "testing instance" );
180             service_info_test( new Factory_Test() );
181             System.out.println( "testing instance" );
182             service_info_test( new Factory_Test( xContext ) );
183             System.out.println( "testing instance" );
184             service_info_test( Factory_Test.__create( xContext ) );
185             System.out.println( "testing factory __getComponentFactory()" );
186             service_info_test( __getComponentFactory( m_impl_name ) );
187             for ( int nPos = 0; nPos < m_supported_services.length; ++nPos )
188             {
189                 System.out.println( "testing factory " + m_supported_services[ nPos ] );
190                 service_info_test(
191                     // create Service
192                     xContext.getServiceManager().createInstanceWithContext(
193                         m_supported_services[ nPos ], xContext ) );
194             }
195 
196             XComponent xComp = UnoRuntime.queryInterface( XComponent.class, xContext );
197             xComp.dispose();
198         }
199         catch (Exception exc)
200         {
201             System.err.println( ">>>>>>>>>> exc occured: " + exc.toString() );
202             exc.printStackTrace();
203         }
204         System.exit( 0 );
205     }
206 }
207 
208