source: trunk/icedtea-web/plugin/icedteanp/IcedTeaParseProperties.cc@ 434

Last change on this file since 434 was 429, checked in by dmik, 11 years ago

icedtea-web: Merge version 1.5.1 from vendor to trunk.

File size: 10.0 KB
Line 
1/* IcedTeaRunnable.cc
2
3 Copyright (C) 2013 Red Hat
4
5This file is part of IcedTea.
6
7IcedTea is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12IcedTea is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with IcedTea; see the file COPYING. If not, write to the
19Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301 USA.
21
22Linking this library statically or dynamically with other modules is
23making a combined work based on this library. Thus, the terms and
24conditions of the GNU General Public License cover the whole
25combination.
26
27As a special exception, the copyright holders of this library give you
28permission to link this library with independent modules to produce an
29executable, regardless of the license terms of these independent
30modules, and to copy and distribute the resulting executable under
31terms of your choice, provided that you also meet, for each linked
32independent module, the terms and conditions of the license of that
33module. An independent module is a module which is not derived from
34or based on this library. If you modify this library, you may extend
35this exception to your version of the library, but you are not
36obligated to do so. If you do not wish to do so, delete this
37exception statement from your version. */
38
39#include <unistd.h>
40#include <sys/types.h>
41#include <pwd.h>
42#include <cstdio>
43#include <cstdlib>
44#include <string>
45#include <functional>
46#include <cctype>
47#include <locale>
48#include <iostream>
49#include <fstream>
50
51
52#include "IcedTeaPluginUtils.h"
53#include "IcedTeaNPPlugin.h"
54#include "IcedTeaParseProperties.h"
55/*
56 The public api is nearly impossible to test due to "hardcoded paths"
57 All public methods have theirs equivalents wit set-up-able files, and those are
58 tested.
59*/
60
61using namespace std;
62//private api
63
64void remove_all_spaces(string& str);
65bool get_property_value(string c, string& dest);
66bool starts_with(string c1, string c2);
67string user_properties_file();
68string main_properties_file();
69string default_java_properties_file();
70//for passing three dummy files
71bool find_system_config_file(string main_file, string custom_jre_file, bool usecustom_jre, string default_java_file, string& dest);
72bool find_property(string filename, string property, string& dest);
73//for passing two dummy files
74bool read_deploy_property_value(string user_file, string system_file, bool usesystem_file, string property, string& dest);
75//for passing two dummy files
76bool find_custom_jre(string user_file, string main_file,string& dest);
77//end of non-public IcedTeaParseProperties api
78const std::string default_file_ITW_deploy_props_name = "deployment.properties";
79const std::string default_itw_log_dir_name = "log";
80const std::string custom_jre_key = "deployment.jre.dir";
81
82void remove_all_spaces(string& str)
83{
84 for(int i=0; i<str.length(); i++){
85 if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
86 str.erase(i,1);
87 i--;
88 }
89 }
90}
91
92bool get_property_value(string c, string& dest){
93 int i = c.find("=");
94 if (i < 0) return false;
95 int l = c.length();
96 dest = c.substr(i+1, l-i);
97 IcedTeaPluginUtilities::trim(dest);
98 return true;
99}
100
101
102bool starts_with(string c1, string c2){
103 return (c1.find(c2) == 0);
104}
105
106
107string user_properties_file(){
108 int myuid = getuid();
109 struct passwd *mypasswd = getpwuid(myuid);
110 // try pre 1.5 file location
111 string old_name = string(mypasswd->pw_dir)+"/.icedtea/"+default_file_ITW_deploy_props_name;
112 //exists? then itw was not yet migrated. Use it
113 if (IcedTeaPluginUtilities::file_exists(old_name)) {
114 PLUGIN_ERROR("IcedTea-Web plugin is using out-dated configuration\n");
115 return old_name;
116 }
117 //we are probably on XDG specification now
118 //is specified custom value?
119 if (getenv ("XDG_CONFIG_HOME") != NULL){
120 return string(getenv ("XDG_CONFIG_HOME"))+"/icedtea-web/"+default_file_ITW_deploy_props_name;
121 }
122 //if not then use default
123 return string(mypasswd->pw_dir)+"/.config/icedtea-web/"+default_file_ITW_deploy_props_name;
124}
125
126string get_log_dir(){
127 string value;
128 if (!read_deploy_property_value("deployment.user.logdir", value)) {
129 string config_dir;
130 if (getenv ("XDG_CONFIG_HOME") != NULL){
131 config_dir = string(getenv("XDG_CONFIG_HOME"));
132 } else {
133 int myuid = getuid();
134 struct passwd *mypasswd = getpwuid(myuid);
135 config_dir = string(mypasswd->pw_dir) + "/.config";
136 }
137 string itw_dir = config_dir+"/icedtea-web";
138 string log_dir = itw_dir+"/"+default_itw_log_dir_name;
139 mkdir_checked(itw_dir);
140 mkdir_checked(log_dir);
141 return log_dir;
142 }
143 return value;
144}
145
146void mkdir_checked(string dir){
147 if (!IcedTeaPluginUtilities::file_exists(dir))
148 {
149 const int PERMISSIONS_MASK = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; // 0755
150 int stat = g_mkdir(dir.c_str(), PERMISSIONS_MASK);
151 if (stat != 0)
152 {
153 PLUGIN_DEBUG("WARNING: Creation of directory %s failed: %s\n", dir.c_str(), strerror(errno));
154 }
155 }
156}
157
158string main_properties_file(){
159 return "/etc/.java/deployment/"+default_file_ITW_deploy_props_name;
160}
161
162string default_java_properties_file(){
163 return ICEDTEA_WEB_JRE "/lib/"+default_file_ITW_deploy_props_name;
164}
165
166
167/* this is the same search done by icedtea-web settings:
168 try the main file in /etc/.java/deployment
169 if found, then return this file
170 try to find setUp jre
171 if found, then try if this file exists and end
172 if no jre custom jvm is set, then tries default jre
173 if its deploy file exists, then return
174 not found otherwise*/
175bool find_system_config_file(string& dest){
176 string jdest;
177 bool found = find_custom_jre(jdest);
178 if (found) {
179 jdest = jdest + "/lib/"+default_file_ITW_deploy_props_name;
180 }
181 return find_system_config_file(main_properties_file(), jdest, found, default_java_properties_file(), dest);
182}
183
184bool is_java_console_enabled(){
185 string value;
186 if (!read_deploy_property_value("deployment.console.startup.mode", value)) {
187 return true;
188 }
189 if (value == "DISABLE") {
190 return false;
191 } else {
192 return true;
193 }
194}
195
196bool read_bool_property(string key, bool defaultValue){
197 string value;
198 if (!read_deploy_property_value(key, value)) {
199 return defaultValue;
200 }
201 if (value == "true") {
202 return true;
203 } else {
204 return false;
205 }
206}
207
208bool is_debug_on(){
209 return read_bool_property("deployment.log",false);
210}
211bool is_debug_header_on(){
212 return read_bool_property("deployment.log.headers",false);
213}
214bool is_logging_to_file(){
215 return read_bool_property("deployment.log.file",false);
216}
217bool is_logging_to_stds(){
218 return read_bool_property("deployment.log.stdstreams",true);
219}
220bool is_logging_to_system(){
221 return read_bool_property("deployment.log.system",true);
222}
223
224
225//abstraction for testing purposes
226bool find_system_config_file(string main_file, string custom_jre_file, bool usecustom_jre, string default_java_file, string& dest){
227 if (IcedTeaPluginUtilities::file_exists(main_file)) {
228 dest = main_file;
229 return true;
230 } else {
231 if (usecustom_jre){
232 if(IcedTeaPluginUtilities::file_exists(custom_jre_file) ) {
233 dest = custom_jre_file;
234 return true;
235 }
236 } else {
237 if(IcedTeaPluginUtilities::file_exists(default_java_file)) {
238 dest = default_java_file;
239 return true;
240 }
241 }
242 }
243return false; //nothing of above found
244}
245
246//Returns whether property was found, if found stores result in 'dest'
247bool find_property(string filename, string property, string& dest){
248 string property_matcher(property);
249 IcedTeaPluginUtilities::trim( property_matcher);
250 property_matcher= property_matcher+"=";
251 ifstream input( filename.c_str() );
252 for( string line; getline( input, line ); ){ /* read a line */
253 string copy = line;
254 //java tolerates spaces around = char, remove them for matching
255 remove_all_spaces(copy);
256 if (starts_with(copy, property_matcher)) {
257 //provide non-spaced value, trimming is done in get_property_value
258 get_property_value(line, dest);
259 return true;
260 }
261 }
262
263 return false;
264 }
265
266
267/* this is reimplementation of itw-settings operations
268 first check in user's settings, if found, return
269 then check in global file (see the magic of find_system_config_file)*/
270bool read_deploy_property_value(string property, string& dest){
271 string futurefile;
272 bool found = find_system_config_file(futurefile);
273 return read_deploy_property_value(user_properties_file(), futurefile, found, property, dest);
274}
275//abstraction for testing purposes
276bool read_deploy_property_value(string user_file, string system_file, bool usesystem_file, string property, string& dest){
277 //is it in user's file?
278 bool found = find_property(user_file, property, dest);
279 if (found) {
280 return true;
281 }
282 //is it in global file?
283 if (usesystem_file) {
284 return find_property(system_file, property, dest);
285 }
286 return false;
287}
288
289//This is different from common get property, as it is avoiding to search in *java*
290//properties files
291bool find_custom_jre(string& dest){
292 return find_custom_jre(user_properties_file(), main_properties_file(), dest);
293}
294//abstraction for testing purposes
295bool find_custom_jre(string user_file, string main_file,string& dest){
296 string key = custom_jre_key;
297 if(IcedTeaPluginUtilities::file_exists(user_file)) {
298 bool a = find_property(user_file, key, dest);
299 if (a) {
300 return true;
301 }
302 }
303 if(IcedTeaPluginUtilities::file_exists(main_file)) {
304 return find_property(main_file, key, dest);
305 }
306return false;
307}
308
309
310
311int test_main(void){
312 cout << ("user's settings file\n");
313 cout << user_properties_file();
314 cout << ("\nmain settings file:\n");
315 cout << (main_properties_file());
316 cout << ("\njava settings file \n");
317 cout << (default_java_properties_file());
318 cout << ("\nsystem config file\n");
319 string a1;
320 find_system_config_file(a1);
321 cout << a1;
322 cout << ("\ncustom jre\n");
323 string a2;
324 find_custom_jre(a2);
325 cout << a2;
326 cout << ("\nsome custom property\n");
327 string a3;
328 read_deploy_property_value("deployment.security.level", a3);
329 cout << a3;
330 cout << ("\n");
331 return 0;
332}
Note: See TracBrowser for help on using the repository browser.