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