xref: /trunk/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/SServices.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 <sal/types.h>
36*cdf0e10cSrcweir #include "SDriver.hxx"
37*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
38*cdf0e10cSrcweir #include <osl/diagnose.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace connectivity::skeleton;
41*cdf0e10cSrcweir using ::rtl::OUString;
42*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
43*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
44*cdf0e10cSrcweir using ::com::sun::star::registry::XRegistryKey;
45*cdf0e10cSrcweir using ::com::sun::star::lang::XSingleServiceFactory;
46*cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir typedef Reference< XSingleServiceFactory > (SAL_CALL *createFactoryFunc)
49*cdf0e10cSrcweir         (
50*cdf0e10cSrcweir             const Reference< XMultiServiceFactory > & rServiceManager,
51*cdf0e10cSrcweir             const OUString & rComponentName,
52*cdf0e10cSrcweir             ::cppu::ComponentInstantiation pCreateFunction,
53*cdf0e10cSrcweir             const Sequence< OUString > & rServiceNames,
54*cdf0e10cSrcweir             rtl_ModuleCount* _pTemp
55*cdf0e10cSrcweir         );
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //***************************************************************************************
58*cdf0e10cSrcweir //
59*cdf0e10cSrcweir // The required C-Api must be provided!
60*cdf0e10cSrcweir // It contains of 3 special functions that have to be exported.
61*cdf0e10cSrcweir //
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir //---------------------------------------------------------------------------------------
64*cdf0e10cSrcweir void REGISTER_PROVIDER(
65*cdf0e10cSrcweir         const OUString& aServiceImplName,
66*cdf0e10cSrcweir         const Sequence< OUString>& Services,
67*cdf0e10cSrcweir         const Reference< ::com::sun::star::registry::XRegistryKey > & xKey)
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir     OUString aMainKeyName;
70*cdf0e10cSrcweir     aMainKeyName = OUString::createFromAscii("/");
71*cdf0e10cSrcweir     aMainKeyName += aServiceImplName;
72*cdf0e10cSrcweir     aMainKeyName += OUString::createFromAscii("/UNO/SERVICES");
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     Reference< ::com::sun::star::registry::XRegistryKey >  xNewKey( xKey->createKey(aMainKeyName) );
75*cdf0e10cSrcweir     OSL_ENSURE(xNewKey.is(), "SKELETON::component_writeInfo : could not create a registry key !");
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     for (sal_uInt32 i=0; i<Services.getLength(); ++i)
78*cdf0e10cSrcweir         xNewKey->createKey(Services[i]);
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir //---------------------------------------------------------------------------------------
83*cdf0e10cSrcweir struct ProviderRequest
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     Reference< XSingleServiceFactory > xRet;
86*cdf0e10cSrcweir     Reference< XMultiServiceFactory > const xServiceManager;
87*cdf0e10cSrcweir     OUString const sImplementationName;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     ProviderRequest(
90*cdf0e10cSrcweir         void* pServiceManager,
91*cdf0e10cSrcweir         sal_Char const* pImplementationName
92*cdf0e10cSrcweir     )
93*cdf0e10cSrcweir     : xServiceManager(reinterpret_cast<XMultiServiceFactory*>(pServiceManager))
94*cdf0e10cSrcweir     , sImplementationName(OUString::createFromAscii(pImplementationName))
95*cdf0e10cSrcweir     {
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     inline
99*cdf0e10cSrcweir     sal_Bool CREATE_PROVIDER(
100*cdf0e10cSrcweir                 const OUString& Implname,
101*cdf0e10cSrcweir                 const Sequence< OUString > & Services,
102*cdf0e10cSrcweir                 ::cppu::ComponentInstantiation Factory,
103*cdf0e10cSrcweir                 createFactoryFunc creator
104*cdf0e10cSrcweir             )
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         if (!xRet.is() && (Implname == sImplementationName))
107*cdf0e10cSrcweir         try
108*cdf0e10cSrcweir         {
109*cdf0e10cSrcweir             xRet = creator( xServiceManager, sImplementationName,Factory, Services,0);
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir         catch(...)
112*cdf0e10cSrcweir         {
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir         return xRet.is();
115*cdf0e10cSrcweir     }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     void* getProvider() const { return xRet.get(); }
118*cdf0e10cSrcweir };
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir //---------------------------------------------------------------------------------------
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
123*cdf0e10cSrcweir                 const sal_Char  **ppEnvTypeName,
124*cdf0e10cSrcweir                 uno_Environment **ppEnv
125*cdf0e10cSrcweir             )
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir // This method not longer necessary since OOo 3.4 where the component registration was
131*cdf0e10cSrcweir // was changed to passive component registration. For more details see
132*cdf0e10cSrcweir // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
133*cdf0e10cSrcweir //---------------------------------------------------------------------------------------
134*cdf0e10cSrcweir // extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(
135*cdf0e10cSrcweir //              void* pServiceManager,
136*cdf0e10cSrcweir //              void* pRegistryKey
137*cdf0e10cSrcweir //          )
138*cdf0e10cSrcweir // {
139*cdf0e10cSrcweir //  if (pRegistryKey)
140*cdf0e10cSrcweir //  try
141*cdf0e10cSrcweir //  {
142*cdf0e10cSrcweir //      Reference< ::com::sun::star::registry::XRegistryKey > xKey(reinterpret_cast< ::com::sun::star::registry::XRegistryKey*>(pRegistryKey));
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir //      REGISTER_PROVIDER(
145*cdf0e10cSrcweir //          SkeletonDriver::getImplementationName_Static(),
146*cdf0e10cSrcweir //          SkeletonDriver::getSupportedServiceNames_Static(), xKey);
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir //      return sal_True;
149*cdf0e10cSrcweir //  }
150*cdf0e10cSrcweir //  catch (::com::sun::star::registry::InvalidRegistryException& )
151*cdf0e10cSrcweir //  {
152*cdf0e10cSrcweir //      OSL_ENSURE(sal_False, "SKELETON::component_writeInfo : could not create a registry key ! ## InvalidRegistryException !");
153*cdf0e10cSrcweir //  }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir //  return sal_False;
156*cdf0e10cSrcweir // }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir //---------------------------------------------------------------------------------------
159*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory(
160*cdf0e10cSrcweir                     const sal_Char* pImplementationName,
161*cdf0e10cSrcweir                     void* pServiceManager,
162*cdf0e10cSrcweir                     void* pRegistryKey)
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir     void* pRet = 0;
165*cdf0e10cSrcweir     if (pServiceManager)
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         ProviderRequest aReq(pServiceManager,pImplementationName);
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         aReq.CREATE_PROVIDER(
170*cdf0e10cSrcweir             SkeletonDriver::getImplementationName_Static(),
171*cdf0e10cSrcweir             SkeletonDriver::getSupportedServiceNames_Static(),
172*cdf0e10cSrcweir             SkeletonDriver_CreateInstance, ::cppu::createSingleFactory)
173*cdf0e10cSrcweir         ;
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir         if(aReq.xRet.is())
176*cdf0e10cSrcweir             aReq.xRet->acquire();
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir         pRet = aReq.getProvider();
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     return pRet;
182*cdf0e10cSrcweir };
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 
185