You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2004-2005 Cendio AB.
  3. * Copyright 2012 Brian P. Hinz
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  18. * USA.
  19. */
  20. //
  21. // Configuration - class for dealing with configuration parameters.
  22. //
  23. package com.tigervnc.rfb;
  24. import java.io.FileInputStream;
  25. import java.io.PrintWriter;
  26. import java.lang.reflect.Field;
  27. import java.text.DateFormat;
  28. import java.text.SimpleDateFormat;
  29. import java.util.*;
  30. import com.tigervnc.vncviewer.VncViewer;
  31. public class Configuration {
  32. static LogWriter vlog = new LogWriter("Configuration");
  33. private static final String IDENTIFIER_STRING = "TigerVNC Configuration file Version 1.0";
  34. public enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer };
  35. // -=- The Global/server/viewer Configuration objects
  36. private static Configuration global_ = null;
  37. private static Configuration server_ = null;
  38. private static Configuration viewer_ = null;
  39. public static Configuration global() {
  40. if (global_ == null)
  41. global_ = new Configuration("Global");
  42. return global_;
  43. }
  44. public static Configuration server() {
  45. if (server_ == null)
  46. server_ = new Configuration("Server");
  47. return server_;
  48. }
  49. public static Configuration viewer() {
  50. if (viewer_ == null)
  51. viewer_ = new Configuration("Viewer");
  52. return viewer_;
  53. }
  54. // Enable server/viewer specific parameters
  55. public static void enableServerParams() { global().appendConfiguration(server()); }
  56. public static void enableViewerParams() { global().appendConfiguration(viewer()); }
  57. // Append configuration object to this instance.
  58. // NOTE: conf instance can be only one configuration object
  59. public void appendConfiguration(Configuration conf) {
  60. conf._next = _next; _next = conf;
  61. }
  62. // -=- Configuration implementation
  63. public Configuration(String name_, Configuration attachToGroup) {
  64. name = name_; head = null; _next = null;
  65. if (attachToGroup != null) {
  66. _next = attachToGroup._next;
  67. attachToGroup._next = this;
  68. }
  69. }
  70. public Configuration(String name_) {
  71. this(name_, null);
  72. }
  73. // - Return the buffer containing the Configuration's name
  74. final public String getName() { return name; }
  75. // - Assignment operator. For every Parameter in this Configuration's
  76. // group, get()s the corresponding source parameter and copies its
  77. // content.
  78. public Configuration assign(Configuration src) {
  79. VoidParameter current = head;
  80. while (current != null) {
  81. VoidParameter srcParam = ((Configuration)src).get(current.getName());
  82. if (srcParam != null) {
  83. current.immutable = false;
  84. String value = srcParam.getValueStr();
  85. vlog.debug("operator=("+current.getName()+", "+value+")");
  86. current.setParam(value);
  87. }
  88. current = current._next;
  89. }
  90. if (_next != null)
  91. _next = src;
  92. return this;
  93. }
  94. // - Set named parameter to value
  95. public boolean set(String n, String v, boolean immutable) {
  96. return set(n, n.length(), v, immutable);
  97. }
  98. public boolean set(String n, String v) {
  99. return set(n, n.length(), v, false);
  100. }
  101. // - Set parameter to value (separated by "=")
  102. public boolean set(String name, int len,
  103. String val, boolean immutable)
  104. {
  105. VoidParameter current = head;
  106. while (current != null) {
  107. if (current.getName().length() == len &&
  108. current.getName().equalsIgnoreCase(name.substring(0, len)))
  109. {
  110. boolean b = current.setParam(val);
  111. current.setHasBeenSet();
  112. if (b && immutable)
  113. current.setImmutable();
  114. return b;
  115. }
  116. current = current._next;
  117. }
  118. return (_next != null) ? _next.set(name, len, val, immutable) : false;
  119. }
  120. // - Set named parameter to value, with name truncated at len
  121. boolean set(String config, boolean immutable) {
  122. boolean hyphen = false;
  123. if (config.charAt(0) == '-') {
  124. hyphen = true;
  125. config = config.substring(1);
  126. if (config.charAt(0) == '-') config = config.substring(1); // allow gnu-style --<option>
  127. }
  128. int equal = config.indexOf('=');
  129. if (equal > -1) {
  130. return set(config, equal, config.substring(equal+1), immutable);
  131. } else if (hyphen) {
  132. VoidParameter current = head;
  133. while (current != null) {
  134. if (current.getName().equalsIgnoreCase(config)) {
  135. boolean b = current.setParam();
  136. current.setHasBeenSet();
  137. if (b && immutable)
  138. current.setImmutable();
  139. return b;
  140. }
  141. current = current._next;
  142. }
  143. }
  144. return (_next != null) ? _next.set(config, immutable) : false;
  145. }
  146. boolean set(String config) {
  147. return set(config, false);
  148. }
  149. // - Container for process-wide Global parameters
  150. public static boolean setParam(String param, String value, boolean immutable) {
  151. return global().set(param, value, immutable);
  152. }
  153. public static boolean setParam(String param, String value) {
  154. return setParam(param, value, false);
  155. }
  156. public static boolean setParam(String config, boolean immutable) {
  157. return global().set(config, immutable);
  158. }
  159. public static boolean setParam(String config) {
  160. return setParam(config, false);
  161. }
  162. public static boolean setParam(String name, int len,
  163. String val, boolean immutable) {
  164. return global().set(name, len, val, immutable);
  165. }
  166. // - Get named parameter
  167. public VoidParameter get(String param)
  168. {
  169. VoidParameter current = head;
  170. while (current != null) {
  171. if (current.getName().equalsIgnoreCase(param))
  172. return current;
  173. current = current._next;
  174. }
  175. return (_next != null) ? _next.get(param) : null;
  176. }
  177. public static VoidParameter getParam(String param) { return global().get(param); }
  178. public static void listParams(int width, int nameWidth) {
  179. global().list(width, nameWidth);
  180. }
  181. public static void listParams() {
  182. listParams(79, 10);
  183. }
  184. public void list(int width, int nameWidth) {
  185. VoidParameter current = head;
  186. System.err.format("%s Parameters:%n", name);
  187. while (current != null) {
  188. String def_str = current.getDefaultStr();
  189. String desc = current.getDescription().trim();
  190. String format = " %-"+nameWidth+"s -";
  191. System.err.format(format, current.getName());
  192. int column = current.getName().length();
  193. if (column < nameWidth) column = nameWidth;
  194. column += 4;
  195. while (true) {
  196. int s = desc.indexOf(' ');
  197. int wordLen;
  198. if (s > -1) wordLen = s;
  199. else wordLen = desc.length();
  200. if (column + wordLen + 1 > width) {
  201. format = "%n%"+(nameWidth+4)+"s";
  202. System.err.format(format, "");
  203. column = nameWidth+4;
  204. }
  205. format = " %"+wordLen+"s";
  206. System.err.format(format, desc.substring(0, wordLen));
  207. column += wordLen + 1;
  208. if (s == -1) break;
  209. desc = desc.substring(wordLen+1);
  210. }
  211. if (def_str != null) {
  212. if (column + def_str.length() + 11 > width)
  213. System.err.format("%n%"+(nameWidth+4)+"s","");
  214. System.err.format(" (default=%s)%n",def_str);
  215. def_str = null;
  216. } else {
  217. System.err.format("%n");
  218. }
  219. current = current._next;
  220. }
  221. if (_next != null)
  222. _next.list(width, nameWidth);
  223. }
  224. public void list() {
  225. list(79, 10);
  226. }
  227. // Name for this Configuration
  228. private String name;
  229. // - Pointer to first Parameter in this group
  230. public VoidParameter head;
  231. // Pointer to next Configuration in this group
  232. public Configuration _next;
  233. }