Преглед изворни кода

Add an AliasLoader and associated plumbing.

Introduce a global lifecycle component interface which provides a
way to start/stop components, and will help with The Quitting Problem™
when we get around to it.

Change-Id: I6d43f354e90e8206bea3f61a74d09728c9cac5aa
Reviewed-on: http://gerrit.dmdirc.com/3520
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
pull/1/head
Chris Smith пре 10 година
родитељ
комит
bd3583313c

+ 54
- 0
src/com/dmdirc/commandparser/aliases/AliasLifecycleManager.java Прегледај датотеку

@@ -0,0 +1,54 @@
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.commandparser.aliases;
24
+
25
+import com.dmdirc.interfaces.SystemLifecycleComponent;
26
+
27
+import javax.inject.Inject;
28
+import javax.inject.Singleton;
29
+
30
+/**
31
+ * Manages the lifecycle of the alias system, loading at startup and saving at shutdown.
32
+ */
33
+@Singleton
34
+public class AliasLifecycleManager implements SystemLifecycleComponent {
35
+
36
+    /** The loader to use to load/save aliases. */
37
+    private final AliasLoader loader;
38
+
39
+    @Inject
40
+    public AliasLifecycleManager(final AliasLoader loader) {
41
+        this.loader = loader;
42
+    }
43
+
44
+    @Override
45
+    public void startUp() {
46
+        loader.load();
47
+    }
48
+
49
+    @Override
50
+    public void shutDown() {
51
+        loader.save();
52
+    }
53
+
54
+}

+ 57
- 0
src/com/dmdirc/commandparser/aliases/AliasLoader.java Прегледај датотеку

@@ -0,0 +1,57 @@
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.commandparser.aliases;
24
+
25
+import javax.inject.Inject;
26
+
27
+/**
28
+ * Loads aliases from a store into a manager.
29
+ */
30
+public class AliasLoader {
31
+
32
+    private final AliasManager manager;
33
+    private final AliasStore store;
34
+
35
+    @Inject
36
+    public AliasLoader(final AliasManager manager, final AliasStore store) {
37
+        this.manager = manager;
38
+        this.store = store;
39
+    }
40
+
41
+    /**
42
+     * Loads aliases from the store into the manager.
43
+     */
44
+    public void load() {
45
+        for (Alias alias : store.readAliases()) {
46
+            manager.addAlias(alias);
47
+        }
48
+    }
49
+
50
+    /**
51
+     * Saves aliases from the manager to the store.
52
+     */
53
+    public void save() {
54
+        store.writeAliases(manager.getAliases());
55
+    }
56
+
57
+}

+ 43
- 0
src/com/dmdirc/interfaces/SystemLifecycleComponent.java Прегледај датотеку

@@ -0,0 +1,43 @@
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.interfaces;
24
+
25
+/**
26
+ * A component that participates in or needs to know about the system lifecycle.
27
+ */
28
+public interface SystemLifecycleComponent {
29
+
30
+    /**
31
+     * Called when the system or component is starting up.
32
+     */
33
+    void startUp();
34
+
35
+    /**
36
+     * Called when the system or component is shutting down.
37
+     * <p>
38
+     * Any resources or threads used by the implementing class must be released before returning
39
+     * from this method.
40
+     */
41
+    void shutDown();
42
+
43
+}

Loading…
Откажи
Сачувај