xref: /trunk/main/stoc/test/excomp/excomp2.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 );
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();
76     virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId();
77 
78     // XServiceInfo
79     virtual OUString SAL_CALL getImplementationName(  );
80     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName );
81     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  );
82     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
83 
84     // XTest
85     virtual OUString SAL_CALL getMessage();
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 {
107     Any aRet = ::cppu::queryInterface(rType,
108                                       static_cast< XTypeProvider * >( this ),
109                                       static_cast< XServiceInfo * >( this ),
110                                       static_cast< XTest * >( this ) );
111     if ( aRet.hasValue() )
112         return aRet;
113 
114     return OWeakObject::queryInterface( rType );
115 }
116 
117 //*************************************************************************
getTypes()118 Sequence< Type > SAL_CALL ExampleComponent2Impl::getTypes()
119 {
120     static OTypeCollection * pTypes = 0;
121     if (! pTypes)
122     {
123         MutexGuard aGuard( m_mutex );
124         if (! pTypes)
125         {
126             static OTypeCollection aTypes(
127                 ::getCppuType( (const Reference< XInterface > *)0 ),
128                 ::getCppuType( (const Reference< XWeak > *)0 ),
129                 ::getCppuType( (const Reference< XTypeProvider > *)0 ),
130                 ::getCppuType( (const Reference< XServiceInfo > *)0 ),
131                 ::getCppuType( (const Reference< XTest > *)0 ) );
132             pTypes = &aTypes;
133         }
134     }
135     return pTypes->getTypes();
136 }
137 
138 //*************************************************************************
getImplementationId()139 Sequence< sal_Int8 > SAL_CALL ExampleComponent2Impl::getImplementationId()
140 {
141     static OImplementationId * pId = 0;
142     if (! pId)
143     {
144         MutexGuard aGuard( m_mutex );
145         if (! pId)
146         {
147             static OImplementationId aId;
148             pId = &aId;
149         }
150     }
151     return pId->getImplementationId();
152 }
153 
154 //*************************************************************************
getImplementationName()155 OUString SAL_CALL ExampleComponent2Impl::getImplementationName(  )
156 {
157     Guard< Mutex > aGuard( m_mutex );
158     return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) );
159 }
160 
161 //*************************************************************************
supportsService(const OUString & ServiceName)162 sal_Bool SAL_CALL ExampleComponent2Impl::supportsService( const OUString& ServiceName )
163 {
164     Guard< Mutex > aGuard( m_mutex );
165     Sequence< OUString > aSNL = getSupportedServiceNames();
166     const OUString * pArray = aSNL.getConstArray();
167     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
168         if( pArray[i] == ServiceName )
169             return sal_True;
170     return sal_False;
171 }
172 
173 //*************************************************************************
getSupportedServiceNames()174 Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames(  )
175 {
176     Guard< Mutex > aGuard( m_mutex );
177     return getSupportedServiceNames_Static();
178 }
179 
180 //*************************************************************************
getSupportedServiceNames_Static()181 Sequence<OUString> SAL_CALL ExampleComponent2Impl::getSupportedServiceNames_Static(  )
182 {
183     OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME2) );
184     return Sequence< OUString >( &aName, 1 );
185 }
186 
187 //*************************************************************************
getMessage()188 OUString SAL_CALL ExampleComponent2Impl::getMessage()
189 {
190     Guard< Mutex > aGuard( m_mutex );
191     return OUString::createFromAscii("Alle meine Entchen schwimmen auf dem See, schwimmen auf dem See ...");
192 }
193 
194 
195 //*************************************************************************
ExampleComponent2_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)196 Reference<XInterface> SAL_CALL ExampleComponent2_CreateInstance( const Reference<XMultiServiceFactory>& rSMgr )
197 {
198     Reference<XInterface> xRet;
199 
200     XTest *pXTest = (XTest*) new ExampleComponent2Impl(rSMgr);
201 
202     if (pXTest)
203     {
204         xRet = Reference< XInterface >::query(pXTest);
205     }
206 
207     return xRet;
208 }
209 
210 
211 } // excomp_impl
212 
213 
214 extern "C"
215 {
216 //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)217 void SAL_CALL component_getImplementationEnvironment(
218     const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
219 {
220     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
221 }
222 //==================================================================================================
component_writeInfo(void *,void * pRegistryKey)223 sal_Bool SAL_CALL component_writeInfo(
224     void * /* pServiceManager */, void * pRegistryKey )
225 {
226     if (pRegistryKey)
227     {
228         try
229         {
230             // ExampleComponent2
231             Reference< XRegistryKey > xNewKey(
232                 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
233                     OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME2 "/UNO/SERVICES") ) ) );
234 
235             const Sequence< OUString > & rSNL =
236                 ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static();
237             const OUString * pArray = rSNL.getConstArray();
238             for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
239                 xNewKey->createKey( pArray[nPos] );
240 
241             return sal_True;
242         }
243         catch (InvalidRegistryException &)
244         {
245             OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
246         }
247     }
248     return sal_False;
249 }
250 //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)251 void * SAL_CALL component_getFactory(
252     const sal_Char * pImplName, void * pServiceManager, void * /* pRegistryKey */ )
253 {
254     void * pRet = 0;
255 
256     if (rtl_str_compare( pImplName, IMPLNAME2 ) == 0)
257     {
258         Reference< XSingleServiceFactory > xFactory( createSingleFactory(
259             reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
260             OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME2) ),
261             ::excomp2_impl::ExampleComponent2_CreateInstance,
262             ::excomp2_impl::ExampleComponent2Impl::getSupportedServiceNames_Static() ) );
263 
264         if (xFactory.is())
265         {
266             xFactory->acquire();
267             pRet = xFactory.get();
268         }
269     }
270 
271     return pRet;
272 }
273 }
274