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