xref: /AOO41X/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/SDriver.cxx (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
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) throw( ::com::sun::star::uno::Exception )
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(  ) throw(RuntimeException)
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(  ) throw (RuntimeException)
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(  ) throw(RuntimeException)
86 {
87     return getImplementationName_Static();
88 }
89 
90 //------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)91 sal_Bool SAL_CALL SkeletonDriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
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(  ) throw(RuntimeException)
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 ) throw(SQLException, RuntimeException)
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         throw(SQLException, RuntimeException)
122 {
123     // here we have to look if we support this url format
124     // change the URL format to your needs, but please aware that the first on who accepts the URl wins.
125     return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:skeleton:"),14));
126 }
127 // --------------------------------------------------------------------------------
getPropertyInfo(const::rtl::OUString & url,const Sequence<PropertyValue> & info)128 Sequence< DriverPropertyInfo > SAL_CALL SkeletonDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
129 {
130     // if you have somthing special to say, return it here :-)
131     return Sequence< DriverPropertyInfo >();
132 }
133 // --------------------------------------------------------------------------------
getMajorVersion()134 sal_Int32 SAL_CALL SkeletonDriver::getMajorVersion(  ) throw(RuntimeException)
135 {
136     return 0; // depends on you
137 }
138 // --------------------------------------------------------------------------------
getMinorVersion()139 sal_Int32 SAL_CALL SkeletonDriver::getMinorVersion(  ) throw(RuntimeException)
140 {
141     return 1; // depends on you
142 }
143 // --------------------------------------------------------------------------------
144 
145 //.........................................................................
146 namespace connectivity
147 {
148     namespace skeleton
149     {
150 //.........................................................................
151 
release(oslInterlockedCount & _refCount,::cppu::OBroadcastHelper & rBHelper,::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _xInterface,::com::sun::star::lang::XComponent * _pObject)152 void release(oslInterlockedCount& _refCount,
153              ::cppu::OBroadcastHelper& rBHelper,
154              ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
155              ::com::sun::star::lang::XComponent* _pObject)
156 {
157     if (osl_decrementInterlockedCount( &_refCount ) == 0)
158     {
159         osl_incrementInterlockedCount( &_refCount );
160 
161         if (!rBHelper.bDisposed && !rBHelper.bInDispose)
162         {
163             // remember the parent
164             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xParent;
165             {
166                 ::osl::MutexGuard aGuard( rBHelper.rMutex );
167                 xParent = _xInterface;
168                 _xInterface = NULL;
169             }
170 
171             // First dispose
172             _pObject->dispose();
173 
174             // only the alive ref holds the object
175             OSL_ASSERT( _refCount == 1 );
176 
177             // release the parent in the ~
178             if (xParent.is())
179             {
180                 ::osl::MutexGuard aGuard( rBHelper.rMutex );
181                 _xInterface = xParent;
182             }
183         }
184     }
185     else
186         osl_incrementInterlockedCount( &_refCount );
187 }
188 
checkDisposed(sal_Bool _bThrow)189 void checkDisposed(sal_Bool _bThrow) throw ( DisposedException )
190 {
191     if (_bThrow)
192         throw DisposedException();
193 
194 }
195 //.........................................................................
196     }
197 }
198 //.........................................................................
199 
200