1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_ucb.hxx"
30 #include "DAVSessionFactory.hxx"
31 #include "NeonSession.hxx"
32 #include "NeonUri.hxx"
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 
35 using namespace webdav_ucp;
36 using namespace com::sun::star;
37 
38 DAVSessionFactory::~DAVSessionFactory()
39 {
40 }
41 
42 rtl::Reference< DAVSession > DAVSessionFactory::createDAVSession(
43 				const ::rtl::OUString & inUri,
44 				const uno::Reference< lang::XMultiServiceFactory > & rxSMgr )
45     throw( DAVException )
46 {
47     m_xMSF = rxSMgr;
48 
49     osl::MutexGuard aGuard( m_aMutex );
50 
51     if ( !m_xProxyDecider.get() )
52         m_xProxyDecider.reset( new ucbhelper::InternetProxyDecider( rxSMgr ) );
53 
54     Map::iterator aIt( m_aMap.begin() );
55     Map::iterator aEnd( m_aMap.end() );
56 
57     while ( aIt != aEnd )
58 	{
59         if ( (*aIt).second->CanUse( inUri ) )
60 			break;
61 
62         ++aIt;
63 	}
64 
65     if ( aIt == aEnd )
66     {
67         NeonUri aURI( inUri );
68 
69         std::auto_ptr< DAVSession > xElement(
70             new NeonSession( this, inUri, *m_xProxyDecider.get() ) );
71 
72         aIt = m_aMap.insert( Map::value_type( inUri, xElement.get() ) ).first;
73         aIt->second->m_aContainerIt = aIt;
74         xElement.release();
75         return aIt->second;
76     }
77     else if ( osl_incrementInterlockedCount( &aIt->second->m_nRefCount ) > 1 )
78     {
79         rtl::Reference< DAVSession > xElement( aIt->second );
80         osl_decrementInterlockedCount( &aIt->second->m_nRefCount );
81         return xElement;
82     }
83     else
84     {
85         osl_decrementInterlockedCount( &aIt->second->m_nRefCount );
86         aIt->second->m_aContainerIt = m_aMap.end();
87 
88         // If URL scheme is different from http or https we definitely
89         // have to use a proxy and therefore can optimize the getProxy
90         // call a little:
91         NeonUri aURI( inUri );
92 
93         aIt->second = new NeonSession( this, inUri, *m_xProxyDecider.get() );
94         aIt->second->m_aContainerIt = aIt;
95         return aIt->second;
96     }
97 }
98 
99 void DAVSessionFactory::releaseElement( DAVSession * pElement ) SAL_THROW(())
100 {
101     OSL_ASSERT( pElement );
102     osl::MutexGuard aGuard( m_aMutex );
103     if ( pElement->m_aContainerIt != m_aMap.end() )
104         m_aMap.erase( pElement->m_aContainerIt );
105 }
106 
107