For deserialization exception, debug print the actual XML message. Helps in debugging.
[beagle.git] / tools / Config.cs
blob35edba406041f355c30206cd3511711e677edc54
1 //
2 // Config.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Diagnostics;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Reflection;
34 using Beagle;
35 using Beagle.Util;
37 using Gtk;
39 public static class ConfigTool {
41 private static void PrintUsageAndExit ()
43 string usage =
44 "beagle-config: Command-line interface to the Beagle config file.\n" +
45 "Web page: http://www.gnome.org/projects/beagle\n" +
46 "Copyright (C) 2005 Novell, Inc.\n\n";
47 usage +=
48 "Usage: beagle-config [OPTIONS]\n" +
49 " or: beagle-config <SECTION>\n" +
50 " or: beagle-config <SECTION> <SECTIONOPTION> [PARAMS]\n\n" +
51 "Options:\n" +
52 " --beagled-reload-config\tAsk the beagle daemon to reload\n" +
53 " \tthe configuration file.\n" +
54 " --list-sections\t\tList all available configuration sections.\n" +
55 " --help\t\t\tPrint this usage message.\n\n" +
56 "If a section is specified with no options, then a list of the available commands for that section is shown.\n";
58 Console.WriteLine (usage);
60 System.Environment.Exit (0);
63 private static void ListSectionsAndExit ()
65 Console.WriteLine ("Available configuration sections: ");
66 foreach (string key in Conf.Sections.Keys)
67 Console.WriteLine (" - {0}", key);
69 System.Environment.Exit (0);
72 private static void ListSectionOptionsAndExit (string sectionname, Hashtable options)
74 Console.WriteLine ("Available options for section '{0}':", sectionname);
75 foreach (string key in options.Keys) {
76 Console.Write (" - {0}", key);
77 if (options [key] != null)
78 Console.Write (" ({0})", options [key]);
80 Console.WriteLine ();
82 if (sectionname == "daemon")
83 Console.WriteLine (" - ListBackends (List enabled and disabled backends)");
85 System.Environment.Exit (0);
88 private static void ReloadConfigAndExit ()
90 try {
91 ReloadConfigRequest request = new ReloadConfigRequest ();
92 request.Send ();
93 Console.WriteLine ("ReloadConfig request was sent successfully.");
94 System.Environment.Exit (0);
95 } catch (Exception e) {
96 Console.Error.WriteLine ("ERROR: Could not send ReloadConfig request: {0}", e.Message);
97 System.Environment.Exit (-1);
101 public static void Main (string [] args)
103 Application.InitCheck ("beagle-config", ref args);
105 if (args.Length == 0)
106 PrintUsageAndExit ();
108 int i = 0;
109 while (i < args.Length) {
110 switch (args [i]) {
111 case "--list-sections":
112 Conf.Load ();
113 ListSectionsAndExit ();
114 return;
116 case "--reload":
117 case "--beagled-reload-config":
118 ReloadConfigAndExit ();
119 return;
121 case "--help":
122 case "--usage":
123 PrintUsageAndExit ();
124 return;
126 default:
127 break;
129 ++i;
132 Conf.Load ();
134 string sectionname = args [0];
136 if (! Conf.Sections.ContainsKey (sectionname)) {
137 Console.Error.WriteLine ("ERROR: Invalid section name '{0}'", sectionname);
138 Environment.Exit (-1);
141 Conf.Section section = (Conf.Section) Conf.Sections [sectionname];
142 Hashtable options = Conf.GetOptions (section);
144 // No option specified?
145 if (args.Length == 1)
146 ListSectionOptionsAndExit (sectionname, options);
148 string optionname = args [1];
149 if (! options.ContainsKey (optionname)) {
150 if (sectionname == "daemon" && optionname == "ListBackends") {
151 ListBackends ();
152 Environment.Exit (0);
153 } else {
154 Console.Error.WriteLine ("ERROR: Invalid option name '{0}'", optionname);
155 Environment.Exit (-2);
160 // Invoke the method the user has chosen
163 // Pack the remaining command line params into an array used for
164 // params of the method.
165 string [] optionparams = new string [args.Length - 2];
166 int j, k;
167 for (j = 0, k = 2; k < args.Length; j++, k++)
168 optionparams [j] = args [k];
170 // Invoke the method
171 string output = null;
172 bool result = false;
174 try {
175 result = Conf.InvokeOption (section, optionname, optionparams, out output);
176 } catch (Exception e) {
177 Console.Error.WriteLine("ERROR: Command execution failed - caught exception.");
178 Console.Error.WriteLine(e.Message);
179 Environment.Exit (-3);
182 // Read the result and show the output
183 if (result == true)
184 Console.WriteLine (output);
185 else {
186 Console.Error.WriteLine ("ERROR: Command execution failed.");
187 Console.Error.WriteLine (output);
188 Environment.Exit (-4);
191 Conf.Save ();
192 Environment.Exit (0);
196 private static void ListBackends ()
198 ArrayList backends = new ArrayList ();
200 ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_BACKEND_PATH", PathFinder.BackendDir);
202 // Add BeagleDaemonLib if it hasn't already been added.
203 bool found_daemon_lib = false;
204 foreach (Assembly assembly in assemblies) {
205 if (assembly.GetName ().Name == "BeagleDaemonLib") {
206 found_daemon_lib = true;
207 break;
211 if (!found_daemon_lib) {
212 try {
213 assemblies.Add (Assembly.LoadFrom (Path.Combine (PathFinder.PkgLibDir, "BeagleDaemonLib.dll")));
214 } catch (FileNotFoundException) {
215 Console.WriteLine ("WARNING: Could not find backend list.");
216 Environment.Exit (1);
220 foreach (Assembly assembly in assemblies) {
221 foreach (Type type in ReflectionFu.ScanAssemblyForInterface (assembly, typeof (Beagle.Daemon.IQueryable))) {
222 foreach (Beagle.Daemon.QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute (type, typeof (Beagle.Daemon.QueryableFlavor)))
223 backends.Add (flavor.Name);
227 if ( Directory.Exists (PathFinder.SystemIndexesDir)) {
228 foreach (DirectoryInfo index_dir in new DirectoryInfo (PathFinder.SystemIndexesDir).GetDirectories ())
229 backends.Add (index_dir.Name);
232 bool found_any = false;
234 Console.WriteLine ("Allowed backends:");
235 foreach (string name in backends) {
236 if (Conf.Daemon.DeniedBackends.Contains (name))
237 continue;
238 Console.WriteLine (" - {0}", name);
239 found_any = true;
242 if (! found_any)
243 Console.WriteLine (" (none)");
245 Console.WriteLine ();
247 found_any = false;
249 Console.WriteLine ("Denied backends:");
250 foreach (string name in Conf.Daemon.DeniedBackends) {
251 Console.WriteLine (" - {0}", name);
252 found_any = true;
255 if (! found_any)
256 Console.WriteLine (" (none)");