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