xref: /trunk/main/stoc/test/excomp/excomp2.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_stoc.hxx"
30*cdf0e10cSrcweir #include <osl/diagnose.h>
31*cdf0e10cSrcweir #include <osl/mutex.hxx>
32*cdf0e10cSrcweir #include <rtl/alloc.h>
33*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
34*cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx>
35*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
36*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <example/XTest.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace example;
46*cdf0e10cSrcweir using namespace com::sun::star::uno;
47*cdf0e10cSrcweir using namespace com::sun::star::lang;
48*cdf0e10cSrcweir using namespace com::sun::star::registry;
49*cdf0e10cSrcweir using namespace cppu;
50*cdf0e10cSrcweir using namespace osl;
51*cdf0e10cSrcweir using namespace rtl;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #define SERVICENAME2 "example.ExampleComponent2"
54*cdf0e10cSrcweir #define IMPLNAME2   "example.ExampleComponent2.Impl"
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir namespace excomp2_impl {
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir //*************************************************************************
59*cdf0e10cSrcweir // ExampleComponent2Impl
60*cdf0e10cSrcweir //*************************************************************************
61*cdf0e10cSrcweir class ExampleComponent2Impl : public OWeakObject
62*cdf0e10cSrcweir                             , public XTypeProvider
63*cdf0e10cSrcweir                             , public XServiceInfo
64*cdf0e10cSrcweir                             , public XTest
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir public:
67*cdf0e10cSrcweir     ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr );
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     ~ExampleComponent2Impl();
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     // XInterface
72*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
73*cdf0e10cSrcweir     virtual void SAL_CALL acquire() throw()
74*cdf0e10cSrcweir         { OWeakObject::acquire(); }
75*cdf0e10cSrcweir     virtual void SAL_CALL release() throw()
76*cdf0e10cSrcweir         { OWeakObject::release(); }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     // XTypeProvider
79*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
80*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() throw (::com::sun::star::uno::RuntimeException);
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     // XServiceInfo
83*cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
84*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
85*cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
86*cdf0e10cSrcweir     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     // XTest
89*cdf0e10cSrcweir     virtual OUString SAL_CALL getMessage() throw(RuntimeException);
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir protected:
92*cdf0e10cSrcweir     Mutex       m_mutex;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     Reference<XMultiServiceFactory> m_xSMgr;
95*cdf0e10cSrcweir };
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir //*************************************************************************
98*cdf0e10cSrcweir ExampleComponent2Impl::ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr )
99*cdf0e10cSrcweir     : m_xSMgr(rXSMgr)
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir //*************************************************************************
104*cdf0e10cSrcweir ExampleComponent2Impl::~ExampleComponent2Impl()
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir //*************************************************************************
109*cdf0e10cSrcweir Any SAL_CALL ExampleComponent2Impl::queryInterface( const ::com::sun::star::uno::Type & rType )
110*cdf0e10cSrcweir     throw(::com::sun::star::uno::RuntimeException)
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     Any aRet = ::cppu::queryInterface(rType,
113*cdf0e10cSrcweir                                       static_cast< XTypeProvider * >( this ),
114*cdf0e10cSrcweir                                       static_cast< XServiceInfo * >( this ),
115*cdf0e10cSrcweir                                       static_cast< XTest * >( this ) );
116*cdf0e10cSrcweir     if ( aRet.hasValue() )
117*cdf0e10cSrcweir         return aRet;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     return OWeakObject::queryInterface( rType );
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir //*************************************************************************
123*cdf0e10cSrcweir Sequence< Type > SAL_CALL ExampleComponent2Impl::getTypes()
124*cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     static OTypeCollection * pTypes = 0;
127*cdf0e10cSrcweir     if (! pTypes)
128*cdf0e10cSrcweir     {
129*cdf0e10cSrcweir         MutexGuard aGuard( m_mutex );
130*cdf0e10cSrcweir         if (! pTypes)
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             static OTypeCollection aTypes(
133*cdf0e10cSrcweir                 ::getCppuType( (const Reference< XInterface > *)0 ),
134*cdf0e10cSrcweir                 ::getCppuType( (const Reference< XWeak > *)0 ),
135*cdf0e10cSrcweir                 ::getCppuType( (const Reference< XTypeProvider > *)0 ),
136*cdf0e10cSrcweir                 ::getCppuType( (const Reference< XServiceInfo > *)0 ),
137*cdf0e10cSrcweir                 ::getCppuType( (const Reference< XTest > *)0 ) );
138*cdf0e10cSrcweir             pTypes = &aTypes;
139*cdf0e10cSrcweir         }
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir     return pTypes->getTypes();
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir //*************************************************************************
145*cdf0e10cSrcweir Sequence< sal_Int8 > SAL_CALL ExampleComponent2Impl::getImplementationId()
146*cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir     static OImplementationId * pId = 0;
149*cdf0e10cSrcweir     if (! pId)
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         MutexGuard aGuard( m_mutex );
152*cdf0e10cSrcweir         if (! pId)
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             static OImplementationId aId;
155*cdf0e10cSrcweir             pId = &aId;
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir     return pId->getImplementationId();
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir //*************************************************************************
162*cdf0e10cSrcweir OUString SAL_CALL ExampleComponent2Impl::getImplementationName(  )
163*cdf0e10cSrcweir     throw(RuntimeException)
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
166*cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) );
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir //*************************************************************************
170*cdf0e10cSrcweir sal_Bool SAL_CALL ExampleComponent2Impl::supportsService( const OUString& ServiceName )
171*cdf0e10cSrcweir     throw(RuntimeException)
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
174*cdf0e10cSrcweir     Sequence< OUString > aSNL = getSupportedServiceNames();
175*cdf0e10cSrcweir     const OUString * pArray = aSNL.getConstArray();
176*cdf0e10cSrcweir     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
177*cdf0e10cSrcweir         if( pArray[i] == ServiceName )
178*cdf0e10cSrcweir             return sal_True;
179*cdf0e10cSrcweir     return sal_False;
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir //*************************************************************************
183*cdf0e10cSrcweir Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames(  )
184*cdf0e10cSrcweir     throw(RuntimeException)
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
187*cdf0e10cSrcweir     return getSupportedServiceNames_Static();
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir //*************************************************************************
191*cdf0e10cSrcweir Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames_Static(  )
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir     OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2) );
194*cdf0e10cSrcweir     return Sequence< OUString >( &aName, 1 );
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir //*************************************************************************
198*cdf0e10cSrcweir OUString SAL_CALL ExampleComponent2Impl::getMessage() throw(RuntimeException)
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
201*cdf0e10cSrcweir     return OUString::createFromAscii("Alle meine Entchen schwimmen auf dem See, schwimmen auf dem See ...");
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir //*************************************************************************
206*cdf0e10cSrcweir Reference<XInterface> SAL_CALL ExampleComponent2_CreateInstance( const Reference<XMultiServiceFactory>& rSMgr )
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir     Reference<XInterface> xRet;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     XTest *pXTest = (XTest*) new ExampleComponent2Impl(rSMgr);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     if (pXTest)
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir         xRet = Reference< XInterface >::query(pXTest);
215*cdf0e10cSrcweir     }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     return xRet;
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir } // excomp_impl
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir extern "C"
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir //==================================================================================================
227*cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
228*cdf0e10cSrcweir     const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir //==================================================================================================
233*cdf0e10cSrcweir sal_Bool SAL_CALL component_writeInfo(
234*cdf0e10cSrcweir     void * /* pServiceManager */, void * pRegistryKey )
235*cdf0e10cSrcweir {
236*cdf0e10cSrcweir     if (pRegistryKey)
237*cdf0e10cSrcweir     {
238*cdf0e10cSrcweir         try
239*cdf0e10cSrcweir         {
240*cdf0e10cSrcweir             // ExampleComponent2
241*cdf0e10cSrcweir             Reference< XRegistryKey > xNewKey(
242*cdf0e10cSrcweir                 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
243*cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME2 "/UNO/SERVICES") ) ) );
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir             const Sequence< OUString > & rSNL =
246*cdf0e10cSrcweir                 ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static();
247*cdf0e10cSrcweir             const OUString * pArray = rSNL.getConstArray();
248*cdf0e10cSrcweir             for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
249*cdf0e10cSrcweir                 xNewKey->createKey( pArray[nPos] );
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir             return sal_True;
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir         catch (InvalidRegistryException &)
254*cdf0e10cSrcweir         {
255*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
256*cdf0e10cSrcweir         }
257*cdf0e10cSrcweir     }
258*cdf0e10cSrcweir     return sal_False;
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir //==================================================================================================
261*cdf0e10cSrcweir void * SAL_CALL component_getFactory(
262*cdf0e10cSrcweir     const sal_Char * pImplName, void * pServiceManager, void * /* pRegistryKey */ )
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir     void * pRet = 0;
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     if (rtl_str_compare( pImplName, IMPLNAME2 ) == 0)
267*cdf0e10cSrcweir     {
268*cdf0e10cSrcweir         Reference< XSingleServiceFactory > xFactory( createSingleFactory(
269*cdf0e10cSrcweir             reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
270*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) ),
271*cdf0e10cSrcweir             ::excomp2_impl::ExampleComponent2_CreateInstance,
272*cdf0e10cSrcweir             ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static() ) );
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir         if (xFactory.is())
275*cdf0e10cSrcweir         {
276*cdf0e10cSrcweir             xFactory->acquire();
277*cdf0e10cSrcweir             pRet = xFactory.get();
278*cdf0e10cSrcweir         }
279*cdf0e10cSrcweir     }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     return pRet;
282*cdf0e10cSrcweir }
283*cdf0e10cSrcweir }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 
287