xref: /AOO41X/main/odk/examples/DevelopersGuide/Components/CppComponent/service1_impl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <osl/interlck.h>
36*cdf0e10cSrcweir #include <osl/mutex.hxx>
37*cdf0e10cSrcweir #include <rtl/uuid.h>
38*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
42*cdf0e10cSrcweir #include <my_module/XSomething.hpp>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace ::rtl; // for OUString
46*cdf0e10cSrcweir using namespace ::com::sun::star; // for odk interfaces
47*cdf0e10cSrcweir using namespace ::com::sun::star::uno; // for basic types
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace my_sc_impl
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl()
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     Sequence< OUString > names(1);
55*cdf0e10cSrcweir     names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1"));
56*cdf0e10cSrcweir     return names;
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir OUString SAL_CALL getImplementationName_MyService1Impl()
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
62*cdf0e10cSrcweir                          "my_module.my_sc_implementation.MyService1") );
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir class MyService1Impl
67*cdf0e10cSrcweir     : public ::my_module::XSomething
68*cdf0e10cSrcweir     , public lang::XServiceInfo
69*cdf0e10cSrcweir     , public lang::XTypeProvider
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir     oslInterlockedCount m_refcount;
72*cdf0e10cSrcweir     OUString m_sData;
73*cdf0e10cSrcweir     // it's good practise to store the context for further use when you use
74*cdf0e10cSrcweir     // other UNO API's in your implementation
75*cdf0e10cSrcweir     Reference< XComponentContext > m_xContext;
76*cdf0e10cSrcweir public:
77*cdf0e10cSrcweir     inline MyService1Impl(Reference< XComponentContext > const & xContext) throw ()
78*cdf0e10cSrcweir         : m_refcount( 0 ),
79*cdf0e10cSrcweir           m_xContext(xContext)
80*cdf0e10cSrcweir         {}
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     virtual ~MyService1Impl() {}
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     // XInterface
85*cdf0e10cSrcweir     virtual Any SAL_CALL queryInterface( Type const & type )
86*cdf0e10cSrcweir         throw (RuntimeException);
87*cdf0e10cSrcweir     virtual void SAL_CALL acquire()
88*cdf0e10cSrcweir         throw ();
89*cdf0e10cSrcweir     virtual void SAL_CALL release()
90*cdf0e10cSrcweir         throw ();
91*cdf0e10cSrcweir     // XTypeProvider
92*cdf0e10cSrcweir     virtual Sequence< Type > SAL_CALL getTypes()
93*cdf0e10cSrcweir         throw (RuntimeException);
94*cdf0e10cSrcweir     virtual Sequence< sal_Int8 > SAL_CALL getImplementationId()
95*cdf0e10cSrcweir         throw (RuntimeException);
96*cdf0e10cSrcweir     // XSomething
97*cdf0e10cSrcweir     virtual OUString SAL_CALL methodOne( OUString const & str )
98*cdf0e10cSrcweir         throw (RuntimeException);
99*cdf0e10cSrcweir     virtual OUString SAL_CALL methodTwo( )
100*cdf0e10cSrcweir         throw (RuntimeException);
101*cdf0e10cSrcweir     // XServiceInfo
102*cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName()
103*cdf0e10cSrcweir         throw (RuntimeException);
104*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
105*cdf0e10cSrcweir         throw (RuntimeException);
106*cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
107*cdf0e10cSrcweir         throw (RuntimeException);
108*cdf0e10cSrcweir };
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir // XInterface implementation
111*cdf0e10cSrcweir Any MyService1Impl::queryInterface( Type const & type )
112*cdf0e10cSrcweir     throw (RuntimeException)
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     if (type.equals(::cppu::UnoType< Reference< XInterface > >::get()))
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir         // return XInterface interface
117*cdf0e10cSrcweir         // (resolve ambiguity by casting to lang::XTypeProvider)
118*cdf0e10cSrcweir         Reference< XInterface > x(
119*cdf0e10cSrcweir             static_cast< lang::XTypeProvider * >( this ) );
120*cdf0e10cSrcweir         return makeAny( x );
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir     if (type.equals(::cppu::UnoType< Reference< lang::XTypeProvider > >::get()))
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         // return XInterface interface
125*cdf0e10cSrcweir         Reference< XInterface > x(
126*cdf0e10cSrcweir             static_cast< lang::XTypeProvider * >( this ) );
127*cdf0e10cSrcweir         return makeAny( x );
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir     if (type.equals(::cppu::UnoType< Reference< lang::XServiceInfo > >::get()))
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         // return XServiceInfo interface
132*cdf0e10cSrcweir         Reference< lang::XServiceInfo > x(
133*cdf0e10cSrcweir             static_cast< lang::XServiceInfo * >( this ) );
134*cdf0e10cSrcweir         return makeAny( x );
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir     if (type.equals(::cppu::UnoType< Reference< ::my_module::XSomething > >::get()))
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         // return sample interface
139*cdf0e10cSrcweir         Reference< ::my_module::XSomething > x(
140*cdf0e10cSrcweir             static_cast< ::my_module::XSomething * >( this ) );
141*cdf0e10cSrcweir         return makeAny( x );
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir     // querying for unsupported type
144*cdf0e10cSrcweir     return Any();
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir void MyService1Impl::acquire()
148*cdf0e10cSrcweir     throw ()
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir     // thread-safe incrementation of reference count
151*cdf0e10cSrcweir     ::osl_incrementInterlockedCount( &m_refcount );
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir void MyService1Impl::release()
155*cdf0e10cSrcweir     throw ()
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir     // thread-safe decrementation of reference count
158*cdf0e10cSrcweir     if (0 == ::osl_decrementInterlockedCount( &m_refcount ))
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         delete this; // shutdown this object
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir // XTypeProvider implementation
165*cdf0e10cSrcweir Sequence< Type > MyService1Impl::getTypes()
166*cdf0e10cSrcweir     throw (RuntimeException)
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir     Sequence< Type > seq( 3 );
169*cdf0e10cSrcweir     seq[ 0 ] = ::cppu::UnoType< Reference< lang::XTypeProvider > >::get();
170*cdf0e10cSrcweir     seq[ 1 ] = ::cppu::UnoType< Reference< lang::XServiceInfo > >::get();
171*cdf0e10cSrcweir     seq[ 2 ] = ::cppu::UnoType< Reference< ::my_module::XSomething > >::get();
172*cdf0e10cSrcweir     return seq;
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir Sequence< sal_Int8 > MyService1Impl::getImplementationId()
175*cdf0e10cSrcweir     throw (RuntimeException)
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir     static Sequence< sal_Int8 > * s_pId = 0;
178*cdf0e10cSrcweir     if (! s_pId)
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         // create unique id
181*cdf0e10cSrcweir         Sequence< sal_Int8 > id( 16 );
182*cdf0e10cSrcweir         ::rtl_createUuid( (sal_uInt8 *)id.getArray(), 0, sal_True );
183*cdf0e10cSrcweir         // guard initialization with some mutex
184*cdf0e10cSrcweir         ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
185*cdf0e10cSrcweir         if (! s_pId)
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             static Sequence< sal_Int8 > s_id( id );
188*cdf0e10cSrcweir             s_pId = &s_id;
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir     return *s_pId;
192*cdf0e10cSrcweir }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir // XSomething implementation
195*cdf0e10cSrcweir OUString MyService1Impl::methodOne( OUString const & str )
196*cdf0e10cSrcweir     throw (RuntimeException)
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir     m_sData = str;
199*cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
200*cdf0e10cSrcweir         "called methodOne() of MyService1 implementation: ") ) + m_sData;
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir OUString MyService1Impl::methodTwo( )
204*cdf0e10cSrcweir     throw (RuntimeException)
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
207*cdf0e10cSrcweir         "called methodTwo() of MyService1 implementation: ") ) + m_sData;
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir // XServiceInfo implementation
211*cdf0e10cSrcweir OUString MyService1Impl::getImplementationName()
212*cdf0e10cSrcweir     throw (RuntimeException)
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir     // unique implementation name
215*cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
216*cdf0e10cSrcweir                          "my_module.my_sc_implementation.MyService1") );
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir sal_Bool MyService1Impl::supportsService( OUString const & serviceName )
219*cdf0e10cSrcweir     throw (RuntimeException)
220*cdf0e10cSrcweir {
221*cdf0e10cSrcweir     // this object only supports one service, so the test is simple
222*cdf0e10cSrcweir     return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
223*cdf0e10cSrcweir                                          "my_module.MyService1") );
224*cdf0e10cSrcweir }
225*cdf0e10cSrcweir Sequence< OUString > MyService1Impl::getSupportedServiceNames()
226*cdf0e10cSrcweir     throw (RuntimeException)
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir     // this object only supports one service
229*cdf0e10cSrcweir     OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") );
230*cdf0e10cSrcweir     return Sequence< OUString >( &serviceName, 1 );
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir Reference< XInterface > SAL_CALL create_MyService1Impl(
234*cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
235*cdf0e10cSrcweir     SAL_THROW( () )
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir     return static_cast< lang::XTypeProvider * >( new MyService1Impl( xContext) );
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir // forward decl: implemented in service2_impl.cxx
241*cdf0e10cSrcweir Reference< XInterface > SAL_CALL create_MyService2Impl(
242*cdf0e10cSrcweir     Reference< XComponentContext > const & ) SAL_THROW( () );
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir /*
247*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
248*cdf0e10cSrcweir     sal_Char const ** ppEnvTypeName, uno_Environment ** )
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
251*cdf0e10cSrcweir }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir // This method not longer necessary since OOo 3.4 where the component registration was
254*cdf0e10cSrcweir // was changed to passive component registration. For more details see
255*cdf0e10cSrcweir // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
256*cdf0e10cSrcweir //
257*cdf0e10cSrcweir // extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(
258*cdf0e10cSrcweir //     lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry )
259*cdf0e10cSrcweir // {
260*cdf0e10cSrcweir //     if (xRegistry)
261*cdf0e10cSrcweir //     {
262*cdf0e10cSrcweir //         try
263*cdf0e10cSrcweir //         {
264*cdf0e10cSrcweir //             // implementation of MyService1A
265*cdf0e10cSrcweir //             Reference< registry::XRegistryKey > xKey(
266*cdf0e10cSrcweir //                 xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
267*cdf0e10cSrcweir //                     "my_module.my_sc_implementation.MyService1/UNO/SERVICES") ) ) );
268*cdf0e10cSrcweir //             // subkeys denote implemented services of implementation
269*cdf0e10cSrcweir //             xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
270*cdf0e10cSrcweir //                 "my_module.MyService1") ) );
271*cdf0e10cSrcweir //             // implementation of MyService1B
272*cdf0e10cSrcweir //             xKey = xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
273*cdf0e10cSrcweir //                 "my_module.my_sc_implementation.MyService2/UNO/SERVICES") ) );
274*cdf0e10cSrcweir //             // subkeys denote implemented services of implementation
275*cdf0e10cSrcweir //             xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
276*cdf0e10cSrcweir //                 "my_module.MyService2") ) );
277*cdf0e10cSrcweir //             return sal_True; // success
278*cdf0e10cSrcweir //         }
279*cdf0e10cSrcweir //         catch (registry::InvalidRegistryException &)
280*cdf0e10cSrcweir //         {
281*cdf0e10cSrcweir //             // function fails if exception caught
282*cdf0e10cSrcweir //         }
283*cdf0e10cSrcweir //     }
284*cdf0e10cSrcweir //     return sal_False;
285*cdf0e10cSrcweir // }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
288*cdf0e10cSrcweir     sal_Char const * implName, lang::XMultiServiceFactory * xMgr, void * )
289*cdf0e10cSrcweir {
290*cdf0e10cSrcweir     Reference< lang::XSingleComponentFactory > xFactory;
291*cdf0e10cSrcweir     if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService1" ))
292*cdf0e10cSrcweir     {
293*cdf0e10cSrcweir         // create component factory for MyService1 implementation
294*cdf0e10cSrcweir         OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") );
295*cdf0e10cSrcweir         xFactory = ::cppu::createSingleComponentFactory(
296*cdf0e10cSrcweir             ::my_sc_impl::create_MyService1Impl,
297*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService1") ),
298*cdf0e10cSrcweir             Sequence< OUString >( &serviceName, 1 ) );
299*cdf0e10cSrcweir     }
300*cdf0e10cSrcweir     else if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService2" ))
301*cdf0e10cSrcweir     {
302*cdf0e10cSrcweir         // create component factory for MyService12 implementation
303*cdf0e10cSrcweir         OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2") );
304*cdf0e10cSrcweir         xFactory = ::cppu::createSingleComponentFactory(
305*cdf0e10cSrcweir             ::my_sc_impl::create_MyService2Impl,
306*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService2") ),
307*cdf0e10cSrcweir             Sequence< OUString >( &serviceName, 1 ) );
308*cdf0e10cSrcweir     }
309*cdf0e10cSrcweir     if (xFactory.is())
310*cdf0e10cSrcweir         xFactory->acquire();
311*cdf0e10cSrcweir     return xFactory.get(); // return acquired interface pointer or null
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir */
314*cdf0e10cSrcweir 
315