1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: excomp2.cxx,v $
10 * $Revision: 1.5.16.1 $
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 <osl/mutex.hxx>
35 #include <rtl/alloc.h>
36 #include <cppuhelper/factory.hxx>
37 #include <cppuhelper/queryinterface.hxx>
38 #include <cppuhelper/weak.hxx>
39 #include <cppuhelper/typeprovider.hxx>
41 #include <example/XTest.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/lang/XServiceInfo.hpp>
44 #include <com/sun/star/lang/XTypeProvider.hpp>
45 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46 #include <com/sun/star/registry/XRegistryKey.hpp>
48 using namespace example
;
49 using namespace com::sun::star::uno
;
50 using namespace com::sun::star::lang
;
51 using namespace com::sun::star::registry
;
56 #define SERVICENAME2 "example.ExampleComponent2"
57 #define IMPLNAME2 "example.ExampleComponent2.Impl"
59 namespace excomp2_impl
{
61 //*************************************************************************
62 // ExampleComponent2Impl
63 //*************************************************************************
64 class ExampleComponent2Impl
: public OWeakObject
65 , public XTypeProvider
70 ExampleComponent2Impl( const Reference
<XMultiServiceFactory
> & rXSMgr
);
72 ~ExampleComponent2Impl();
75 virtual ::com::sun::star::uno::Any SAL_CALL
queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
);
76 virtual void SAL_CALL
acquire() throw()
77 { OWeakObject::acquire(); }
78 virtual void SAL_CALL
release() throw()
79 { OWeakObject::release(); }
82 virtual ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> SAL_CALL
getTypes() throw (::com::sun::star::uno::RuntimeException
);
83 virtual ::com::sun::star::uno::Sequence
< sal_Int8
> SAL_CALL
getImplementationId() throw (::com::sun::star::uno::RuntimeException
);
86 virtual OUString SAL_CALL
getImplementationName( ) throw(RuntimeException
);
87 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw(RuntimeException
);
88 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) throw(RuntimeException
);
89 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_Static( );
92 virtual OUString SAL_CALL
getMessage() throw(RuntimeException
);
97 Reference
<XMultiServiceFactory
> m_xSMgr
;
100 //*************************************************************************
101 ExampleComponent2Impl::ExampleComponent2Impl( const Reference
<XMultiServiceFactory
> & rXSMgr
)
106 //*************************************************************************
107 ExampleComponent2Impl::~ExampleComponent2Impl()
111 //*************************************************************************
112 Any SAL_CALL
ExampleComponent2Impl::queryInterface( const ::com::sun::star::uno::Type
& rType
)
113 throw(::com::sun::star::uno::RuntimeException
)
115 Any aRet
= ::cppu::queryInterface(rType
,
116 static_cast< XTypeProvider
* >( this ),
117 static_cast< XServiceInfo
* >( this ),
118 static_cast< XTest
* >( this ) );
119 if ( aRet
.hasValue() )
122 return OWeakObject::queryInterface( rType
);
125 //*************************************************************************
126 Sequence
< Type
> SAL_CALL
ExampleComponent2Impl::getTypes()
127 throw (::com::sun::star::uno::RuntimeException
)
129 static OTypeCollection
* pTypes
= 0;
132 MutexGuard
aGuard( m_mutex
);
135 static OTypeCollection
aTypes(
136 ::getCppuType( (const Reference
< XInterface
> *)0 ),
137 ::getCppuType( (const Reference
< XWeak
> *)0 ),
138 ::getCppuType( (const Reference
< XTypeProvider
> *)0 ),
139 ::getCppuType( (const Reference
< XServiceInfo
> *)0 ),
140 ::getCppuType( (const Reference
< XTest
> *)0 ) );
144 return pTypes
->getTypes();
147 //*************************************************************************
148 Sequence
< sal_Int8
> SAL_CALL
ExampleComponent2Impl::getImplementationId()
149 throw (::com::sun::star::uno::RuntimeException
)
151 static OImplementationId
* pId
= 0;
154 MutexGuard
aGuard( m_mutex
);
157 static OImplementationId aId
;
161 return pId
->getImplementationId();
164 //*************************************************************************
165 OUString SAL_CALL
ExampleComponent2Impl::getImplementationName( )
166 throw(RuntimeException
)
168 Guard
< Mutex
> aGuard( m_mutex
);
169 return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2
) );
172 //*************************************************************************
173 sal_Bool SAL_CALL
ExampleComponent2Impl::supportsService( const OUString
& ServiceName
)
174 throw(RuntimeException
)
176 Guard
< Mutex
> aGuard( m_mutex
);
177 Sequence
< OUString
> aSNL
= getSupportedServiceNames();
178 const OUString
* pArray
= aSNL
.getConstArray();
179 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); i
++ )
180 if( pArray
[i
] == ServiceName
)
185 //*************************************************************************
186 Sequence
<OUString
> SAL_CALL
ExampleComponent2Impl::getSupportedServiceNames( )
187 throw(RuntimeException
)
189 Guard
< Mutex
> aGuard( m_mutex
);
190 return getSupportedServiceNames_Static();
193 //*************************************************************************
194 Sequence
<OUString
> SAL_CALL
ExampleComponent2Impl::getSupportedServiceNames_Static( )
196 OUString
aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2
) );
197 return Sequence
< OUString
>( &aName
, 1 );
200 //*************************************************************************
201 OUString SAL_CALL
ExampleComponent2Impl::getMessage() throw(RuntimeException
)
203 Guard
< Mutex
> aGuard( m_mutex
);
204 return OUString::createFromAscii("Alle meine Entchen schwimmen auf dem See, schwimmen auf dem See ...");
208 //*************************************************************************
209 Reference
<XInterface
> SAL_CALL
ExampleComponent2_CreateInstance( const Reference
<XMultiServiceFactory
>& rSMgr
)
211 Reference
<XInterface
> xRet
;
213 XTest
*pXTest
= (XTest
*) new ExampleComponent2Impl(rSMgr
);
217 xRet
= Reference
< XInterface
>::query(pXTest
);
229 //==================================================================================================
230 void SAL_CALL
component_getImplementationEnvironment(
231 const sal_Char
** ppEnvTypeName
, uno_Environment
** /* ppEnv */ )
233 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
235 //==================================================================================================
236 sal_Bool SAL_CALL
component_writeInfo(
237 void * /* pServiceManager */, void * pRegistryKey
)
244 Reference
< XRegistryKey
> xNewKey(
245 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
246 OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME2
"/UNO/SERVICES") ) ) );
248 Sequence
< OUString
> aSNL
=
249 ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static();
250 const OUString
* pArray
= aSNL
.getConstArray();
251 for ( sal_Int32 nPos
= aSNL
.getLength(); nPos
--; )
252 xNewKey
->createKey( pArray
[nPos
] );
256 catch (InvalidRegistryException
&)
258 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
263 //==================================================================================================
264 void * SAL_CALL
component_getFactory(
265 const sal_Char
* pImplName
, void * pServiceManager
, void * /* pRegistryKey */ )
269 if (rtl_str_compare( pImplName
, IMPLNAME2
) == 0)
271 Reference
< XSingleServiceFactory
> xFactory( createSingleFactory(
272 reinterpret_cast< XMultiServiceFactory
* >( pServiceManager
),
273 OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2
) ),
274 ::excomp2_impl::ExampleComponent2_CreateInstance
,
275 ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static() ) );
280 pRet
= xFactory
.get();