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