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 
28 package com.sun.star.comp.helper;
29 
30 import com.sun.star.comp.loader.JavaLoader;
31 
32 import com.sun.star.comp.servicemanager.ServiceManager;
33 import com.sun.star.uno.UnoRuntime;
34 
35 import com.sun.star.container.XSet;
36 import com.sun.star.container.XContentEnumerationAccess;
37 import com.sun.star.container.XEnumeration;
38 import com.sun.star.container.XEnumerationAccess;
39 import com.sun.star.container.XElementAccess;
40 
41 import com.sun.star.lang.XComponent;
42 
43 import com.sun.star.lang.XServiceInfo;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.lang.XSingleServiceFactory;
46 import com.sun.star.lang.XInitialization;
47 
48 import com.sun.star.loader.XImplementationLoader;
49 
50 import com.sun.star.registry.XSimpleRegistry;
51 
52 
53 public class SharedLibraryLoader_Test {
54 
55 	private static final String NATIVE_SERVICE_MANAGER_IMP_NAME = "com.sun.star.comp.stoc.OServiceManager";
56 	private static final String NATIVE_SERVICE_MANAGER_LIB_NAME = "servicemgr.uno";
57 	private static final String NATIVE_REGISTRY_IMP_NAME = "com.sun.star.comp.stoc.SimpleRegistry";
58 	private static final String NATIVE_REGISTRY_LIB_NAME = "simplereg.uno";
59 
60 	private static XMultiServiceFactory		nativeServiceManager		= null;
61 	private static XSingleServiceFactory 	sharedLibraryLoaderFactory 	= null;
62 	private static XImplementationLoader 	sharedLibraryLoader			= null;
63 	private static XSimpleRegistry			simpleRegistry				= null;
64 
65 	static public boolean test_getSharedLibraryLoaderFactory()
66 			throws java.lang.Exception
67 	{
68 		sharedLibraryLoaderFactory = null;
69 		System.out.println("*******************************************************************");
70 		System.out.println("Test: <<< get SharedLibraryLoader factory >>>");
71 		sharedLibraryLoaderFactory = SharedLibraryLoader.getServiceFactory(null, null);
72 
73 		System.out.print("Test - ");
74 		System.out.println(sharedLibraryLoaderFactory == null? "failed" : "successfull");
75 		System.out.println("*******************************************************************");
76 		System.out.println();
77 
78 		return sharedLibraryLoaderFactory != null;
79 	}
80 
81 	static public boolean test_instantiateSharedLibraryLoader()
82 			throws java.lang.Exception
83 	{
84 		sharedLibraryLoader = null;
85 		System.out.println("*******************************************************************");
86 		System.out.println("Test: <<< instantiate SharedLibraryLoader >>>");
87 		if ( sharedLibraryLoaderFactory == null )
88 			if ( ! test_getSharedLibraryLoaderFactory() )
89 				return false;
90 
91 		sharedLibraryLoader = UnoRuntime.queryInterface(
92 				XImplementationLoader.class, sharedLibraryLoaderFactory.createInstance() );
93 
94 		System.out.print("Test - ");
95 		System.out.println(sharedLibraryLoader == null? "failed" : "successfull");
96 		System.out.println("*******************************************************************");
97 		System.out.println();
98 
99 		return sharedLibraryLoader != null;
100 	}
101 
102 	static public boolean test_loadNativeServiceManager()
103 			throws java.lang.Exception
104 	{
105 		nativeServiceManager = null;
106 
107 		System.out.println("*******************************************************************");
108 		System.out.println("Test: <<< load native ServiceManager >>>");
109 		if ( sharedLibraryLoader == null )
110 			if ( ! test_instantiateSharedLibraryLoader() )
111 				return false;
112 
113 		System.err.println("- get the native ServiceManger factory");
114 		XSingleServiceFactory aSMgrFac =
115 			UnoRuntime.queryInterface( XSingleServiceFactory.class,
116 						sharedLibraryLoader.activate(NATIVE_SERVICE_MANAGER_IMP_NAME, null, NATIVE_SERVICE_MANAGER_LIB_NAME, null));
117 
118 		System.err.println("- instantiate the native ServiceManger");
119 		nativeServiceManager = UnoRuntime.queryInterface( XMultiServiceFactory.class, aSMgrFac.createInstance() );
120 
121 		System.out.print("Test - ");
122 		System.out.println(nativeServiceManager == null? "failed" : "successfull");
123 
124 		System.out.println("*******************************************************************");
125 		System.out.println();
126 		return nativeServiceManager != null;
127 	}
128 
129 	static public boolean test_loadNativeSimpleRegistry()
130 			throws java.lang.Exception
131 	{
132 		boolean result = false;
133 		System.out.println("*******************************************************************");
134 		System.out.println("Test: <<< load native SimpleRegistry >>>");
135 		if ( sharedLibraryLoader == null )
136 			if ( ! test_instantiateSharedLibraryLoader() )
137 				return false;
138 
139 		System.err.println("- get factory of the Registry");
140 		XSingleServiceFactory aRegFac =
141 			UnoRuntime.queryInterface( XSingleServiceFactory.class,
142 						sharedLibraryLoader.activate(NATIVE_REGISTRY_IMP_NAME, null, NATIVE_REGISTRY_LIB_NAME, null)
143 			);
144 		System.err.println("- instantiate the Registry");
145 		simpleRegistry =
146 			UnoRuntime.queryInterface( XSimpleRegistry.class, aRegFac.createInstance() );
147 		System.out.print("Test - ");
148 		System.out.println(simpleRegistry == null? "failed" : "successfull");
149 		System.out.println("*******************************************************************");
150 		System.err.println();
151 		return true;
152 	}
153 
154 	static public boolean test_registerSharedLibraryLoader()
155 			throws java.lang.Exception
156 	{
157 		boolean result = true;
158 		System.out.println("*******************************************************************");
159 		System.out.println("Test: <<< register SharedLibraryLoader at the Registry >>>");
160 
161 		if ( simpleRegistry == null )
162 			if ( ! test_loadNativeSimpleRegistry() )
163 				return false;
164 
165 		com.sun.star.registry.XRegistryKey regKey = simpleRegistry.getRootKey();
166 		result = SharedLibraryLoader.writeRegistryServiceInfo( null,  regKey );
167 
168 		System.out.print("Test - ");
169 		System.out.println( result==false ? "failed" : "successfull");
170 		System.out.println("*******************************************************************");
171 		System.out.println();
172 		return result;
173 	}
174 
175 	static public boolean test() throws java.lang.Exception {
176 		boolean passed = true;
177 
178 		System.err.println("SharedLibraryLoader - doing tests...");
179 		passed = test_getSharedLibraryLoaderFactory() && passed;
180 		passed = test_instantiateSharedLibraryLoader() && passed;
181 		passed = test_loadNativeServiceManager() && passed;
182 		passed = test_loadNativeSimpleRegistry() && passed;
183 		//passed = test_registerSharedLibraryLoader() && passed;
184 
185 		System.err.println("SharedLibraryLoader test passed? " + passed);
186 
187 		return passed;
188 	}
189 
190 	static public void main(String args[]) throws java.lang.Exception {
191 		System.exit( test() == true ? 0: -1 );
192 	}
193 }
194 
195