ソースを参照

EventBus logging for Script Plugin.

Also manually add a factory and a little reformatting.

Change-Id: I8139f957f77181d1422b542e65514329a139b2e6
Reviewed-on: http://gerrit.dmdirc.com/3707
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/07/3707/3
Greg Holmes 9年前
コミット
b47552af14

+ 1
- 4
src/com/dmdirc/addons/lagdisplay/ServerInfoDialog.java ファイルの表示

@@ -28,8 +28,6 @@ import com.dmdirc.ServerState;
28 28
 import com.dmdirc.addons.ui_swing.MainFrame;
29 29
 import com.dmdirc.addons.ui_swing.components.statusbar.StatusbarPanel;
30 30
 import com.dmdirc.addons.ui_swing.components.statusbar.StatusbarPopupWindow;
31
-import com.dmdirc.util.annotations.factory.Factory;
32
-import com.dmdirc.util.annotations.factory.Unbound;
33 31
 
34 32
 import java.util.List;
35 33
 
@@ -40,7 +38,6 @@ import javax.swing.JSeparator;
40 38
 /**
41 39
  * Shows information about all connected servers.
42 40
  */
43
-@Factory(inject = true, singleton = true)
44 41
 public class ServerInfoDialog extends StatusbarPopupWindow {
45 42
 
46 43
     /** A version number for this class. */
@@ -62,7 +59,7 @@ public class ServerInfoDialog extends StatusbarPopupWindow {
62 59
      */
63 60
     public ServerInfoDialog(
64 61
             final LagDisplayManager manager,
65
-            @Unbound final StatusbarPanel<JLabel> parent,
62
+            final StatusbarPanel<JLabel> parent,
66 63
             final MainFrame mainFrame,
67 64
             final ServerManager serverManager) {
68 65
         super(parent, mainFrame);

+ 56
- 0
src/com/dmdirc/addons/lagdisplay/ServerInfoDialogFactory.java ファイルの表示

@@ -0,0 +1,56 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.lagdisplay;
24
+
25
+import com.dmdirc.ServerManager;
26
+import com.dmdirc.addons.ui_swing.MainFrame;
27
+import com.dmdirc.addons.ui_swing.components.statusbar.StatusbarPanel;
28
+
29
+import javax.inject.Inject;
30
+import javax.inject.Singleton;
31
+import javax.swing.JLabel;
32
+
33
+/**
34
+ * Factory for {@link ServerInfoDialog}s.
35
+ */
36
+@Singleton
37
+public class ServerInfoDialogFactory {
38
+
39
+    private final LagDisplayManager manager;
40
+    private final MainFrame mainFrame;
41
+    private final ServerManager serverManager;
42
+
43
+    @Inject
44
+    public ServerInfoDialogFactory(final LagDisplayManager manager, final MainFrame mainFrame,
45
+            final ServerManager serverManager) {
46
+        this.manager = manager;
47
+        this.mainFrame = mainFrame;
48
+        this.serverManager = serverManager;
49
+    }
50
+
51
+    public ServerInfoDialog getServerInfoDialog(
52
+            final StatusbarPanel<JLabel> parent) {
53
+        return new ServerInfoDialog(manager, parent, mainFrame, serverManager);
54
+    }
55
+
56
+}

+ 13
- 4
src/com/dmdirc/addons/scriptplugin/ScriptCommand.java ファイルの表示

@@ -37,12 +37,15 @@ import com.dmdirc.interfaces.config.AggregateConfigProvider;
37 37
 import com.dmdirc.plugins.PluginDomain;
38 38
 import com.dmdirc.ui.input.AdditionalTabTargets;
39 39
 
40
+import com.google.common.eventbus.EventBus;
41
+
40 42
 import java.io.File;
41 43
 import java.io.FileNotFoundException;
42 44
 import java.io.FileWriter;
43 45
 import java.io.IOException;
44 46
 import java.util.Map;
45 47
 
48
+import javax.annotation.Nonnull;
46 49
 import javax.inject.Inject;
47 50
 import javax.script.ScriptEngineManager;
48 51
 import javax.script.ScriptException;
@@ -65,11 +68,14 @@ public class ScriptCommand extends Command implements IntelligentCommand {
65 68
     private final String scriptDirectory;
66 69
     /** Script manager to handle scripts. */
67 70
     private final ScriptManager scriptManager;
71
+    /** The event bus to post errors to. */
72
+    private final EventBus eventBus;
68 73
 
69 74
     /**
70 75
      * Creates a new instance of this command.
71 76
      *
72 77
      * @param scriptManager       Used to manage scripts
78
+     * @param eventBus            The event bus to post errors to
73 79
      * @param globalConfig        Global config
74 80
      * @param commandController   The controller to use for command information.
75 81
      * @param domain              This plugin's settings domain
@@ -78,6 +84,7 @@ public class ScriptCommand extends Command implements IntelligentCommand {
78 84
      */
79 85
     @Inject
80 86
     public ScriptCommand(final ScriptManager scriptManager,
87
+            final EventBus eventBus,
81 88
             @Directory(ScriptModule.SCRIPTS) final String scriptDirectory,
82 89
             @GlobalConfig final AggregateConfigProvider globalConfig,
83 90
             final CommandController commandController,
@@ -89,10 +96,11 @@ public class ScriptCommand extends Command implements IntelligentCommand {
89 96
         this.scriptEngineManager = scriptEngineManager;
90 97
         this.scriptDirectory = scriptDirectory;
91 98
         this.scriptManager = scriptManager;
99
+        this.eventBus = eventBus;
92 100
     }
93 101
 
94 102
     @Override
95
-    public void execute(final FrameContainer origin, final CommandArguments args,
103
+    public void execute(@Nonnull final FrameContainer origin, final CommandArguments args,
96 104
             final CommandContext context) {
97 105
         final String[] sargs = args.getArguments();
98 106
 
@@ -127,12 +135,13 @@ public class ScriptCommand extends Command implements IntelligentCommand {
127 135
                         final String baseFile = scriptDirectory + '/'
128 136
                                 + globalConfig.getOption(domain, "eval.baseFile");
129 137
                         if (new File(baseFile).exists()) {
130
-                            wrapper = new ScriptEngineWrapper(scriptEngineManager, baseFile);
138
+                            wrapper = new ScriptEngineWrapper(scriptEngineManager, eventBus,
139
+                                    baseFile);
131 140
                         } else {
132
-                            wrapper = new ScriptEngineWrapper(scriptEngineManager, null);
141
+                            wrapper = new ScriptEngineWrapper(scriptEngineManager, eventBus, null);
133 142
                         }
134 143
                     } else {
135
-                        wrapper = new ScriptEngineWrapper(scriptEngineManager, null);
144
+                        wrapper = new ScriptEngineWrapper(scriptEngineManager, eventBus, null);
136 145
                     }
137 146
                     wrapper.getScriptEngine().put("cmd_origin", origin);
138 147
                     wrapper.getScriptEngine().put("cmd_isSilent", args.isSilent());

+ 13
- 6
src/com/dmdirc/addons/scriptplugin/ScriptEngineWrapper.java ファイルの表示

@@ -22,10 +22,11 @@
22 22
 
23 23
 package com.dmdirc.addons.scriptplugin;
24 24
 
25
+import com.dmdirc.events.UserErrorEvent;
25 26
 import com.dmdirc.logger.ErrorLevel;
26
-import com.dmdirc.logger.Logger;
27 27
 
28 28
 import com.google.common.base.Preconditions;
29
+import com.google.common.eventbus.EventBus;
29 30
 
30 31
 import java.io.File;
31 32
 import java.io.FileNotFoundException;
@@ -42,6 +43,8 @@ import javax.script.ScriptException;
42 43
  */
43 44
 public class ScriptEngineWrapper {
44 45
 
46
+    /** The event bus to post errors to. */
47
+    private final EventBus eventBus;
45 48
     /** The Script Engine this wrapper wraps */
46 49
     private ScriptEngine engine;
47 50
     /** The File this script is from */
@@ -55,14 +58,17 @@ public class ScriptEngineWrapper {
55 58
      * Create a new ScriptEngineWrapper
56 59
      *
57 60
      * @param scriptEngineManager Manager to get script engines
61
+     * @param eventBus            The event bus to post errors to
58 62
      * @param filename            Filename of script
59 63
      *
60 64
      * @throws java.io.FileNotFoundException If file is not found
61 65
      * @throws javax.script.ScriptException  If there was an error during creation
62 66
      */
63 67
     protected ScriptEngineWrapper(final ScriptEngineManager scriptEngineManager,
64
-            final String filename) throws FileNotFoundException, ScriptException {
68
+            final EventBus eventBus, final String filename)
69
+            throws FileNotFoundException, ScriptException {
65 70
         Preconditions.checkNotNull(filename, "File cannot be null");
71
+        this.eventBus = eventBus;
66 72
         this.scriptEngineManager = scriptEngineManager;
67 73
         file = new File(filename);
68 74
 
@@ -136,8 +142,9 @@ public class ScriptEngineWrapper {
136 142
             // and do nothing rather that add an error every time a method is called
137 143
             // that doesn't exist (such as the action_* methods)
138 144
         } catch (ScriptException e) {
139
-            Logger.userError(ErrorLevel.LOW, "Error calling '" + functionName + "' in '" + file.
140
-                    getPath() + "': " + e.getMessage(), e);
145
+            eventBus.post(new UserErrorEvent(ErrorLevel.LOW, e,
146
+                    "Error calling '" + functionName + "' in '" + file.getPath() + "': " +
147
+                            e.getMessage(), ""));
141 148
         }
142 149
     }
143 150
 
@@ -156,8 +163,8 @@ public class ScriptEngineWrapper {
156 163
             // Tell it that it has been rehashed
157 164
             callFunction("onRehashSucess");
158 165
         } catch (ScriptException e) {
159
-            Logger.userError(ErrorLevel.LOW, "Reloading '" + file.getPath() + "' failed: " + e.
160
-                    getMessage(), e);
166
+            eventBus.post(new UserErrorEvent(ErrorLevel.LOW, e,
167
+                    "Reloading '" + file.getPath() + "' failed: " + e.getMessage(), ""));
161 168
             // Tell it that its rehash failed
162 169
             callFunction("onRehashFailed", e);
163 170
             return false;

+ 11
- 5
src/com/dmdirc/addons/scriptplugin/ScriptManager.java ファイルの表示

@@ -23,8 +23,10 @@
23 23
 package com.dmdirc.addons.scriptplugin;
24 24
 
25 25
 import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
26
+import com.dmdirc.events.UserErrorEvent;
26 27
 import com.dmdirc.logger.ErrorLevel;
27
-import com.dmdirc.logger.Logger;
28
+
29
+import com.google.common.eventbus.EventBus;
28 30
 
29 31
 import java.io.File;
30 32
 import java.io.FileNotFoundException;
@@ -46,12 +48,16 @@ public class ScriptManager {
46 48
     private final String scriptDirectory;
47 49
     /** Store Script State Name,Engine */
48 50
     private final Map<String, ScriptEngineWrapper> scripts = new HashMap<>();
51
+    /** The event bus to post events to. */
52
+    private final EventBus eventBus;
49 53
 
50 54
     @Inject
51 55
     public ScriptManager(final ScriptEngineManager scriptEngineManager,
52
-            @Directory(ScriptModule.SCRIPTS) final String scriptDirectory) {
56
+            @Directory(ScriptModule.SCRIPTS) final String scriptDirectory,
57
+            final EventBus eventBus) {
53 58
         this.scriptEngineManager = scriptEngineManager;
54 59
         this.scriptDirectory = scriptDirectory;
60
+        this.eventBus = eventBus;
55 61
     }
56 62
 
57 63
     /**
@@ -96,11 +102,11 @@ public class ScriptManager {
96 102
         if (!scripts.containsKey(scriptFilename)) {
97 103
             try {
98 104
                 final ScriptEngineWrapper wrapper = new ScriptEngineWrapper(scriptEngineManager,
99
-                        scriptFilename);
105
+                        eventBus, scriptFilename);
100 106
                 scripts.put(scriptFilename, wrapper);
101 107
             } catch (FileNotFoundException | ScriptException e) {
102
-                Logger.userError(ErrorLevel.LOW, "Error loading '" + scriptFilename + "': " + e.
103
-                        getMessage(), e);
108
+                eventBus.post(new UserErrorEvent(ErrorLevel.LOW, e,
109
+                        "Error loading '" + scriptFilename + "': " + e.getMessage(), ""));
104 110
                 return false;
105 111
             }
106 112
         }

+ 6
- 9
src/com/dmdirc/addons/scriptplugin/ScriptPluginManager.java ファイルの表示

@@ -26,9 +26,8 @@ import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
26 26
 import com.dmdirc.events.DMDircEvent;
27 27
 import com.dmdirc.events.PluginLoadedEvent;
28 28
 import com.dmdirc.events.PluginUnloadedEvent;
29
-import com.dmdirc.interfaces.ActionController;
29
+import com.dmdirc.events.UserErrorEvent;
30 30
 import com.dmdirc.logger.ErrorLevel;
31
-import com.dmdirc.logger.Logger;
32 31
 
33 32
 import com.google.common.eventbus.EventBus;
34 33
 import com.google.common.eventbus.Subscribe;
@@ -47,18 +46,15 @@ import javax.script.ScriptEngineManager;
47 46
 public class ScriptPluginManager {
48 47
 
49 48
     private final EventBus eventBus;
50
-    private final ActionController actionController;
51 49
     private final String scriptDir;
52 50
     private final ScriptManager scriptManager;
53 51
     private final TypedProperties globalVariables;
54 52
 
55 53
     @Inject
56 54
     public ScriptPluginManager(final EventBus eventBus,
57
-            final ActionController actionController,
58 55
             @Directory(ScriptModule.SCRIPTS) final String scriptDir,
59 56
             final ScriptManager scriptManager,
60 57
             final ScriptEngineManager scriptEngineManager) {
61
-        this.actionController = actionController;
62 58
         this.scriptDir = scriptDir;
63 59
         this.scriptManager = scriptManager;
64 60
         this.eventBus = eventBus;
@@ -81,8 +77,9 @@ public class ScriptPluginManager {
81 77
             try (FileInputStream fis = new FileInputStream(savedVariables)) {
82 78
                 globalVariables.load(fis);
83 79
             } catch (IOException e) {
84
-                Logger.userError(ErrorLevel.LOW, "Error reading savedVariables from '"
85
-                        + savedVariables.getPath() + "': " + e.getMessage(), e);
80
+                eventBus.post(new UserErrorEvent(ErrorLevel.LOW, e,
81
+                        "Error reading savedVariables from '" + savedVariables.getPath() + "': "
82
+                                + e.getMessage(), ""));
86 83
             }
87 84
         }
88 85
     }
@@ -94,8 +91,8 @@ public class ScriptPluginManager {
94 91
         try (FileOutputStream fos = new FileOutputStream(savedVariables)) {
95 92
             globalVariables.store(fos, "# DMDirc Script Plugin savedVariables");
96 93
         } catch (IOException e) {
97
-            Logger.userError(ErrorLevel.LOW, "Error reading savedVariables to '" + savedVariables.
98
-                    getPath() + "': " + e.getMessage(), e);
94
+            eventBus.post(new UserErrorEvent(ErrorLevel.LOW, e,"Error reading savedVariables to '"
95
+                    + savedVariables.getPath() + "': " + e.getMessage(), ""));
99 96
         }
100 97
     }
101 98
 

読み込み中…
キャンセル
保存