Browse Source

Begin DI for web ui

Change-Id: Iee6c747a565f08f35966e7a5889f7e04ab008adc
Reviewed-on: http://gerrit.dmdirc.com/3117
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes 10 years ago
parent
commit
f3c987edff

+ 6
- 15
src/com/dmdirc/addons/ui_web/StaticRequestHandler.java View File

26
 
26
 
27
 import java.io.IOException;
27
 import java.io.IOException;
28
 
28
 
29
+import javax.inject.Inject;
29
 import javax.servlet.ServletException;
30
 import javax.servlet.ServletException;
30
 import javax.servlet.http.HttpServletRequest;
31
 import javax.servlet.http.HttpServletRequest;
31
 import javax.servlet.http.HttpServletResponse;
32
 import javax.servlet.http.HttpServletResponse;
39
  */
40
  */
40
 public class StaticRequestHandler extends AbstractHandler {
41
 public class StaticRequestHandler extends AbstractHandler {
41
 
42
 
42
-    private ResourceManager rm;
43
-
44
-    private final WebInterfaceUI controller;
43
+    private final ResourceManager rm;
45
 
44
 
46
     /**
45
     /**
47
      * Create a new StaticRequestHandler.
46
      * Create a new StaticRequestHandler.
48
      *
47
      *
49
-     * @param controller UI Controller that owns this handler.
48
+     * @param rm Resource manager
50
      */
49
      */
51
-    public StaticRequestHandler(final WebInterfaceUI controller) {
52
-        this.controller = controller;
50
+    @Inject
51
+    public StaticRequestHandler(final ResourceManager rm) {
52
+        this.rm = rm;
53
     }
53
     }
54
 
54
 
55
-    /** {@inheritDoc} */
56
     @Override
55
     @Override
57
     public void handle(final String target, final HttpServletRequest request,
56
     public void handle(final String target, final HttpServletRequest request,
58
             final HttpServletResponse response, final int dispatch)
57
             final HttpServletResponse response, final int dispatch)
59
             throws IOException, ServletException {
58
             throws IOException, ServletException {
60
 
59
 
61
-        if (rm == null) {
62
-            try {
63
-                rm = controller.getPluginInfo().getResourceManager();
64
-            } catch (IOException ex) {
65
-                // Die horribly
66
-            }
67
-        }
68
-
69
         if (((request instanceof Request) ? (Request) request
60
         if (((request instanceof Request) ? (Request) request
70
                 : HttpConnection.getCurrentConnection().getRequest()).isHandled()) {
61
                 : HttpConnection.getCurrentConnection().getRequest()).isHandled()) {
71
             return;
62
             return;

+ 65
- 0
src/com/dmdirc/addons/ui_web/WebInterfaceModule.java View File

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.ui_web;
24
+
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.plugins.PluginInfo;
27
+
28
+import com.dmdirc.util.resourcemanager.ResourceManager;
29
+
30
+import java.io.IOException;
31
+import javax.inject.Qualifier;
32
+import javax.inject.Singleton;
33
+
34
+import dagger.Module;
35
+import dagger.Provides;
36
+
37
+@Module(injects={WebInterfaceUI.class, StaticRequestHandler.class}, addsTo=ClientModule.class)
38
+public class WebInterfaceModule {
39
+
40
+    private final PluginInfo pluginInfo;
41
+
42
+    public WebInterfaceModule(final PluginInfo pluginInfo) {
43
+        this.pluginInfo = pluginInfo;
44
+    }
45
+
46
+    @Qualifier
47
+    public @interface WebUIDomain {};
48
+
49
+    @Provides
50
+    @WebUIDomain
51
+    public String getSettingsDomain() {
52
+        return pluginInfo.getDomain();
53
+    }
54
+
55
+    @Singleton
56
+    @Provides
57
+    public ResourceManager getResourceManager() {
58
+        try {
59
+            return pluginInfo.getResourceManager();
60
+        } catch (IOException ex) {
61
+            throw new IllegalStateException("Die Horrible", ex);
62
+        }
63
+    }
64
+
65
+}

+ 6
- 56
src/com/dmdirc/addons/ui_web/WebInterfacePlugin.java View File

22
 
22
 
23
 package com.dmdirc.addons.ui_web;
23
 package com.dmdirc.addons.ui_web;
24
 
24
 
25
-import com.dmdirc.ServerManager;
26
-import com.dmdirc.interfaces.config.IdentityController;
27
 import com.dmdirc.interfaces.ui.UIController;
25
 import com.dmdirc.interfaces.ui.UIController;
28
 import com.dmdirc.plugins.Exported;
26
 import com.dmdirc.plugins.Exported;
29
 import com.dmdirc.plugins.PluginInfo;
27
 import com.dmdirc.plugins.PluginInfo;
30
-import com.dmdirc.plugins.PluginManager;
31
 import com.dmdirc.plugins.implementations.BasePlugin;
28
 import com.dmdirc.plugins.implementations.BasePlugin;
32
-import com.dmdirc.ui.WindowManager;
33
-import com.dmdirc.ui.core.components.StatusBarManager;
34
 
29
 
35
-import org.mortbay.jetty.Handler;
30
+import dagger.ObjectGraph;
36
 
31
 
37
 /**
32
 /**
38
  * The main web interface plugin.
33
  * The main web interface plugin.
39
  */
34
  */
40
 public class WebInterfacePlugin extends BasePlugin {
35
 public class WebInterfacePlugin extends BasePlugin {
41
 
36
 
42
-    /** Server manager to use. */
43
-    private final ServerManager serverManager;
44
-
45
-    /** The controller to read/write settings with. */
46
-    private final IdentityController identityController;
47
-
48
-    /** Plugin manager to use. */
49
-    private final PluginManager pluginManager;
50
-
51
-    /** This plugin's information object. */
52
-    private final PluginInfo pluginInfo;
53
-
54
-    /** Window management. */
55
-    private final WindowManager windowManager;
56
-
57
-    /** The status bar manager to use. */
58
-    private final StatusBarManager statusBarManager;
59
-
60
     /** The UI that we're using. */
37
     /** The UI that we're using. */
61
     private WebInterfaceUI controller;
38
     private WebInterfaceUI controller;
62
 
39
 
63
-    public WebInterfacePlugin(final ServerManager serverManager,
64
-            final IdentityController identityController, final PluginManager pluginManager,
65
-            final PluginInfo pluginInfo, final WindowManager windowManager,
66
-            final StatusBarManager statusBarManager) {
67
-        this.serverManager = serverManager;
68
-        this.identityController = identityController;
69
-        this.pluginManager = pluginManager;
70
-        this.pluginInfo = pluginInfo;
71
-        this.windowManager = windowManager;
72
-        this.statusBarManager = statusBarManager;
40
+    @Override
41
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
42
+        super.load(pluginInfo, graph);
43
+        setObjectGraph(graph.plus(new WebInterfaceModule(pluginInfo)));
73
     }
44
     }
74
 
45
 
75
     /** {@inheritDoc} */
46
     /** {@inheritDoc} */
76
     @Override
47
     @Override
77
     public void onLoad() {
48
     public void onLoad() {
78
-        if (controller == null) {
79
-            controller = new WebInterfaceUI(getDomain(),
80
-                    identityController, serverManager,
81
-                    pluginManager, pluginInfo, windowManager,
82
-                    statusBarManager);
83
-        }
84
-    }
85
-
86
-    /**
87
-     * Adds the specified handler to the WebInterface's web server.
88
-     *
89
-     * @param newHandler The handler to be added
90
-     */
91
-    public void addWebHandler(final Handler newHandler) {
92
-        if (controller == null) {
93
-            controller = new WebInterfaceUI(getDomain(),
94
-                    identityController, serverManager,
95
-                    pluginManager, pluginInfo, windowManager,
96
-                    statusBarManager);
97
-        }
98
-
99
-        controller.addWebHandler(newHandler);
49
+        getObjectGraph().get(WebInterfaceUI.class);
100
     }
50
     }
101
 
51
 
102
     /**
52
     /**

+ 9
- 14
src/com/dmdirc/addons/ui_web/WebInterfaceUI.java View File

25
 import com.dmdirc.Channel;
25
 import com.dmdirc.Channel;
26
 import com.dmdirc.Server;
26
 import com.dmdirc.Server;
27
 import com.dmdirc.ServerManager;
27
 import com.dmdirc.ServerManager;
28
+import com.dmdirc.addons.ui_web.WebInterfaceModule.WebUIDomain;
28
 import com.dmdirc.addons.ui_web.uicomponents.WebStatusBar;
29
 import com.dmdirc.addons.ui_web.uicomponents.WebStatusBar;
29
 import com.dmdirc.interfaces.config.IdentityController;
30
 import com.dmdirc.interfaces.config.IdentityController;
30
 import com.dmdirc.interfaces.ui.UIController;
31
 import com.dmdirc.interfaces.ui.UIController;
31
 import com.dmdirc.interfaces.ui.Window;
32
 import com.dmdirc.interfaces.ui.Window;
32
-import com.dmdirc.plugins.PluginInfo;
33
 import com.dmdirc.plugins.PluginManager;
33
 import com.dmdirc.plugins.PluginManager;
34
 import com.dmdirc.ui.WindowManager;
34
 import com.dmdirc.ui.WindowManager;
35
 import com.dmdirc.ui.core.components.StatusBarManager;
35
 import com.dmdirc.ui.core.components.StatusBarManager;
36
 
36
 
37
 import java.net.URI;
37
 import java.net.URI;
38
 
38
 
39
+import javax.inject.Inject;
40
+
39
 import org.mortbay.jetty.Handler;
41
 import org.mortbay.jetty.Handler;
40
 import org.mortbay.jetty.security.Constraint;
42
 import org.mortbay.jetty.security.Constraint;
41
 import org.mortbay.jetty.security.ConstraintMapping;
43
 import org.mortbay.jetty.security.ConstraintMapping;
56
     /** The dynamic request handler in use. */
58
     /** The dynamic request handler in use. */
57
     private final DynamicRequestHandler handler;
59
     private final DynamicRequestHandler handler;
58
 
60
 
59
-    /** The PluginInfo object for this plugin. */
60
-    private final PluginInfo pluginInfo;
61
-
62
     /** The plugin manager used to find other plugins. */
61
     /** The plugin manager used to find other plugins. */
63
     private final PluginManager pluginManager;
62
     private final PluginManager pluginManager;
64
 
63
 
69
      * @param identityController The controller to read/write settings with.
68
      * @param identityController The controller to read/write settings with.
70
      * @param serverManager The manager to use to find and create servers
69
      * @param serverManager The manager to use to find and create servers
71
      * @param pluginManager The manager to use to find other plugins
70
      * @param pluginManager The manager to use to find other plugins
72
-     * @param pluginInfo The information object for this UI's plugin.
73
      * @param coreWindowManager Window management
71
      * @param coreWindowManager Window management
74
      * @param statusBarManager The status bar manager.
72
      * @param statusBarManager The status bar manager.
73
+     * @param staticRequestHandler Status request handler
75
      */
74
      */
75
+    @Inject
76
     public WebInterfaceUI(
76
     public WebInterfaceUI(
77
-            final String domain,
77
+            @WebUIDomain final String domain,
78
             final IdentityController identityController,
78
             final IdentityController identityController,
79
             final ServerManager serverManager,
79
             final ServerManager serverManager,
80
             final PluginManager pluginManager,
80
             final PluginManager pluginManager,
81
-            final PluginInfo pluginInfo,
82
             final WindowManager coreWindowManager,
81
             final WindowManager coreWindowManager,
83
-            final StatusBarManager statusBarManager) {
82
+            final StatusBarManager statusBarManager,
83
+            final StaticRequestHandler staticRequestHandler) {
84
         super();
84
         super();
85
 
85
 
86
         this.pluginManager = pluginManager;
86
         this.pluginManager = pluginManager;
87
-        this.pluginInfo = pluginInfo;
88
 
87
 
89
         final SecurityHandler sh = new SecurityHandler();
88
         final SecurityHandler sh = new SecurityHandler();
90
         final Constraint constraint = new Constraint();
89
         final Constraint constraint = new Constraint();
106
         webServer.setHandlers(new Handler[]{
105
         webServer.setHandlers(new Handler[]{
107
             sh,
106
             sh,
108
             new RootRequestHandler(),
107
             new RootRequestHandler(),
109
-            new StaticRequestHandler(this),
108
+            staticRequestHandler,
110
             new DMDircRequestHandler(),
109
             new DMDircRequestHandler(),
111
             handler,
110
             handler,
112
         });
111
         });
130
         return handler;
129
         return handler;
131
     }
130
     }
132
 
131
 
133
-    public PluginInfo getPluginInfo() {
134
-        return pluginInfo;
135
-    }
136
-
137
     public PluginManager getPluginManager() {
132
     public PluginManager getPluginManager() {
138
         return pluginManager;
133
         return pluginManager;
139
     }
134
     }

Loading…
Cancel
Save