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 <osl/interlck.h>
25 #include <osl/mutex.hxx>
26 #include <rtl/uuid.h>
27 #include <cppuhelper/factory.hxx>
28 
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/XTypeProvider.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 namespace my_sc_impl
39 {
40 
getSupportedServiceNames_MyService1Impl()41 Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl()
42 {
43     Sequence< OUString > names(1);
44     names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1"));
45     return names;
46 }
47 
getImplementationName_MyService1Impl()48 OUString SAL_CALL getImplementationName_MyService1Impl()
49 {
50     return OUString( RTL_CONSTASCII_USTRINGPARAM(
51                          "my_module.my_sc_implementation.MyService1") );
52 }
53 
54 
55 class MyService1Impl
56     : public ::my_module::XSomething
57     , public lang::XServiceInfo
58     , public lang::XTypeProvider
59 {
60     oslInterlockedCount m_refcount;
61     OUString m_sData;
62     // it's good practice to store the context for further use when you use
63     // other UNO API's in your implementation
64     Reference< XComponentContext > m_xContext;
65 public:
MyService1Impl(Reference<XComponentContext> const & xContext)66     inline MyService1Impl(Reference< XComponentContext > const & xContext) throw ()
67         : m_refcount( 0 ),
68           m_xContext(xContext)
69         {}
70 
~MyService1Impl()71     virtual ~MyService1Impl() {}
72 
73     // XInterface
74     virtual Any SAL_CALL queryInterface( Type const & type )
75         throw (RuntimeException);
76     virtual void SAL_CALL acquire()
77         throw ();
78     virtual void SAL_CALL release()
79         throw ();
80     // XTypeProvider
81     virtual Sequence< Type > SAL_CALL getTypes()
82         throw (RuntimeException);
83     virtual Sequence< sal_Int8 > SAL_CALL getImplementationId()
84         throw (RuntimeException);
85     // XSomething
86     virtual OUString SAL_CALL methodOne( OUString const & str )
87         throw (RuntimeException);
88     virtual OUString SAL_CALL methodTwo( )
89         throw (RuntimeException);
90     // XServiceInfo
91     virtual OUString SAL_CALL getImplementationName()
92         throw (RuntimeException);
93     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
94         throw (RuntimeException);
95     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
96         throw (RuntimeException);
97 };
98 
99 // XInterface implementation
queryInterface(Type const & type)100 Any MyService1Impl::queryInterface( Type const & type )
101     throw (RuntimeException)
102 {
103     if (type.equals(::cppu::UnoType< Reference< XInterface > >::get()))
104     {
105         // return XInterface interface
106         // (resolve ambiguity by casting to lang::XTypeProvider)
107         Reference< XInterface > x(
108             static_cast< lang::XTypeProvider * >( this ) );
109         return makeAny( x );
110     }
111     if (type.equals(::cppu::UnoType< Reference< lang::XTypeProvider > >::get()))
112     {
113         // return XInterface interface
114         Reference< XInterface > x(
115             static_cast< lang::XTypeProvider * >( this ) );
116         return makeAny( x );
117     }
118     if (type.equals(::cppu::UnoType< Reference< lang::XServiceInfo > >::get()))
119     {
120         // return XServiceInfo interface
121         Reference< lang::XServiceInfo > x(
122             static_cast< lang::XServiceInfo * >( this ) );
123         return makeAny( x );
124     }
125     if (type.equals(::cppu::UnoType< Reference< ::my_module::XSomething > >::get()))
126     {
127         // return sample interface
128         Reference< ::my_module::XSomething > x(
129             static_cast< ::my_module::XSomething * >( this ) );
130         return makeAny( x );
131     }
132     // querying for unsupported type
133     return Any();
134 }
135 
acquire()136 void MyService1Impl::acquire()
137     throw ()
138 {
139     // thread-safe incrementation of reference count
140     ::osl_incrementInterlockedCount( &m_refcount );
141 }
142 
release()143 void MyService1Impl::release()
144     throw ()
145 {
146     // thread-safe decrementation of reference count
147     if (0 == ::osl_decrementInterlockedCount( &m_refcount ))
148     {
149         delete this; // shutdown this object
150     }
151 }
152 
153 // XTypeProvider implementation
getTypes()154 Sequence< Type > MyService1Impl::getTypes()
155     throw (RuntimeException)
156 {
157     Sequence< Type > seq( 3 );
158     seq[ 0 ] = ::cppu::UnoType< Reference< lang::XTypeProvider > >::get();
159     seq[ 1 ] = ::cppu::UnoType< Reference< lang::XServiceInfo > >::get();
160     seq[ 2 ] = ::cppu::UnoType< Reference< ::my_module::XSomething > >::get();
161     return seq;
162 }
getImplementationId()163 Sequence< sal_Int8 > MyService1Impl::getImplementationId()
164     throw (RuntimeException)
165 {
166     static Sequence< sal_Int8 > * s_pId = 0;
167     if (! s_pId)
168     {
169         // create unique id
170         Sequence< sal_Int8 > id( 16 );
171         ::rtl_createUuid( (sal_uInt8 *)id.getArray(), 0, sal_True );
172         // guard initialization with some mutex
173         ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
174         if (! s_pId)
175         {
176             static Sequence< sal_Int8 > s_id( id );
177             s_pId = &s_id;
178         }
179     }
180     return *s_pId;
181 }
182 
183 // XSomething implementation
methodOne(OUString const & str)184 OUString MyService1Impl::methodOne( OUString const & str )
185     throw (RuntimeException)
186 {
187     m_sData = str;
188     return OUString( RTL_CONSTASCII_USTRINGPARAM(
189         "called methodOne() of MyService1 implementation: ") ) + m_sData;
190 }
191 
methodTwo()192 OUString MyService1Impl::methodTwo( )
193     throw (RuntimeException)
194 {
195     return OUString( RTL_CONSTASCII_USTRINGPARAM(
196         "called methodTwo() of MyService1 implementation: ") ) + m_sData;
197 }
198 
199 // XServiceInfo implementation
getImplementationName()200 OUString MyService1Impl::getImplementationName()
201     throw (RuntimeException)
202 {
203     // unique implementation name
204     return OUString( RTL_CONSTASCII_USTRINGPARAM(
205                          "my_module.my_sc_implementation.MyService1") );
206 }
supportsService(OUString const & serviceName)207 sal_Bool MyService1Impl::supportsService( OUString const & serviceName )
208     throw (RuntimeException)
209 {
210     // this object only supports one service, so the test is simple
211     return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
212                                          "my_module.MyService1") );
213 }
getSupportedServiceNames()214 Sequence< OUString > MyService1Impl::getSupportedServiceNames()
215     throw (RuntimeException)
216 {
217     // this object only supports one service
218     OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") );
219     return Sequence< OUString >( &serviceName, 1 );
220 }
221 
create_MyService1Impl(Reference<XComponentContext> const & xContext)222 Reference< XInterface > SAL_CALL create_MyService1Impl(
223     Reference< XComponentContext > const & xContext )
224     SAL_THROW( () )
225 {
226     return static_cast< lang::XTypeProvider * >( new MyService1Impl( xContext) );
227 }
228 
229 // forward decl: implemented in service2_impl.cxx
230 Reference< XInterface > SAL_CALL create_MyService2Impl(
231     Reference< XComponentContext > const & ) SAL_THROW( () );
232 
233 }
234 
235 /*
236 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
237     sal_Char const ** ppEnvTypeName, uno_Environment ** )
238 {
239     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
240 }
241 
242 // This method not longer necessary since OOo 3.4 where the component registration was
243 // was changed to passive component registration. For more details see
244 // https://wiki.openoffice.org/wiki/Passive_Component_Registration
245 //
246 // extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(
247 //     lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry )
248 // {
249 //     if (xRegistry)
250 //     {
251 //         try
252 //         {
253 //             // implementation of MyService1A
254 //             Reference< registry::XRegistryKey > xKey(
255 //                 xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
256 //                     "my_module.my_sc_implementation.MyService1/UNO/SERVICES") ) ) );
257 //             // subkeys denote implemented services of implementation
258 //             xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
259 //                 "my_module.MyService1") ) );
260 //             // implementation of MyService1B
261 //             xKey = xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
262 //                 "my_module.my_sc_implementation.MyService2/UNO/SERVICES") ) );
263 //             // subkeys denote implemented services of implementation
264 //             xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
265 //                 "my_module.MyService2") ) );
266 //             return sal_True; // success
267 //         }
268 //         catch (registry::InvalidRegistryException &)
269 //         {
270 //             // function fails if exception caught
271 //         }
272 //     }
273 //     return sal_False;
274 // }
275 
276 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
277     sal_Char const * implName, lang::XMultiServiceFactory * xMgr, void * )
278 {
279     Reference< lang::XSingleComponentFactory > xFactory;
280     if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService1" ))
281     {
282         // create component factory for MyService1 implementation
283         OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") );
284         xFactory = ::cppu::createSingleComponentFactory(
285             ::my_sc_impl::create_MyService1Impl,
286             OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService1") ),
287             Sequence< OUString >( &serviceName, 1 ) );
288     }
289     else if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService2" ))
290     {
291         // create component factory for MyService12 implementation
292         OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2") );
293         xFactory = ::cppu::createSingleComponentFactory(
294             ::my_sc_impl::create_MyService2Impl,
295             OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService2") ),
296             Sequence< OUString >( &serviceName, 1 ) );
297     }
298     if (xFactory.is())
299         xFactory->acquire();
300     return xFactory.get(); // return acquired interface pointer or null
301 }
302 */
303 
304