1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx> // "3" implementing three interfaces 36*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 37*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 42*cdf0e10cSrcweir #include <my_module/XSomething.hpp> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir using namespace ::rtl; // for OUString 46*cdf0e10cSrcweir using namespace ::com::sun::star; // for odk interfaces 47*cdf0e10cSrcweir using namespace ::com::sun::star::uno; // for basic types 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir namespace my_sc_impl 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir extern Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl(); 54*cdf0e10cSrcweir extern OUString SAL_CALL getImplementationName_MyService1Impl(); 55*cdf0e10cSrcweir extern Reference< XInterface > SAL_CALL create_MyService1Impl( 56*cdf0e10cSrcweir Reference< XComponentContext > const & xContext ) 57*cdf0e10cSrcweir SAL_THROW( () ); 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir static Sequence< OUString > getSupportedServiceNames_MyService2Impl() 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir Sequence<OUString> names(1); 62*cdf0e10cSrcweir names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2")); 63*cdf0e10cSrcweir return names; 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir static OUString getImplementationName_MyService2Impl() 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 69*cdf0e10cSrcweir "my_module.my_sc_implementation.MyService2") ); 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir class MyService2Impl : public ::cppu::WeakImplHelper3< 73*cdf0e10cSrcweir ::my_module::XSomething, lang::XServiceInfo, lang::XInitialization > 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir OUString m_sData; 76*cdf0e10cSrcweir // it's good practise to store the context for further use when you use 77*cdf0e10cSrcweir // other UNO API's in your implementation 78*cdf0e10cSrcweir Reference< XComponentContext > m_xContext; 79*cdf0e10cSrcweir public: 80*cdf0e10cSrcweir inline MyService2Impl(Reference< XComponentContext > const & xContext) throw () 81*cdf0e10cSrcweir : m_xContext(xContext) 82*cdf0e10cSrcweir {} 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir virtual ~MyService2Impl() {} 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // focus on three given interfaces, 87*cdf0e10cSrcweir // no need to implement XInterface, XTypeProvider, XWeak 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // XInitialization will be called upon 90*cdf0e10cSrcweir // createInstanceWithArguments[AndContext]() 91*cdf0e10cSrcweir virtual void SAL_CALL initialize( Sequence< Any > const & args ) 92*cdf0e10cSrcweir throw (Exception); 93*cdf0e10cSrcweir // XSomething 94*cdf0e10cSrcweir virtual OUString SAL_CALL methodOne( OUString const & str ) 95*cdf0e10cSrcweir throw (RuntimeException); 96*cdf0e10cSrcweir virtual OUString SAL_CALL methodTwo( ) 97*cdf0e10cSrcweir throw (RuntimeException); 98*cdf0e10cSrcweir // XServiceInfo 99*cdf0e10cSrcweir virtual OUString SAL_CALL getImplementationName() 100*cdf0e10cSrcweir throw (RuntimeException); 101*cdf0e10cSrcweir virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) 102*cdf0e10cSrcweir throw (RuntimeException); 103*cdf0e10cSrcweir virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() 104*cdf0e10cSrcweir throw (RuntimeException); 105*cdf0e10cSrcweir }; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir // XInitialization implemention 108*cdf0e10cSrcweir void MyService2Impl::initialize( Sequence< Any > const & args ) 109*cdf0e10cSrcweir throw (Exception) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir if (args.getLength() != 1) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir throw lang::IllegalArgumentException( 114*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( 115*cdf0e10cSrcweir "give a string instanciating this component!") ), 116*cdf0e10cSrcweir // resolve to XInterface reference: 117*cdf0e10cSrcweir static_cast< ::cppu::OWeakObject * >(this), 118*cdf0e10cSrcweir 0 ); // argument pos 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir if (! (args[ 0 ] >>= m_sData)) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir throw lang::IllegalArgumentException( 123*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( 124*cdf0e10cSrcweir "no string given as argument!") ), 125*cdf0e10cSrcweir // resolve to XInterface reference: 126*cdf0e10cSrcweir static_cast< ::cppu::OWeakObject * >(this), 127*cdf0e10cSrcweir 0 ); // argument pos 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir // XSomething implementation 132*cdf0e10cSrcweir OUString MyService2Impl::methodOne( OUString const & str ) 133*cdf0e10cSrcweir throw (RuntimeException) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir m_sData = str; 136*cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 137*cdf0e10cSrcweir "called methodOne() of MyService2 implementation: ") ) + m_sData; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir OUString MyService2Impl::methodTwo( ) 141*cdf0e10cSrcweir throw (RuntimeException) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 144*cdf0e10cSrcweir "called methodTwo() of MyService2 implementation: ") ) + m_sData; 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir // XServiceInfo implementation 148*cdf0e10cSrcweir OUString MyService2Impl::getImplementationName() 149*cdf0e10cSrcweir throw (RuntimeException) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir // unique implementation name 152*cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( 153*cdf0e10cSrcweir "my_module.my_sc_implementation.MyService2") ); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir sal_Bool MyService2Impl::supportsService( OUString const & serviceName ) 157*cdf0e10cSrcweir throw (RuntimeException) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir // this object only supports one service, so the test is simple 160*cdf0e10cSrcweir return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 161*cdf0e10cSrcweir "my_module.MyService2") ); 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir Sequence< OUString > MyService2Impl::getSupportedServiceNames() 165*cdf0e10cSrcweir throw (RuntimeException) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir return getSupportedServiceNames_MyService2Impl(); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir Reference< XInterface > SAL_CALL create_MyService2Impl( 171*cdf0e10cSrcweir Reference< XComponentContext > const & xContext ) 172*cdf0e10cSrcweir SAL_THROW( () ) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir return static_cast< ::cppu::OWeakObject * >( new MyService2Impl( xContext ) ); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir /* shared lib exports implemented without helpers in service_impl1.cxx */ 180*cdf0e10cSrcweir namespace my_sc_impl 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir static struct ::cppu::ImplementationEntry s_component_entries [] = 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir create_MyService1Impl, getImplementationName_MyService1Impl, 186*cdf0e10cSrcweir getSupportedServiceNames_MyService1Impl, 187*cdf0e10cSrcweir ::cppu::createSingleComponentFactory, 188*cdf0e10cSrcweir 0, 0 189*cdf0e10cSrcweir }, 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir create_MyService2Impl, getImplementationName_MyService2Impl, 192*cdf0e10cSrcweir getSupportedServiceNames_MyService2Impl, 193*cdf0e10cSrcweir ::cppu::createSingleComponentFactory, 194*cdf0e10cSrcweir 0, 0 195*cdf0e10cSrcweir }, 196*cdf0e10cSrcweir { 0, 0, 0, 0, 0, 0 } 197*cdf0e10cSrcweir }; 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir extern "C" 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment( 203*cdf0e10cSrcweir sal_Char const ** ppEnvTypeName, uno_Environment ** ) 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir // This method not longer necessary since OOo 3.4 where the component registration was 209*cdf0e10cSrcweir // was changed to passive component registration. For more details see 210*cdf0e10cSrcweir // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration 211*cdf0e10cSrcweir // 212*cdf0e10cSrcweir // sal_Bool SAL_CALL component_writeInfo( 213*cdf0e10cSrcweir // lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry ) 214*cdf0e10cSrcweir // { 215*cdf0e10cSrcweir // return ::cppu::component_writeInfoHelper( 216*cdf0e10cSrcweir // xMgr, xRegistry, ::my_sc_impl::s_component_entries ); 217*cdf0e10cSrcweir // } 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory( 221*cdf0e10cSrcweir sal_Char const * implName, lang::XMultiServiceFactory * xMgr, 222*cdf0e10cSrcweir registry::XRegistryKey * xRegistry ) 223*cdf0e10cSrcweir { 224*cdf0e10cSrcweir return ::cppu::component_getFactoryHelper( 225*cdf0e10cSrcweir implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries ); 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir 231