xref: /AOO42X/main/stoc/test/excomp/excomp2.cxx (revision b1c5455db1639c48e26c568e4fa7ee78ca5d60ee)
1*647a425cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647a425cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647a425cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647a425cSAndrew Rist  * distributed with this work for additional information
6*647a425cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647a425cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647a425cSAndrew Rist  * "License"); you may not use this file except in compliance
9*647a425cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*647a425cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647a425cSAndrew Rist  * software distributed under the License is distributed on an
15*647a425cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647a425cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647a425cSAndrew Rist  * specific language governing permissions and limitations
18*647a425cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*647a425cSAndrew Rist  *************************************************************/
21*647a425cSAndrew Rist 
22*647a425cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_stoc.hxx"
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include <osl/mutex.hxx>
28cdf0e10cSrcweir #include <rtl/alloc.h>
29cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
30cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx>
31cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
32cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <example/XTest.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
38cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace example;
42cdf0e10cSrcweir using namespace com::sun::star::uno;
43cdf0e10cSrcweir using namespace com::sun::star::lang;
44cdf0e10cSrcweir using namespace com::sun::star::registry;
45cdf0e10cSrcweir using namespace cppu;
46cdf0e10cSrcweir using namespace osl;
47cdf0e10cSrcweir using namespace rtl;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #define SERVICENAME2 "example.ExampleComponent2"
50cdf0e10cSrcweir #define IMPLNAME2   "example.ExampleComponent2.Impl"
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace excomp2_impl {
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //*************************************************************************
55cdf0e10cSrcweir // ExampleComponent2Impl
56cdf0e10cSrcweir //*************************************************************************
57cdf0e10cSrcweir class ExampleComponent2Impl : public OWeakObject
58cdf0e10cSrcweir                             , public XTypeProvider
59cdf0e10cSrcweir                             , public XServiceInfo
60cdf0e10cSrcweir                             , public XTest
61cdf0e10cSrcweir {
62cdf0e10cSrcweir public:
63cdf0e10cSrcweir     ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     ~ExampleComponent2Impl();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     // XInterface
68cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
acquire()69cdf0e10cSrcweir     virtual void SAL_CALL acquire() throw()
70cdf0e10cSrcweir         { OWeakObject::acquire(); }
release()71cdf0e10cSrcweir     virtual void SAL_CALL release() throw()
72cdf0e10cSrcweir         { OWeakObject::release(); }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     // XTypeProvider
75cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
76cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() throw (::com::sun::star::uno::RuntimeException);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     // XServiceInfo
79cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
80cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
81cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
82cdf0e10cSrcweir     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     // XTest
85cdf0e10cSrcweir     virtual OUString SAL_CALL getMessage() throw(RuntimeException);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir protected:
88cdf0e10cSrcweir     Mutex       m_mutex;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     Reference<XMultiServiceFactory> m_xSMgr;
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
93cdf0e10cSrcweir //*************************************************************************
ExampleComponent2Impl(const Reference<XMultiServiceFactory> & rXSMgr)94cdf0e10cSrcweir ExampleComponent2Impl::ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr )
95cdf0e10cSrcweir     : m_xSMgr(rXSMgr)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //*************************************************************************
~ExampleComponent2Impl()100cdf0e10cSrcweir ExampleComponent2Impl::~ExampleComponent2Impl()
101cdf0e10cSrcweir {
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir //*************************************************************************
queryInterface(const::com::sun::star::uno::Type & rType)105cdf0e10cSrcweir Any SAL_CALL ExampleComponent2Impl::queryInterface( const ::com::sun::star::uno::Type & rType )
106cdf0e10cSrcweir     throw(::com::sun::star::uno::RuntimeException)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     Any aRet = ::cppu::queryInterface(rType,
109cdf0e10cSrcweir                                       static_cast< XTypeProvider * >( this ),
110cdf0e10cSrcweir                                       static_cast< XServiceInfo * >( this ),
111cdf0e10cSrcweir                                       static_cast< XTest * >( this ) );
112cdf0e10cSrcweir     if ( aRet.hasValue() )
113cdf0e10cSrcweir         return aRet;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     return OWeakObject::queryInterface( rType );
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //*************************************************************************
getTypes()119cdf0e10cSrcweir Sequence< Type > SAL_CALL ExampleComponent2Impl::getTypes()
120cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     static OTypeCollection * pTypes = 0;
123cdf0e10cSrcweir     if (! pTypes)
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         MutexGuard aGuard( m_mutex );
126cdf0e10cSrcweir         if (! pTypes)
127cdf0e10cSrcweir         {
128cdf0e10cSrcweir             static OTypeCollection aTypes(
129cdf0e10cSrcweir                 ::getCppuType( (const Reference< XInterface > *)0 ),
130cdf0e10cSrcweir                 ::getCppuType( (const Reference< XWeak > *)0 ),
131cdf0e10cSrcweir                 ::getCppuType( (const Reference< XTypeProvider > *)0 ),
132cdf0e10cSrcweir                 ::getCppuType( (const Reference< XServiceInfo > *)0 ),
133cdf0e10cSrcweir                 ::getCppuType( (const Reference< XTest > *)0 ) );
134cdf0e10cSrcweir             pTypes = &aTypes;
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir     return pTypes->getTypes();
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir //*************************************************************************
getImplementationId()141cdf0e10cSrcweir Sequence< sal_Int8 > SAL_CALL ExampleComponent2Impl::getImplementationId()
142cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir     static OImplementationId * pId = 0;
145cdf0e10cSrcweir     if (! pId)
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         MutexGuard aGuard( m_mutex );
148cdf0e10cSrcweir         if (! pId)
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             static OImplementationId aId;
151cdf0e10cSrcweir             pId = &aId;
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir     return pId->getImplementationId();
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir //*************************************************************************
getImplementationName()158cdf0e10cSrcweir OUString SAL_CALL ExampleComponent2Impl::getImplementationName(  )
159cdf0e10cSrcweir     throw(RuntimeException)
160cdf0e10cSrcweir {
161cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
162cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) );
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir //*************************************************************************
supportsService(const OUString & ServiceName)166cdf0e10cSrcweir sal_Bool SAL_CALL ExampleComponent2Impl::supportsService( const OUString& ServiceName )
167cdf0e10cSrcweir     throw(RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
170cdf0e10cSrcweir     Sequence< OUString > aSNL = getSupportedServiceNames();
171cdf0e10cSrcweir     const OUString * pArray = aSNL.getConstArray();
172cdf0e10cSrcweir     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
173cdf0e10cSrcweir         if( pArray[i] == ServiceName )
174cdf0e10cSrcweir             return sal_True;
175cdf0e10cSrcweir     return sal_False;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames()179cdf0e10cSrcweir Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames(  )
180cdf0e10cSrcweir     throw(RuntimeException)
181cdf0e10cSrcweir {
182cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
183cdf0e10cSrcweir     return getSupportedServiceNames_Static();
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames_Static()187cdf0e10cSrcweir Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames_Static(  )
188cdf0e10cSrcweir {
189cdf0e10cSrcweir     OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2) );
190cdf0e10cSrcweir     return Sequence< OUString >( &aName, 1 );
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir //*************************************************************************
getMessage()194cdf0e10cSrcweir OUString SAL_CALL ExampleComponent2Impl::getMessage() throw(RuntimeException)
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     Guard< Mutex > aGuard( m_mutex );
197cdf0e10cSrcweir     return OUString::createFromAscii("Alle meine Entchen schwimmen auf dem See, schwimmen auf dem See ...");
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 
201cdf0e10cSrcweir //*************************************************************************
ExampleComponent2_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)202cdf0e10cSrcweir Reference<XInterface> SAL_CALL ExampleComponent2_CreateInstance( const Reference<XMultiServiceFactory>& rSMgr )
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     Reference<XInterface> xRet;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     XTest *pXTest = (XTest*) new ExampleComponent2Impl(rSMgr);
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     if (pXTest)
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         xRet = Reference< XInterface >::query(pXTest);
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     return xRet;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 
217cdf0e10cSrcweir } // excomp_impl
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 
220cdf0e10cSrcweir extern "C"
221cdf0e10cSrcweir {
222cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)223cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
224cdf0e10cSrcweir     const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
227cdf0e10cSrcweir }
228cdf0e10cSrcweir //==================================================================================================
component_writeInfo(void *,void * pRegistryKey)229cdf0e10cSrcweir sal_Bool SAL_CALL component_writeInfo(
230cdf0e10cSrcweir     void * /* pServiceManager */, void * pRegistryKey )
231cdf0e10cSrcweir {
232cdf0e10cSrcweir     if (pRegistryKey)
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         try
235cdf0e10cSrcweir         {
236cdf0e10cSrcweir             // ExampleComponent2
237cdf0e10cSrcweir             Reference< XRegistryKey > xNewKey(
238cdf0e10cSrcweir                 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
239cdf0e10cSrcweir                     OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME2 "/UNO/SERVICES") ) ) );
240cdf0e10cSrcweir 
241cdf0e10cSrcweir             const Sequence< OUString > & rSNL =
242cdf0e10cSrcweir                 ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static();
243cdf0e10cSrcweir             const OUString * pArray = rSNL.getConstArray();
244cdf0e10cSrcweir             for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
245cdf0e10cSrcweir                 xNewKey->createKey( pArray[nPos] );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir             return sal_True;
248cdf0e10cSrcweir         }
249cdf0e10cSrcweir         catch (InvalidRegistryException &)
250cdf0e10cSrcweir         {
251cdf0e10cSrcweir             OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
252cdf0e10cSrcweir         }
253cdf0e10cSrcweir     }
254cdf0e10cSrcweir     return sal_False;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)257cdf0e10cSrcweir void * SAL_CALL component_getFactory(
258cdf0e10cSrcweir     const sal_Char * pImplName, void * pServiceManager, void * /* pRegistryKey */ )
259cdf0e10cSrcweir {
260cdf0e10cSrcweir     void * pRet = 0;
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     if (rtl_str_compare( pImplName, IMPLNAME2 ) == 0)
263cdf0e10cSrcweir     {
264cdf0e10cSrcweir         Reference< XSingleServiceFactory > xFactory( createSingleFactory(
265cdf0e10cSrcweir             reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
266cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) ),
267cdf0e10cSrcweir             ::excomp2_impl::ExampleComponent2_CreateInstance,
268cdf0e10cSrcweir             ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static() ) );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir         if (xFactory.is())
271cdf0e10cSrcweir         {
272cdf0e10cSrcweir             xFactory->acquire();
273cdf0e10cSrcweir             pRet = xFactory.get();
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir     }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     return pRet;
278cdf0e10cSrcweir }
279cdf0e10cSrcweir }
280