1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
/*TODO
- change "singleton" behaviour by using new helper ::comhelper::SingletonRef
- rename method exist() to existHandlerForURL() or similar one
- may it's a good idea to replace struct ProtocolHandler by css::beans::NamedValue type?!
*/
#include <classes/protocolhandlercache.hxx>
#include <classes/converter.hxx>
#include <tools/wldcrd.hxx>
#include <unotools/configpaths.hxx>
#include <rtl/ustrbuf.hxx>
#include <vcl/svapp.hxx>
#define SETNAME_HANDLER "HandlerSet" // name of configuration set inside package
namespace framework{
/**
@short overloaded index operator of hash map to support pattern key search
@descr All keys inside this hash map are URL pattern which points to an uno
implementation name of a protocol handler service which is registered
for this pattern. This operator makes it easy to find such registered
handler by using a full qualified URL and compare it with all pattern
keys.
@param sURL
the full qualified URL which should match to a registered pattern
@return An iterator which points to the found item inside the hash or PatternHash::end()
if no pattern match this given <var>sURL</var>.
*/
namespace {
PatternHash::const_iterator findPatternKey(PatternHash const * hash, const OUString& sURL)
{
auto pItem = hash->begin();
while( pItem!=hash->end() )
{
WildCard aPattern(pItem->first);
if (aPattern.Matches(sURL))
break;
++pItem;
}
return pItem;
}
}
/**
@short initialize static member of class HandlerCache
@descr We use a singleton pattern to implement this handler cache.
That means it use two static member list to hold all necessary information
and a ref count mechanism to create/destroy it on demand.
*/
HandlerHash* HandlerCache::m_pHandler = nullptr;
PatternHash* HandlerCache::m_pPattern = nullptr;
sal_Int32 HandlerCache::m_nRefCount = 0;
HandlerCFGAccess* HandlerCache::m_pConfig = nullptr;
/**
@short ctor of the cache of all registered protocol handler
@descr It tries to open the right configuration package automatically
and fill the internal structures. After that the cache can be
used for read access on this data and perform some search
operations on it.
*/
HandlerCache::HandlerCache()
{
SolarMutexGuard aGuard;
if (m_nRefCount==0)
{
m_pHandler = new HandlerHash();
m_pPattern = new PatternHash();
m_pConfig = new HandlerCFGAccess(PACKAGENAME_PROTOCOLHANDLER);
m_pConfig->read(&m_pHandler,&m_pPattern);
m_pConfig->setCache(this);
}
++m_nRefCount;
}
/**
@short dtor of the cache
@descr It frees all used memory. In further implementations (may if we support write access too)
it's a good place to flush changes back to the configuration - but not needed yet.
*/
HandlerCache::~HandlerCache()
{
SolarMutexGuard aGuard;
if( m_nRefCount==1)
{
m_pConfig->setCache(nullptr);
delete m_pConfig;
delete m_pHandler;
delete m_pPattern;
m_pConfig = nullptr;
m_pHandler= nullptr;
m_pPattern= nullptr;
}
--m_nRefCount;
}
/**
@short dtor of the cache
@descr It frees all used memory. In further implementations (may if we support write access too)
it's a good place to flush changes back to the configuration - but not needed yet.
*/
bool HandlerCache::search( const OUString& sURL, ProtocolHandler* pReturn ) const
{
bool bFound = false;
SolarMutexGuard aGuard;
PatternHash::const_iterator pItem = findPatternKey(m_pPattern, sURL);
if (pItem!=m_pPattern->end())
{
*pReturn = (*m_pHandler)[pItem->second];
bFound = true;
}
return bFound;
}
/**
@short search for a registered handler by using an URL struct
@descr We combine necessary parts of this struct to a valid URL string
and call our other search method ...
It's a helper for outside code.
*/
bool HandlerCache::search( const css::util::URL& aURL, ProtocolHandler* pReturn ) const
{
return search( aURL.Complete, pReturn );
}
void HandlerCache::takeOver(HandlerHash* pHandler, PatternHash* pPattern)
{
SolarMutexGuard aGuard;
HandlerHash* pOldHandler = m_pHandler;
PatternHash* pOldPattern = m_pPattern;
m_pHandler = pHandler;
m_pPattern = pPattern;
delete pOldHandler;
delete pOldPattern;
}
/**
@short dtor of the config access class
@descr It opens the configuration package automatically by using base class mechanism.
After that "read()" method of this class should be called to use it.
@param sPackage
specifies the package name of the configuration data which should be used
*/
HandlerCFGAccess::HandlerCFGAccess( const OUString& sPackage )
: ConfigItem(sPackage)
, m_pCache(nullptr)
{
css::uno::Sequence< OUString > lListenPaths { SETNAME_HANDLER };
EnableNotification(lListenPaths);
}
/**
@short use base class mechanism to fill given structures
@descr User use us as a wrapper between configuration api and his internal structures.
He give us some pointer to his member and we fill it.
@param pHandler
pointer to a list of protocol handler infos
@param pPattern
reverse map of handler pattern to her uno names
*/
void HandlerCFGAccess::read( HandlerHash** ppHandler ,
PatternHash** ppPattern )
{
// list of all uno implementation names without encoding
css::uno::Sequence< OUString > lNames = GetNodeNames( SETNAME_HANDLER, ::utl::CONFIG_NAME_LOCAL_PATH );
sal_Int32 nSourceCount = lNames.getLength();
sal_Int32 nTargetCount = nSourceCount;
// list of all full qualified path names of configuration entries
css::uno::Sequence< OUString > lFullNames ( nTargetCount );
// expand names to full path names
sal_Int32 nSource=0;
sal_Int32 nTarget=0;
for( nSource=0; nSource<nSourceCount; ++nSource )
{
OUStringBuffer sPath( SETNAME_HANDLER );
sPath.append(CFG_PATH_SEPARATOR);
sPath.append(lNames[nSource]);
sPath.append(CFG_PATH_SEPARATOR);
sPath.append(PROPERTY_PROTOCOLS);
lFullNames[nTarget] = sPath.makeStringAndClear();
++nTarget;
}
// get values at all
css::uno::Sequence< css::uno::Any > lValues = GetProperties( lFullNames );
SAL_WARN_IF( lFullNames.getLength()!=lValues.getLength(), "fwk", "HandlerCFGAccess::read(): Miss some configuration values of handler set!" );
// fill structures
nSource = 0;
for( nTarget=0; nTarget<nTargetCount; ++nTarget )
{
// create it new for every loop to guarantee a real empty object!
ProtocolHandler aHandler;
aHandler.m_sUNOName = ::utl::extractFirstFromConfigurationPath(lNames[nSource]);
// unpack all values of this handler
css::uno::Sequence< OUString > lTemp;
lValues[nTarget] >>= lTemp;
aHandler.m_lProtocols = Converter::convert_seqOUString2OUStringList(lTemp);
// register his pattern into the performance search hash
for (std::vector<OUString>::iterator pItem =aHandler.m_lProtocols.begin();
pItem!=aHandler.m_lProtocols.end();
++pItem )
{
(**ppPattern)[*pItem] = lNames[nSource];
}
// insert the handler info into the normal handler cache
(**ppHandler)[lNames[nSource]] = aHandler;
++nSource;
}
}
void HandlerCFGAccess::Notify(const css::uno::Sequence< OUString >& /*lPropertyNames*/)
{
HandlerHash* pHandler = new HandlerHash;
PatternHash* pPattern = new PatternHash;
read(&pHandler, &pPattern);
if (m_pCache)
m_pCache->takeOver(pHandler, pPattern);
else
{
delete pHandler;
delete pPattern;
}
}
void HandlerCFGAccess::ImplCommit()
{
}
} // namespace framework
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|