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