1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx> // "3" implementing three interfaces
25cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
26cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
31cdf0e10cSrcweir #include <my_module/XSomething.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::rtl; // for OUString
35cdf0e10cSrcweir using namespace ::com::sun::star; // for odk interfaces
36cdf0e10cSrcweir using namespace ::com::sun::star::uno; // for basic types
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace my_sc_impl
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir extern Sequence< OUString > SAL_CALL  getSupportedServiceNames_MyService1Impl();
43cdf0e10cSrcweir extern OUString SAL_CALL getImplementationName_MyService1Impl();
44cdf0e10cSrcweir extern Reference< XInterface > SAL_CALL create_MyService1Impl(
45cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
46cdf0e10cSrcweir     SAL_THROW( () );
47cdf0e10cSrcweir 
getSupportedServiceNames_MyService2Impl()48cdf0e10cSrcweir static Sequence< OUString > getSupportedServiceNames_MyService2Impl()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     Sequence<OUString> names(1);
51cdf0e10cSrcweir     names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2"));
52cdf0e10cSrcweir     return names;
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
getImplementationName_MyService2Impl()55cdf0e10cSrcweir static OUString getImplementationName_MyService2Impl()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
58cdf0e10cSrcweir                          "my_module.my_sc_implementation.MyService2") );
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir class MyService2Impl : public ::cppu::WeakImplHelper3<
62cdf0e10cSrcweir       ::my_module::XSomething, lang::XServiceInfo, lang::XInitialization >
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     OUString m_sData;
65cdf0e10cSrcweir     // it's good practise to store the context for further use when you use
66cdf0e10cSrcweir     // other UNO API's in your implementation
67cdf0e10cSrcweir     Reference< XComponentContext > m_xContext;
68cdf0e10cSrcweir public:
MyService2Impl(Reference<XComponentContext> const & xContext)69cdf0e10cSrcweir     inline MyService2Impl(Reference< XComponentContext > const & xContext) throw ()
70cdf0e10cSrcweir         : m_xContext(xContext)
71cdf0e10cSrcweir         {}
72cdf0e10cSrcweir 
~MyService2Impl()73cdf0e10cSrcweir     virtual ~MyService2Impl() {}
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     // focus on three given interfaces,
76cdf0e10cSrcweir     // no need to implement XInterface, XTypeProvider, XWeak
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     // XInitialization will be called upon
79cdf0e10cSrcweir     // createInstanceWithArguments[AndContext]()
80cdf0e10cSrcweir     virtual void SAL_CALL initialize( Sequence< Any > const & args )
81cdf0e10cSrcweir         throw (Exception);
82cdf0e10cSrcweir     // XSomething
83cdf0e10cSrcweir     virtual OUString SAL_CALL methodOne( OUString const & str )
84cdf0e10cSrcweir         throw (RuntimeException);
85cdf0e10cSrcweir     virtual OUString SAL_CALL methodTwo( )
86cdf0e10cSrcweir         throw (RuntimeException);
87cdf0e10cSrcweir     // XServiceInfo
88cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName()
89cdf0e10cSrcweir         throw (RuntimeException);
90cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
91cdf0e10cSrcweir         throw (RuntimeException);
92cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
93cdf0e10cSrcweir         throw (RuntimeException);
94cdf0e10cSrcweir };
95cdf0e10cSrcweir 
96cdf0e10cSrcweir // XInitialization implemention
initialize(Sequence<Any> const & args)97cdf0e10cSrcweir void MyService2Impl::initialize( Sequence< Any > const & args )
98cdf0e10cSrcweir     throw (Exception)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     if (args.getLength() != 1)
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         throw lang::IllegalArgumentException(
103cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(
104cdf0e10cSrcweir                           "give a string instanciating this component!") ),
105cdf0e10cSrcweir             // resolve to XInterface reference:
106cdf0e10cSrcweir             static_cast< ::cppu::OWeakObject * >(this),
107cdf0e10cSrcweir             0 ); // argument pos
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir     if (! (args[ 0 ] >>= m_sData))
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         throw lang::IllegalArgumentException(
112cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(
113cdf0e10cSrcweir                           "no string given as argument!") ),
114cdf0e10cSrcweir             // resolve to XInterface reference:
115cdf0e10cSrcweir             static_cast< ::cppu::OWeakObject * >(this),
116cdf0e10cSrcweir             0 ); // argument pos
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir // XSomething implementation
methodOne(OUString const & str)121cdf0e10cSrcweir OUString MyService2Impl::methodOne( OUString const & str )
122cdf0e10cSrcweir     throw (RuntimeException)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     m_sData = str;
125cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
126cdf0e10cSrcweir         "called methodOne() of MyService2 implementation: ") ) + m_sData;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
methodTwo()129cdf0e10cSrcweir OUString MyService2Impl::methodTwo( )
130cdf0e10cSrcweir     throw (RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
133cdf0e10cSrcweir         "called methodTwo() of MyService2 implementation: ") ) + m_sData;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir // XServiceInfo implementation
getImplementationName()137cdf0e10cSrcweir OUString MyService2Impl::getImplementationName()
138cdf0e10cSrcweir     throw (RuntimeException)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     // unique implementation name
141cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(
142cdf0e10cSrcweir                          "my_module.my_sc_implementation.MyService2") );
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
supportsService(OUString const & serviceName)145cdf0e10cSrcweir sal_Bool MyService2Impl::supportsService( OUString const & serviceName )
146cdf0e10cSrcweir     throw (RuntimeException)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     // this object only supports one service, so the test is simple
149cdf0e10cSrcweir     return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
150cdf0e10cSrcweir                                          "my_module.MyService2") );
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
getSupportedServiceNames()153cdf0e10cSrcweir Sequence< OUString > MyService2Impl::getSupportedServiceNames()
154cdf0e10cSrcweir     throw (RuntimeException)
155cdf0e10cSrcweir {
156cdf0e10cSrcweir     return getSupportedServiceNames_MyService2Impl();
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
create_MyService2Impl(Reference<XComponentContext> const & xContext)159cdf0e10cSrcweir Reference< XInterface > SAL_CALL create_MyService2Impl(
160cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
161cdf0e10cSrcweir     SAL_THROW( () )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >( new MyService2Impl( xContext ) );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir /* shared lib exports implemented without helpers in service_impl1.cxx */
169cdf0e10cSrcweir namespace my_sc_impl
170cdf0e10cSrcweir {
171cdf0e10cSrcweir static struct ::cppu::ImplementationEntry s_component_entries [] =
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         create_MyService1Impl, getImplementationName_MyService1Impl,
175cdf0e10cSrcweir         getSupportedServiceNames_MyService1Impl,
176cdf0e10cSrcweir         ::cppu::createSingleComponentFactory,
177cdf0e10cSrcweir         0, 0
178cdf0e10cSrcweir     },
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         create_MyService2Impl, getImplementationName_MyService2Impl,
181cdf0e10cSrcweir         getSupportedServiceNames_MyService2Impl,
182cdf0e10cSrcweir         ::cppu::createSingleComponentFactory,
183cdf0e10cSrcweir         0, 0
184cdf0e10cSrcweir     },
185cdf0e10cSrcweir     { 0, 0, 0, 0, 0, 0 }
186cdf0e10cSrcweir };
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir extern "C"
190cdf0e10cSrcweir {
component_getImplementationEnvironment(sal_Char const ** ppEnvTypeName,uno_Environment **)191cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
192cdf0e10cSrcweir     sal_Char const ** ppEnvTypeName, uno_Environment ** )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir // This method not longer necessary since OOo 3.4 where the component registration was
198cdf0e10cSrcweir // was changed to passive component registration. For more details see
199cdf0e10cSrcweir // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
200cdf0e10cSrcweir //
201cdf0e10cSrcweir // sal_Bool SAL_CALL component_writeInfo(
202cdf0e10cSrcweir //     lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry )
203cdf0e10cSrcweir // {
204cdf0e10cSrcweir //     return ::cppu::component_writeInfoHelper(
205cdf0e10cSrcweir //         xMgr, xRegistry, ::my_sc_impl::s_component_entries );
206cdf0e10cSrcweir // }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 
component_getFactory(sal_Char const * implName,lang::XMultiServiceFactory * xMgr,registry::XRegistryKey * xRegistry)209cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
210cdf0e10cSrcweir     sal_Char const * implName, lang::XMultiServiceFactory * xMgr,
211cdf0e10cSrcweir     registry::XRegistryKey * xRegistry )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     return ::cppu::component_getFactoryHelper(
214cdf0e10cSrcweir         implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries );
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 
220