Source code: irate/plugin/PluginManager.java
1 // Copyright 2003 Stephen Blackheath
2
3 package irate.plugin;
4
5 import java.io.*;
6 import java.util.Enumeration;
7 import java.util.Hashtable;
8 import java.util.List;
9 import java.util.Vector;
10 import nanoxml.*;
11
12 import irate.plugin.lircremote.LircRemoteControlPlugin;
13
14 /**
15 * Manager to manage the list of plugins.
16 *
17 * @author Stephen Blackheath
18 */
19 public class PluginManager
20 {
21 private PluginApplication app;
22 private File configDir;
23
24 private Vector plugins;
25
26 public PluginManager(PluginApplication app, File configDir)
27 {
28 this.app = app;
29 this.configDir = configDir;
30
31 loadPlugins();
32 }
33
34 public PluginApplication getApp()
35 {
36 return app;
37 }
38
39 public List getPlugins()
40 {
41 return plugins;
42 }
43
44 /**
45 * This may be extended to load plugins from files, or wherever, but currently
46 * it's hard-coded.
47 */
48 private void loadPlugins()
49 {
50 plugins = new Vector();
51 plugins.add(new LircRemoteControlPlugin());
52 try {
53 loadConfig();
54 }
55 catch (IOException e) {
56 }
57 }
58
59 private File getConfigFile()
60 {
61 return new File(configDir, "irate.xml");
62 }
63
64 private File getConfigFileTemporary()
65 {
66 return new File(configDir, "irate.xml~");
67 }
68
69 private void loadConfig()
70 throws IOException
71 {
72 FileInputStream fis = new FileInputStream(getConfigFile());
73 loadConfig(new FileInputStream(getConfigFile()));
74 }
75
76 private void loadConfig(InputStream is)
77 throws IOException
78 {
79 XMLElement docElt = new XMLElement(new Hashtable(), false, false);
80 docElt.parseFromReader(new InputStreamReader(is));
81
82 Enumeration enum = docElt.enumerateChildren();
83 while(enum.hasMoreElements()) {
84 XMLElement elt = (XMLElement)enum.nextElement();
85 if (elt.getName().equals("plugin")) {
86 String identifier = elt.getStringAttribute("id");
87 if (identifier != null)
88 for (int i = 0; i < plugins.size(); i++) {
89 Plugin plugin = (Plugin) plugins.get(i);
90 if (plugin.getIdentifier().equals(identifier)) {
91 plugin.parseConfig(elt);
92 String attached = elt.getStringAttribute("attached");
93 if (attached != null && attached.equals("true"))
94 plugin.attach(app);
95 break;
96 }
97 }
98 }
99 }
100 }
101
102 /**
103 * Save the configuration of all the plugins.
104 */
105 public void saveConfig()
106 throws IOException
107 {
108 XMLElement docElt = new XMLElement(new Hashtable(), false, false);
109 docElt.setName("irate");
110 for (int i = 0; i < plugins.size(); i++) {
111 Plugin plugin = (Plugin) plugins.get(i);
112 XMLElement elt = new XMLElement(new Hashtable(), false, false);
113 elt.setName("plugin");
114 docElt.addChild(elt);
115 elt.setAttribute("id", plugin.getIdentifier());
116 elt.setAttribute("attached", plugin.isAttached()?"true":"false");
117 plugin.formatConfig(elt);
118 }
119 FileWriter fw = new FileWriter(getConfigFileTemporary());
120 try {
121 fw.write("<?xml version=\"1.0\"?>\n");
122 fw.write(docElt.toString());
123 fw.write("\n");
124 fw.close();
125 fw = null;
126 // If we wrote the file successfully, then rename the temporary
127 // file to the real name of the configuration file. This makes
128 // the writing of the new file effectively atomic.
129 if (!getConfigFileTemporary().renameTo(getConfigFile())) {
130 getConfigFile().delete();
131 if (!getConfigFileTemporary().renameTo(getConfigFile()))
132 throw new IOException("Failed to rename "+getConfigFileTemporary()+" to "+getConfigFile());
133 }
134 }
135 finally {
136 if (fw != null)
137 fw.close();
138 }
139 }
140 }
141