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