xref: /AOO41X/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/SServices.cxx (revision 97581ef47cb9ffaae6b8757544c8939c93b1f0cc)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <sal/types.h>
25 #include "SDriver.hxx"
26 #include <cppuhelper/factory.hxx>
27 #include <osl/diagnose.h>
28 
29 using namespace connectivity::skeleton;
30 using ::rtl::OUString;
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Sequence;
33 using ::com::sun::star::registry::XRegistryKey;
34 using ::com::sun::star::lang::XSingleServiceFactory;
35 using ::com::sun::star::lang::XMultiServiceFactory;
36 
37 typedef Reference< XSingleServiceFactory > (SAL_CALL *createFactoryFunc)
38         (
39             const Reference< XMultiServiceFactory > & rServiceManager,
40             const OUString & rComponentName,
41             ::cppu::ComponentInstantiation pCreateFunction,
42             const Sequence< OUString > & rServiceNames,
43             rtl_ModuleCount* _pTemp
44         );
45 
46 //***************************************************************************************
47 //
48 // The required C-Api must be provided!
49 // It contains of 3 special functions that have to be exported.
50 //
51 
52 //---------------------------------------------------------------------------------------
REGISTER_PROVIDER(const OUString & aServiceImplName,const Sequence<OUString> & Services,const Reference<::com::sun::star::registry::XRegistryKey> & xKey)53 void REGISTER_PROVIDER(
54         const OUString& aServiceImplName,
55         const Sequence< OUString>& Services,
56         const Reference< ::com::sun::star::registry::XRegistryKey > & xKey)
57 {
58     OUString aMainKeyName;
59     aMainKeyName = OUString::createFromAscii("/");
60     aMainKeyName += aServiceImplName;
61     aMainKeyName += OUString::createFromAscii("/UNO/SERVICES");
62 
63     Reference< ::com::sun::star::registry::XRegistryKey > xNewKey( xKey->createKey(aMainKeyName) );
64     OSL_ENSURE(xNewKey.is(), "SKELETON::component_writeInfo : could not create a registry key !");
65 
66     for (sal_uInt32 i=0; i<Services.getLength(); ++i)
67         xNewKey->createKey(Services[i]);
68 }
69 
70 
71 //---------------------------------------------------------------------------------------
72 struct ProviderRequest
73 {
74     Reference< XSingleServiceFactory > xRet;
75     Reference< XMultiServiceFactory > const xServiceManager;
76     OUString const sImplementationName;
77 
ProviderRequestProviderRequest78     ProviderRequest(
79         void* pServiceManager,
80         sal_Char const* pImplementationName
81     )
82     : xServiceManager(reinterpret_cast<XMultiServiceFactory*>(pServiceManager))
83     , sImplementationName(OUString::createFromAscii(pImplementationName))
84     {
85     }
86 
87     inline
CREATE_PROVIDERProviderRequest88     sal_Bool CREATE_PROVIDER(
89                 const OUString& Implname,
90                 const Sequence< OUString > & Services,
91                 ::cppu::ComponentInstantiation Factory,
92                 createFactoryFunc creator
93             )
94     {
95         if (!xRet.is() && (Implname == sImplementationName))
96         try
97         {
98             xRet = creator( xServiceManager, sImplementationName,Factory, Services,0);
99         }
100         catch(...)
101         {
102         }
103         return xRet.is();
104     }
105 
getProviderProviderRequest106     void* getProvider() const { return xRet.get(); }
107 };
108 
109 //---------------------------------------------------------------------------------------
110 
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment ** ppEnv)111 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
112                 const sal_Char  **ppEnvTypeName,
113                 uno_Environment **ppEnv
114             )
115 {
116     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
117 }
118 
119 // This method not longer necessary since OOo 3.4 where the component registration was
120 // was changed to passive component registration. For more details see
121 // https://wiki.openoffice.org/wiki/Passive_Component_Registration
122 //---------------------------------------------------------------------------------------
123 // extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(
124 //              void* pServiceManager,
125 //              void* pRegistryKey
126 //          )
127 // {
128 //  if (pRegistryKey)
129 //  try
130 //  {
131 //      Reference< ::com::sun::star::registry::XRegistryKey > xKey(reinterpret_cast< ::com::sun::star::registry::XRegistryKey*>(pRegistryKey));
132 
133 //      REGISTER_PROVIDER(
134 //          SkeletonDriver::getImplementationName_Static(),
135 //          SkeletonDriver::getSupportedServiceNames_Static(), xKey);
136 
137 //      return sal_True;
138 //  }
139 //  catch (::com::sun::star::registry::InvalidRegistryException& )
140 //  {
141 //      OSL_ENSURE(sal_False, "SKELETON::component_writeInfo : could not create a registry key ! ## InvalidRegistryException !");
142 //  }
143 
144 //  return sal_False;
145 // }
146 
147 //---------------------------------------------------------------------------------------
component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void * pRegistryKey)148 extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory(
149                     const sal_Char* pImplementationName,
150                     void* pServiceManager,
151                     void* pRegistryKey)
152 {
153     void* pRet = 0;
154     if (pServiceManager)
155     {
156         ProviderRequest aReq(pServiceManager,pImplementationName);
157 
158         aReq.CREATE_PROVIDER(
159             SkeletonDriver::getImplementationName_Static(),
160             SkeletonDriver::getSupportedServiceNames_Static(),
161             SkeletonDriver_CreateInstance, ::cppu::createSingleFactory)
162         ;
163 
164         if(aReq.xRet.is())
165             aReq.xRet->acquire();
166 
167         pRet = aReq.getProvider();
168     }
169 
170     return pRet;
171 };
172 
173