xref: /trunk/main/ucb/source/ucp/webdav/DAVSessionFactory.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_webdav.hxx"
26 #include "DAVSessionFactory.hxx"
27 #include "CurlSession.hxx"
28 #include "CurlUri.hxx"
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 
31 using namespace http_dav_ucp;
32 using namespace com::sun::star;
33 
34 DAVSessionFactory::~DAVSessionFactory()
35 {
36 }
37 
38 rtl::Reference< DAVSession > DAVSessionFactory::createDAVSession(
39                 const ::rtl::OUString & inUri,
40                 const uno::Reference< lang::XMultiServiceFactory > & rxSMgr )
41 {
42     m_xMSF = rxSMgr;
43 
44     osl::MutexGuard aGuard( m_aMutex );
45 
46     if ( !m_xProxyDecider.get() )
47         m_xProxyDecider.reset( new ucbhelper::InternetProxyDecider( rxSMgr ) );
48 
49     Map::iterator aIt( m_aMap.begin() );
50     Map::iterator aEnd( m_aMap.end() );
51 
52     while ( aIt != aEnd )
53     {
54         if ( (*aIt).second->CanUse( inUri ) )
55             break;
56 
57         ++aIt;
58     }
59 
60     if ( aIt == aEnd )
61     {
62         CurlUri aURI( inUri );
63 
64         std::auto_ptr< DAVSession > xElement(
65             new CurlSession( this, inUri, *m_xProxyDecider.get() ) );
66 
67         aIt = m_aMap.insert( Map::value_type( inUri, xElement.get() ) ).first;
68         aIt->second->m_aContainerIt = aIt;
69         xElement.release();
70         return aIt->second;
71     }
72     else if ( osl_incrementInterlockedCount( &aIt->second->m_nRefCount ) > 1 )
73     {
74         rtl::Reference< DAVSession > xElement( aIt->second );
75         osl_decrementInterlockedCount( &aIt->second->m_nRefCount );
76         return xElement;
77     }
78     else
79     {
80         osl_decrementInterlockedCount( &aIt->second->m_nRefCount );
81         aIt->second->m_aContainerIt = m_aMap.end();
82 
83         // If URL scheme is different from http or https we definitely
84         // have to use a proxy and therefore can optimize the getProxy
85         // call a little:
86         CurlUri aURI( inUri );
87 
88         aIt->second = new CurlSession( this, inUri, *m_xProxyDecider.get() );
89         aIt->second->m_aContainerIt = aIt;
90         return aIt->second;
91     }
92 }
93 
94 void DAVSessionFactory::releaseElement( DAVSession * pElement ) SAL_THROW(())
95 {
96     OSL_ASSERT( pElement );
97     osl::MutexGuard aGuard( m_aMutex );
98     if ( pElement->m_aContainerIt != m_aMap.end() )
99         m_aMap.erase( pElement->m_aContainerIt );
100 }
101