xref: /aoo4110/main/stoc/test/excomp/excomp2.cxx (revision b1cdbd2c)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_stoc.hxx"
26 #include <osl/diagnose.h>
27 #include <osl/mutex.hxx>
28 #include <rtl/alloc.h>
29 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/queryinterface.hxx>
31 #include <cppuhelper/weak.hxx>
32 #include <cppuhelper/typeprovider.hxx>
33 
34 #include <example/XTest.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/lang/XTypeProvider.hpp>
38 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 #include <com/sun/star/registry/XRegistryKey.hpp>
40 
41 using namespace example;
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::registry;
45 using namespace cppu;
46 using namespace osl;
47 using namespace rtl;
48 
49 #define SERVICENAME2 "example.ExampleComponent2"
50 #define IMPLNAME2	"example.ExampleComponent2.Impl"
51 
52 namespace excomp2_impl {
53 
54 //*************************************************************************
55 // ExampleComponent2Impl
56 //*************************************************************************
57 class ExampleComponent2Impl	: public OWeakObject
58 							, public XTypeProvider
59 							, public XServiceInfo
60 							, public XTest
61 {
62 public:
63 	ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr );
64 
65 	~ExampleComponent2Impl();
66 
67 	// XInterface
68     virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
acquire()69     virtual void SAL_CALL acquire() throw()
70 		{ OWeakObject::acquire(); }
release()71     virtual void SAL_CALL release() throw()
72 		{ OWeakObject::release(); }
73 
74     // XTypeProvider
75 	virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes() throw (::com::sun::star::uno::RuntimeException);
76 	virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() throw (::com::sun::star::uno::RuntimeException);
77 
78     // XServiceInfo
79     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
80     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
81     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
82     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
83 
84 	// XTest
85     virtual OUString SAL_CALL getMessage() throw(RuntimeException);
86 
87 protected:
88 	Mutex		m_mutex;
89 
90 	Reference<XMultiServiceFactory> m_xSMgr;
91 };
92 
93 //*************************************************************************
ExampleComponent2Impl(const Reference<XMultiServiceFactory> & rXSMgr)94 ExampleComponent2Impl::ExampleComponent2Impl( const Reference<XMultiServiceFactory> & rXSMgr )
95 	: m_xSMgr(rXSMgr)
96 {
97 }
98 
99 //*************************************************************************
~ExampleComponent2Impl()100 ExampleComponent2Impl::~ExampleComponent2Impl()
101 {
102 }
103 
104 //*************************************************************************
queryInterface(const::com::sun::star::uno::Type & rType)105 Any SAL_CALL ExampleComponent2Impl::queryInterface( const ::com::sun::star::uno::Type & rType )
106 	throw(::com::sun::star::uno::RuntimeException)
107 {
108 	Any aRet = ::cppu::queryInterface(rType,
109 									  static_cast< XTypeProvider * >( this ),
110 									  static_cast< XServiceInfo * >( this ),
111 						  			  static_cast< XTest * >( this ) );
112 	if ( aRet.hasValue() )
113 		return aRet;
114 
115 	return OWeakObject::queryInterface( rType );
116 }
117 
118 //*************************************************************************
getTypes()119 Sequence< Type > SAL_CALL ExampleComponent2Impl::getTypes()
120 	throw (::com::sun::star::uno::RuntimeException)
121 {
122 	static OTypeCollection * pTypes = 0;
123 	if (! pTypes)
124 	{
125 		MutexGuard aGuard( m_mutex );
126 		if (! pTypes)
127 		{
128 			static OTypeCollection aTypes(
129 				::getCppuType( (const Reference< XInterface > *)0 ),
130 				::getCppuType( (const Reference< XWeak > *)0 ),
131 				::getCppuType( (const Reference< XTypeProvider > *)0 ),
132 				::getCppuType( (const Reference< XServiceInfo > *)0 ),
133 				::getCppuType( (const Reference< XTest > *)0 ) );
134 			pTypes = &aTypes;
135 		}
136 	}
137 	return pTypes->getTypes();
138 }
139 
140 //*************************************************************************
getImplementationId()141 Sequence< sal_Int8 > SAL_CALL ExampleComponent2Impl::getImplementationId()
142 	throw (::com::sun::star::uno::RuntimeException)
143 {
144 	static OImplementationId * pId = 0;
145 	if (! pId)
146 	{
147 		MutexGuard aGuard( m_mutex );
148 		if (! pId)
149 		{
150 			static OImplementationId aId;
151 			pId = &aId;
152 		}
153 	}
154 	return pId->getImplementationId();
155 }
156 
157 //*************************************************************************
getImplementationName()158 OUString SAL_CALL ExampleComponent2Impl::getImplementationName(  )
159 	throw(RuntimeException)
160 {
161 	Guard< Mutex > aGuard( m_mutex );
162 	return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) );
163 }
164 
165 //*************************************************************************
supportsService(const OUString & ServiceName)166 sal_Bool SAL_CALL ExampleComponent2Impl::supportsService( const OUString& ServiceName )
167 	throw(RuntimeException)
168 {
169 	Guard< Mutex > aGuard( m_mutex );
170 	Sequence< OUString > aSNL = getSupportedServiceNames();
171 	const OUString * pArray = aSNL.getConstArray();
172 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
173 		if( pArray[i] == ServiceName )
174 			return sal_True;
175 	return sal_False;
176 }
177 
178 //*************************************************************************
getSupportedServiceNames()179 Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames(  )
180 	throw(RuntimeException)
181 {
182 	Guard< Mutex > aGuard( m_mutex );
183 	return getSupportedServiceNames_Static();
184 }
185 
186 //*************************************************************************
getSupportedServiceNames_Static()187 Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames_Static(  )
188 {
189 	OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2) );
190 	return Sequence< OUString >( &aName, 1 );
191 }
192 
193 //*************************************************************************
getMessage()194 OUString SAL_CALL ExampleComponent2Impl::getMessage() throw(RuntimeException)
195 {
196 	Guard< Mutex > aGuard( m_mutex );
197 	return OUString::createFromAscii("Alle meine Entchen schwimmen auf dem See, schwimmen auf dem See ...");
198 }
199 
200 
201 //*************************************************************************
ExampleComponent2_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)202 Reference<XInterface> SAL_CALL ExampleComponent2_CreateInstance( const Reference<XMultiServiceFactory>& rSMgr )
203 {
204 	Reference<XInterface> xRet;
205 
206 	XTest *pXTest = (XTest*) new ExampleComponent2Impl(rSMgr);
207 
208 	if (pXTest)
209 	{
210 		xRet = Reference< XInterface >::query(pXTest);
211 	}
212 
213 	return xRet;
214 }
215 
216 
217 } // excomp_impl
218 
219 
220 extern "C"
221 {
222 //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)223 void SAL_CALL component_getImplementationEnvironment(
224 	const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
225 {
226 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
227 }
228 //==================================================================================================
component_writeInfo(void *,void * pRegistryKey)229 sal_Bool SAL_CALL component_writeInfo(
230 	void * /* pServiceManager */, void * pRegistryKey )
231 {
232 	if (pRegistryKey)
233 	{
234 		try
235 		{
236 			// ExampleComponent2
237 			Reference< XRegistryKey > xNewKey(
238 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
239 					OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME2 "/UNO/SERVICES") ) ) );
240 
241 			const Sequence< OUString > & rSNL =
242 				::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static();
243 			const OUString * pArray = rSNL.getConstArray();
244 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
245 				xNewKey->createKey( pArray[nPos] );
246 
247 			return sal_True;
248 		}
249 		catch (InvalidRegistryException &)
250 		{
251 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
252 		}
253 	}
254 	return sal_False;
255 }
256 //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)257 void * SAL_CALL component_getFactory(
258 	const sal_Char * pImplName, void * pServiceManager, void * /* pRegistryKey */ )
259 {
260 	void * pRet = 0;
261 
262 	if (rtl_str_compare( pImplName, IMPLNAME2 ) == 0)
263 	{
264 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
265 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
266 			OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) ),
267 			::excomp2_impl::ExampleComponent2_CreateInstance,
268 			::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static() ) );
269 
270 		if (xFactory.is())
271 		{
272 			xFactory->acquire();
273 			pRet = xFactory.get();
274 		}
275 	}
276 
277 	return pRet;
278 }
279 }
280 
281 
282 
283