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 practise 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 throw (Exception);
82 // XSomething
83 virtual OUString SAL_CALL methodOne( OUString const & str )
84 throw (RuntimeException);
85 virtual OUString SAL_CALL methodTwo( )
86 throw (RuntimeException);
87 // XServiceInfo
88 virtual OUString SAL_CALL getImplementationName()
89 throw (RuntimeException);
90 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
91 throw (RuntimeException);
92 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
93 throw (RuntimeException);
94 };
95
96 // XInitialization implemention
initialize(Sequence<Any> const & args)97 void MyService2Impl::initialize( Sequence< Any > const & args )
98 throw (Exception)
99 {
100 if (args.getLength() != 1)
101 {
102 throw lang::IllegalArgumentException(
103 OUString( RTL_CONSTASCII_USTRINGPARAM(
104 "give a string instanciating this component!") ),
105 // resolve to XInterface reference:
106 static_cast< ::cppu::OWeakObject * >(this),
107 0 ); // argument pos
108 }
109 if (! (args[ 0 ] >>= m_sData))
110 {
111 throw lang::IllegalArgumentException(
112 OUString( RTL_CONSTASCII_USTRINGPARAM(
113 "no string given as argument!") ),
114 // resolve to XInterface reference:
115 static_cast< ::cppu::OWeakObject * >(this),
116 0 ); // argument pos
117 }
118 }
119
120 // XSomething implementation
methodOne(OUString const & str)121 OUString MyService2Impl::methodOne( OUString const & str )
122 throw (RuntimeException)
123 {
124 m_sData = str;
125 return OUString( RTL_CONSTASCII_USTRINGPARAM(
126 "called methodOne() of MyService2 implementation: ") ) + m_sData;
127 }
128
methodTwo()129 OUString MyService2Impl::methodTwo( )
130 throw (RuntimeException)
131 {
132 return OUString( RTL_CONSTASCII_USTRINGPARAM(
133 "called methodTwo() of MyService2 implementation: ") ) + m_sData;
134 }
135
136 // XServiceInfo implementation
getImplementationName()137 OUString MyService2Impl::getImplementationName()
138 throw (RuntimeException)
139 {
140 // unique implementation name
141 return OUString( RTL_CONSTASCII_USTRINGPARAM(
142 "my_module.my_sc_implementation.MyService2") );
143 }
144
supportsService(OUString const & serviceName)145 sal_Bool MyService2Impl::supportsService( OUString const & serviceName )
146 throw (RuntimeException)
147 {
148 // this object only supports one service, so the test is simple
149 return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
150 "my_module.MyService2") );
151 }
152
getSupportedServiceNames()153 Sequence< OUString > MyService2Impl::getSupportedServiceNames()
154 throw (RuntimeException)
155 {
156 return getSupportedServiceNames_MyService2Impl();
157 }
158
create_MyService2Impl(Reference<XComponentContext> const & xContext)159 Reference< XInterface > SAL_CALL create_MyService2Impl(
160 Reference< XComponentContext > const & xContext )
161 SAL_THROW( () )
162 {
163 return static_cast< ::cppu::OWeakObject * >( new MyService2Impl( xContext ) );
164 }
165
166 }
167
168 /* shared lib exports implemented without helpers in service_impl1.cxx */
169 namespace my_sc_impl
170 {
171 static struct ::cppu::ImplementationEntry s_component_entries [] =
172 {
173 {
174 create_MyService1Impl, getImplementationName_MyService1Impl,
175 getSupportedServiceNames_MyService1Impl,
176 ::cppu::createSingleComponentFactory,
177 0, 0
178 },
179 {
180 create_MyService2Impl, getImplementationName_MyService2Impl,
181 getSupportedServiceNames_MyService2Impl,
182 ::cppu::createSingleComponentFactory,
183 0, 0
184 },
185 { 0, 0, 0, 0, 0, 0 }
186 };
187 }
188
189 extern "C"
190 {
component_getImplementationEnvironment(sal_Char const ** ppEnvTypeName,uno_Environment **)191 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
192 sal_Char const ** ppEnvTypeName, uno_Environment ** )
193 {
194 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
195 }
196
197 // This method not longer necessary since OOo 3.4 where the component registration was
198 // was changed to passive component registration. For more details see
199 // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
200 //
201 // sal_Bool SAL_CALL component_writeInfo(
202 // lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry )
203 // {
204 // return ::cppu::component_writeInfoHelper(
205 // xMgr, xRegistry, ::my_sc_impl::s_component_entries );
206 // }
207
208
component_getFactory(sal_Char const * implName,lang::XMultiServiceFactory * xMgr,registry::XRegistryKey * xRegistry)209 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
210 sal_Char const * implName, lang::XMultiServiceFactory * xMgr,
211 registry::XRegistryKey * xRegistry )
212 {
213 return ::cppu::component_getFactoryHelper(
214 implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries );
215 }
216
217 }
218
219
220