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