xref: /trunk/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/SDriver.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 "SDriver.hxx"
25 #include "SConnection.hxx"
26 
27 using namespace com::sun::star::uno;
28 using namespace com::sun::star::lang;
29 using namespace com::sun::star::beans;
30 using namespace com::sun::star::sdbc;
31 using namespace connectivity::skeleton;
32 
33 namespace connectivity
34 {
35     namespace skeleton
36     {
37         //------------------------------------------------------------------
SkeletonDriver_CreateInstance(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & _rxFactory)38         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >  SAL_CALL SkeletonDriver_CreateInstance(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory)
39         {
40             return *(new SkeletonDriver());
41         }
42     }
43 }
44 // --------------------------------------------------------------------------------
SkeletonDriver()45 SkeletonDriver::SkeletonDriver()
46     : ODriver_BASE(m_aMutex)
47 {
48 }
49 // --------------------------------------------------------------------------------
disposing()50 void SkeletonDriver::disposing()
51 {
52     ::osl::MutexGuard aGuard(m_aMutex);
53 
54     // when driver will be destroied so all our connections have to be destroied as well
55     for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
56     {
57         Reference< XComponent > xComp(i->get(), UNO_QUERY);
58         if (xComp.is())
59             xComp->dispose();
60     }
61     m_xConnections.clear();
62 
63     ODriver_BASE::disposing();
64 }
65 
66 // static ServiceInfo
67 //------------------------------------------------------------------------------
getImplementationName_Static()68 rtl::OUString SkeletonDriver::getImplementationName_Static(  )
69 {
70     return rtl::OUString::createFromAscii("com.sun.star.comp.sdbc.SkeletonDriver");
71         // this name is referenced in the configuration and in the skeleton.xml
72         // Please take care when changing it.
73 }
74 //------------------------------------------------------------------------------
getSupportedServiceNames_Static()75 Sequence< ::rtl::OUString > SkeletonDriver::getSupportedServiceNames_Static(  )
76 {
77     // which service is supported
78     // for more information @see com.sun.star.sdbc.Driver
79     Sequence< ::rtl::OUString > aSNS( 1 );
80     aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
81     return aSNS;
82 }
83 
84 //------------------------------------------------------------------
getImplementationName()85 ::rtl::OUString SAL_CALL SkeletonDriver::getImplementationName(  )
86 {
87     return getImplementationName_Static();
88 }
89 
90 //------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)91 sal_Bool SAL_CALL SkeletonDriver::supportsService( const ::rtl::OUString& _rServiceName )
92 {
93     Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
94     const ::rtl::OUString* pSupported = aSupported.getConstArray();
95     const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
96     for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
97         ;
98 
99     return pSupported != pEnd;
100 }
101 
102 //------------------------------------------------------------------
getSupportedServiceNames()103 Sequence< ::rtl::OUString > SAL_CALL SkeletonDriver::getSupportedServiceNames(  )
104 {
105     return getSupportedServiceNames_Static();
106 }
107 
108 // --------------------------------------------------------------------------------
connect(const::rtl::OUString & url,const Sequence<PropertyValue> & info)109 Reference< XConnection > SAL_CALL SkeletonDriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info )
110 {
111     // create a new connection with the given properties and append it to our vector
112     OConnection* pCon = new OConnection(this);
113     Reference< XConnection > xCon = pCon;   // important here because otherwise the connection could be deleted inside (refcount goes -> 0)
114     pCon->construct(url,info);              // late constructor call which can throw exception and allows a correct dtor call when so
115     m_xConnections.push_back(WeakReferenceHelper(*pCon));
116 
117     return xCon;
118 }
119 // --------------------------------------------------------------------------------
acceptsURL(const::rtl::OUString & url)120 sal_Bool SAL_CALL SkeletonDriver::acceptsURL( const ::rtl::OUString& url )
121 {
122     // here we have to look if we support this url format
123     // change the URL format to your needs, but please aware that the first on who accepts the URl wins.
124     return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:skeleton:"),14));
125 }
126 // --------------------------------------------------------------------------------
getPropertyInfo(const::rtl::OUString & url,const Sequence<PropertyValue> & info)127 Sequence< DriverPropertyInfo > SAL_CALL SkeletonDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& info )
128 {
129     // if you have something special to say, return it here :-)
130     return Sequence< DriverPropertyInfo >();
131 }
132 // --------------------------------------------------------------------------------
getMajorVersion()133 sal_Int32 SAL_CALL SkeletonDriver::getMajorVersion(  )
134 {
135     return 0; // depends on you
136 }
137 // --------------------------------------------------------------------------------
getMinorVersion()138 sal_Int32 SAL_CALL SkeletonDriver::getMinorVersion(  )
139 {
140     return 1; // depends on you
141 }
142 // --------------------------------------------------------------------------------
143 
144 //.........................................................................
145 namespace connectivity
146 {
147     namespace skeleton
148     {
149 //.........................................................................
150 
release(oslInterlockedCount & _refCount,::cppu::OBroadcastHelper & rBHelper,::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _xInterface,::com::sun::star::lang::XComponent * _pObject)151 void release(oslInterlockedCount& _refCount,
152              ::cppu::OBroadcastHelper& rBHelper,
153              ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
154              ::com::sun::star::lang::XComponent* _pObject)
155 {
156     if (osl_decrementInterlockedCount( &_refCount ) == 0)
157     {
158         osl_incrementInterlockedCount( &_refCount );
159 
160         if (!rBHelper.bDisposed && !rBHelper.bInDispose)
161         {
162             // remember the parent
163             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xParent;
164             {
165                 ::osl::MutexGuard aGuard( rBHelper.rMutex );
166                 xParent = _xInterface;
167                 _xInterface = NULL;
168             }
169 
170             // First dispose
171             _pObject->dispose();
172 
173             // only the alive ref holds the object
174             OSL_ASSERT( _refCount == 1 );
175 
176             // release the parent in the ~
177             if (xParent.is())
178             {
179                 ::osl::MutexGuard aGuard( rBHelper.rMutex );
180                 _xInterface = xParent;
181             }
182         }
183     }
184     else
185         osl_incrementInterlockedCount( &_refCount );
186 }
187 
checkDisposed(sal_Bool _bThrow)188 void checkDisposed(sal_Bool _bThrow)
189 {
190     if (_bThrow)
191         throw DisposedException();
192 
193 }
194 //.........................................................................
195     }
196 }
197 //.........................................................................
198