xref: /trunk/main/cppuhelper/test/testcmp/TestComponent.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_cppuhelper.hxx"
26 
27 
28 #define CPPUHELPER_TEST_COMPONENT_IMPL
29 #include "TestComponent.hxx"
30 
31 
32 #include <string.h>
33 
34 #include "osl/thread.h"
35 
36 #include "cppuhelper/implbase1.hxx"
37 #include "cppuhelper/implementationentry.hxx"
38 
39 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
40 #include "com/sun/star/lang/XServiceInfo.hpp"
41 
42 #include "com/sun/star/uno/XComponentContext.hpp"
43 
44 
45 #include "cppu/EnvDcp.hxx"
46 
47 #include <uno/environment.hxx>
48 
49 using namespace ::com::sun::star;
50 
51 
52 #define LOG_LIFECYCLE_TestComponent
53 #ifdef LOG_LIFECYCLE_TestComponent
54 #  include <iostream>
55 #  define LOG_LIFECYCLE_TestComponent_emit(x) x
56 
57 #else
58 #  define LOG_LIFECYCLE_TestComponent_emit(x)
59 
60 #endif
61 
62 
63 class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
64 {
65     rtl::OUString m_implName;
66 
67 public:
68     static uno::Reference<uno::XInterface> create(
69         uno::Reference<uno::XComponentContext> const & xCtx
70     );
71 
72 
73     static uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames_Static();
74 
75     explicit TestComponent(uno::Reference<uno::XComponentContext> const & xCtx);
76     virtual ~TestComponent();
77 
78     uno::Any SAL_CALL queryInterface(uno::Type const & rType );
79     void SAL_CALL release() throw ();
80     void SAL_CALL acquire() throw ();
81 
82     // lang::XServiceInfo
83     virtual rtl::OUString SAL_CALL getImplementationName();
84     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName);
85     virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames();
86 
87 protected:
88     uno::Reference<uno::XComponentContext> m_xComponentContext;
89 };
90 
91 
create(uno::Reference<uno::XComponentContext> const & xCtx)92 uno::Reference<uno::XInterface> SAL_CALL TestComponent::create(
93     uno::Reference<uno::XComponentContext> const & xCtx
94 )
95 {
96     try
97     {
98         return static_cast<cppu::OWeakObject *>(new TestComponent(xCtx));
99     }
100     catch (std::bad_alloc &)
101     {
102         throw uno::RuntimeException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
103                                      uno::Reference<uno::XInterface>());
104     }
105 }
106 
getSupportedServiceNames_Static()107 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames_Static()
108 {
109     uno::Sequence<rtl::OUString> serviceNames(1);
110     serviceNames[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.lang.ServiceInfo"));
111 
112     return serviceNames;
113 }
114 
115 
TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)116 TestComponent::TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)
117     : m_xComponentContext(xCtx)
118 {
119     LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::TestComponent()", this));
120 }
121 
~TestComponent()122 TestComponent::~TestComponent()
123 {
124     LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::~TestComponent", this));
125 }
126 
getImplementationName()127 rtl::OUString SAL_CALL TestComponent::getImplementationName()
128 {
129     return m_implName;
130 }
131 
acquire()132 void SAL_CALL TestComponent::acquire() throw ()
133 {
134     cppu::WeakImplHelper1<lang::XServiceInfo>::acquire();
135 }
136 
release()137 void SAL_CALL TestComponent::release() throw ()
138 {
139     cppu::WeakImplHelper1<lang::XServiceInfo>::release();
140 }
141 
queryInterface(uno::Type const & rType)142 uno::Any SAL_CALL TestComponent::queryInterface(uno::Type const & rType )
143 {
144     return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
145 }
146 
supportsService(rtl::OUString const & ServiceName)147 sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName)
148 {
149     uno::Sequence<rtl::OUString> serviceNames = getSupportedServiceNames_Static();
150 
151     for (sal_Int32 n = 0; n < serviceNames.getLength(); ++n)
152     {
153         if (serviceNames[n] == ServiceName)
154             return true;
155     }
156 
157     return false;
158 }
159 
getSupportedServiceNames()160 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames()
161 {
162     return getSupportedServiceNames_Static();
163 }
164 
component_writeInfo(void *,void *)165 extern "C" sal_Bool SAL_CALL component_writeInfo(
166     void * /*serviceManager*/,
167     void * /*registryKey*/
168 )
169 {
170     g_envDcp = uno::Environment::getCurrent().getTypeName();
171 
172     return true;
173 }
174 
component_getFactory(char const * pImplName,void *,void *)175 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
176     char const * pImplName,
177     void       * /*serviceManager*/,
178     void       * /*registryKey*/
179 )
180 {
181     g_envDcp = uno::Environment::getCurrent().getTypeName();
182 
183     uno::Reference< lang::XSingleComponentFactory > xFactory;
184 
185     rtl::OUString uTmp(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
186 
187     rtl::OUString uImplName(cppu::EnvDcp::getTypeName(uTmp));
188     rtl::OUString cmpName(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent"));
189 
190     if (uImplName.equals(cmpName))
191     {
192         xFactory = cppu::createSingleComponentFactory(
193             TestComponent::create,
194             uImplName,
195             TestComponent::getSupportedServiceNames_Static());
196 
197         xFactory->acquire();
198     }
199 
200     return xFactory.get();
201 }
202 
component_getImplementationEnvironmentExt(sal_Char const ** envTypeName,uno_Environment **,sal_Char const * pImplName,uno_Environment *)203 extern "C" SAL_DLLPUBLIC_EXPORT  void SAL_CALL component_getImplementationEnvironmentExt(
204     sal_Char        const ** envTypeName,
205     uno_Environment       ** /*ppEnv*/,
206     sal_Char        const  * pImplName,
207     uno_Environment        * /*pSrcEnv*/
208 )
209 {
210     rtl::OString purpose;
211 
212     if (pImplName) // this is the purpose for a specified impl
213     {
214         rtl::OUString uImplName(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
215         purpose = rtl::OUStringToOString(cppu::EnvDcp::getPurpose(uImplName), RTL_TEXTENCODING_ASCII_US);
216     }
217 
218     if (!purpose.getLength())
219     {
220         char * pPurpose = getenv("TestComponent.uno");
221         if (pPurpose)
222             purpose = rtl::OString(pPurpose);
223     }
224 
225     if (purpose.getLength() == 0)
226         *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
227 
228     else
229     {
230         char buff[256];
231         strcpy(buff, CPPU_STRINGIFY(CPPU_ENV));
232         strcat(buff, purpose.getStr());
233 
234         *envTypeName = strdup(buff);
235     }
236 }
237