xref: /AOO41X/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/SDriver.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "SDriver.hxx"
36*cdf0e10cSrcweir #include "SConnection.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir using namespace com::sun::star::uno;
39*cdf0e10cSrcweir using namespace com::sun::star::lang;
40*cdf0e10cSrcweir using namespace com::sun::star::beans;
41*cdf0e10cSrcweir using namespace com::sun::star::sdbc;
42*cdf0e10cSrcweir using namespace connectivity::skeleton;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir namespace connectivity
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir 	namespace skeleton
47*cdf0e10cSrcweir 	{
48*cdf0e10cSrcweir 		//------------------------------------------------------------------
49*cdf0e10cSrcweir 		::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*cdf0e10cSrcweir 		{
51*cdf0e10cSrcweir 			return *(new SkeletonDriver());
52*cdf0e10cSrcweir 		}
53*cdf0e10cSrcweir 	}
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir // --------------------------------------------------------------------------------
56*cdf0e10cSrcweir SkeletonDriver::SkeletonDriver()
57*cdf0e10cSrcweir 	: ODriver_BASE(m_aMutex)
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir // --------------------------------------------------------------------------------
61*cdf0e10cSrcweir void SkeletonDriver::disposing()
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	// when driver will be destroied so all our connections have to be destroied as well
66*cdf0e10cSrcweir 	for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
67*cdf0e10cSrcweir 	{
68*cdf0e10cSrcweir 		Reference< XComponent > xComp(i->get(), UNO_QUERY);
69*cdf0e10cSrcweir 		if (xComp.is())
70*cdf0e10cSrcweir 			xComp->dispose();
71*cdf0e10cSrcweir 	}
72*cdf0e10cSrcweir 	m_xConnections.clear();
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	ODriver_BASE::disposing();
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir // static ServiceInfo
78*cdf0e10cSrcweir //------------------------------------------------------------------------------
79*cdf0e10cSrcweir rtl::OUString SkeletonDriver::getImplementationName_Static(  ) throw(RuntimeException)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	return rtl::OUString::createFromAscii("com.sun.star.comp.sdbc.SkeletonDriver");
82*cdf0e10cSrcweir 		// this name is referenced in the configuration and in the skeleton.xml
83*cdf0e10cSrcweir 		// Please take care when changing it.
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir //------------------------------------------------------------------------------
86*cdf0e10cSrcweir Sequence< ::rtl::OUString > SkeletonDriver::getSupportedServiceNames_Static(  ) throw (RuntimeException)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir 	// which service is supported
89*cdf0e10cSrcweir 	// for more information @see com.sun.star.sdbc.Driver
90*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSNS( 1 );
91*cdf0e10cSrcweir 	aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
92*cdf0e10cSrcweir 	return aSNS;
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir //------------------------------------------------------------------
96*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SkeletonDriver::getImplementationName(  ) throw(RuntimeException)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	return getImplementationName_Static();
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir //------------------------------------------------------------------
102*cdf0e10cSrcweir sal_Bool SAL_CALL SkeletonDriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
105*cdf0e10cSrcweir 	const ::rtl::OUString* pSupported = aSupported.getConstArray();
106*cdf0e10cSrcweir 	const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
107*cdf0e10cSrcweir 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
108*cdf0e10cSrcweir 		;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	return pSupported != pEnd;
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir //------------------------------------------------------------------
114*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SkeletonDriver::getSupportedServiceNames(  ) throw(RuntimeException)
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir // --------------------------------------------------------------------------------
120*cdf0e10cSrcweir Reference< XConnection > SAL_CALL SkeletonDriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	// create a new connection with the given properties and append it to our vector
123*cdf0e10cSrcweir 	OConnection* pCon = new OConnection(this);
124*cdf0e10cSrcweir 	Reference< XConnection > xCon = pCon;	// important here because otherwise the connection could be deleted inside (refcount goes -> 0)
125*cdf0e10cSrcweir 	pCon->construct(url,info);				// late constructor call which can throw exception and allows a correct dtor call when so
126*cdf0e10cSrcweir 	m_xConnections.push_back(WeakReferenceHelper(*pCon));
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	return xCon;
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir // --------------------------------------------------------------------------------
131*cdf0e10cSrcweir sal_Bool SAL_CALL SkeletonDriver::acceptsURL( const ::rtl::OUString& url )
132*cdf0e10cSrcweir 		throw(SQLException, RuntimeException)
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir 	// here we have to look if we support this url format
135*cdf0e10cSrcweir 	// change the URL format to your needs, but please aware that the first on who accepts the URl wins.
136*cdf0e10cSrcweir 	return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:skeleton:"),14));
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir // --------------------------------------------------------------------------------
139*cdf0e10cSrcweir Sequence< DriverPropertyInfo > SAL_CALL SkeletonDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	// if you have somthing special to say, return it here :-)
142*cdf0e10cSrcweir 	return Sequence< DriverPropertyInfo >();
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir // --------------------------------------------------------------------------------
145*cdf0e10cSrcweir sal_Int32 SAL_CALL SkeletonDriver::getMajorVersion(  ) throw(RuntimeException)
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir 	return 0; // depends on you
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir // --------------------------------------------------------------------------------
150*cdf0e10cSrcweir sal_Int32 SAL_CALL SkeletonDriver::getMinorVersion(  ) throw(RuntimeException)
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	return 1; // depends on you
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir // --------------------------------------------------------------------------------
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir //.........................................................................
157*cdf0e10cSrcweir namespace connectivity
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	namespace skeleton
160*cdf0e10cSrcweir 	{
161*cdf0e10cSrcweir //.........................................................................
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir void release(oslInterlockedCount& _refCount,
164*cdf0e10cSrcweir 			 ::cppu::OBroadcastHelper& rBHelper,
165*cdf0e10cSrcweir 			 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
166*cdf0e10cSrcweir 			 ::com::sun::star::lang::XComponent* _pObject)
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir 	if (osl_decrementInterlockedCount( &_refCount ) == 0)
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir 		osl_incrementInterlockedCount( &_refCount );
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 		if (!rBHelper.bDisposed && !rBHelper.bInDispose)
173*cdf0e10cSrcweir 		{
174*cdf0e10cSrcweir 			// remember the parent
175*cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xParent;
176*cdf0e10cSrcweir 			{
177*cdf0e10cSrcweir 				::osl::MutexGuard aGuard( rBHelper.rMutex );
178*cdf0e10cSrcweir 				xParent = _xInterface;
179*cdf0e10cSrcweir 				_xInterface = NULL;
180*cdf0e10cSrcweir 			}
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 			// First dispose
183*cdf0e10cSrcweir 			_pObject->dispose();
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 			// only the alive ref holds the object
186*cdf0e10cSrcweir 			OSL_ASSERT( _refCount == 1 );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 			// release the parent in the ~
189*cdf0e10cSrcweir 			if (xParent.is())
190*cdf0e10cSrcweir 			{
191*cdf0e10cSrcweir 				::osl::MutexGuard aGuard( rBHelper.rMutex );
192*cdf0e10cSrcweir 				_xInterface = xParent;
193*cdf0e10cSrcweir 			}
194*cdf0e10cSrcweir 		}
195*cdf0e10cSrcweir 	}
196*cdf0e10cSrcweir 	else
197*cdf0e10cSrcweir 		osl_incrementInterlockedCount( &_refCount );
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir void checkDisposed(sal_Bool _bThrow) throw ( DisposedException )
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir 	if (_bThrow)
203*cdf0e10cSrcweir 		throw DisposedException();
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir //.........................................................................
207*cdf0e10cSrcweir 	}
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir //.........................................................................
210*cdf0e10cSrcweir 
211