1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 //________________________________
28 //	my own includes
29 #include <jobs/configaccess.hxx>
30 #include <threadhelp/readguard.hxx>
31 #include <threadhelp/writeguard.hxx>
32 #include <threadhelp/resetableguard.hxx>
33 #include <general.h>
34 #include <services.h>
35 
36 //________________________________
37 //	interface includes
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/beans/XMultiHierarchicalPropertySet.hpp>
40 #include <com/sun/star/container/XNameAccess.hpp>
41 #include <com/sun/star/beans/PropertyValue.hpp>
42 #include <com/sun/star/util/XChangesBatch.hpp>
43 
44 //________________________________
45 //	includes of other projects
46 #include <unotools/configpathes.hxx>
47 #include <rtl/ustrbuf.hxx>
48 
49 //________________________________
50 //	namespace
51 
52 namespace framework{
53 
54 //________________________________
55 //	non exported const
56 
57 //________________________________
58 //	non exported definitions
59 
60 //________________________________
61 //	declarations
62 
63 //________________________________
64 /**
65     @short  open the configuration of this job
66     @descr  We open the configuration of this job only. Not the whole package or the whole
67             job set. We are interested on our own properties only.
68             We set the opened configuration access as our member. So any following method,
69             which needs cfg access, can use it. That prevent us against multiple open/close requests.
70             But you can use this method to upgrade an already opened configuration too.
71 
72     @param  eMode
73                 force opening of the configuration access in readonly or in read/write mode
74  */
75 ConfigAccess::ConfigAccess( /*IN*/ const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
76                             /*IN*/ const ::rtl::OUString&                                        sRoot )
77     : ThreadHelpBase(          )
78     , m_xSMGR       ( xSMGR    )
79     , m_sRoot       ( sRoot    )
80     , m_eMode       ( E_CLOSED )
81 {
82 }
83 
84 //________________________________
85 /**
86     @short  last chance to close an open configuration access point
87     @descr  In case our user forgot to close this configuration point
88             in the right way, normaly he will run into some trouble -
89             e.g. losing data.
90  */
91 ConfigAccess::~ConfigAccess()
92 {
93     close();
94 }
95 
96 //________________________________
97 /**
98     @short  return the internal mode of this instance
99     @descr  May be the outside user need any information about successfully opened
100             or closed config access point objects. He can control the internal mode to do so.
101 
102     @return The internal open state of this object.
103  */
104 ConfigAccess::EOpenMode ConfigAccess::getMode() const
105 {
106     /* SAFE { */
107     ReadGuard aReadLock(m_aLock);
108     return m_eMode;
109     /* } SAFE */
110 }
111 
112 //________________________________
113 /**
114     @short  open the configuration access in the specified mode
115     @descr  We set the opened configuration access as our member. So any following method,
116             which needs cfg access, can use it. That prevent us against multiple open/close requests.
117             But you can use this method to upgrade an already opened configuration too.
118             It's possible to open a config access in READONLY mode first and "open" it at a second
119             time within the mode READWRITE. Then we will upgrade it. Dowgrade will be possible too.
120 
121             But note: closing will be done explicitly by calling method close() ... not by
122             downgrading with mode CLOSED!
123 
124     @param  eMode
125                 force (re)opening of the configuration access in readonly or in read/write mode
126  */
127 void ConfigAccess::open( /*IN*/ EOpenMode eMode )
128 {
129     /* SAFE { */
130     // We must lock the whole method to be shure, that nobody
131     // outside uses our internal member m_xAccess!
132     WriteGuard aWriteLock(m_aLock);
133 
134     // check if configuration is already open in the right mode.
135     // By the way: Don't allow closing by using this method!
136     if (
137         (eMode  !=E_CLOSED) &&
138         (m_eMode!=eMode   )
139        )
140     {
141         // We have to close the old access point without any question here.
142         // It will be open again using the new mode.
143         // can be called without checks! It does the checks by itself ...
144         // e.g. for already closed or not opened configuration.
145         // Flushing of all made changes will be done here too.
146         close();
147 
148         // create the configuration provider, which provides sub access points
149         css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(m_xSMGR->createInstance(SERVICENAME_CFGPROVIDER), css::uno::UNO_QUERY);
150         if (xConfigProvider.is())
151         {
152             css::beans::PropertyValue aParam;
153             aParam.Name    = DECLARE_ASCII("nodepath");
154             aParam.Value <<= m_sRoot;
155 
156             css::uno::Sequence< css::uno::Any > lParams(1);
157             lParams[0] <<= aParam;
158 
159             // open it
160             try
161             {
162                 if (eMode==E_READONLY)
163                     m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS  , lParams);
164                 else
165                 if (eMode==E_READWRITE)
166                     m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
167             }
168             catch(css::uno::Exception& ex)
169             {
170                 (void) ex; // avoid warning
171                 LOG_WARNING("open config ...", U2B(ex.Message))
172             }
173 
174             m_eMode = E_CLOSED;
175             if (m_xConfig.is())
176                 m_eMode = eMode;
177         }
178     }
179 
180     aWriteLock.unlock();
181     /* } SAFE */
182 }
183 
184 //________________________________
185 /**
186     @short  close the internal opened configuration access and flush all changes
187     @descr  It checks, if the given access is valid and react in the right way.
188             It flushes all changes ... so nobody else must know this state.
189  */
190 void ConfigAccess::close()
191 {
192     /* SAFE { */
193     // Lock the whole method, to be shure that nobody else uses our internal members
194     // during this time.
195     WriteGuard aWriteLock(m_aLock);
196 
197     // check already closed configuration
198     if (m_xConfig.is())
199     {
200         css::uno::Reference< css::util::XChangesBatch > xFlush(m_xConfig, css::uno::UNO_QUERY);
201         if (xFlush.is())
202             xFlush->commitChanges();
203         m_xConfig = css::uno::Reference< css::uno::XInterface >();
204         m_eMode   = E_CLOSED;
205     }
206 
207     aWriteLock.unlock();
208     /* } SAFE */
209 }
210 
211 //________________________________
212 /**
213     @short  provides an access to the internal wrapped configuration access
214     @descr  It's not allowed to safe this c++ (!) reference outside. You have
215             to use it directly. Further you must use our public lock member m_aLock
216             to synchronize your code with our internal structures and our interface
217             methods. Acquire it before you call cfg() and release it afterwards immediately.
218 
219             E.g.:   ConfigAccess aAccess(...);
220                     ReadGuard aReadLock(aAccess.m_aLock);
221                     Reference< XPropertySet > xSet(aAccess.cfg(), UNO_QUERY);
222                     Any aProp = xSet->getPropertyValue("...");
223                     aReadLock.unlock();
224 
225     @attention  During this time it's not allowed to call the methods open() or close()!
226                 Otherwhise you will change your own referenced config access. Anything will
227                 be possible then.
228 
229     @return A c++(!) reference to the uno instance of the configuration access point.
230  */
231 const css::uno::Reference< css::uno::XInterface >& ConfigAccess::cfg()
232 {
233     // must be synchronized from outside!
234     // => no lock here ...
235     return m_xConfig;
236 }
237 
238 } // namespace framework
239