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
|
/* -*- 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 .
*/
#include <jobs/configaccess.hxx>
#include <threadhelp/readguard.hxx>
#include <threadhelp/writeguard.hxx>
#include <threadhelp/resetableguard.hxx>
#include <general.h>
#include <services.h>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XMultiHierarchicalPropertySet.hpp>
#include <com/sun/star/configuration/theDefaultProvider.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/util/XChangesBatch.hpp>
#include <unotools/configpaths.hxx>
#include <rtl/ustrbuf.hxx>
namespace framework{
/**
@short open the configuration of this job
@descr We open the configuration of this job only. Not the whole package or the whole
job set. We are interested on our own properties only.
We set the opened configuration access as our member. So any following method,
which needs cfg access, can use it. That prevent us against multiple open/close requests.
But you can use this method to upgrade an already opened configuration too.
@param eMode
force opening of the configuration access in readonly or in read/write mode
*/
ConfigAccess::ConfigAccess( /*IN*/ const css::uno::Reference< css::uno::XComponentContext >& rxContext,
/*IN*/ const OUString& sRoot )
: ThreadHelpBase( )
, m_xContext ( rxContext)
, m_sRoot ( sRoot )
, m_eMode ( E_CLOSED )
{
}
//________________________________
/**
@short last chance to close an open configuration access point
@descr In case our user forgot to close this configuration point
in the right way, normaly he will run into some trouble -
e.g. losing data.
*/
ConfigAccess::~ConfigAccess()
{
close();
}
//________________________________
/**
@short return the internal mode of this instance
@descr May be the outside user need any information about successfully opened
or closed config access point objects. He can control the internal mode to do so.
@return The internal open state of this object.
*/
ConfigAccess::EOpenMode ConfigAccess::getMode() const
{
/* SAFE { */
ReadGuard aReadLock(m_aLock);
return m_eMode;
/* } SAFE */
}
//________________________________
/**
@short open the configuration access in the specified mode
@descr We set the opened configuration access as our member. So any following method,
which needs cfg access, can use it. That prevent us against multiple open/close requests.
But you can use this method to upgrade an already opened configuration too.
It's possible to open a config access in READONLY mode first and "open" it at a second
time within the mode READWRITE. Then we will upgrade it. Dowgrade will be possible too.
But note: closing will be done explicitly by calling method close() ... not by
downgrading with mode CLOSED!
@param eMode
force (re)opening of the configuration access in readonly or in read/write mode
*/
void ConfigAccess::open( /*IN*/ EOpenMode eMode )
{
/* SAFE { */
// We must lock the whole method to be shure, that nobody
// outside uses our internal member m_xAccess!
WriteGuard aWriteLock(m_aLock);
// check if configuration is already open in the right mode.
// By the way: Don't allow closing by using this method!
if (
(eMode !=E_CLOSED) &&
(m_eMode!=eMode )
)
{
// We have to close the old access point without any question here.
// It will be open again using the new mode.
// can be called without checks! It does the checks by itself ...
// e.g. for already closed or not opened configuration.
// Flushing of all made changes will be done here too.
close();
// create the configuration provider, which provides sub access points
css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider = css::configuration::theDefaultProvider::get(m_xContext);
css::beans::PropertyValue aParam;
aParam.Name = DECLARE_ASCII("nodepath");
aParam.Value <<= m_sRoot;
css::uno::Sequence< css::uno::Any > lParams(1);
lParams[0] <<= aParam;
// open it
try
{
if (eMode==E_READONLY)
m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS , lParams);
else
if (eMode==E_READWRITE)
m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
}
catch(const css::uno::Exception& ex)
{
(void) ex; // avoid warning
SAL_INFO("fwk", "open config: " << ex.Message);
}
m_eMode = E_CLOSED;
if (m_xConfig.is())
m_eMode = eMode;
}
aWriteLock.unlock();
/* } SAFE */
}
//________________________________
/**
@short close the internal opened configuration access and flush all changes
@descr It checks, if the given access is valid and react in the right way.
It flushes all changes ... so nobody else must know this state.
*/
void ConfigAccess::close()
{
/* SAFE { */
// Lock the whole method, to be shure that nobody else uses our internal members
// during this time.
WriteGuard aWriteLock(m_aLock);
// check already closed configuration
if (m_xConfig.is())
{
css::uno::Reference< css::util::XChangesBatch > xFlush(m_xConfig, css::uno::UNO_QUERY);
if (xFlush.is())
xFlush->commitChanges();
m_xConfig = css::uno::Reference< css::uno::XInterface >();
m_eMode = E_CLOSED;
}
aWriteLock.unlock();
/* } SAFE */
}
//________________________________
/**
@short provides an access to the internal wrapped configuration access
@descr It's not allowed to safe this c++ (!) reference outside. You have
to use it directly. Further you must use our public lock member m_aLock
to synchronize your code with our internal structures and our interface
methods. Acquire it before you call cfg() and release it afterwards immediately.
E.g.: ConfigAccess aAccess(...);
ReadGuard aReadLock(aAccess.m_aLock);
Reference< XPropertySet > xSet(aAccess.cfg(), UNO_QUERY);
Any aProp = xSet->getPropertyValue("...");
aReadLock.unlock();
@attention During this time it's not allowed to call the methods open() or close()!
Otherwhise you will change your own referenced config access. Anything will
be possible then.
@return A c++(!) reference to the uno instance of the configuration access point.
*/
const css::uno::Reference< css::uno::XInterface >& ConfigAccess::cfg()
{
// must be synchronized from outside!
// => no lock here ...
return m_xConfig;
}
} // namespace framework
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|