1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski #include "SDriver.hxx"
25*b1cdbd2cSJim Jagielski #include "SConnection.hxx"
26*b1cdbd2cSJim Jagielski 
27*b1cdbd2cSJim Jagielski using namespace com::sun::star::uno;
28*b1cdbd2cSJim Jagielski using namespace com::sun::star::lang;
29*b1cdbd2cSJim Jagielski using namespace com::sun::star::beans;
30*b1cdbd2cSJim Jagielski using namespace com::sun::star::sdbc;
31*b1cdbd2cSJim Jagielski using namespace connectivity::skeleton;
32*b1cdbd2cSJim Jagielski 
33*b1cdbd2cSJim Jagielski namespace connectivity
34*b1cdbd2cSJim Jagielski {
35*b1cdbd2cSJim Jagielski 	namespace skeleton
36*b1cdbd2cSJim Jagielski 	{
37*b1cdbd2cSJim Jagielski 		//------------------------------------------------------------------
SkeletonDriver_CreateInstance(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & _rxFactory)38*b1cdbd2cSJim Jagielski 		::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 )
39*b1cdbd2cSJim Jagielski 		{
40*b1cdbd2cSJim Jagielski 			return *(new SkeletonDriver());
41*b1cdbd2cSJim Jagielski 		}
42*b1cdbd2cSJim Jagielski 	}
43*b1cdbd2cSJim Jagielski }
44*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
SkeletonDriver()45*b1cdbd2cSJim Jagielski SkeletonDriver::SkeletonDriver()
46*b1cdbd2cSJim Jagielski 	: ODriver_BASE(m_aMutex)
47*b1cdbd2cSJim Jagielski {
48*b1cdbd2cSJim Jagielski }
49*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
disposing()50*b1cdbd2cSJim Jagielski void SkeletonDriver::disposing()
51*b1cdbd2cSJim Jagielski {
52*b1cdbd2cSJim Jagielski 	::osl::MutexGuard aGuard(m_aMutex);
53*b1cdbd2cSJim Jagielski 
54*b1cdbd2cSJim Jagielski 	// when driver will be destroied so all our connections have to be destroied as well
55*b1cdbd2cSJim Jagielski 	for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
56*b1cdbd2cSJim Jagielski 	{
57*b1cdbd2cSJim Jagielski 		Reference< XComponent > xComp(i->get(), UNO_QUERY);
58*b1cdbd2cSJim Jagielski 		if (xComp.is())
59*b1cdbd2cSJim Jagielski 			xComp->dispose();
60*b1cdbd2cSJim Jagielski 	}
61*b1cdbd2cSJim Jagielski 	m_xConnections.clear();
62*b1cdbd2cSJim Jagielski 
63*b1cdbd2cSJim Jagielski 	ODriver_BASE::disposing();
64*b1cdbd2cSJim Jagielski }
65*b1cdbd2cSJim Jagielski 
66*b1cdbd2cSJim Jagielski // static ServiceInfo
67*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------------
getImplementationName_Static()68*b1cdbd2cSJim Jagielski rtl::OUString SkeletonDriver::getImplementationName_Static(  ) throw(RuntimeException)
69*b1cdbd2cSJim Jagielski {
70*b1cdbd2cSJim Jagielski 	return rtl::OUString::createFromAscii("com.sun.star.comp.sdbc.SkeletonDriver");
71*b1cdbd2cSJim Jagielski 		// this name is referenced in the configuration and in the skeleton.xml
72*b1cdbd2cSJim Jagielski 		// Please take care when changing it.
73*b1cdbd2cSJim Jagielski }
74*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------------
getSupportedServiceNames_Static()75*b1cdbd2cSJim Jagielski Sequence< ::rtl::OUString > SkeletonDriver::getSupportedServiceNames_Static(  ) throw (RuntimeException)
76*b1cdbd2cSJim Jagielski {
77*b1cdbd2cSJim Jagielski 	// which service is supported
78*b1cdbd2cSJim Jagielski 	// for more information @see com.sun.star.sdbc.Driver
79*b1cdbd2cSJim Jagielski 	Sequence< ::rtl::OUString > aSNS( 1 );
80*b1cdbd2cSJim Jagielski 	aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
81*b1cdbd2cSJim Jagielski 	return aSNS;
82*b1cdbd2cSJim Jagielski }
83*b1cdbd2cSJim Jagielski 
84*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
getImplementationName()85*b1cdbd2cSJim Jagielski ::rtl::OUString SAL_CALL SkeletonDriver::getImplementationName(  ) throw(RuntimeException)
86*b1cdbd2cSJim Jagielski {
87*b1cdbd2cSJim Jagielski 	return getImplementationName_Static();
88*b1cdbd2cSJim Jagielski }
89*b1cdbd2cSJim Jagielski 
90*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)91*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL SkeletonDriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
92*b1cdbd2cSJim Jagielski {
93*b1cdbd2cSJim Jagielski 	Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
94*b1cdbd2cSJim Jagielski 	const ::rtl::OUString* pSupported = aSupported.getConstArray();
95*b1cdbd2cSJim Jagielski 	const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
96*b1cdbd2cSJim Jagielski 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
97*b1cdbd2cSJim Jagielski 		;
98*b1cdbd2cSJim Jagielski 
99*b1cdbd2cSJim Jagielski 	return pSupported != pEnd;
100*b1cdbd2cSJim Jagielski }
101*b1cdbd2cSJim Jagielski 
102*b1cdbd2cSJim Jagielski //------------------------------------------------------------------
getSupportedServiceNames()103*b1cdbd2cSJim Jagielski Sequence< ::rtl::OUString > SAL_CALL SkeletonDriver::getSupportedServiceNames(  ) throw(RuntimeException)
104*b1cdbd2cSJim Jagielski {
105*b1cdbd2cSJim Jagielski 	return getSupportedServiceNames_Static();
106*b1cdbd2cSJim Jagielski }
107*b1cdbd2cSJim Jagielski 
108*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
connect(const::rtl::OUString & url,const Sequence<PropertyValue> & info)109*b1cdbd2cSJim Jagielski Reference< XConnection > SAL_CALL SkeletonDriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
110*b1cdbd2cSJim Jagielski {
111*b1cdbd2cSJim Jagielski 	// create a new connection with the given properties and append it to our vector
112*b1cdbd2cSJim Jagielski 	OConnection* pCon = new OConnection(this);
113*b1cdbd2cSJim Jagielski 	Reference< XConnection > xCon = pCon;	// important here because otherwise the connection could be deleted inside (refcount goes -> 0)
114*b1cdbd2cSJim Jagielski 	pCon->construct(url,info);				// late constructor call which can throw exception and allows a correct dtor call when so
115*b1cdbd2cSJim Jagielski 	m_xConnections.push_back(WeakReferenceHelper(*pCon));
116*b1cdbd2cSJim Jagielski 
117*b1cdbd2cSJim Jagielski 	return xCon;
118*b1cdbd2cSJim Jagielski }
119*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
acceptsURL(const::rtl::OUString & url)120*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL SkeletonDriver::acceptsURL( const ::rtl::OUString& url )
121*b1cdbd2cSJim Jagielski 		throw(SQLException, RuntimeException)
122*b1cdbd2cSJim Jagielski {
123*b1cdbd2cSJim Jagielski 	// here we have to look if we support this url format
124*b1cdbd2cSJim Jagielski 	// change the URL format to your needs, but please aware that the first on who accepts the URl wins.
125*b1cdbd2cSJim Jagielski 	return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:skeleton:"),14));
126*b1cdbd2cSJim Jagielski }
127*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
getPropertyInfo(const::rtl::OUString & url,const Sequence<PropertyValue> & info)128*b1cdbd2cSJim Jagielski Sequence< DriverPropertyInfo > SAL_CALL SkeletonDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
129*b1cdbd2cSJim Jagielski {
130*b1cdbd2cSJim Jagielski 	// if you have somthing special to say, return it here :-)
131*b1cdbd2cSJim Jagielski 	return Sequence< DriverPropertyInfo >();
132*b1cdbd2cSJim Jagielski }
133*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
getMajorVersion()134*b1cdbd2cSJim Jagielski sal_Int32 SAL_CALL SkeletonDriver::getMajorVersion(  ) throw(RuntimeException)
135*b1cdbd2cSJim Jagielski {
136*b1cdbd2cSJim Jagielski 	return 0; // depends on you
137*b1cdbd2cSJim Jagielski }
138*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
getMinorVersion()139*b1cdbd2cSJim Jagielski sal_Int32 SAL_CALL SkeletonDriver::getMinorVersion(  ) throw(RuntimeException)
140*b1cdbd2cSJim Jagielski {
141*b1cdbd2cSJim Jagielski 	return 1; // depends on you
142*b1cdbd2cSJim Jagielski }
143*b1cdbd2cSJim Jagielski // --------------------------------------------------------------------------------
144*b1cdbd2cSJim Jagielski 
145*b1cdbd2cSJim Jagielski //.........................................................................
146*b1cdbd2cSJim Jagielski namespace connectivity
147*b1cdbd2cSJim Jagielski {
148*b1cdbd2cSJim Jagielski 	namespace skeleton
149*b1cdbd2cSJim Jagielski 	{
150*b1cdbd2cSJim Jagielski //.........................................................................
151*b1cdbd2cSJim Jagielski 
release(oslInterlockedCount & _refCount,::cppu::OBroadcastHelper & rBHelper,::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _xInterface,::com::sun::star::lang::XComponent * _pObject)152*b1cdbd2cSJim Jagielski void release(oslInterlockedCount& _refCount,
153*b1cdbd2cSJim Jagielski 			 ::cppu::OBroadcastHelper& rBHelper,
154*b1cdbd2cSJim Jagielski 			 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
155*b1cdbd2cSJim Jagielski 			 ::com::sun::star::lang::XComponent* _pObject)
156*b1cdbd2cSJim Jagielski {
157*b1cdbd2cSJim Jagielski 	if (osl_decrementInterlockedCount( &_refCount ) == 0)
158*b1cdbd2cSJim Jagielski 	{
159*b1cdbd2cSJim Jagielski 		osl_incrementInterlockedCount( &_refCount );
160*b1cdbd2cSJim Jagielski 
161*b1cdbd2cSJim Jagielski 		if (!rBHelper.bDisposed && !rBHelper.bInDispose)
162*b1cdbd2cSJim Jagielski 		{
163*b1cdbd2cSJim Jagielski 			// remember the parent
164*b1cdbd2cSJim Jagielski 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xParent;
165*b1cdbd2cSJim Jagielski 			{
166*b1cdbd2cSJim Jagielski 				::osl::MutexGuard aGuard( rBHelper.rMutex );
167*b1cdbd2cSJim Jagielski 				xParent = _xInterface;
168*b1cdbd2cSJim Jagielski 				_xInterface = NULL;
169*b1cdbd2cSJim Jagielski 			}
170*b1cdbd2cSJim Jagielski 
171*b1cdbd2cSJim Jagielski 			// First dispose
172*b1cdbd2cSJim Jagielski 			_pObject->dispose();
173*b1cdbd2cSJim Jagielski 
174*b1cdbd2cSJim Jagielski 			// only the alive ref holds the object
175*b1cdbd2cSJim Jagielski 			OSL_ASSERT( _refCount == 1 );
176*b1cdbd2cSJim Jagielski 
177*b1cdbd2cSJim Jagielski 			// release the parent in the ~
178*b1cdbd2cSJim Jagielski 			if (xParent.is())
179*b1cdbd2cSJim Jagielski 			{
180*b1cdbd2cSJim Jagielski 				::osl::MutexGuard aGuard( rBHelper.rMutex );
181*b1cdbd2cSJim Jagielski 				_xInterface = xParent;
182*b1cdbd2cSJim Jagielski 			}
183*b1cdbd2cSJim Jagielski 		}
184*b1cdbd2cSJim Jagielski 	}
185*b1cdbd2cSJim Jagielski 	else
186*b1cdbd2cSJim Jagielski 		osl_incrementInterlockedCount( &_refCount );
187*b1cdbd2cSJim Jagielski }
188*b1cdbd2cSJim Jagielski 
checkDisposed(sal_Bool _bThrow)189*b1cdbd2cSJim Jagielski void checkDisposed(sal_Bool _bThrow) throw ( DisposedException )
190*b1cdbd2cSJim Jagielski {
191*b1cdbd2cSJim Jagielski 	if (_bThrow)
192*b1cdbd2cSJim Jagielski 		throw DisposedException();
193*b1cdbd2cSJim Jagielski 
194*b1cdbd2cSJim Jagielski }
195*b1cdbd2cSJim Jagielski //.........................................................................
196*b1cdbd2cSJim Jagielski 	}
197*b1cdbd2cSJim Jagielski }
198*b1cdbd2cSJim Jagielski //.........................................................................
199*b1cdbd2cSJim Jagielski 
200