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