| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <com/sun/star/container/XHierarchicalNameAccess.hpp> |
| 22 |
#include <com/sun/star/container/XSet.hpp> |
| 23 |
#include <com/sun/star/lang/XMain.hpp> |
| 24 |
#include <com/sun/star/lang/XSingleComponentFactory.hpp> |
| 25 |
#include <com/sun/star/lang/IllegalArgumentException.hpp> |
| 26 |
#include <com/sun/star/reflection/XIndirectTypeDescription.hpp> |
| 27 |
#include <com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp> |
| 28 |
#include <com/sun/star/reflection/XPublished.hpp> |
| 29 |
#include <com/sun/star/reflection/XStructTypeDescription.hpp> |
| 30 |
#include <com/sun/star/reflection/XTypeDescription.hpp> |
| 31 |
#include <com/sun/star/registry/InvalidRegistryException.hpp> |
| 32 |
#include <com/sun/star/registry/XRegistryKey.hpp> |
| 33 |
#include <com/sun/star/registry/XSimpleRegistry.hpp> |
| 34 |
#include <com/sun/star/uno/Exception.hpp> |
| 35 |
#include <com/sun/star/uno/Reference.hxx> |
| 36 |
#include <com/sun/star/uno/RuntimeException.hpp> |
| 37 |
#include <com/sun/star/uno/Sequence.hxx> |
| 38 |
#include <com/sun/star/uno/TypeClass.hpp> |
| 39 |
#include <com/sun/star/uno/XComponentContext.hpp> |
| 40 |
#include <com/sun/star/uno/XInterface.hpp> |
| 41 |
#include <cppuhelper/factory.hxx> |
| 42 |
#include <cppuhelper/implbase.hxx> |
| 43 |
#include <cppuhelper/weak.hxx> |
| 44 |
#include <osl/file.h> |
| 45 |
#include <osl/thread.h> |
| 46 |
#include <rtl/textenc.h> |
| 47 |
#include <rtl/ustring.h> |
| 48 |
#include <rtl/ustring.hxx> |
| 49 |
#include <sal/types.h> |
| 50 |
#include <uno/environment.h> |
| 51 |
|
| 52 |
#include /*MSVC trouble: <cstdlib>*/ <stdlib.h> |
| 53 |
#include <iostream> |
| 54 |
#include <ostream> |
| 55 |
|
| 56 |
namespace { |
| 57 |
|
| 58 |
class Service: public cppu::WeakImplHelper< css::lang::XMain > { |
| 59 |
public: |
| 60 |
virtual sal_Int32 SAL_CALL |
| 61 |
run(css::uno::Sequence< OUString > const & arguments) |
| 62 |
throw (css::uno::RuntimeException); |
| 63 |
|
| 64 |
static OUString getImplementationName(); |
| 65 |
|
| 66 |
static css::uno::Sequence< OUString > getSupportedServiceNames(); |
| 67 |
|
| 68 |
static css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance( |
| 69 |
css::uno::Reference< css::uno::XComponentContext > const & context) |
| 70 |
throw (css::uno::Exception); |
| 71 |
|
| 72 |
private: |
| 73 |
explicit Service( |
| 74 |
css::uno::Reference< css::uno::XComponentContext > const & context): |
| 75 |
m_context(context) |
| 76 |
{} |
| 77 |
|
| 78 |
css::uno::Reference< css::uno::XComponentContext > m_context; |
| 79 |
}; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
namespace { |
| 84 |
|
| 85 |
std::ostream & operator <<(std::ostream & out, OUString const & value) { |
| 86 |
return out << OUStringToOString(value, RTL_TEXTENCODING_UTF8).getStr(); |
| 87 |
} |
| 88 |
|
| 89 |
void assertTrue(bool argument) { |
| 90 |
if (!argument) { |
| 91 |
std::cerr |
| 92 |
<< "assertTrue(" << argument << ") failed" << std::endl; |
| 93 |
/*MSVC trouble: std::*/abort(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void assertFalse(bool argument) { |
| 98 |
if (argument) { |
| 99 |
std::cerr |
| 100 |
<< "assertFalse(" << argument << ") failed" << std::endl; |
| 101 |
/*MSVC trouble: std::*/abort(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
template< typename T > void assertEqual(T const & value, T const & argument) { |
| 106 |
if (argument != value) { |
| 107 |
std::cerr |
| 108 |
<< "assertEqual(" << value << ", " << argument << ") failed" |
| 109 |
<< std::endl; |
| 110 |
/*MSVC trouble: std::*/abort(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
sal_Int32 Service::run(css::uno::Sequence< OUString > const & arguments) |
| 117 |
throw (css::uno::RuntimeException) |
| 118 |
{ |
| 119 |
css::uno::Reference< css::lang::XMultiComponentFactory > factory( |
| 120 |
m_context->getServiceManager()); |
| 121 |
assertTrue(factory.is()); |
| 122 |
css::uno::Reference< css::container::XHierarchicalNameAccess > manager( |
| 123 |
m_context->getValueByName( |
| 124 |
OUString( |
| 125 |
"/singletons/" |
| 126 |
"com.sun.star.reflection.theTypeDescriptionManager")), |
| 127 |
css::uno::UNO_QUERY_THROW); |
| 128 |
|
| 129 |
|
| 130 |
// test: add cmd line rdbs to manager |
| 131 |
|
| 132 |
|
| 133 |
OSL_ASSERT( arguments.getLength() > 0 ); |
| 134 |
css::uno::Reference<css::container::XSet> xSet( |
| 135 |
manager, css::uno::UNO_QUERY_THROW ); |
| 136 |
for ( sal_Int32 argPos = 0; argPos < arguments.getLength(); ++argPos ) { |
| 137 |
OUString url; |
| 138 |
OSL_VERIFY( osl_File_E_None == osl_getFileURLFromSystemPath( |
| 139 |
arguments[argPos].pData, &url.pData ) ); |
| 140 |
bool supposedToBeCompatible = ! url.endsWithIgnoreAsciiCase("_incomp.rdb"); |
| 141 |
|
| 142 |
css::uno::Reference<css::registry::XSimpleRegistry> xReg( |
| 143 |
m_context->getServiceManager()->createInstanceWithContext( |
| 144 |
"com.sun.star.registry.SimpleRegistry", |
| 145 |
m_context ), css::uno::UNO_QUERY_THROW ); |
| 146 |
xReg->open( url, true /* read-only */, false /* ! create */ ); |
| 147 |
css::uno::Any arg( css::uno::makeAny(xReg) ); |
| 148 |
css::uno::Reference<css::container::XHierarchicalNameAccess> xTDprov( |
| 149 |
m_context->getServiceManager()-> |
| 150 |
createInstanceWithArgumentsAndContext( |
| 151 |
"com.sun.star.comp.stoc.RegistryTypeDescriptionProvider", |
| 152 |
css::uno::Sequence<css::uno::Any>( &arg, 1 ), m_context ), |
| 153 |
css::uno::UNO_QUERY_THROW ); |
| 154 |
try { |
| 155 |
xSet->insert( css::uno::makeAny(xTDprov) ); |
| 156 |
if (! supposedToBeCompatible) |
| 157 |
std::cerr << "current rdb file: " << |
| 158 |
OUStringToOString( |
| 159 |
url, osl_getThreadTextEncoding()).getStr() << std::endl; |
| 160 |
assertTrue(supposedToBeCompatible); |
| 161 |
} catch (css::lang::IllegalArgumentException &) { |
| 162 |
if (supposedToBeCompatible) |
| 163 |
throw; |
| 164 |
assertFalse(supposedToBeCompatible); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/ |
| 169 |
|
| 170 |
css::uno::Reference< css::reflection::XIndirectTypeDescription > sequence( |
| 171 |
manager->getByHierarchicalName( |
| 172 |
OUString("[][]boolean")), |
| 173 |
css::uno::UNO_QUERY_THROW); |
| 174 |
assertEqual(css::uno::TypeClass_SEQUENCE, sequence->getTypeClass()); |
| 175 |
assertEqual( |
| 176 |
OUString("[][]boolean"), |
| 177 |
sequence->getName()); |
| 178 |
assertEqual( |
| 179 |
OUString("[]boolean"), |
| 180 |
sequence->getReferencedType()->getName()); |
| 181 |
|
| 182 |
css::uno::Reference< css::reflection::XStructTypeDescription > structure( |
| 183 |
manager->getByHierarchicalName( |
| 184 |
OUString( "test.tdmanager.Struct<boolean,test.tdmanager.Struct<" |
| 185 |
"any,com.sun.star.uno.XInterface>>")), |
| 186 |
css::uno::UNO_QUERY_THROW); |
| 187 |
assertEqual(css::uno::TypeClass_STRUCT, structure->getTypeClass()); |
| 188 |
assertEqual( |
| 189 |
OUString( "test.tdmanager.Struct<boolean,test.tdmanager.Struct<" |
| 190 |
"any,com.sun.star.uno.XInterface>>"), |
| 191 |
structure->getName()); |
| 192 |
assertEqual< bool >(false, structure->getBaseType().is()); |
| 193 |
assertEqual< sal_Int32 >(1, structure->getMemberTypes().getLength()); |
| 194 |
assertEqual( |
| 195 |
OUString( "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>"), |
| 196 |
structure->getMemberTypes()[0]->getName()); |
| 197 |
assertEqual< sal_Int32 >(1, structure->getMemberNames().getLength()); |
| 198 |
assertEqual( |
| 199 |
OUString("s"), |
| 200 |
structure->getMemberNames()[0]); |
| 201 |
assertEqual< sal_Int32 >(0, structure->getTypeParameters().getLength()); |
| 202 |
assertEqual< sal_Int32 >(2, structure->getTypeArguments().getLength()); |
| 203 |
assertEqual( |
| 204 |
OUString("boolean"), |
| 205 |
structure->getTypeArguments()[0]->getName()); |
| 206 |
assertEqual( |
| 207 |
OUString( "test.tdmanager.Struct<any,com.sun.star.uno.XInterface>"), |
| 208 |
structure->getTypeArguments()[1]->getName()); |
| 209 |
|
| 210 |
css::uno::Reference< css::reflection::XInterfaceMethodTypeDescription > |
| 211 |
method( |
| 212 |
manager->getByHierarchicalName( |
| 213 |
OUString( "com.sun.star.uno.XComponentContext::getValueByName")), |
| 214 |
css::uno::UNO_QUERY_THROW); |
| 215 |
assertEqual(css::uno::TypeClass_INTERFACE_METHOD, method->getTypeClass()); |
| 216 |
assertEqual( |
| 217 |
OUString( "com.sun.star.uno.XComponentContext::getValueByName"), |
| 218 |
method->getName()); |
| 219 |
assertEqual( |
| 220 |
OUString("getValueByName"), |
| 221 |
method->getMemberName()); |
| 222 |
assertEqual< sal_Int32 >(3, method->getPosition()); |
| 223 |
assertEqual( |
| 224 |
OUString("any"), |
| 225 |
method->getReturnType()->getName()); |
| 226 |
assertEqual< bool >(false, method->isOneway()); |
| 227 |
assertEqual< sal_Int32 >(1, method->getParameters().getLength()); |
| 228 |
assertEqual( |
| 229 |
OUString("Name"), |
| 230 |
method->getParameters()[0]->getName()); |
| 231 |
assertEqual( |
| 232 |
OUString("string"), |
| 233 |
method->getParameters()[0]->getType()->getName()); |
| 234 |
assertEqual< bool >(true, method->getParameters()[0]->isIn()); |
| 235 |
assertEqual< bool >(false, method->getParameters()[0]->isOut()); |
| 236 |
assertEqual< sal_Int32 >(0, method->getParameters()[0]->getPosition()); |
| 237 |
assertEqual< sal_Int32 >(0, method->getExceptions().getLength()); |
| 238 |
|
| 239 |
assertFalse( |
| 240 |
css::uno::Reference< css::reflection::XPublished >( |
| 241 |
css::uno::Reference< css::reflection::XTypeDescription >( |
| 242 |
manager->getByHierarchicalName( |
| 243 |
OUString("[]boolean")), |
| 244 |
css::uno::UNO_QUERY_THROW), |
| 245 |
css::uno::UNO_QUERY).is()); |
| 246 |
assertFalse( |
| 247 |
css::uno::Reference< css::reflection::XPublished >( |
| 248 |
css::uno::Reference< css::reflection::XTypeDescription >( |
| 249 |
manager->getByHierarchicalName( |
| 250 |
OUString( "com.sun.star.beans.XIntroTest::ObjectName")), |
| 251 |
css::uno::UNO_QUERY_THROW), |
| 252 |
css::uno::UNO_QUERY).is()); |
| 253 |
assertFalse( |
| 254 |
css::uno::Reference< css::reflection::XPublished >( |
| 255 |
css::uno::Reference< css::reflection::XTypeDescription >( |
| 256 |
manager->getByHierarchicalName( |
| 257 |
OUString( "com.sun.star.beans.XIntroTest::writeln")), |
| 258 |
css::uno::UNO_QUERY_THROW), |
| 259 |
css::uno::UNO_QUERY).is()); |
| 260 |
//TODO: check that the reflection of a property of an accumulation-based |
| 261 |
// service does not support XPublished |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
OUString Service::getImplementationName() { |
| 267 |
return OUString("test.tdmanager.impl"); |
| 268 |
} |
| 269 |
|
| 270 |
css::uno::Sequence< OUString > Service::getSupportedServiceNames() { |
| 271 |
return css::uno::Sequence< OUString >(); |
| 272 |
} |
| 273 |
|
| 274 |
css::uno::Reference< css::uno::XInterface > Service::createInstance( |
| 275 |
css::uno::Reference< css::uno::XComponentContext > const & context) |
| 276 |
throw (css::uno::Exception) |
| 277 |
{ |
| 278 |
return cppu::getXWeak(new Service(context)); |
| 279 |
} |
| 280 |
|
| 281 |
extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(char const * implName, |
| 282 |
void * serviceManager, void *) { |
| 283 |
void * p = 0; |
| 284 |
if (serviceManager != 0) { |
| 285 |
css::uno::Reference< css::lang::XSingleComponentFactory > f; |
| 286 |
if (Service::getImplementationName().equalsAscii(implName)) { |
| 287 |
f = cppu::createSingleComponentFactory( |
| 288 |
&Service::createInstance, Service::getImplementationName(), |
| 289 |
Service::getSupportedServiceNames()); |
| 290 |
} |
| 291 |
if (f.is()) { |
| 292 |
f->acquire(); |
| 293 |
p = f.get(); |
| 294 |
} |
| 295 |
} |
| 296 |
return p; |
| 297 |
} |
| 298 |
|
| 299 |
namespace { |
| 300 |
|
| 301 |
bool writeInfo(void * registryKey, OUString const & implementationName, |
| 302 |
css::uno::Sequence< OUString > const & serviceNames) { |
| 303 |
OUString keyName = "/" + implementationName + "/UNO/SERVICES"; |
| 304 |
css::uno::Reference< css::registry::XRegistryKey > key; |
| 305 |
try { |
| 306 |
key = static_cast< css::registry::XRegistryKey * >(registryKey)-> |
| 307 |
createKey(keyName); |
| 308 |
} catch (css::registry::InvalidRegistryException &) {} |
| 309 |
if (!key.is()) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
bool success = true; |
| 313 |
for (sal_Int32 i = 0; i < serviceNames.getLength(); ++i) { |
| 314 |
try { |
| 315 |
key->createKey(serviceNames[i]); |
| 316 |
} catch (css::registry::InvalidRegistryException &) { |
| 317 |
success = false; |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
return success; |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
extern "C" sal_Bool SAL_CALL component_writeInfo(void *, void * registryKey) { |
| 327 |
return registryKey |
| 328 |
&& writeInfo(registryKey, Service::getImplementationName(), |
| 329 |
Service::getSupportedServiceNames()); |
| 330 |
} |
| 331 |
|
| 332 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |