xref: /trunk/main/stoc/test/excomp/excomp2.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_stoc.hxx"
30 #include <osl/diagnose.h>
31 #include <osl/mutex.hxx>
32 #include <rtl/alloc.h>
33 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/queryinterface.hxx>
35 #include <cppuhelper/weak.hxx>
36 #include <cppuhelper/typeprovider.hxx>
37 
38 #include <example/XTest.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/lang/XTypeProvider.hpp>
42 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43 #include <com/sun/star/registry/XRegistryKey.hpp>
44 
45 using namespace example;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::lang;
48 using namespace com::sun::star::registry;
49 using namespace cppu;
50 using namespace osl;
51 using namespace rtl;
52 
53 #define SERVICENAME2 "example.ExampleComponent2"
54 #define IMPLNAME2	"example.ExampleComponent2.Impl"
55 
56 namespace excomp2_impl {
57 
58 //*************************************************************************
59 // ExampleComponent2Impl
60 //*************************************************************************
61 class ExampleComponent2Impl	: public OWeakObject
62 							, public XTypeProvider
63 							, public XServiceInfo
64 							, public XTest
65 {
66 public:
67 	ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr );
68 
69 	~ExampleComponent2Impl();
70 
71 	// XInterface
72     virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
73     virtual void SAL_CALL acquire() throw()
74 		{ OWeakObject::acquire(); }
75     virtual void SAL_CALL release() throw()
76 		{ OWeakObject::release(); }
77 
78     // XTypeProvider
79 	virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
80 	virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() throw (::com::sun::star::uno::RuntimeException);
81 
82     // XServiceInfo
83     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
84     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
85     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
86     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
87 
88 	// XTest
89     virtual OUString SAL_CALL getMessage() throw(RuntimeException);
90 
91 protected:
92 	Mutex		m_mutex;
93 
94 	Reference<XMultiServiceFactory> m_xSMgr;
95 };
96 
97 //*************************************************************************
98 ExampleComponent2Impl::ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr )
99 	: m_xSMgr(rXSMgr)
100 {
101 }
102 
103 //*************************************************************************
104 ExampleComponent2Impl::~ExampleComponent2Impl()
105 {
106 }
107 
108 //*************************************************************************
109 Any SAL_CALL ExampleComponent2Impl::queryInterface( const ::com::sun::star::uno::Type & rType )
110 	throw(::com::sun::star::uno::RuntimeException)
111 {
112 	Any aRet = ::cppu::queryInterface(rType,
113 									  static_cast< XTypeProvider * >( this ),
114 									  static_cast< XServiceInfo * >( this ),
115 						  			  static_cast< XTest * >( this ) );
116 	if ( aRet.hasValue() )
117 		return aRet;
118 
119 	return OWeakObject::queryInterface( rType );
120 }
121 
122 //*************************************************************************
123 Sequence< Type > SAL_CALL ExampleComponent2Impl::getTypes()
124 	throw (::com::sun::star::uno::RuntimeException)
125 {
126 	static OTypeCollection * pTypes = 0;
127 	if (! pTypes)
128 	{
129 		MutexGuard aGuard( m_mutex );
130 		if (! pTypes)
131 		{
132 			static OTypeCollection aTypes(
133 				::getCppuType( (const Reference< XInterface > *)0 ),
134 				::getCppuType( (const Reference< XWeak > *)0 ),
135 				::getCppuType( (const Reference< XTypeProvider > *)0 ),
136 				::getCppuType( (const Reference< XServiceInfo > *)0 ),
137 				::getCppuType( (const Reference< XTest > *)0 ) );
138 			pTypes = &aTypes;
139 		}
140 	}
141 	return pTypes->getTypes();
142 }
143 
144 //*************************************************************************
145 Sequence< sal_Int8 > SAL_CALL ExampleComponent2Impl::getImplementationId()
146 	throw (::com::sun::star::uno::RuntimeException)
147 {
148 	static OImplementationId * pId = 0;
149 	if (! pId)
150 	{
151 		MutexGuard aGuard( m_mutex );
152 		if (! pId)
153 		{
154 			static OImplementationId aId;
155 			pId = &aId;
156 		}
157 	}
158 	return pId->getImplementationId();
159 }
160 
161 //*************************************************************************
162 OUString SAL_CALL ExampleComponent2Impl::getImplementationName(  )
163 	throw(RuntimeException)
164 {
165 	Guard< Mutex > aGuard( m_mutex );
166 	return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) );
167 }
168 
169 //*************************************************************************
170 sal_Bool SAL_CALL ExampleComponent2Impl::supportsService( const OUString& ServiceName )
171 	throw(RuntimeException)
172 {
173 	Guard< Mutex > aGuard( m_mutex );
174 	Sequence< OUString > aSNL = getSupportedServiceNames();
175 	const OUString * pArray = aSNL.getConstArray();
176 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
177 		if( pArray[i] == ServiceName )
178 			return sal_True;
179 	return sal_False;
180 }
181 
182 //*************************************************************************
183 Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames(  )
184 	throw(RuntimeException)
185 {
186 	Guard< Mutex > aGuard( m_mutex );
187 	return getSupportedServiceNames_Static();
188 }
189 
190 //*************************************************************************
191 Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames_Static(  )
192 {
193 	OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2) );
194 	return Sequence< OUString >( &aName, 1 );
195 }
196 
197 //*************************************************************************
198 OUString SAL_CALL ExampleComponent2Impl::getMessage() throw(RuntimeException)
199 {
200 	Guard< Mutex > aGuard( m_mutex );
201 	return OUString::createFromAscii("Alle meine Entchen schwimmen auf dem See, schwimmen auf dem See ...");
202 }
203 
204 
205 //*************************************************************************
206 Reference<XInterface> SAL_CALL ExampleComponent2_CreateInstance( const Reference<XMultiServiceFactory>& rSMgr )
207 {
208 	Reference<XInterface> xRet;
209 
210 	XTest *pXTest = (XTest*) new ExampleComponent2Impl(rSMgr);
211 
212 	if (pXTest)
213 	{
214 		xRet = Reference< XInterface >::query(pXTest);
215 	}
216 
217 	return xRet;
218 }
219 
220 
221 } // excomp_impl
222 
223 
224 extern "C"
225 {
226 //==================================================================================================
227 void SAL_CALL component_getImplementationEnvironment(
228 	const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
229 {
230 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
231 }
232 //==================================================================================================
233 sal_Bool SAL_CALL component_writeInfo(
234 	void * /* pServiceManager */, void * pRegistryKey )
235 {
236 	if (pRegistryKey)
237 	{
238 		try
239 		{
240 			// ExampleComponent2
241 			Reference< XRegistryKey > xNewKey(
242 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
243 					OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME2 "/UNO/SERVICES") ) ) );
244 
245 			const Sequence< OUString > & rSNL =
246 				::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static();
247 			const OUString * pArray = rSNL.getConstArray();
248 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
249 				xNewKey->createKey( pArray[nPos] );
250 
251 			return sal_True;
252 		}
253 		catch (InvalidRegistryException &)
254 		{
255 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
256 		}
257 	}
258 	return sal_False;
259 }
260 //==================================================================================================
261 void * SAL_CALL component_getFactory(
262 	const sal_Char * pImplName, void * pServiceManager, void * /* pRegistryKey */ )
263 {
264 	void * pRet = 0;
265 
266 	if (rtl_str_compare( pImplName, IMPLNAME2 ) == 0)
267 	{
268 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
269 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
270 			OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) ),
271 			::excomp2_impl::ExampleComponent2_CreateInstance,
272 			::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static() ) );
273 
274 		if (xFactory.is())
275 		{
276 			xFactory->acquire();
277 			pRet = xFactory.get();
278 		}
279 	}
280 
281 	return pRet;
282 }
283 }
284 
285 
286 
287