Update ooo320-m1
[ooovba.git] / framework / source / classes / protocolhandlercache.cxx
blob3a1c4131c5579791921ae6ec7e38baf752957a26
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: protocolhandlercache.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 /*TODO
35 - change "singleton" behaviour by using new helper ::comhelper::SingletonRef
36 - rename method exist() to existHandlerForURL() or similar one
37 - may its a good idea to replace struct ProtocolHandler by css::beans::NamedValue type?!
40 //_________________________________________________________________________________________________________________
41 // my own includes
42 //_________________________________________________________________________________________________________________
44 #include <classes/protocolhandlercache.hxx>
45 #include <classes/converter.hxx>
46 #include <threadhelp/readguard.hxx>
47 #include <threadhelp/writeguard.hxx>
48 #include <threadhelp/lockhelper.hxx>
50 //_________________________________________________________________________________________________________________
51 // interface includes
52 //_________________________________________________________________________________________________________________
54 //_________________________________________________________________________________________________________________
55 // other includes
56 //_________________________________________________________________________________________________________________
57 #include <tools/wldcrd.hxx>
58 #include <unotools/configpathes.hxx>
59 #include <rtl/ustrbuf.hxx>
61 //_________________________________________________________________________________________________________________
62 // namespace
63 //_________________________________________________________________________________________________________________
65 namespace framework{
67 //_________________________________________________________________________________________________________________
68 // non exported const
69 //_________________________________________________________________________________________________________________
71 //_________________________________________________________________________________________________________________
72 // non exported definitions
73 //_________________________________________________________________________________________________________________
75 /**
76 @short overloaded index operator of hash map to support pattern key search
77 @descr All keys inside this hash map are URL pattern which points to an uno
78 implementation name of a protocol handler service which is registered
79 for this pattern. This operator makes it easy to find such registered
80 handler by using a full qualified URL and compare it with all pattern
81 keys.
83 @param sURL
84 the full qualified URL which should match to a registered pattern
86 @return An iterator which points to the found item inside the hash or PatternHash::end()
87 if no pattern match this given <var>sURL</var>.
89 @modified 30.04.2002 09:52, as96863
91 PatternHash::iterator PatternHash::findPatternKey( const ::rtl::OUString& sURL )
93 PatternHash::iterator pItem = this->begin();
94 while( pItem!=this->end() )
96 WildCard aPattern(pItem->first);
97 if (aPattern.Matches(sURL))
98 break;
99 ++pItem;
101 return pItem;
104 //_________________________________________________________________________________________________________________
107 @short initialize static member of class HandlerCache
108 @descr We use a singleton pattern to implement this handler cache.
109 That means it use two static member list to hold all neccessary informations
110 and a ref count mechanism to create/destroy it on demand.
112 @modified 30.04.2002 11:13, as96863
114 HandlerHash* HandlerCache::m_pHandler = NULL;
115 PatternHash* HandlerCache::m_pPattern = NULL;
116 sal_Int32 HandlerCache::m_nRefCount = 0 ;
117 HandlerCFGAccess* HandlerCache::m_pConfig = NULL;
119 //_________________________________________________________________________________________________________________
122 @short ctor of the cache of all registered protoco handler
123 @descr It tries to open the right configuration package automaticly
124 and fill the internal structures. After that the cache can be
125 used for read access on this data and perform some search
126 operations on it.
128 @modified 30.04.2002 10:02, as96863
130 HandlerCache::HandlerCache()
132 /* SAFE */{
133 WriteGuard aGlobalLock( LockHelper::getGlobalLock() );
135 if (m_nRefCount==0)
137 m_pHandler = new HandlerHash();
138 m_pPattern = new PatternHash();
139 m_pConfig = new HandlerCFGAccess(PACKAGENAME_PROTOCOLHANDLER);
140 m_pConfig->read(&m_pHandler,&m_pPattern);
141 m_pConfig->setCache(this);
144 ++m_nRefCount;
145 /* SAFE */}
148 //_________________________________________________________________________________________________________________
151 @short dtor of the cache
152 @descr It frees all used memory. In further implementations (may if we support write access too)
153 it's a good place to flush changes back to the configuration - but not needed yet.
155 @modified 30.04.2002 09:54, as96863
157 HandlerCache::~HandlerCache()
159 /* SAFE */{
160 WriteGuard aGlobalLock( LockHelper::getGlobalLock() );
162 if( m_nRefCount==1)
164 m_pConfig->setCache(NULL);
165 m_pHandler->free();
166 m_pPattern->free();
168 delete m_pConfig;
169 delete m_pHandler;
170 delete m_pPattern;
171 m_pConfig = NULL;
172 m_pHandler= NULL;
173 m_pPattern= NULL;
176 --m_nRefCount;
177 /* SAFE */}
180 //_________________________________________________________________________________________________________________
183 @short dtor of the cache
184 @descr It frees all used memory. In further implementations (may if we support write access too)
185 it's a good place to flush changes back to the configuration - but not needed yet.
187 @modified 30.04.2002 09:54, as96863
189 sal_Bool HandlerCache::search( const ::rtl::OUString& sURL, ProtocolHandler* pReturn ) const
191 sal_Bool bFound = sal_False;
192 /* SAFE */{
193 ReadGuard aReadLock( LockHelper::getGlobalLock() );
194 PatternHash::const_iterator pItem = m_pPattern->findPatternKey(sURL);
195 if (pItem!=m_pPattern->end())
197 *pReturn = (*m_pHandler)[pItem->second];
198 bFound = sal_True;
200 /* SAFE */}
201 return bFound;
204 //_________________________________________________________________________________________________________________
207 @short search for a registered handler by using an URL struct
208 @descr We combine neccessary parts of this struct to a valid URL string
209 and call our other search method ...
210 It's a helper for outside code.
212 @modified 30.04.2002 09:54, as96863
214 sal_Bool HandlerCache::search( const css::util::URL& aURL, ProtocolHandler* pReturn ) const
216 return search( aURL.Complete, pReturn );
219 //_________________________________________________________________________________________________________________
221 sal_Bool HandlerCache::exists( const ::rtl::OUString& sURL ) const
223 sal_Bool bFound = sal_False;
224 /* SAFE */{
225 ReadGuard aReadLock( LockHelper::getGlobalLock() );
226 PatternHash::const_iterator pItem = m_pPattern->findPatternKey(sURL);
227 bFound = pItem!=m_pPattern->end();
228 /* SAFE */}
229 return bFound;
232 //_________________________________________________________________________________________________________________
233 void HandlerCache::takeOver(HandlerHash* pHandler, PatternHash* pPattern)
235 // SAFE ->
236 WriteGuard aWriteLock( LockHelper::getGlobalLock() );
238 HandlerHash* pOldHandler = m_pHandler;
239 PatternHash* pOldPattern = m_pPattern;
241 m_pHandler = pHandler;
242 m_pPattern = pPattern;
244 pOldHandler->free();
245 pOldPattern->free();
246 delete pOldHandler;
247 delete pOldPattern;
249 aWriteLock.unlock();
250 // <- SAFE
253 //_________________________________________________________________________________________________________________
256 @short dtor of the config access class
257 @descr It opens the configuration package automaticly by using base class mechanism.
258 After that "read()" method of this class should be called to use it.
260 @param sPackage
261 specifies the package name of the configuration data which should be used
263 @modified 30.04.2002 10:06, as96863
265 HandlerCFGAccess::HandlerCFGAccess( const ::rtl::OUString& sPackage )
266 : ConfigItem( sPackage )
268 css::uno::Sequence< ::rtl::OUString > lListenPathes(1);
269 lListenPathes[0] = SETNAME_HANDLER;
270 EnableNotification(lListenPathes);
273 //_________________________________________________________________________________________________________________
276 @short use base class mechanism to fill given structures
277 @descr User use us as a wrapper between configuration api and his internal structures.
278 He give us some pointer to his member and we fill it.
280 @param pHandler
281 pointer to a list of protocol handler infos
283 @param pPattern
284 reverse map of handler pattern to her uno names
286 @modified 30.04.2002 09:54, as96863
288 void HandlerCFGAccess::read( HandlerHash** ppHandler ,
289 PatternHash** ppPattern )
291 // list of all uno implementation names without encoding
292 css::uno::Sequence< ::rtl::OUString > lNames = GetNodeNames( SETNAME_HANDLER, ::utl::CONFIG_NAME_LOCAL_PATH );
293 sal_Int32 nSourceCount = lNames.getLength();
294 sal_Int32 nTargetCount = nSourceCount;
295 // list of all full qualified path names of configuration entries
296 css::uno::Sequence< ::rtl::OUString > lFullNames ( nTargetCount );
298 // expand names to full path names
299 sal_Int32 nSource=0;
300 sal_Int32 nTarget=0;
301 for( nSource=0; nSource<nSourceCount; ++nSource )
303 ::rtl::OUStringBuffer sPath( SETNAME_HANDLER );
304 sPath.append(CFG_PATH_SEPERATOR);
305 sPath.append(lNames[nSource]);
306 sPath.append(CFG_PATH_SEPERATOR);
307 sPath.append(PROPERTY_PROTOCOLS);
309 lFullNames[nTarget] = sPath.makeStringAndClear();
310 ++nTarget;
313 // get values at all
314 css::uno::Sequence< css::uno::Any > lValues = GetProperties( lFullNames );
315 LOG_ASSERT2( lFullNames.getLength()!=lValues.getLength(), "HandlerCFGAccess::read()", "Miss some configuration values of handler set!" )
317 // fill structures
318 nSource = 0;
319 for( nTarget=0; nTarget<nTargetCount; ++nTarget )
321 // create it new for every loop to guarantee a real empty object!
322 ProtocolHandler aHandler;
323 aHandler.m_sUNOName = ::utl::extractFirstFromConfigurationPath(lNames[nSource]);
325 // unpack all values of this handler
326 css::uno::Sequence< ::rtl::OUString > lTemp;
327 lValues[nTarget] >>= lTemp;
328 aHandler.m_lProtocols = Converter::convert_seqOUString2OUStringList(lTemp);
330 // register his pattern into the performance search hash
331 for (OUStringList::iterator pItem =aHandler.m_lProtocols.begin();
332 pItem!=aHandler.m_lProtocols.end() ;
333 ++pItem )
335 (**ppPattern)[*pItem] = lNames[nSource];
338 // �nsert the handler info into the normal handler cache
339 (**ppHandler)[lNames[nSource]] = aHandler;
340 ++nSource;
344 //_________________________________________________________________________________________________________________
345 void HandlerCFGAccess::Notify(const css::uno::Sequence< rtl::OUString >& /*lPropertyNames*/)
347 HandlerHash* pHandler = new HandlerHash;
348 PatternHash* pPattern = new PatternHash;
350 read(&pHandler, &pPattern);
351 if (m_pCache)
352 m_pCache->takeOver(pHandler, pPattern);
353 else
355 delete pHandler;
356 delete pPattern;
360 } // namespace framework