1 /*************************************************************************
3 * $RCSfile: service2_impl.cxx,v $
7 * last change: $Author: kz $ $Date: 2006-11-06 15:00:19 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 #include <cppuhelper/implbase3.hxx> // "3" implementing three interfaces
42 #include <cppuhelper/factory.hxx>
43 #include <cppuhelper/implementationentry.hxx>
45 #include <com/sun/star/lang/XServiceInfo.hpp>
46 #include <com/sun/star/lang/XInitialization.hpp>
47 #include <com/sun/star/lang/IllegalArgumentException.hpp>
48 #include <my_module/XSomething.hpp>
51 using namespace ::rtl
; // for OUString
52 using namespace ::com::sun::star
; // for odk interfaces
53 using namespace ::com::sun::star::uno
; // for basic types
59 extern Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_MyService1Impl();
60 extern OUString SAL_CALL
getImplementationName_MyService1Impl();
61 extern Reference
< XInterface
> SAL_CALL
create_MyService1Impl(
62 Reference
< XComponentContext
> const & xContext
)
65 static Sequence
< OUString
> getSupportedServiceNames_MyService2Impl()
67 Sequence
<OUString
> names(1);
68 names
[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2"));
72 static OUString
getImplementationName_MyService2Impl()
74 return OUString( RTL_CONSTASCII_USTRINGPARAM(
75 "my_module.my_sc_implementation.MyService2") );
78 class MyService2Impl
: public ::cppu::WeakImplHelper3
<
79 ::my_module::XSomething
, lang::XServiceInfo
, lang::XInitialization
>
82 // it's good practise to store the context for further use when you use
83 // other UNO API's in your implementation
84 Reference
< XComponentContext
> m_xContext
;
86 inline MyService2Impl(Reference
< XComponentContext
> const & xContext
) throw ()
87 : m_xContext(xContext
)
90 virtual ~MyService2Impl() {}
92 // focus on three given interfaces,
93 // no need to implement XInterface, XTypeProvider, XWeak
95 // XInitialization will be called upon
96 // createInstanceWithArguments[AndContext]()
97 virtual void SAL_CALL
initialize( Sequence
< Any
> const & args
)
100 virtual OUString SAL_CALL
methodOne( OUString
const & str
)
101 throw (RuntimeException
);
102 virtual OUString SAL_CALL
methodTwo( )
103 throw (RuntimeException
);
105 virtual OUString SAL_CALL
getImplementationName()
106 throw (RuntimeException
);
107 virtual sal_Bool SAL_CALL
supportsService( OUString
const & serviceName
)
108 throw (RuntimeException
);
109 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
110 throw (RuntimeException
);
113 // XInitialization implemention
114 void MyService2Impl::initialize( Sequence
< Any
> const & args
)
117 if (args
.getLength() != 1)
119 throw lang::IllegalArgumentException(
120 OUString( RTL_CONSTASCII_USTRINGPARAM(
121 "give a string instanciating this component!") ),
122 // resolve to XInterface reference:
123 static_cast< ::cppu::OWeakObject
* >(this),
126 if (! (args
[ 0 ] >>= m_sData
))
128 throw lang::IllegalArgumentException(
129 OUString( RTL_CONSTASCII_USTRINGPARAM(
130 "no string given as argument!") ),
131 // resolve to XInterface reference:
132 static_cast< ::cppu::OWeakObject
* >(this),
137 // XSomething implementation
138 OUString
MyService2Impl::methodOne( OUString
const & str
)
139 throw (RuntimeException
)
142 return OUString( RTL_CONSTASCII_USTRINGPARAM(
143 "called methodOne() of MyService2 implementation: ") ) + m_sData
;
146 OUString
MyService2Impl::methodTwo( )
147 throw (RuntimeException
)
149 return OUString( RTL_CONSTASCII_USTRINGPARAM(
150 "called methodTwo() of MyService2 implementation: ") ) + m_sData
;
153 // XServiceInfo implementation
154 OUString
MyService2Impl::getImplementationName()
155 throw (RuntimeException
)
157 // unique implementation name
158 return OUString( RTL_CONSTASCII_USTRINGPARAM(
159 "my_module.my_sc_implementation.MyService2") );
162 sal_Bool
MyService2Impl::supportsService( OUString
const & serviceName
)
163 throw (RuntimeException
)
165 // this object only supports one service, so the test is simple
166 return serviceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
167 "my_module.MyService2") );
170 Sequence
< OUString
> MyService2Impl::getSupportedServiceNames()
171 throw (RuntimeException
)
173 return getSupportedServiceNames_MyService2Impl();
176 Reference
< XInterface
> SAL_CALL
create_MyService2Impl(
177 Reference
< XComponentContext
> const & xContext
)
180 return static_cast< ::cppu::OWeakObject
* >( new MyService2Impl( xContext
) );
185 /* shared lib exports implemented without helpers in service_impl1.cxx */
188 static struct ::cppu::ImplementationEntry s_component_entries
[] =
191 create_MyService1Impl
, getImplementationName_MyService1Impl
,
192 getSupportedServiceNames_MyService1Impl
,
193 ::cppu::createSingleComponentFactory
,
197 create_MyService2Impl
, getImplementationName_MyService2Impl
,
198 getSupportedServiceNames_MyService2Impl
,
199 ::cppu::createSingleComponentFactory
,
208 void SAL_CALL
component_getImplementationEnvironment(
209 sal_Char
const ** ppEnvTypeName
, uno_Environment
** )
211 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
214 sal_Bool SAL_CALL
component_writeInfo(
215 lang::XMultiServiceFactory
* xMgr
, registry::XRegistryKey
* xRegistry
)
217 return ::cppu::component_writeInfoHelper(
218 xMgr
, xRegistry
, ::my_sc_impl::s_component_entries
);
221 void * SAL_CALL
component_getFactory(
222 sal_Char
const * implName
, lang::XMultiServiceFactory
* xMgr
,
223 registry::XRegistryKey
* xRegistry
)
225 return ::cppu::component_getFactoryHelper(
226 implName
, xMgr
, xRegistry
, ::my_sc_impl::s_component_entries
);