xref: /aoo42x/main/jvmfwk/source/elements.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #if !defined INCLUDED_JVMFWK_ELEMENTS_HXX
28 #define INCLUDED_JVMFWK_ELEMENTS_HXX
29 
30 #include <vector>
31 #include "jvmfwk/framework.h"
32 #include "fwkutil.hxx"
33 #include "rtl/ustring.hxx"
34 #include "rtl/byteseq.hxx"
35 #include "libxml/parser.h"
36 #include "boost/optional.hpp"
37 
38 #define NS_JAVA_FRAMEWORK "http://openoffice.org/2004/java/framework/1.0"
39 #define NS_SCHEMA_INSTANCE "http://www.w3.org/2001/XMLSchema-instance"
40 
41 namespace jfw
42 {
43 
44 /** gets the value of the updated element from the javavendors.xml.
45  */
46 rtl::OString getElementUpdated();
47 
48 /** create the child elements within the root structure for each platform.
49 
50     @param bNeedsSave
51     [out]If true then the respective structure of elements was added and the
52     document needs to be saved.
53  */
54 void createSettingsStructure(
55     xmlDoc * document, bool * bNeedsSave);
56 
57 
58 /** represents the settings saved in the /java/javaInfo element.
59     It is used within class NodeJava which determines the settings
60     file.
61 */
62 class CNodeJavaInfo
63 {
64 public:
65     CNodeJavaInfo();
66     ~CNodeJavaInfo();
67 
68     /** if true, then javaInfo is empty. When writeToNode is called
69         then all child elements are deleted.
70      */
71     bool m_bEmptyNode;
72     /** Contains the value of the <updated> element of
73         the javavendors.xml after loadFromNode was called.
74         It is not used, when the javaInfo node is written.
75         see writeToNode
76      */
77     ::rtl::OString sAttrVendorUpdate;
78     /** contains the nil value of the /java/javaInfo@xsi:nil attribute.
79         Default is true;
80      */
81     bool bNil;
82     /** contains the value of the /java/javaInfo@autoSelect attribute.
83         Default is true. If it is false then the user has modified the JRE
84         selection by actively choosing a JRE from the options dialog. That is,
85         the function jfw_setSelectedJRE was called. Contrary, the function
86         jfw_findAndSelectJRE sets the attribute to true.
87      */
88     bool bAutoSelect;
89     ::rtl::OUString sVendor;
90     ::rtl::OUString sLocation;
91     ::rtl::OUString sVersion;
92     sal_uInt64 nFeatures;
93     sal_uInt64 nRequirements;
94     ::rtl::ByteSequence arVendorData;
95 
96     /** reads the node /java/javaInfo.
97         If javaInfo@xsi:nil = true then member bNil is set to true
98         an no further elements are read.
99      */
100     void loadFromNode(xmlDoc * pDoc,xmlNode * pJavaInfo);
101     /** The attribut nil will be set to false. The function gets the value
102         javaSettings/updated from the javavendors.xml and writes it to
103         javaInfo@vendorUpdate in javasettings.xml
104      */
105     void writeToNode(xmlDoc * pDoc, xmlNode * pJavaInfo) const;
106 
107     /** returns NULL if javaInfo is nil.
108      */
109     JavaInfo * makeJavaInfo() const;
110 };
111 
112 /** this class represents the java settings  based on a particular
113     settings file.
114 
115     Which settings file is used is determined by the value passed into the
116     constructo and the values of the bootstrap parameters UNO_JAVA_JFW_USER_DATA,
117     UNO_JAVA_JFW_SHARED_DATA,_JAVA_JFW_INSTALL_DATA.
118 
119     If the value is USER_OR_INSTALL then it depends of the bootstrap parameter
120     UNO_JAVA_JFW_INSTALL_DATA. If it has as value then it is used. Otherwise the
121     value from UNO_JAVA_JFW_USER_DATA is used.
122 
123     The method load reads the data from the settings file.
124     The method write stores the data into the settings file.
125  */
126 class NodeJava
127 {
128 public:
129     enum Layer { USER_OR_INSTALL, USER, SHARED, INSTALL };
130 private:
131 
132     /** creates settings file and fills it with default values.
133 
134         When this function is called then it creates the
135         settings file at the possition determined by the bootstrap parameters
136         (UNO_JAVA_JFW_USER_DATA, UNO_JAVA_JFW_SHARED_DATA,
137         UNO_JAVA_JFW_INSTALL_DATA) and m_layer, unless the file already exists
138         (see createSettingsDocument).
139 
140         @return
141         JFW_E_CONFIG_READWRITE
142     */
143     void prepareSettingsDocument() const;
144 
145     /** helper function for prepareSettingsDocument.
146     */
147     void createSettingsDocument() const;
148 
149     /** returns the system path to the data file which is to be used. The value
150         depends on
151         the the member m_layer and the bootstrap paramters UNO_JAVA_JFW_USER_DATA,
152         UNO_JAVA_JFW_SHARED_DATA and UNO_JAVA_JFW_INSTALL_DATA which this may be.
153     */
154     ::rtl::OString getSettingsPath() const;
155 
156     /** returns the file URL to the data file which is to be used. See getSettingsPath.
157     */
158     ::rtl::OUString getSettingsURL() const;
159 
160     /** Verifies if the respective settings file exist. In case UNO_JAVA_JFW_INSTALL_DATA
161         is used, the age is checked. If the file is too old then we assume that it does not
162         exist and wipe its contents. Then still FILE_DOES_NOT_EXIST is returned.
163      */
164     jfw::FileStatus checkSettingsFileStatus() const;
165 
166     /** Determines the layer for which the instance the loads and writes the
167         data.
168     */
169     Layer m_layer;
170 
171     /** User configurable option.  /java/enabled
172         If /java/enabled@xsi:nil == true then the value will be uninitialized
173         after a call to load().
174     */
175     boost::optional<sal_Bool> m_enabled;
176 
177     /** User configurable option. /java/userClassPath
178         If /java/userClassPath@xsi:nil == true then the value is uninitialized
179         after a call to load().
180     */
181     boost::optional< ::rtl::OUString> m_userClassPath;
182     /** User configurable option.  /java/javaInfo
183         If /java/javaInfo@xsi:nil == true then the value is uninitialized
184         after a call to load.
185      */
186     boost::optional<CNodeJavaInfo> m_javaInfo;
187     /** User configurable option. /java/vmParameters
188         If /java/vmParameters@xsi:nil == true then the value is uninitialized
189         after a call to load.
190     */
191     boost::optional< ::std::vector< ::rtl::OUString> > m_vmParameters;
192     /** User configurable option. /java/jreLocations
193         If /java/jreLocaltions@xsi:nil == true then the value is uninitialized
194         after a call to load.
195     */
196     boost::optional< ::std::vector< ::rtl::OUString> > m_JRELocations;
197 
198     /** Only in INSTALL mode. Then NodeJava.write writes a <modified> element
199         which contains the seconds value of the TimeValue (osl/time.h), obtained
200         with osl_getSystemTime.
201         It returns 0 if the value cannot be obtained.
202         This is used to fix the problem that the modified time of the settings
203         file is incorrect because it resides on an NFS volume where the NFS
204         server and NFS client do not have the same system time. For example if
205         the server time is ahead of the client time then checkSettingsFileStatus
206         deleted the settings. So even if javaldx determined a Java
207         (jfw_findAndSelectJRE) then jfw_startVM returned a JFW_E_NO_SELECT. Then
208         it looked again for a java by calling jfw_findAndSelectJRE, which
209         returned a JFW_E_NONE. But the following jfw_startVM returned again
210         JFW_E_NO_SELECT. So it looped. (see issue i114509)
211 
212         NFS server and NFS client should have the same time. It is common
213         practise to enforce this in networks. We actually should not work
214         around a malconfigured network. We must however, make sure that we do
215         not loop. Maybe a better approach is, that:
216         - assume that mtime and system time are reliable
217         - checkSettingsFile uses system time and mtime of the settings file,
218         instset of using getModifiedTime.
219         - allow a small error margin
220         - jfw_startVM must return a JFW_E_EXPIRED_SETTINGS
221         - XJavaVM::startVM should prevent the loop by processing the new return+        value
222 
223     */
224     sal_uInt32 getModifiedTime() const;
225 
226 public:
227 
228     NodeJava(Layer theLayer = USER_OR_INSTALL);
229 
230     /** sets m_enabled.
231         /java/enabled@xsi:nil will be set to false when write is called.
232      */
233     void setEnabled(sal_Bool bEnabled);
234 
235     /** sets m_sUserClassPath. See setEnabled.
236      */
237     void setUserClassPath(const ::rtl::OUString & sClassPath);
238 
239     /** sets m_aInfo. See setEnabled.
240         @param bAutoSelect
241         true- called by jfw_setSelectedJRE
242         false called by jfw_findAndSelectJRE
243      */
244     void setJavaInfo(const JavaInfo * pInfo, bool bAutoSelect);
245 
246     /** sets the /java/vmParameters/param elements.
247         When this method all previous values are removed and replaced
248         by those in arParameters.
249         /java/vmParameters@xsi:nil will be set to true when write() is
250         called.
251      */
252     void setVmParameters(rtl_uString  * * arParameters, sal_Int32 size);
253 
254     /** sets the /java/jreLocations/location elements.
255         When this method is called then all previous values are removed
256         and replaced by those in arParamters.
257         /java/jreLocations@xsi:nil will be set to true write() is called.
258      */
259     void setJRELocations(rtl_uString  * * arParameters, sal_Int32 size);
260 
261     /** adds a location to the already existing locations.
262         Note: call load() before, then add the location and then call write().
263     */
264     void addJRELocation(rtl_uString * sLocation);
265 
266     /** writes the data to user settings.
267      */
268     void write() const;
269 
270     /** load the values of the settings file.
271      */
272     void load();
273 
274     /** returns the value of the element /java/enabled
275      */
276     const boost::optional<sal_Bool> & getEnabled() const;
277     /** returns the value of the element /java/userClassPath.
278      */
279     const boost::optional< ::rtl::OUString> & getUserClassPath() const;
280 
281     /** returns the value of the element /java/javaInfo.
282      */
283     const boost::optional<CNodeJavaInfo> & getJavaInfo() const;
284 
285     /** returns the parameters from the element /java/vmParameters/param.
286      */
287     const boost::optional< ::std::vector< ::rtl::OUString> > & getVmParameters() const;
288 
289     /** returns the parameters from the element /java/jreLocations/location.
290      */
291     const boost::optional< ::std::vector< ::rtl::OUString> > & getJRELocations() const;
292 };
293 
294 /** merges the settings for shared, user and installation during construction.
295     The class uses a simple merge mechanism for the javasettings.xml files in share and
296     user. The following elements completly overwrite the corresponding elements
297     from share:
298     /java/enabled
299     /java/userClassPath
300     /java/vmParameters
301     /java/jreLocations
302     /java/javaInfo
303 
304     In case of an installation, the shared and user settings are completely
305     disregarded.
306 
307     The locations of the different settings files is obtained through the
308     bootstrap variables:
309     UNO_JAVA_JFW_USER_DATA
310     UNO_JAVA_JFW_SHARED_DATA
311     UNO_JAVA_JFW_INSTALL_DATA
312 
313     The class also determines useful default values for settings which have not been made.
314 */
315 class MergedSettings
316 {
317 private:
318     const MergedSettings& operator = (MergedSettings&);
319     MergedSettings(MergedSettings&);
320 
321     void merge(const NodeJava & share, const NodeJava & user);
322 
323     sal_Bool m_bEnabled;
324 
325     ::rtl::OUString m_sClassPath;
326 
327     ::std::vector< ::rtl::OUString> m_vmParams;
328 
329     ::std::vector< ::rtl::OUString> m_JRELocations;
330 
331     CNodeJavaInfo m_javaInfo;
332 
333 public:
334     MergedSettings();
335     virtual ~MergedSettings();
336 
337     /** the default is true.
338      */
339     sal_Bool getEnabled() const;
340 
341     const ::rtl::OUString & getUserClassPath() const;
342 
343     ::std::vector< ::rtl::OString> getVmParametersUtf8() const;
344     /** returns a JavaInfo structure representing the node
345         /java/javaInfo. Every time a new JavaInfo structure is created
346         which needs to be freed by the caller.
347         If both, user and share settings are nil, then NULL is returned.
348     */
349     JavaInfo * createJavaInfo() const;
350 
351     /** returns the value of the attribute /java/javaInfo[@vendorUpdate].
352      */
353     ::rtl::OString const & getJavaInfoAttrVendorUpdate() const;
354 
355 #ifdef WNT
356     /** returns the javaInfo@autoSelect attribute.
357         Before calling this function loadFromSettings must be called.
358         It uses the javaInfo@autoSelect attribute  to determine
359         the return value;
360      */
361     bool getJavaInfoAttrAutoSelect() const;
362 #endif
363 
364     /** returns an array.
365         Caller must free the strings and the array.
366      */
367     void getVmParametersArray(rtl_uString *** parParameters, sal_Int32 * size) const;
368 
369     /** returns an array.
370         Caller must free the strings and the array.
371      */
372     void getJRELocations(rtl_uString *** parLocations, sal_Int32 * size) const;
373 
374     const ::std::vector< ::rtl::OUString> & getJRELocations() const;
375 };
376 
377 
378 class VersionInfo
379 {
380     ::std::vector< ::rtl::OUString> vecExcludeVersions;
381     rtl_uString ** arVersions;
382 
383 public:
384     VersionInfo();
385     ~VersionInfo();
386 
387     void addExcludeVersion(const ::rtl::OUString& sVersion);
388 
389     ::rtl::OUString sMinVersion;
390     ::rtl::OUString sMaxVersion;
391 
392     /** The caller DOES NOT get ownership of the strings. That is he
393         does not need to release the strings.
394         The array exists as long as this object exists.
395     */
396 
397     rtl_uString** getExcludeVersions();
398     sal_Int32 getExcludeVersionSize();
399 };
400 
401 struct PluginLibrary
402 {
403     PluginLibrary()
404     {
405     }
406     PluginLibrary(rtl::OUString vendor,::rtl::OUString path) :
407         sVendor(vendor), sPath(path)
408     {
409     }
410     /** contains the vendor string which is later userd in the xml API
411      */
412     ::rtl::OUString sVendor;
413     /** File URL the plug-in library
414      */
415     ::rtl::OUString sPath;
416 };
417 
418 } //end namespace
419 #endif
420