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