xref: /aoo42x/main/stoc/test/excomp/excomp1.cxx (revision 647a425c)
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
10*647a425cSAndrew Rist  *
11*647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647a425cSAndrew Rist  *
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.
19*647a425cSAndrew Rist  *
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/implbase2.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <example/XTest.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
36cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace example;
39cdf0e10cSrcweir using namespace com::sun::star::uno;
40cdf0e10cSrcweir using namespace com::sun::star::lang;
41cdf0e10cSrcweir using namespace com::sun::star::registry;
42cdf0e10cSrcweir using namespace cppu;
43cdf0e10cSrcweir using namespace osl;
44cdf0e10cSrcweir using namespace rtl;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #define SERVICENAME1 "example.ExampleComponent1"
47cdf0e10cSrcweir #define IMPLNAME1	"example.ExampleComponent1.Impl"
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace excomp_impl {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir //*************************************************************************
52cdf0e10cSrcweir // ExampleComponent1Impl
53cdf0e10cSrcweir //*************************************************************************
54cdf0e10cSrcweir class ExampleComponent1Impl	: public WeakImplHelper2< XTest, XServiceInfo >
55cdf0e10cSrcweir {
56cdf0e10cSrcweir public:
57cdf0e10cSrcweir 	ExampleComponent1Impl( const Reference<XMultiServiceFactory> & rXSMgr );
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	~ExampleComponent1Impl();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     // XServiceInfo
62cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
63cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
64cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
65cdf0e10cSrcweir     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	// XSimpleRegistry
68cdf0e10cSrcweir     virtual OUString SAL_CALL getMessage() throw(RuntimeException);
69cdf0e10cSrcweir 
70cdf0e10cSrcweir protected:
71cdf0e10cSrcweir 	Mutex		m_mutex;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	Reference<XMultiServiceFactory> m_xSMgr;
74cdf0e10cSrcweir };
75cdf0e10cSrcweir 
76cdf0e10cSrcweir //*************************************************************************
ExampleComponent1Impl(const Reference<XMultiServiceFactory> & rXSMgr)77cdf0e10cSrcweir ExampleComponent1Impl::ExampleComponent1Impl( const Reference<XMultiServiceFactory> & rXSMgr )
78cdf0e10cSrcweir 	: m_xSMgr(rXSMgr)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir //*************************************************************************
~ExampleComponent1Impl()83cdf0e10cSrcweir ExampleComponent1Impl::~ExampleComponent1Impl()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir //*************************************************************************
getImplementationName()88cdf0e10cSrcweir OUString SAL_CALL ExampleComponent1Impl::getImplementationName(  )
89cdf0e10cSrcweir 	throw(RuntimeException)
90cdf0e10cSrcweir {
91cdf0e10cSrcweir 	Guard< Mutex > aGuard( m_mutex );
92cdf0e10cSrcweir 	return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME1) );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir //*************************************************************************
supportsService(const OUString & ServiceName)96cdf0e10cSrcweir sal_Bool SAL_CALL ExampleComponent1Impl::supportsService( const OUString& ServiceName )
97cdf0e10cSrcweir 	throw(RuntimeException)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	Guard< Mutex > aGuard( m_mutex );
100cdf0e10cSrcweir 	Sequence< OUString > aSNL = getSupportedServiceNames();
101cdf0e10cSrcweir 	const OUString * pArray = aSNL.getArray();
102cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
103cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
104cdf0e10cSrcweir 			return sal_True;
105cdf0e10cSrcweir 	return sal_False;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames()109cdf0e10cSrcweir Sequence<OUString> SAL_CALL ExampleComponent1Impl::getSupportedServiceNames(  )
110cdf0e10cSrcweir 	throw(RuntimeException)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	Guard< Mutex > aGuard( m_mutex );
113cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames_Static()117cdf0e10cSrcweir Sequence<OUString> SAL_CALL ExampleComponent1Impl::getSupportedServiceNames_Static(  )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1) );
120cdf0e10cSrcweir 	return Sequence< OUString >( &aName, 1 );
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir //*************************************************************************
getMessage()124cdf0e10cSrcweir OUString SAL_CALL ExampleComponent1Impl::getMessage() throw(RuntimeException)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir 	Guard< Mutex > aGuard( m_mutex );
127cdf0e10cSrcweir 	return OUString::createFromAscii("Lalelu nur der Mann im Mond schaut zu ...");
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir //*************************************************************************
ExampleComponent1_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)132cdf0e10cSrcweir Reference<XInterface> SAL_CALL ExampleComponent1_CreateInstance( const Reference<XMultiServiceFactory>& rSMgr )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir 	Reference<XInterface> xRet;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	XTest *pXTest = (XTest*) new ExampleComponent1Impl(rSMgr);
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	if (pXTest)
139cdf0e10cSrcweir 	{
140cdf0e10cSrcweir 		xRet = Reference< XInterface >::query(pXTest);
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	return xRet;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir } // excomp_impl
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 
149cdf0e10cSrcweir extern "C"
150cdf0e10cSrcweir {
151cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)152cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
153cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
154cdf0e10cSrcweir {
155cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir //==================================================================================================
component_writeInfo(void *,void * pRegistryKey)158cdf0e10cSrcweir sal_Bool SAL_CALL component_writeInfo(
159cdf0e10cSrcweir 	void * /* pServiceManager */ , void * pRegistryKey )
160cdf0e10cSrcweir {
161cdf0e10cSrcweir 	if (pRegistryKey)
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		try
164cdf0e10cSrcweir 		{
165cdf0e10cSrcweir 			// ExampleComponent1
166cdf0e10cSrcweir 			Reference< XRegistryKey > xNewKey(
167cdf0e10cSrcweir 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
168cdf0e10cSrcweir 					OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME1 "/UNO/SERVICES") ) ) );
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 			const Sequence< OUString > & rSNL =
171cdf0e10cSrcweir 				::excomp_impl::ExampleComponent1Impl::getSupportedServiceNames_Static();
172cdf0e10cSrcweir 			const OUString * pArray = rSNL.getConstArray();
173cdf0e10cSrcweir 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
174cdf0e10cSrcweir 				xNewKey->createKey( pArray[nPos] );
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 			return sal_True;
177cdf0e10cSrcweir 		}
178cdf0e10cSrcweir 		catch (InvalidRegistryException &)
179cdf0e10cSrcweir 		{
180cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
181cdf0e10cSrcweir 		}
182cdf0e10cSrcweir 	}
183cdf0e10cSrcweir 	return sal_False;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)186cdf0e10cSrcweir void * SAL_CALL component_getFactory(
187cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * /* pRegistryKey */ )
188cdf0e10cSrcweir {
189cdf0e10cSrcweir 	void * pRet = 0;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	if (rtl_str_compare( pImplName, IMPLNAME1 ) == 0)
192cdf0e10cSrcweir 	{
193cdf0e10cSrcweir 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
194cdf0e10cSrcweir 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
195cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME1) ),
196cdf0e10cSrcweir 			::excomp_impl::ExampleComponent1_CreateInstance,
197cdf0e10cSrcweir 			::excomp_impl::ExampleComponent1Impl::getSupportedServiceNames_Static() ) );
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		if (xFactory.is())
200cdf0e10cSrcweir 		{
201cdf0e10cSrcweir 			xFactory->acquire();
202cdf0e10cSrcweir 			pRet = xFactory.get();
203cdf0e10cSrcweir 		}
204cdf0e10cSrcweir 	}
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 	return pRet;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 
212