merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / backend / binarycache.cxx
blob005e418a0bcb6cdb674952454faba478baab9fe6
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: binarycache.cxx,v $
10 * $Revision: 1.10 $
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_configmgr.hxx"
34 #include "binarycache.hxx"
36 #include "binaryreadhandler.hxx"
37 #include "binarywritehandler.hxx"
39 #include "mergedcomponentdata.hxx"
40 #include "filehelper.hxx"
41 #include "typeconverter.hxx"
43 #ifndef _CONFIGMGR_BOOTSTRAP_HXX
44 #include "bootstrap.hxx"
45 #endif
46 #include <osl/file.hxx>
47 #include "tools/getprocessworkingdir.hxx"
48 #include <rtl/ustrbuf.hxx>
49 #include <rtl/logfile.hxx>
51 #define RTL_LOGFILE_OU2A(rtlOUString) (::rtl::OUStringToOString((rtlOUString), RTL_TEXTENCODING_ASCII_US).getStr())
53 namespace configmgr
55 // -----------------------------------------------------------------------------
56 namespace backend
58 const rtl::OUString aSettingName(
59 RTL_CONSTASCII_USTRINGPARAM( CONTEXT_ITEM_PREFIX_ "CacheUrl"));
60 // ---------------------------------------------------------------------------------------
61 static inline bool isValidFileURL (rtl::OUString const& _sFileURL)
63 rtl::OUString sSystemPath;
64 return _sFileURL.getLength() && (osl::File::E_None == osl::File::getSystemPathFromFileURL(_sFileURL, sSystemPath));
66 // -----------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------------------
68 static
69 bool implEnsureAbsoluteURL(rtl::OUString & _rsURL) // also strips embedded dots etc.
71 if (!_rsURL.getLength())
72 return false;
74 if (!isValidFileURL(_rsURL))
76 OSL_TRACE("Binary cache: File URL %s is invalid.",
77 rtl::OUStringToOString(_rsURL,RTL_TEXTENCODING_ASCII_US).getStr());
78 return false;
81 rtl::OUString sBasePath;
82 OSL_VERIFY(tools::getProcessWorkingDir(&sBasePath));
84 rtl::OUString sAbsolute;
85 if ( osl::File::E_None == osl::File::getAbsoluteFileURL(sBasePath, _rsURL, sAbsolute))
87 _rsURL = sAbsolute;
88 return isValidFileURL(_rsURL);
90 else
92 OSL_ENSURE(!isValidFileURL(_rsURL), "Could not get absolute file URL for valid URL");
93 return false;
96 // ---------------------------------------------------------------------------------------
97 static const sal_Unicode kComponentSeparator = '.' ;
98 static const sal_Unicode kPathSeparator = '/' ;
99 static const char kBinarySuffix[] = ".dat" ;
101 rtl::OUString BinaryCache::getCacheFileURL(const rtl::OUString& aComponent) const
103 rtl::OUStringBuffer retCode (mBaseURL);
104 retCode.append(kPathSeparator) ;
105 // retCode.append(aComponent.replace(kComponentSeparator, kPathSeparator)) ;
106 retCode.append(aComponent) ;
107 retCode.appendAscii(RTL_CONSTASCII_STRINGPARAM(kBinarySuffix));
109 rtl::OUString aResult = retCode.makeStringAndClear() ;
111 if (isValidFileURL(aResult))
113 return aResult;
115 else
117 OSL_ENSURE(false, "Component File URL is invalid");
118 return rtl::OUString();
121 // -----------------------------------------------------------------------------
122 BinaryCache::BinaryCache(const uno::Reference<uno::XComponentContext>& xContext )
123 : mBaseURL()
124 , mOwnerEntity()
125 , mbCacheEnabled(false)
128 //initialise the base URL
129 ContextReader aReader(xContext);
131 rtl::OUString sCacheUrl;
132 if (!aReader.isAdminService())
134 mbCacheEnabled = (aReader.getBestContext()->getValueByName(aSettingName) >>= sCacheUrl)
135 && implEnsureAbsoluteURL(sCacheUrl);
138 if (mbCacheEnabled)
140 mBaseURL = sCacheUrl;
141 if (!FileHelper::dirExists(sCacheUrl))
143 osl::File::RC errorCode = FileHelper::mkdirs(sCacheUrl);
144 if (errorCode)
146 #if (OSL_DEBUG_LEVEL > 0)
147 rtl::OString sURL = rtl::OUStringToOString(sCacheUrl,RTL_TEXTENCODING_ASCII_US);
148 rtl::OString sErr = rtl::OUStringToOString(FileHelper::createOSLErrorString(errorCode),RTL_TEXTENCODING_ASCII_US);
149 ::osl_trace("Configuration: Cannot create cache directory \"%s\". "
150 "Error is %s [%d]",sURL.getStr(),sErr.getStr(),int(errorCode)) ;
151 #endif
152 mbCacheEnabled = false;
157 // -----------------------------------------------------------------------------
159 void BinaryCache::setOwnerEntity(const rtl::OUString & aOwnerEntity)
161 OSL_PRECOND(mOwnerEntity.getLength() == 0, "Owner entity of cache already set");
162 mOwnerEntity = aOwnerEntity;
164 // -----------------------------------------------------------------------------
166 void BinaryCache::disableCache()
168 mbCacheEnabled = false;
170 // -----------------------------------------------------------------------------
172 bool BinaryCache::isCacheEnabled(rtl::OUString const & aEntity) const
174 if (!mbCacheEnabled) return false;
176 // default entity is empty
177 if (aEntity.getLength() == 0) return true;
179 return aEntity.equals(mOwnerEntity);
181 // -----------------------------------------------------------------------------
182 bool BinaryCache::readComponentData(MergedComponentData & aComponentData,
183 uno::Reference< lang::XMultiServiceFactory > const & aFactory,
184 rtl::OUString const & aComponent,
185 rtl::OUString const & aSchemaVersion,
186 rtl::OUString const & aEntity,
187 com::sun::star::lang::Locale const & aRequestedLocale,
188 std::vector< com::sun::star::lang::Locale > & outKnownLocales,
189 const uno::Reference<backenduno::XLayer> * pLayers,
190 sal_Int32 nNumLayers,
191 bool bIncludeTemplates)
193 if (isCacheEnabled(aEntity))
196 RTL_LOGFILE_CONTEXT_AUTHOR(aLog, "configmgr::backend::BinaryCache", "jb99855", "configmgr: BinaryCache::readComponentData() - enabled");
197 BinaryReadHandler aCacheReader(getCacheFileURL(aComponent),aComponent,aFactory);
199 // #i49148# Invalidate cache when schema version changes - using former 'owner' parameter for version
200 if(aCacheReader.validateHeader(pLayers, nNumLayers, aSchemaVersion, aRequestedLocale, outKnownLocales))
202 RTL_LOGFILE_CONTEXT_AUTHOR(aLog1, "configmgr::backend::BinaryCache", "jb99855", "configmgr: BinaryCache::readComponentData() - cache hit");
203 aComponentData.setSchemaRoot( aCacheReader.readComponentTree() );
204 if (bIncludeTemplates)
205 aComponentData.setTemplatesTree( aCacheReader.readTemplatesTree() );
206 return true;
209 catch (uno::Exception & e)
211 OSL_TRACE("Binary Cache read failed - exception: %s", rtl::OUStringToOString(e.Message,RTL_TEXTENCODING_ASCII_US).getStr());
213 return false;
215 // -----------------------------------------------------------------------------
217 bool BinaryCache::writeComponentData(MergedComponentData const & aComponentData,
218 uno::Reference< lang::XMultiServiceFactory > const & aFactory,
219 rtl::OUString const & aComponent,
220 rtl::OUString const & aSchemaVersion,
221 rtl::OUString const & aEntity,
222 std::vector< com::sun::star::lang::Locale > const & aKnownLocales,
223 const uno::Reference<backenduno::XLayer> * pLayers,
224 sal_Int32 nNumLayers)
226 if (isCacheEnabled(aEntity))
229 RTL_LOGFILE_CONTEXT_AUTHOR(aLog3, "configmgr::backend::BinaryCache", "jb99855", "configmgr: BinaryCache::writeComponentData() - enabled");
230 BinaryWriteHandler aCacheWriter(getCacheFileURL(aComponent),aComponent, aFactory);
232 //write data to cache
233 // #i49148# Invalidate cache when schema version changes - using former 'owner' parameter for schema
234 if (aCacheWriter.generateHeader(pLayers, nNumLayers, aSchemaVersion, aKnownLocales))
236 aCacheWriter.writeComponentTree(aComponentData.getSchemaTree());
237 aCacheWriter.writeTemplatesTree(aComponentData.getTemplatesTree());
238 return true;
241 catch (uno::Exception & e)
243 OSL_TRACE("Configuration: Cache write failed - exception: %s", rtl::OUStringToOString(e.Message,RTL_TEXTENCODING_ASCII_US).getStr());
245 return false;
247 // -----------------------------------------------------------------------------
250 // -----------------------------------------------------------------------------