1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: excomp1.cxx,v $
10 * $Revision: 1.5.16.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
33 #include <osl/diagnose.h>
34 #include <osl/mutex.hxx>
35 #include <rtl/alloc.h>
36 #include <cppuhelper/factory.hxx>
37 #include <cppuhelper/implbase2.hxx>
39 #include <example/XTest.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <com/sun/star/lang/XServiceInfo.hpp>
42 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43 #include <com/sun/star/registry/XRegistryKey.hpp>
45 using namespace example
;
46 using namespace com::sun::star::uno
;
47 using namespace com::sun::star::lang
;
48 using namespace com::sun::star::registry
;
53 #define SERVICENAME1 "example.ExampleComponent1"
54 #define IMPLNAME1 "example.ExampleComponent1.Impl"
56 namespace excomp_impl
{
58 //*************************************************************************
59 // ExampleComponent1Impl
60 //*************************************************************************
61 class ExampleComponent1Impl
: public WeakImplHelper2
< XTest
, XServiceInfo
>
64 ExampleComponent1Impl( const Reference
<XMultiServiceFactory
> & rXSMgr
);
66 ~ExampleComponent1Impl();
69 virtual OUString SAL_CALL
getImplementationName( ) throw(RuntimeException
);
70 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw(RuntimeException
);
71 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) throw(RuntimeException
);
72 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_Static( );
75 virtual OUString SAL_CALL
getMessage() throw(RuntimeException
);
80 Reference
<XMultiServiceFactory
> m_xSMgr
;
83 //*************************************************************************
84 ExampleComponent1Impl::ExampleComponent1Impl( const Reference
<XMultiServiceFactory
> & rXSMgr
)
89 //*************************************************************************
90 ExampleComponent1Impl::~ExampleComponent1Impl()
94 //*************************************************************************
95 OUString SAL_CALL
ExampleComponent1Impl::getImplementationName( )
96 throw(RuntimeException
)
98 Guard
< Mutex
> aGuard( m_mutex
);
99 return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME1
) );
102 //*************************************************************************
103 sal_Bool SAL_CALL
ExampleComponent1Impl::supportsService( const OUString
& ServiceName
)
104 throw(RuntimeException
)
106 Guard
< Mutex
> aGuard( m_mutex
);
107 Sequence
< OUString
> aSNL
= getSupportedServiceNames();
108 const OUString
* pArray
= aSNL
.getArray();
109 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); i
++ )
110 if( pArray
[i
] == ServiceName
)
115 //*************************************************************************
116 Sequence
<OUString
> SAL_CALL
ExampleComponent1Impl::getSupportedServiceNames( )
117 throw(RuntimeException
)
119 Guard
< Mutex
> aGuard( m_mutex
);
120 return getSupportedServiceNames_Static();
123 //*************************************************************************
124 Sequence
<OUString
> SAL_CALL
ExampleComponent1Impl::getSupportedServiceNames_Static( )
126 OUString
aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME1
) );
127 return Sequence
< OUString
>( &aName
, 1 );
130 //*************************************************************************
131 OUString SAL_CALL
ExampleComponent1Impl::getMessage() throw(RuntimeException
)
133 Guard
< Mutex
> aGuard( m_mutex
);
134 return OUString::createFromAscii("Lalelu nur der Mann im Mond schaut zu ...");
138 //*************************************************************************
139 Reference
<XInterface
> SAL_CALL
ExampleComponent1_CreateInstance( const Reference
<XMultiServiceFactory
>& rSMgr
)
141 Reference
<XInterface
> xRet
;
143 XTest
*pXTest
= (XTest
*) new ExampleComponent1Impl(rSMgr
);
147 xRet
= Reference
< XInterface
>::query(pXTest
);
158 //==================================================================================================
159 void SAL_CALL
component_getImplementationEnvironment(
160 const sal_Char
** ppEnvTypeName
, uno_Environment
** /* ppEnv */ )
162 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
164 //==================================================================================================
165 sal_Bool SAL_CALL
component_writeInfo(
166 void * /* pServiceManager */ , void * pRegistryKey
)
173 Reference
< XRegistryKey
> xNewKey(
174 reinterpret_cast< XRegistryKey
* >( pRegistryKey
)->createKey(
175 OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME1
"/UNO/SERVICES") ) ) );
177 Sequence
< OUString
> aSNL
=
178 ::excomp_impl::ExampleComponent1Impl::getSupportedServiceNames_Static();
179 const OUString
* pArray
= aSNL
.getConstArray();
180 for ( sal_Int32 nPos
= aSNL
.getLength(); nPos
--; )
181 xNewKey
->createKey( pArray
[nPos
] );
185 catch (InvalidRegistryException
&)
187 OSL_ENSURE( sal_False
, "### InvalidRegistryException!" );
192 //==================================================================================================
193 void * SAL_CALL
component_getFactory(
194 const sal_Char
* pImplName
, void * pServiceManager
, void * /* pRegistryKey */ )
198 if (rtl_str_compare( pImplName
, IMPLNAME1
) == 0)
200 Reference
< XSingleServiceFactory
> xFactory( createSingleFactory(
201 reinterpret_cast< XMultiServiceFactory
* >( pServiceManager
),
202 OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME1
) ),
203 ::excomp_impl::ExampleComponent1_CreateInstance
,
204 ::excomp_impl::ExampleComponent1Impl::getSupportedServiceNames_Static() ) );
209 pRet
= xFactory
.get();