瀏覽代碼

Initialise the IdentityManager in Dagger.

No need for Main to do it - might as well get a fully-initialised
manager when we ask for one.

Change-Id: I55ed29bbc0270a5c610184572e58b0270055e0bc
Reviewed-on: http://gerrit.dmdirc.com/2690
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8rc1
Chris Smith 10 年之前
父節點
當前提交
f0d929f391

+ 71
- 5
src/com/dmdirc/ClientModule.java 查看文件

@@ -24,11 +24,22 @@ package com.dmdirc;
24 24
 
25 25
 import com.dmdirc.actions.ActionManager;
26 26
 import com.dmdirc.commandline.CommandLineOptionsModule;
27
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
28
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
27 29
 import com.dmdirc.config.IdentityManager;
30
+import com.dmdirc.config.InvalidIdentityFileException;
28 31
 import com.dmdirc.interfaces.ActionController;
29 32
 import com.dmdirc.interfaces.IdentityController;
30 33
 import com.dmdirc.interfaces.LifecycleController;
34
+import com.dmdirc.logger.ErrorLevel;
35
+import com.dmdirc.logger.Logger;
36
+import com.dmdirc.plugins.PluginManager;
37
+import com.dmdirc.ui.WarningDialog;
31 38
 
39
+import java.awt.GraphicsEnvironment;
40
+import java.io.File;
41
+import java.text.SimpleDateFormat;
42
+import java.util.Date;
32 43
 import javax.inject.Singleton;
33 44
 
34 45
 import dagger.Module;
@@ -43,14 +54,23 @@ public class ClientModule {
43 54
     /**
44 55
      * Provides an identity manager for the client.
45 56
      *
46
-     * @return An unitialised {@link IdentityManager}.
57
+     * @param directory The directory to load settings from.
58
+     * @return An initialised {@link IdentityManager}.
47 59
      */
48 60
     @Provides
49 61
     @Singleton
50
-    public IdentityManager getIdentityManager() {
51
-        final IdentityManager identityManager = new IdentityManager();
62
+    public IdentityManager getIdentityManager(
63
+            @Directory(DirectoryType.BASE) final String directory) {
64
+        final IdentityManager identityManager = new IdentityManager(directory);
52 65
         IdentityManager.setIdentityManager(identityManager);
53 66
         identityManager.loadVersionIdentity();
67
+
68
+        try {
69
+            identityManager.initialise();
70
+        } catch (InvalidIdentityFileException ex) {
71
+            handleInvalidConfigFile(identityManager, directory);
72
+        }
73
+
54 74
         return identityManager;
55 75
     }
56 76
 
@@ -68,11 +88,12 @@ public class ClientModule {
68 88
     /**
69 89
      * Provides a parser factory.
70 90
      *
91
+     * @param pluginManager The plugin manager to use to find parsers.
71 92
      * @return A parser factory for use in the client.
72 93
      */
73 94
     @Provides
74
-    public ParserFactory getParserFactory() {
75
-        return new ParserFactory(Main.mainInstance.getPluginManager());
95
+    public ParserFactory getParserFactory(final PluginManager pluginManager) {
96
+        return new ParserFactory(pluginManager);
76 97
     }
77 98
 
78 99
     /**
@@ -112,4 +133,49 @@ public class ClientModule {
112 133
         return controller;
113 134
     }
114 135
 
136
+    /**
137
+     * Called when the global config cannot be loaded due to an error. This
138
+     * method informs the user of the problem and installs a new default config
139
+     * file, backing up the old one.
140
+     *
141
+     * @param identityManager The identity manager to re-initialise after installing defaults.
142
+     * @param configdir The directory to extract default settings into.
143
+     */
144
+    private void handleInvalidConfigFile(final IdentityManager identityManager, final String configdir) {
145
+        final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
146
+
147
+        final String message = "DMDirc has detected that your config file "
148
+                + "has become corrupted.<br><br>DMDirc will now backup "
149
+                + "your current config and try restarting with a default "
150
+                + "config.<br><br>Your old config will be saved as:<br>"
151
+                + "dmdirc.config." + date;
152
+
153
+        if (!GraphicsEnvironment.isHeadless()) {
154
+            new WarningDialog("Invalid Config File", message).displayBlocking();
155
+        }
156
+
157
+        // Let command-line users know what is happening.
158
+        System.out.println(message.replace("<br>", "\n"));
159
+
160
+        final File configFile = new File(configdir + "dmdirc.config");
161
+        final File newConfigFile = new File(configdir + "dmdirc.config." + date);
162
+
163
+        if (configFile.renameTo(newConfigFile)) {
164
+            try {
165
+                identityManager.initialise();
166
+            } catch (InvalidIdentityFileException iife) {
167
+                // This shouldn't happen!
168
+                Logger.appError(ErrorLevel.FATAL, "Unable to load global config", iife);
169
+            }
170
+        } else {
171
+            final String newMessage = "DMDirc was unable to rename the "
172
+                    + "global config file and is unable to fix this issue.";
173
+            if (!GraphicsEnvironment.isHeadless()) {
174
+                new WarningDialog("Invalid Config File", newMessage).displayBlocking();
175
+            }
176
+            System.out.println(newMessage.replace("<br>", "\n"));
177
+            System.exit(1);
178
+        }
179
+    }
180
+
115 181
 }

+ 2
- 53
src/com/dmdirc/Main.java 查看文件

@@ -33,7 +33,6 @@ import com.dmdirc.commandparser.CommandLoader;
33 33
 import com.dmdirc.commandparser.CommandManager;
34 34
 import com.dmdirc.config.ConfigManager;
35 35
 import com.dmdirc.config.IdentityManager;
36
-import com.dmdirc.config.InvalidIdentityFileException;
37 36
 import com.dmdirc.interfaces.ui.UIController;
38 37
 import com.dmdirc.logger.DMDircExceptionHandler;
39 38
 import com.dmdirc.logger.ErrorLevel;
@@ -52,9 +51,7 @@ import com.dmdirc.util.resourcemanager.ResourceManager;
52 51
 import java.awt.GraphicsEnvironment;
53 52
 import java.io.File;
54 53
 import java.io.IOException;
55
-import java.text.SimpleDateFormat;
56 54
 import java.util.Collection;
57
-import java.util.Date;
58 55
 import java.util.HashSet;
59 56
 import java.util.List;
60 57
 import java.util.Map;
@@ -143,6 +140,8 @@ public class Main implements LifecycleController {
143 140
      */
144 141
     @SuppressWarnings("PMD.AvoidCatchingThrowable")
145 142
     public static void main(final String[] args) {
143
+        Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
144
+
146 145
         try {
147 146
             ObjectGraph graph = ObjectGraph.create(
148 147
                     new ClientModule(),
@@ -161,14 +160,6 @@ public class Main implements LifecycleController {
161 160
      * @param args The command line arguments
162 161
      */
163 162
     public void init() {
164
-        Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
165
-
166
-        try {
167
-            identityManager.initialise(configdir);
168
-        } catch (InvalidIdentityFileException iife) {
169
-            handleInvalidConfigFile();
170
-        }
171
-
172 163
         UpdateChecker.init(this);
173 164
 
174 165
         MessageSinkManager.getManager().loadDefaultSinks();
@@ -276,48 +267,6 @@ public class Main implements LifecycleController {
276 267
         }
277 268
     }
278 269
 
279
-    /**
280
-     * Called when the global config cannot be loaded due to an error. This
281
-     * method informs the user of the problem and installs a new default config
282
-     * file, backing up the old one.
283
-     */
284
-    private void handleInvalidConfigFile() {
285
-        final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
286
-
287
-        final String message = "DMDirc has detected that your config file "
288
-                + "has become corrupted.<br><br>DMDirc will now backup "
289
-                + "your current config and try restarting with a default "
290
-                + "config.<br><br>Your old config will be saved as:<br>"
291
-                + "dmdirc.config." + date;
292
-
293
-        if (!GraphicsEnvironment.isHeadless()) {
294
-            new WarningDialog("Invalid Config File", message).displayBlocking();
295
-        }
296
-
297
-        // Let command-line users know what is happening.
298
-        System.out.println(message.replace("<br>", "\n"));
299
-
300
-        final File configFile = new File(configdir + "dmdirc.config");
301
-        final File newConfigFile = new File(configdir + "dmdirc.config." + date);
302
-
303
-        if (configFile.renameTo(newConfigFile)) {
304
-            try {
305
-                identityManager.initialise(configdir);
306
-            } catch (InvalidIdentityFileException iife2) {
307
-                // This shouldn't happen!
308
-                Logger.appError(ErrorLevel.FATAL, "Unable to load global config", iife2);
309
-            }
310
-        } else {
311
-            final String newMessage = "DMDirc was unable to rename the "
312
-                    + "global config file and is unable to fix this issue.";
313
-            if (!GraphicsEnvironment.isHeadless()) {
314
-                new WarningDialog("Invalid Config File", newMessage).displayBlocking();
315
-            }
316
-            System.out.println(newMessage.replace("<br>", "\n"));
317
-            System.exit(1);
318
-        }
319
-    }
320
-
321 270
     /**
322 271
      * Ensures that there is at least one provider of the specified
323 272
      * service type by extracting matching core plugins. Plugins must be named

+ 8
- 4
src/com/dmdirc/config/IdentityManager.java 查看文件

@@ -87,8 +87,13 @@ public class IdentityManager implements IdentityFactory, IdentityController {
87 87
     /** The config manager used for global settings. */
88 88
     private ConfigManager globalconfig;
89 89
 
90
-    /** Creates a new instance of IdentityManager. */
91
-    public IdentityManager() {
90
+    /**
91
+     * Creates a new instance of IdentityManager.
92
+     *
93
+     * @param directory The BASE config directory.
94
+     */
95
+    public IdentityManager(final String directory) {
96
+        this.configDirectory = directory;
92 97
     }
93 98
 
94 99
     /** {@inheritDoc} */
@@ -99,8 +104,7 @@ public class IdentityManager implements IdentityFactory, IdentityController {
99 104
 
100 105
     /** {@inheritDoc} */
101 106
     @Override
102
-    public void initialise(final String configDirectory) throws InvalidIdentityFileException {
103
-        this.configDirectory = configDirectory;
107
+    public void initialise() throws InvalidIdentityFileException {
104 108
         identities.clear();
105 109
 
106 110
         loadVersionIdentity();

+ 1
- 2
src/com/dmdirc/interfaces/IdentityController.java 查看文件

@@ -94,11 +94,10 @@ public interface IdentityController {
94 94
     /**
95 95
      * Loads all identity files.
96 96
      *
97
-     * @param configDirectory Config Directory.
98 97
      * @throws InvalidIdentityFileException If there is an error with the config
99 98
      * file.
100 99
      */
101
-    void initialise(final String configDirectory) throws InvalidIdentityFileException;
100
+    void initialise() throws InvalidIdentityFileException;
102 101
 
103 102
     /**
104 103
      * Get the config directory used by this identity controller.

+ 2
- 0
src/com/dmdirc/plugins/PluginManager.java 查看文件

@@ -50,10 +50,12 @@ import java.util.List;
50 50
 import java.util.Map;
51 51
 
52 52
 import javax.inject.Inject;
53
+import javax.inject.Singleton;
53 54
 
54 55
 /**
55 56
  * Searches for and manages plugins and services.
56 57
  */
58
+@Singleton
57 59
 public class PluginManager implements ActionListener, ServiceManager {
58 60
 
59 61
     /** List of known plugins' file names to their corresponding {@link PluginInfo} objects. */

+ 13
- 12
test/com/dmdirc/TestMain.java 查看文件

@@ -46,7 +46,7 @@ public class TestMain extends Main {
46 46
     @Override
47 47
     public void init() {
48 48
         try {
49
-            IdentityManager.getIdentityManager().initialise(configdir);
49
+            identityManager.initialise();
50 50
         } catch (InvalidIdentityFileException ex) {
51 51
             // If a bad config dir exists, try to continue anyway, maybe the
52 52
             // test doesn't need it.
@@ -73,17 +73,6 @@ public class TestMain extends Main {
73 73
      */
74 74
     public static Main getTestMain() {
75 75
         if (instance == null) {
76
-            // TODO: Tests probably shouldn't rely on a config dir... Who knows
77
-            //       what the user has done with their config.
78
-            final IdentityManager identityManager = new IdentityManager();
79
-            IdentityManager.setIdentityManager(identityManager);
80
-            IdentityManager.getIdentityManager().loadVersionIdentity();
81
-
82
-            final ServerManager serverManager = mock(ServerManager.class);
83
-
84
-            final ActionManager actionManager = new ActionManager(serverManager, identityManager);
85
-            ActionManager.setActionManager(actionManager);
86
-
87 76
             try {
88 77
                 File tempFile = File.createTempFile("dmdirc", "test");
89 78
                 tempFile.delete();
@@ -92,6 +81,18 @@ public class TestMain extends Main {
92 81
 
93 82
                 final String configDirectory = tempFile.getAbsolutePath() + File.separator;
94 83
                 final String pluginDirectory = configDirectory + "plugins" + File.separator;
84
+
85
+                // TODO: Tests probably shouldn't rely on a config dir... Who knows
86
+                //       what the user has done with their config.
87
+                final IdentityManager identityManager = new IdentityManager(configDirectory);
88
+                IdentityManager.setIdentityManager(identityManager);
89
+                IdentityManager.getIdentityManager().loadVersionIdentity();
90
+
91
+                final ServerManager serverManager = mock(ServerManager.class);
92
+
93
+                final ActionManager actionManager = new ActionManager(serverManager, identityManager);
94
+                ActionManager.setActionManager(actionManager);
95
+
95 96
                 final PluginManager pluginManager = new PluginManager(identityManager, actionManager, pluginDirectory);
96 97
 
97 98
                 instance = new TestMain(identityManager, serverManager,

Loading…
取消
儲存