update dev300-m58
[ooovba.git] / stoc / source / registry_tdprovider / tdservice.cxx
blobe625d4f8e39e6559e7414e750e94e38db75ccd8d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tdservice.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
33 #include <osl/diagnose.h>
34 #include <rtl/ustrbuf.hxx>
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
36 #include "com/sun/star/uno/RuntimeException.hpp"
38 #include "registry/reader.hxx"
39 #include "registry/version.h"
40 #include "base.hxx"
41 #include "methoddescription.hxx"
43 #include <memory>
45 using namespace com::sun::star;
47 namespace {
49 class Constructor:
50 public cppu::WeakImplHelper1< XServiceConstructorDescription >
52 public:
53 Constructor(
54 Reference< XHierarchicalNameAccess > const & manager,
55 rtl::OUString const & name, Sequence< sal_Int8 > const & bytes,
56 sal_uInt16 index):
57 m_desc(manager, name, bytes, index) {}
59 virtual ~Constructor() {}
61 virtual sal_Bool SAL_CALL isDefaultConstructor() throw (RuntimeException)
62 { return m_desc.getName().getLength() == 0; }
64 virtual rtl::OUString SAL_CALL getName() throw (RuntimeException)
65 { return m_desc.getName(); }
67 virtual Sequence< Reference< XParameter > > SAL_CALL getParameters()
68 throw (RuntimeException)
69 { return m_desc.getParameters(); }
71 virtual Sequence< Reference<XCompoundTypeDescription > > SAL_CALL
72 getExceptions() throw (RuntimeException)
73 { return m_desc.getExceptions(); }
75 private:
76 Constructor(Constructor &); // not implemented
77 void operator =(Constructor); // not implemented
79 stoc::registry_tdprovider::MethodDescription m_desc;
84 namespace stoc_rdbtdp
87 //==================================================================================================
89 // class PropertyTypeDescriptionImpl
91 //==================================================================================================
92 class PropertyTypeDescriptionImpl : public WeakImplHelper1< XPropertyTypeDescription >
94 OUString _aName;
95 Reference< XTypeDescription > _xTD;
96 sal_Int16 _nFlags;
98 public:
99 PropertyTypeDescriptionImpl( const OUString & rName,
100 const Reference< XTypeDescription > & xTD,
101 sal_Int16 nFlags )
102 : _aName( rName ), _xTD( xTD ), _nFlags( nFlags )
104 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
106 virtual ~PropertyTypeDescriptionImpl();
108 // XTypeDescription
109 virtual TypeClass SAL_CALL getTypeClass()
110 throw( RuntimeException );
111 virtual OUString SAL_CALL getName()
112 throw( RuntimeException );
114 // XPropertyTypeDescription
115 virtual sal_Int16 SAL_CALL getPropertyFlags()
116 throw ( RuntimeException );
117 virtual Reference< XTypeDescription > SAL_CALL getPropertyTypeDescription()
118 throw ( RuntimeException );
121 //__________________________________________________________________________________________________
122 // virtual
123 PropertyTypeDescriptionImpl::~PropertyTypeDescriptionImpl()
125 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
128 // XTypeDescription
129 //__________________________________________________________________________________________________
130 // virtual
131 TypeClass PropertyTypeDescriptionImpl::getTypeClass()
132 throw ( RuntimeException )
134 return TypeClass_PROPERTY;
136 //__________________________________________________________________________________________________
137 // virtual
138 OUString PropertyTypeDescriptionImpl::getName()
139 throw ( RuntimeException )
141 return _aName;
144 // XPropertyTypeDescription
145 //__________________________________________________________________________________________________
146 // virtual
147 sal_Int16 SAL_CALL PropertyTypeDescriptionImpl::getPropertyFlags()
148 throw ( RuntimeException )
150 return _nFlags;
153 //__________________________________________________________________________________________________
154 // virtual
155 Reference< XTypeDescription > SAL_CALL
156 PropertyTypeDescriptionImpl::getPropertyTypeDescription()
157 throw ( RuntimeException )
159 return _xTD;
162 //==================================================================================================
164 // ServiceTypeDescriptionImpl implementation
166 //==================================================================================================
168 //__________________________________________________________________________________________________
169 // virtual
170 ServiceTypeDescriptionImpl::~ServiceTypeDescriptionImpl()
172 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
175 // XTypeDescription
176 //__________________________________________________________________________________________________
177 // virtual
178 TypeClass ServiceTypeDescriptionImpl::getTypeClass()
179 throw(::com::sun::star::uno::RuntimeException)
181 return TypeClass_SERVICE;
183 //__________________________________________________________________________________________________
184 // virtual
185 OUString ServiceTypeDescriptionImpl::getName()
186 throw(::com::sun::star::uno::RuntimeException)
188 return _aName;
191 // XServiceTypeDescription
192 //__________________________________________________________________________________________________
193 // virtual
194 Sequence< Reference< XServiceTypeDescription > > SAL_CALL
195 ServiceTypeDescriptionImpl::getMandatoryServices()
196 throw ( RuntimeException )
198 getReferences();
199 return _aMandatoryServices;
202 //__________________________________________________________________________________________________
203 // virtual
204 Sequence< Reference< XServiceTypeDescription > > SAL_CALL
205 ServiceTypeDescriptionImpl::getOptionalServices()
206 throw ( RuntimeException )
208 getReferences();
209 return _aOptionalServices;
212 //__________________________________________________________________________________________________
213 // virtual
214 Sequence< Reference< XInterfaceTypeDescription > > SAL_CALL
215 ServiceTypeDescriptionImpl::getMandatoryInterfaces()
216 throw ( RuntimeException )
218 getReferences();
219 return _aMandatoryInterfaces;
222 //__________________________________________________________________________________________________
223 // virtual
224 Sequence< Reference< XInterfaceTypeDescription > > SAL_CALL
225 ServiceTypeDescriptionImpl::getOptionalInterfaces()
226 throw ( RuntimeException )
228 getReferences();
229 return _aOptionalInterfaces;
232 //__________________________________________________________________________________________________
233 // virtual
234 Sequence< Reference< XPropertyTypeDescription > > SAL_CALL
235 ServiceTypeDescriptionImpl::getProperties()
236 throw ( RuntimeException )
239 MutexGuard guard(getMutex());
240 if (_pProps.get() != 0) {
241 return *_pProps;
245 typereg::Reader aReader(
246 _aBytes.getConstArray(), _aBytes.getLength(), false, TYPEREG_VERSION_1);
248 sal_uInt16 nFields = (sal_uInt16)aReader.getFieldCount();
249 std::auto_ptr< Sequence< Reference< XPropertyTypeDescription > > >
250 pTempProps(
251 new Sequence< Reference< XPropertyTypeDescription > >(nFields));
252 Reference< XPropertyTypeDescription > * pProps = pTempProps->getArray();
254 while ( nFields-- )
256 // name
257 OUStringBuffer aName( _aName );
258 aName.appendAscii( "." );
259 aName.append( aReader.getFieldName( nFields ) );
261 // type description
262 Reference< XTypeDescription > xTD;
265 _xTDMgr->getByHierarchicalName(
266 aReader.getFieldTypeName( nFields ).replace( '/', '.' ) )
267 >>= xTD;
269 catch ( NoSuchElementException const & )
272 OSL_ENSURE( xTD.is(), "### no type description for property!" );
274 // flags
275 RTFieldAccess nFlags = aReader.getFieldFlags( nFields );
277 sal_Int16 nAttribs = 0;
278 if ( nFlags & RT_ACCESS_READONLY )
279 nAttribs |= beans::PropertyAttribute::READONLY;
280 if ( nFlags & RT_ACCESS_OPTIONAL )
281 nAttribs |= beans::PropertyAttribute::OPTIONAL;
282 if ( nFlags & RT_ACCESS_MAYBEVOID )
283 nAttribs |= beans::PropertyAttribute::MAYBEVOID;
284 if ( nFlags & RT_ACCESS_BOUND )
285 nAttribs |= beans::PropertyAttribute::BOUND;
286 if ( nFlags & RT_ACCESS_CONSTRAINED )
287 nAttribs |= beans::PropertyAttribute::CONSTRAINED;
288 if ( nFlags & RT_ACCESS_TRANSIENT )
289 nAttribs |= beans::PropertyAttribute::TRANSIENT;
290 if ( nFlags & RT_ACCESS_MAYBEAMBIGUOUS )
291 nAttribs |= beans::PropertyAttribute::MAYBEAMBIGUOUS;
292 if ( nFlags & RT_ACCESS_MAYBEDEFAULT )
293 nAttribs |= beans::PropertyAttribute::MAYBEDEFAULT;
294 if ( nFlags & RT_ACCESS_REMOVEABLE )
295 nAttribs |= beans::PropertyAttribute::REMOVEABLE;
297 OSL_ENSURE( !(nFlags & RT_ACCESS_PROPERTY),
298 "### RT_ACCESS_PROPERTY is unexpected here!" );
299 OSL_ENSURE( !(nFlags & RT_ACCESS_ATTRIBUTE),
300 "### RT_ACCESS_ATTRIBUTE is unexpected here!" );
301 OSL_ENSURE( !(nFlags & RT_ACCESS_CONST),
302 "### RT_ACCESS_CONST is unexpected here!" );
303 // always set, unless RT_ACCESS_READONLY is set.
304 //OSL_ENSURE( !(nFlags & RT_ACCESS_READWRITE),
305 // "### RT_ACCESS_READWRITE is unexpected here" );
306 OSL_ENSURE( !(nFlags & RT_ACCESS_DEFAULT),
307 "### RT_ACCESS_DEAFAULT is unexpected here" );
309 pProps[ nFields ]
310 = new PropertyTypeDescriptionImpl( aName.makeStringAndClear(),
311 xTD,
312 nAttribs );
315 MutexGuard guard(getMutex());
316 if (_pProps.get() == 0) {
317 _pProps = pTempProps;
319 return *_pProps;
322 sal_Bool ServiceTypeDescriptionImpl::isSingleInterfaceBased()
323 throw (RuntimeException)
325 getReferences();
326 return _xInterfaceTD.is();
329 Reference< XTypeDescription > ServiceTypeDescriptionImpl::getInterface()
330 throw (RuntimeException)
332 getReferences();
333 return _xInterfaceTD;
336 Sequence< Reference< XServiceConstructorDescription > >
337 ServiceTypeDescriptionImpl::getConstructors() throw (RuntimeException) {
338 MutexGuard guard(getMutex());
339 if (_pCtors.get() == 0) {
340 typereg::Reader reader(
341 _aBytes.getConstArray(), _aBytes.getLength(), false,
342 TYPEREG_VERSION_1);
343 sal_uInt16 ctorCount = reader.getMethodCount();
344 std::auto_ptr< Sequence< Reference< XServiceConstructorDescription > > >
345 ctors(
346 new Sequence< Reference< XServiceConstructorDescription > >(
347 ctorCount));
348 for (sal_uInt16 i = 0; i < ctorCount; ++i) {
349 rtl::OUString name(reader.getMethodName(i));
350 if (reader.getMethodFlags(i) != RT_MODE_TWOWAY
351 || (!reader.getMethodReturnTypeName(i).equalsAsciiL(
352 RTL_CONSTASCII_STRINGPARAM("void")))
353 || (name.getLength() == 0
354 && (ctorCount != 1 || reader.getMethodParameterCount(i) != 0
355 || reader.getMethodExceptionCount(i) != 0)))
357 throw RuntimeException(
358 rtl::OUString(
359 RTL_CONSTASCII_USTRINGPARAM(
360 "Service has bad constructors")),
361 static_cast< OWeakObject * >(this));
363 (*ctors)[i] = new Constructor(
364 _xTDMgr, reader.getMethodName(i), _aBytes, i);
366 _pCtors = ctors;
368 return *_pCtors;
371 //__________________________________________________________________________________________________
372 void ServiceTypeDescriptionImpl::getReferences()
373 throw ( RuntimeException )
376 MutexGuard guard(getMutex());
377 if (_bInitReferences) {
378 return;
381 typereg::Reader aReader(
382 _aBytes.getConstArray(), _aBytes.getLength(), false, TYPEREG_VERSION_1);
383 sal_uInt16 superTypes = aReader.getSuperTypeCount();
384 if (superTypes > 1) {
385 throw RuntimeException(
386 rtl::OUString(
387 RTL_CONSTASCII_USTRINGPARAM(
388 "Service has more than one supertype")),
389 static_cast< OWeakObject * >(this));
391 if (superTypes == 1) {
392 OUString aBaseName( aReader.getSuperTypeName(0).replace( '/', '.' ) );
393 if ( aReader.getReferenceCount() != 0
394 || aReader.getFieldCount() != 0 )
395 throw RuntimeException(
396 OUString(
397 RTL_CONSTASCII_USTRINGPARAM(
398 "Service is single-interface--based but also has"
399 " references and/or properties" ) ),
400 static_cast< OWeakObject * >( this ) );
401 Reference< XTypeDescription > ifc;
404 _xTDMgr->getByHierarchicalName( aBaseName ) >>= ifc;
406 catch ( NoSuchElementException const & e )
408 throw RuntimeException(
409 OUString(
410 RTL_CONSTASCII_USTRINGPARAM(
411 "com.sun.star.container.NoSuchElementException: " ) )
412 + e.Message,
413 static_cast< OWeakObject * >( this ) );
415 OSL_ASSERT(ifc.is());
416 if (resolveTypedefs(ifc)->getTypeClass() != TypeClass_INTERFACE) {
417 throw RuntimeException(
418 OUString(
419 RTL_CONSTASCII_USTRINGPARAM(
420 "Single-interface--based service is not based on"
421 " interface type" ) ),
422 static_cast< OWeakObject * >( this ) );
424 MutexGuard guard(getMutex());
425 if (!_bInitReferences) {
426 _xInterfaceTD = ifc;
427 _bInitReferences = true;
430 else
432 sal_uInt16 nRefs = aReader.getReferenceCount();
433 Sequence< Reference< XServiceTypeDescription > > aMandatoryServices(
434 nRefs);
435 Sequence< Reference< XServiceTypeDescription > > aOptionalServices(
436 nRefs);
437 Sequence< Reference< XInterfaceTypeDescription > > aMandatoryInterfaces(
438 nRefs);
439 Sequence< Reference< XInterfaceTypeDescription > > aOptionalInterfaces(
440 nRefs);
441 sal_uInt32 nMS = 0;
442 sal_uInt32 nOS = 0;
443 sal_uInt32 nMI = 0;
444 sal_uInt32 nOI = 0;
446 for ( sal_uInt16 nPos = 0; nPos < nRefs; ++nPos )
448 RTReferenceType eType = aReader.getReferenceSort( nPos );
449 switch ( eType )
451 case RT_REF_EXPORTS: // service
453 uno::Any aTypeDesc;
456 aTypeDesc = _xTDMgr->getByHierarchicalName(
457 aReader.getReferenceTypeName( nPos ).replace(
458 '/', '.' ) );
460 catch ( NoSuchElementException const & e )
462 throw RuntimeException(
463 OUString(
464 RTL_CONSTASCII_USTRINGPARAM(
465 "com.sun.star.container."
466 "NoSuchElementException: " ) )
467 + e.Message,
468 static_cast< OWeakObject * >( this ) );
471 RTFieldAccess nAccess = aReader.getReferenceFlags( nPos );
472 if ( nAccess & RT_ACCESS_OPTIONAL )
474 // optional service
475 if ( !( aTypeDesc >>= aOptionalServices[ nOS ] ) )
476 throw RuntimeException(
477 OUString(
478 RTL_CONSTASCII_USTRINGPARAM(
479 "Service 'export' is not a service" ) ),
480 static_cast< OWeakObject * >( this ) );
481 nOS++;
483 else
485 // mandatory service
486 if ( !( aTypeDesc >>= aMandatoryServices[ nMS ] ) )
487 throw RuntimeException(
488 OUString(
489 RTL_CONSTASCII_USTRINGPARAM(
490 "Service 'export' is not a service" ) ),
491 static_cast< OWeakObject * >( this ) );
492 nMS++;
494 break;
496 case RT_REF_SUPPORTS: // interface
498 uno::Any aTypeDesc;
501 aTypeDesc = _xTDMgr->getByHierarchicalName(
502 aReader.getReferenceTypeName( nPos ).replace(
503 '/', '.' ) );
505 catch ( NoSuchElementException const & e )
507 throw RuntimeException(
508 OUString(
509 RTL_CONSTASCII_USTRINGPARAM(
510 "com.sun.star.container."
511 "NoSuchElementException: " ) )
512 + e.Message,
513 static_cast< OWeakObject * >( this ) );
516 RTFieldAccess nAccess = aReader.getReferenceFlags( nPos );
517 if ( nAccess & RT_ACCESS_OPTIONAL )
519 // optional interface
520 if ( !( aTypeDesc >>= aOptionalInterfaces[ nOI ] ) )
521 throw RuntimeException(
522 OUString(
523 RTL_CONSTASCII_USTRINGPARAM(
524 "Service 'supports' is not an"
525 " interface" ) ),
526 static_cast< OWeakObject * >( this ) );
527 nOI++;
529 else
531 // mandatory interface
532 if ( !( aTypeDesc >>= aMandatoryInterfaces[ nMI ] ) )
533 throw RuntimeException(
534 OUString(
535 RTL_CONSTASCII_USTRINGPARAM(
536 "Service 'supports' is not an"
537 " interface" ) ),
538 static_cast< OWeakObject * >( this ) );
539 nMI++;
541 break;
543 case RT_REF_OBSERVES:
544 case RT_REF_NEEDS:
545 break;
546 default:
547 OSL_ENSURE( sal_False, "### unsupported reference type!" );
548 break;
551 aMandatoryServices.realloc( nMS );
552 aOptionalServices.realloc( nOS );
553 aMandatoryInterfaces.realloc( nMI );
554 aOptionalInterfaces.realloc( nOI );
556 MutexGuard guard(getMutex());
557 if (!_bInitReferences) {
558 _aMandatoryServices = aMandatoryServices;
559 _aOptionalServices = aOptionalServices;
560 _aMandatoryInterfaces = aMandatoryInterfaces;
561 _aOptionalInterfaces = aOptionalInterfaces;
562 _bInitReferences = true;