1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: configaccess.cxx,v $
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 //________________________________
36 #include <jobs/configaccess.hxx>
37 #include <threadhelp/readguard.hxx>
38 #include <threadhelp/writeguard.hxx>
39 #include <threadhelp/resetableguard.hxx>
43 //________________________________
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/beans/XMultiHierarchicalPropertySet.hpp>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/util/XChangesBatch.hpp>
51 //________________________________
52 // includes of other projects
53 #include <unotools/configpathes.hxx>
54 #include <rtl/ustrbuf.hxx>
56 //________________________________
61 //________________________________
64 //________________________________
65 // non exported definitions
67 //________________________________
70 //________________________________
72 @short open the configuration of this job
73 @descr We open the configuration of this job only. Not the whole package or the whole
74 job set. We are interested on our own properties only.
75 We set the opened configuration access as our member. So any following method,
76 which needs cfg access, can use it. That prevent us against multiple open/close requests.
77 But you can use this method to upgrade an already opened configuration too.
80 force opening of the configuration access in readonly or in read/write mode
82 ConfigAccess::ConfigAccess( /*IN*/ const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
,
83 /*IN*/ const ::rtl::OUString
& sRoot
)
87 , m_eMode ( E_CLOSED
)
91 //________________________________
93 @short last chance to close an open configuration access point
94 @descr In case our user forgot to close this configuration point
95 in the right way, normaly he will run into some trouble -
98 ConfigAccess::~ConfigAccess()
103 //________________________________
105 @short return the internal mode of this instance
106 @descr May be the outside user need any information about successfully opened
107 or closed config access point objects. He can control the internal mode to do so.
109 @return The internal open state of this object.
111 ConfigAccess::EOpenMode
ConfigAccess::getMode() const
114 ReadGuard
aReadLock(m_aLock
);
119 //________________________________
121 @short open the configuration access in the specified mode
122 @descr We set the opened configuration access as our member. So any following method,
123 which needs cfg access, can use it. That prevent us against multiple open/close requests.
124 But you can use this method to upgrade an already opened configuration too.
125 It's possible to open a config access in READONLY mode first and "open" it at a second
126 time within the mode READWRITE. Then we will upgrade it. Dowgrade will be possible too.
128 But note: closing will be done explicitly by calling method close() ... not by
129 downgrading with mode CLOSED!
132 force (re)opening of the configuration access in readonly or in read/write mode
134 void ConfigAccess::open( /*IN*/ EOpenMode eMode
)
137 // We must lock the whole method to be shure, that nobody
138 // outside uses our internal member m_xAccess!
139 WriteGuard
aWriteLock(m_aLock
);
141 // check if configuration is already open in the right mode.
142 // By the way: Don't allow closing by using this method!
144 (eMode
!=E_CLOSED
) &&
148 // We have to close the old access point without any question here.
149 // It will be open again using the new mode.
150 // can be called without checks! It does the checks by itself ...
151 // e.g. for already closed or not opened configuration.
152 // Flushing of all made changes will be done here too.
155 // create the configuration provider, which provides sub access points
156 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider(m_xSMGR
->createInstance(SERVICENAME_CFGPROVIDER
), css::uno::UNO_QUERY
);
157 if (xConfigProvider
.is())
159 css::beans::PropertyValue aParam
;
160 aParam
.Name
= DECLARE_ASCII("nodepath");
161 aParam
.Value
<<= m_sRoot
;
163 css::uno::Sequence
< css::uno::Any
> lParams(1);
164 lParams
[0] <<= aParam
;
169 if (eMode
==E_READONLY
)
170 m_xConfig
= xConfigProvider
->createInstanceWithArguments(SERVICENAME_CFGREADACCESS
, lParams
);
172 if (eMode
==E_READWRITE
)
173 m_xConfig
= xConfigProvider
->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS
, lParams
);
175 catch(css::uno::Exception
& ex
)
177 (void) ex
; // avoid warning
178 LOG_WARNING("open config ...", U2B(ex
.Message
))
191 //________________________________
193 @short close the internal opened configuration access and flush all changes
194 @descr It checks, if the given access is valid and react in the right way.
195 It flushes all changes ... so nobody else must know this state.
197 void ConfigAccess::close()
200 // Lock the whole method, to be shure that nobody else uses our internal members
202 WriteGuard
aWriteLock(m_aLock
);
204 // check already closed configuration
207 css::uno::Reference
< css::util::XChangesBatch
> xFlush(m_xConfig
, css::uno::UNO_QUERY
);
209 xFlush
->commitChanges();
210 m_xConfig
= css::uno::Reference
< css::uno::XInterface
>();
218 //________________________________
220 @short provides an access to the internal wrapped configuration access
221 @descr It's not allowed to safe this c++ (!) reference outside. You have
222 to use it directly. Further you must use our public lock member m_aLock
223 to synchronize your code with our internal structures and our interface
224 methods. Acquire it before you call cfg() and release it afterwards immediatly.
226 E.g.: ConfigAccess aAccess(...);
227 ReadGuard aReadLock(aAccess.m_aLock);
228 Reference< XPropertySet > xSet(aAccess.cfg(), UNO_QUERY);
229 Any aProp = xSet->getPropertyValue("...");
232 @attention During this time it's not allowed to call the methods open() or close()!
233 Otherwhise you will change your own referenced config access. Anything will
236 @return A c++(!) reference to the uno instance of the configuration access point.
238 const css::uno::Reference
< css::uno::XInterface
>& ConfigAccess::cfg()
240 // must be synchronized from outside!
241 // => no lock here ...
245 } // namespace framework