xref: /aoo41x/main/ure/source/uretest/cppmain.cc (revision 5f94a14c)
1*5f94a14cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5f94a14cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5f94a14cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5f94a14cSAndrew Rist  * distributed with this work for additional information
6*5f94a14cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5f94a14cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5f94a14cSAndrew Rist  * "License"); you may not use this file except in compliance
9*5f94a14cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5f94a14cSAndrew Rist  *
11*5f94a14cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5f94a14cSAndrew Rist  *
13*5f94a14cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5f94a14cSAndrew Rist  * software distributed under the License is distributed on an
15*5f94a14cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5f94a14cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5f94a14cSAndrew Rist  * specific language governing permissions and limitations
18*5f94a14cSAndrew Rist  * under the License.
19*5f94a14cSAndrew Rist  *
20*5f94a14cSAndrew Rist  *************************************************************/
21*5f94a14cSAndrew Rist 
22*5f94a14cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "sal/config.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <cstddef>
27cdf0e10cSrcweir #include <functional>
28cdf0e10cSrcweir #include <memory>
29cdf0e10cSrcweir #include <new>
30cdf0e10cSrcweir #include <hash_map>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "com/sun/star/lang/XMain.hpp"
33cdf0e10cSrcweir #include "com/sun/star/lang/XMultiComponentFactory.hpp"
34cdf0e10cSrcweir #include "com/sun/star/uno/Exception.hpp"
35cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
36cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
37cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
38cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
39cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
40cdf0e10cSrcweir #include "cppuhelper/factory.hxx"
41cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
42cdf0e10cSrcweir #include "cppuhelper/implementationentry.hxx"
43cdf0e10cSrcweir #include "cppuhelper/interfacecontainer.hxx"
44cdf0e10cSrcweir #include "cppuhelper/unourl.hxx"
45cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
46cdf0e10cSrcweir #include "osl/mutex.hxx"
47cdf0e10cSrcweir #include "osl/thread.h"
48cdf0e10cSrcweir #include "rtl/malformeduriexception.hxx"
49cdf0e10cSrcweir #include "rtl/string.h"
50cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
51cdf0e10cSrcweir #include "rtl/ustring.h"
52cdf0e10cSrcweir #include "rtl/ustring.hxx"
53cdf0e10cSrcweir #include "sal/types.h"
54cdf0e10cSrcweir #include "salhelper/simplereferenceobject.hxx"
55cdf0e10cSrcweir #include "uno/current_context.hxx"
56cdf0e10cSrcweir #include "uno/environment.h"
57cdf0e10cSrcweir #include "uno/lbnames.h"
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #include "test/types/CppTest.hpp"
60cdf0e10cSrcweir #include "test/types/JavaTest.hpp"
61cdf0e10cSrcweir #include "test/types/TestException.hpp"
62cdf0e10cSrcweir #include "test/types/XTest.hpp"
63cdf0e10cSrcweir 
64cdf0e10cSrcweir namespace css = ::com::sun::star;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir namespace {
67cdf0e10cSrcweir 
68cdf0e10cSrcweir class Service: public ::cppu::WeakImplHelper1< ::css::lang::XMain > {
69cdf0e10cSrcweir public:
Service(::css::uno::Reference<::css::uno::XComponentContext> const & context)70cdf0e10cSrcweir     explicit Service(
71cdf0e10cSrcweir         ::css::uno::Reference< ::css::uno::XComponentContext > const & context):
72cdf0e10cSrcweir         context_(context) {}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL run(
75cdf0e10cSrcweir         ::css::uno::Sequence< ::rtl::OUString > const &)
76cdf0e10cSrcweir         throw (::css::uno::RuntimeException);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir private:
79cdf0e10cSrcweir     Service(Service &); // not defined
80cdf0e10cSrcweir     void operator =(Service &); // not defined
81cdf0e10cSrcweir 
~Service()82cdf0e10cSrcweir     virtual ~Service() {}
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     void test(
85cdf0e10cSrcweir         ::css::uno::Reference< test::types::XTest > const & test,
86cdf0e10cSrcweir         ::rtl::OUString const & name);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     ::css::uno::Reference< ::css::uno::XComponentContext > context_;
89cdf0e10cSrcweir };
90cdf0e10cSrcweir 
run(::css::uno::Sequence<::rtl::OUString> const &)91cdf0e10cSrcweir ::sal_Int32 Service::run(::css::uno::Sequence< ::rtl::OUString > const &)
92cdf0e10cSrcweir     throw (::css::uno::RuntimeException)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     osl_getThreadIdentifier(0); // check for sal
95cdf0e10cSrcweir     (new salhelper::SimpleReferenceObject)->release(); // check for salhelper
96cdf0e10cSrcweir     css::uno::getCurrentContext(); // check for cppu
97cdf0e10cSrcweir     try { // check for cppuhelper
98cdf0e10cSrcweir         std::auto_ptr< cppu::UnoUrl > dummy(new cppu::UnoUrl(rtl::OUString()));
99cdf0e10cSrcweir     } catch (rtl::MalformedUriException &) {}
100cdf0e10cSrcweir     { // check for stlport
101cdf0e10cSrcweir         osl::Mutex m;
102cdf0e10cSrcweir         std::auto_ptr< cppu::OMultiTypeInterfaceContainerHelperVar<
103cdf0e10cSrcweir             int, std::hash< int >, std::equal_to< int > > > dummy(
104cdf0e10cSrcweir                 new cppu::OMultiTypeInterfaceContainerHelperVar<
105cdf0e10cSrcweir                 int, std::hash< int >, std::equal_to< int > >(m));
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir     static char const * const services[] = {
108cdf0e10cSrcweir         "com.sun.star.beans.Introspection",
109cdf0e10cSrcweir         "com.sun.star.bridge.Bridge",
110cdf0e10cSrcweir         "com.sun.star.bridge.BridgeFactory",
111cdf0e10cSrcweir         "com.sun.star.bridge.IiopBridge",
112cdf0e10cSrcweir         "com.sun.star.bridge.UnoUrlResolver",
113cdf0e10cSrcweir         "com.sun.star.bridge.UrpBridge",
114cdf0e10cSrcweir         "com.sun.star.connection.Acceptor",
115cdf0e10cSrcweir         "com.sun.star.connection.Connector",
116cdf0e10cSrcweir         "com.sun.star.io.DataInputStream",
117cdf0e10cSrcweir         "com.sun.star.io.DataOutputStream",
118cdf0e10cSrcweir         "com.sun.star.io.MarkableInputStream",
119cdf0e10cSrcweir         "com.sun.star.io.MarkableOutputStream",
120cdf0e10cSrcweir         "com.sun.star.io.ObjectInputStream",
121cdf0e10cSrcweir         "com.sun.star.io.ObjectOutputStream",
122cdf0e10cSrcweir         "com.sun.star.io.Pipe",
123cdf0e10cSrcweir         "com.sun.star.io.Pump",
124cdf0e10cSrcweir         "com.sun.star.io.TextInputStream",
125cdf0e10cSrcweir         "com.sun.star.io.TextOutputStream",
126cdf0e10cSrcweir         "com.sun.star.java.JavaVirtualMachine",
127cdf0e10cSrcweir         "com.sun.star.lang.MultiServiceFactory",
128cdf0e10cSrcweir         "com.sun.star.lang.RegistryServiceManager",
129cdf0e10cSrcweir         "com.sun.star.lang.ServiceManager",
130cdf0e10cSrcweir         "com.sun.star.loader.Java",
131cdf0e10cSrcweir         "com.sun.star.loader.Java2",
132cdf0e10cSrcweir         "com.sun.star.loader.SharedLibrary",
133cdf0e10cSrcweir         "com.sun.star.reflection.CoreReflection",
134cdf0e10cSrcweir         "com.sun.star.reflection.ProxyFactory",
135cdf0e10cSrcweir         "com.sun.star.reflection.TypeDescriptionManager",
136cdf0e10cSrcweir         "com.sun.star.reflection.TypeDescriptionProvider",
137cdf0e10cSrcweir         "com.sun.star.registry.ImplementationRegistration",
138cdf0e10cSrcweir         "com.sun.star.registry.NestedRegistry",
139cdf0e10cSrcweir         "com.sun.star.registry.SimpleRegistry",
140cdf0e10cSrcweir         "com.sun.star.script.Converter",
141cdf0e10cSrcweir         "com.sun.star.script.Invocation",
142cdf0e10cSrcweir         "com.sun.star.script.InvocationAdapterFactory",
143cdf0e10cSrcweir         "com.sun.star.security.AccessController",
144cdf0e10cSrcweir         "com.sun.star.security.Policy",
145cdf0e10cSrcweir         "com.sun.star.uno.NamingService",
146cdf0e10cSrcweir         "com.sun.star.uri.ExternalUriReferenceTranslator",
147cdf0e10cSrcweir         "com.sun.star.uri.UriReferenceFactory",
148cdf0e10cSrcweir         "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript",
149cdf0e10cSrcweir         "com.sun.star.uri.VndSunStarPkgUrlReferenceFactory"
150cdf0e10cSrcweir         // "com.sun.star.beans.PropertyBag",
151cdf0e10cSrcweir         // "com.sun.star.beans.PropertySet",
152cdf0e10cSrcweir         // "com.sun.star.bridge.OleApplicationRegistration",
153cdf0e10cSrcweir         // "com.sun.star.bridge.OleBridgeSupplier",
154cdf0e10cSrcweir         // "com.sun.star.bridge.OleBridgeSupplier2",
155cdf0e10cSrcweir         // "com.sun.star.bridge.OleBridgeSupplierVar1",
156cdf0e10cSrcweir         // "com.sun.star.bridge.OleObjectFactory",
157cdf0e10cSrcweir         // "com.sun.star.bridge.oleautomation.ApplicationRegistration",
158cdf0e10cSrcweir         // "com.sun.star.bridge.oleautomation.BridgeSupplier",
159cdf0e10cSrcweir         // "com.sun.star.bridge.oleautomation.Factory",
160cdf0e10cSrcweir         // "com.sun.star.loader.Dynamic",
161cdf0e10cSrcweir         // "com.sun.star.registry.DefaultRegistry",
162cdf0e10cSrcweir         // "com.sun.star.script.AllListenerAdapter",
163cdf0e10cSrcweir         // "com.sun.star.script.Engine",
164cdf0e10cSrcweir         // "com.sun.star.script.JavaScript",
165cdf0e10cSrcweir         // "com.sun.star.test.TestFactory",
166cdf0e10cSrcweir         // "com.sun.star.util.BootstrapMacroExpander",
167cdf0e10cSrcweir         // "com.sun.star.util.MacroExpander",
168cdf0e10cSrcweir         // "com.sun.star.util.logging.Logger",
169cdf0e10cSrcweir         // "com.sun.star.util.logging.LoggerRemote"
170cdf0e10cSrcweir     };
171cdf0e10cSrcweir     ::css::uno::Reference< ::css::lang::XMultiComponentFactory > manager(
172cdf0e10cSrcweir         context_->getServiceManager());
173cdf0e10cSrcweir     if (!manager.is()) {
174cdf0e10cSrcweir         throw ::css::uno::RuntimeException(
175cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("no service manager")),
176cdf0e10cSrcweir             static_cast< ::cppu::OWeakObject * >(this));
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir     for (::std::size_t i = 0; i < sizeof services / sizeof services[0]; ++i) {
179cdf0e10cSrcweir         ::css::uno::Reference< ::css::uno::XInterface > instance;
180cdf0e10cSrcweir         try {
181cdf0e10cSrcweir             instance = manager->createInstanceWithContext(
182cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii(services[i]), context_);
183cdf0e10cSrcweir         } catch (::css::uno::RuntimeException &) {
184cdf0e10cSrcweir             throw;
185cdf0e10cSrcweir         } catch (::css::uno::Exception &) {
186cdf0e10cSrcweir             throw ::css::uno::RuntimeException(
187cdf0e10cSrcweir                 ::rtl::OUString(
188cdf0e10cSrcweir                     RTL_CONSTASCII_USTRINGPARAM("error creating instance")),
189cdf0e10cSrcweir                 static_cast< ::cppu::OWeakObject * >(this));
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         if (!instance.is()) {
192cdf0e10cSrcweir             throw ::css::uno::RuntimeException(
193cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("no instance")),
194cdf0e10cSrcweir                 static_cast< ::cppu::OWeakObject * >(this));
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir     static char const * const singletons[] = {
198cdf0e10cSrcweir         "com.sun.star.util.theMacroExpander" };
199cdf0e10cSrcweir     for (::std::size_t i = 0; i < sizeof singletons / sizeof singletons[0]; ++i)
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         ::rtl::OUStringBuffer b;
202cdf0e10cSrcweir         b.appendAscii(RTL_CONSTASCII_STRINGPARAM("/singletons/"));
203cdf0e10cSrcweir         b.appendAscii(singletons[i]);
204cdf0e10cSrcweir         ::css::uno::Reference< ::css::uno::XInterface > instance(
205cdf0e10cSrcweir             context_->getValueByName(b.makeStringAndClear()),
206cdf0e10cSrcweir             ::css::uno::UNO_QUERY_THROW);
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir     test(
209cdf0e10cSrcweir         ::test::types::CppTest::create(context_),
210cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("test.types.CppTest")));
211cdf0e10cSrcweir     test(
212cdf0e10cSrcweir         ::test::types::JavaTest::create(context_),
213cdf0e10cSrcweir         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("test.types.JavaTest")));
214cdf0e10cSrcweir     return 0;
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
test(::css::uno::Reference<test::types::XTest> const & test,::rtl::OUString const & name)217cdf0e10cSrcweir void Service::test(
218cdf0e10cSrcweir     ::css::uno::Reference< test::types::XTest > const & test,
219cdf0e10cSrcweir     ::rtl::OUString const & name)
220cdf0e10cSrcweir {
221cdf0e10cSrcweir     bool ok = false;
222cdf0e10cSrcweir     try {
223cdf0e10cSrcweir         test->throwException();
224cdf0e10cSrcweir     } catch (::test::types::TestException &) {
225cdf0e10cSrcweir         ok = true;
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir     if (!ok) {
228cdf0e10cSrcweir         throw ::css::uno::RuntimeException(
229cdf0e10cSrcweir             (name
230cdf0e10cSrcweir              + ::rtl::OUString(
231cdf0e10cSrcweir                  RTL_CONSTASCII_USTRINGPARAM(".throwException failed"))),
232cdf0e10cSrcweir             static_cast< ::cppu::OWeakObject * >(this));
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir namespace CppMain {
237cdf0e10cSrcweir 
create(::css::uno::Reference<::css::uno::XComponentContext> const & context)238cdf0e10cSrcweir ::css::uno::Reference< ::css::uno::XInterface > create(
239cdf0e10cSrcweir     ::css::uno::Reference< ::css::uno::XComponentContext > const & context)
240cdf0e10cSrcweir     SAL_THROW((::css::uno::Exception))
241cdf0e10cSrcweir {
242cdf0e10cSrcweir     try {
243cdf0e10cSrcweir         return static_cast< ::cppu::OWeakObject * >(new Service(context));
244cdf0e10cSrcweir     } catch (::std::bad_alloc &) {
245cdf0e10cSrcweir         throw ::css::uno::RuntimeException(
246cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
247cdf0e10cSrcweir             ::css::uno::Reference< ::css::uno::XInterface >());
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
getImplementationName()251cdf0e10cSrcweir ::rtl::OUString getImplementationName() {
252cdf0e10cSrcweir     return ::rtl::OUString(
253cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM("test.cpp.cppmain.Component"));
254cdf0e10cSrcweir }
255cdf0e10cSrcweir 
getSupportedServiceNames()256cdf0e10cSrcweir ::css::uno::Sequence< ::rtl::OUString > getSupportedServiceNames() {
257cdf0e10cSrcweir     return ::css::uno::Sequence< ::rtl::OUString >();
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir ::cppu::ImplementationEntry entries[] = {
263cdf0e10cSrcweir     { CppMain::create, CppMain::getImplementationName,
264cdf0e10cSrcweir       CppMain::getSupportedServiceNames, ::cppu::createSingleComponentFactory,
265cdf0e10cSrcweir       0, 0 },
266cdf0e10cSrcweir     { 0, 0, 0, 0, 0, 0 } };
267cdf0e10cSrcweir 
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
component_writeInfo(void * serviceManager,void * registryKey)270cdf0e10cSrcweir extern "C" ::sal_Bool SAL_CALL component_writeInfo(
271cdf0e10cSrcweir     void * serviceManager, void * registryKey)
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     return ::cppu::component_writeInfoHelper(
274cdf0e10cSrcweir         serviceManager, registryKey, entries);
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
component_getFactory(char const * implName,void * serviceManager,void * registryKey)277cdf0e10cSrcweir extern "C" void * SAL_CALL component_getFactory(
278cdf0e10cSrcweir     char const * implName, void * serviceManager, void * registryKey)
279cdf0e10cSrcweir {
280cdf0e10cSrcweir     return ::cppu::component_getFactoryHelper(
281cdf0e10cSrcweir         implName, serviceManager, registryKey, entries);
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
component_getImplementationEnvironment(char const ** envTypeName,::uno_Environment **)284cdf0e10cSrcweir extern "C" void SAL_CALL component_getImplementationEnvironment(
285cdf0e10cSrcweir     char const ** envTypeName, ::uno_Environment **)
286cdf0e10cSrcweir {
287cdf0e10cSrcweir     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
288cdf0e10cSrcweir }
289