Procházet zdrojové kódy

Deprecated, move, and generally mess around with various Resource Manager methods

Issue 403

git-svn-id: http://svn.dmdirc.com/trunk@3455 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith před 16 roky
rodič
revize
491f432806

+ 2
- 2
src/com/dmdirc/commandline/CommandLineParser.java Zobrazit soubor

@@ -27,7 +27,7 @@ import com.dmdirc.util.IrcAddress;
27 27
 import com.dmdirc.Main;
28 28
 import com.dmdirc.config.IdentityManager;
29 29
 import com.dmdirc.updater.components.LauncherComponent;
30
-import com.dmdirc.util.resourcemanager.ResourceManager;
30
+import com.dmdirc.util.resourcemanager.DMDircResourceManager;
31 31
 
32 32
 import java.rmi.RemoteException;
33 33
 import java.util.ArrayList;
@@ -203,7 +203,7 @@ public class CommandLineParser {
203 203
             launcherVersion = param;
204 204
             break;
205 205
         case 'p':
206
-            doDirectory(ResourceManager.getCurrentWorkingDirectory());
206
+            doDirectory(DMDircResourceManager.getCurrentWorkingDirectory());
207 207
             break;
208 208
         case 'r':
209 209
             disablereporting = true;

+ 6
- 13
src/com/dmdirc/updater/components/ClientComponent.java Zobrazit soubor

@@ -24,7 +24,7 @@ package com.dmdirc.updater.components;
24 24
 
25 25
 import com.dmdirc.Main;
26 26
 import com.dmdirc.updater.UpdateComponent;
27
-import com.dmdirc.util.resourcemanager.ResourceManager;
27
+import com.dmdirc.util.resourcemanager.DMDircResourceManager;
28 28
 
29 29
 import java.io.File;
30 30
 
@@ -66,18 +66,17 @@ public class ClientComponent implements UpdateComponent {
66 66
         tmpFile.renameTo(targetFile);
67 67
         
68 68
         if (!LauncherComponent.isUsingLauncher()) {
69
-            final ResourceManager.Type type = ResourceManager.getResourceManagerType();
70 69
             final String message;
71
-            if (type == ResourceManager.Type.JAR) {
70
+            if (DMDircResourceManager.isRunningFromJar()) {
72 71
                 message = "A new version of DMDirc has been downloaded, but as you\n"
73 72
                     + "do not seem to be using the DMDirc launcher, it will\n"
74 73
                     + "not be installed automatically.\n\n"
75 74
                     + "To install this update manually, please replace the\n"
76 75
                     + "existing DMDirc.jar file, located at:\n"
77
-                    + " " + ResourceManager.getCurrentWorkingDirectory() + "\n"
76
+                    + " " + DMDircResourceManager.getCurrentWorkingDirectory() + "\n"
78 77
                     + "with the following file:\n"
79 78
                     + "  " + targetFile.getAbsolutePath();
80
-            } else if (type == ResourceManager.Type.FILE) {
79
+            } else {
81 80
                 message = "A new version of DMDirc has been downloaded, but as you\n"
82 81
                     + "do not seem to be using the DMDirc launcher, it will\n"
83 82
                     + "not be installed automatically.\n\n"
@@ -85,15 +84,9 @@ public class ClientComponent implements UpdateComponent {
85 84
                     + "new DMDirc.jar file, located at:\n"
86 85
                     + " " + targetFile.getAbsolutePath() + "\n"
87 86
                     + "over your existing DMDirc install located in:\n"
88
-                    + "  " + ResourceManager.getCurrentWorkingDirectory();
89
-            } else {
90
-                message = "A new version of DMDirc has been downloaded, but as you\n"
91
-                    + "do not seem to be using the DMDirc launcher, it will\n"
92
-                    + "not be installed automatically.\n\n"
93
-                    + "To install this update manually, please place the\n"
94
-                    + targetFile.getAbsolutePath() + " wherever you wish to\n"
95
-                    + " run DMDirc from and update your shortcuts accordingly.";
87
+                    + "  " + DMDircResourceManager.getCurrentWorkingDirectory();
96 88
             }
89
+            
97 90
             Main.getUI().showMessageDialog("Client update downloaded", message);
98 91
         }
99 92
         

+ 85
- 0
src/com/dmdirc/util/resourcemanager/DMDircResourceManager.java Zobrazit soubor

@@ -0,0 +1,85 @@
1
+/*
2
+ * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.util.resourcemanager;
24
+
25
+import com.dmdirc.logger.ErrorLevel;
26
+import com.dmdirc.logger.Logger;
27
+
28
+import java.io.UnsupportedEncodingException;
29
+import java.net.URL;
30
+import java.net.URLDecoder;
31
+
32
+/**
33
+ *
34
+ * @author chris
35
+ */
36
+public class DMDircResourceManager {
37
+
38
+    /**
39
+     * Returns the working directory for the application.
40
+     * 
41
+     * @return Current working directory
42
+     */
43
+    public static synchronized String getCurrentWorkingDirectory() {
44
+        String path = "";        
45
+        final URL resource = Thread.currentThread().getContextClassLoader().
46
+                        getResource("com/dmdirc/Main.class");
47
+        
48
+        final String protocol = resource.getProtocol();
49
+        
50
+        if ("file".equals(protocol)) {
51
+            path = Thread.currentThread().
52
+                    getContextClassLoader().getResource("").getPath();
53
+        } else if ("jar".equals(protocol)) {
54
+            final String tempPath = resource.getPath();
55
+            
56
+            if (System.getProperty("os.name").startsWith("Windows")) {
57
+                path = tempPath.substring(6, tempPath.length() - 23);
58
+            } else {
59
+                path = tempPath.substring(5, tempPath.length() - 23);
60
+            }
61
+            
62
+            path = path.substring(0, path.lastIndexOf('/') + 1);
63
+        }
64
+        
65
+        try {
66
+            path = URLDecoder.decode(path, "UTF-8");
67
+        } catch (UnsupportedEncodingException ex) {
68
+            Logger.userError(ErrorLevel.MEDIUM, "Unable to decode path");
69
+            path = "";
70
+        }
71
+        return path;
72
+    }
73
+    
74
+    /**
75
+     * Determines if this instance of DMDirc is running from a jar or not.
76
+     * 
77
+     * @return True if this instance is running from a JAR, false otherwise
78
+     */
79
+    public static boolean isRunningFromJar() {
80
+        final URL resource = Thread.currentThread().getContextClassLoader().
81
+                        getResource("com/dmdirc/Main.class");
82
+        return "jar".equals(resource.getProtocol());
83
+    }
84
+    
85
+}

+ 36
- 76
src/com/dmdirc/util/resourcemanager/ResourceManager.java Zobrazit soubor

@@ -30,7 +30,6 @@ import java.io.FileOutputStream;
30 30
 import java.io.IOException;
31 31
 import java.io.InputStream;
32 32
 import java.io.UnsupportedEncodingException;
33
-import java.net.URL;
34 33
 import java.util.List;
35 34
 import java.util.Map;
36 35
 import java.util.Map.Entry;
@@ -41,23 +40,15 @@ import java.util.Map.Entry;
41 40
 public abstract class ResourceManager {
42 41
     
43 42
     /** Previously assigned ResourceManager. */
43
+    @Deprecated
44 44
     private static ResourceManager me;
45 45
     
46
-    /** Enum indicating resource manager type. */
47
-    public enum Type {
48
-        /** File resource manager. */
49
-        FILE,
50
-        /** Jar resource manager. */
51
-        JAR,
52
-        /** No resource manager. */
53
-        NONE,
54
-    }
55
-    
56 46
     /**
57 47
      * Returns an appropriate instance of ResourceManager.
58 48
      *
59 49
      * @return ResourceManager implementation
60 50
      */
51
+    @Deprecated
61 52
     public static final synchronized ResourceManager getResourceManager() {
62 53
         if (me == null) {
63 54
             String path = Thread.currentThread().getContextClassLoader().
@@ -92,79 +83,38 @@ public abstract class ResourceManager {
92 83
     }
93 84
     
94 85
     /**
95
-     * Returns an appropriate instance of ResourceManager for the specified url string.
96
-     *
97
-     * @param type  file://path/to/base
98
-     *              jar://path/to/jar
99
-     *              zip://path/to/zip
86
+     * Returns a resource manager for the specified URL. The following URL types
87
+     * are valid:
100 88
      * 
101
-     * @return ResourceManager implementation
89
+     * <ul>
90
+     *  <li>file://path/</li>
91
+     *  <li>zip://path/filename.zip</li>
92
+     *  <li>jar://path/filename.jar</li>
93
+     *  <li>dmdirc://</li>
94
+     *  <li>theme://[themename:]</li>
95
+     * </ul>
96
+     *
97
+     * @param url The URL for which a resource manager is required
98
+     * @return A resource manager for the specified URL
102 99
      * 
103
-     * @throws java.io.IOException if an IO Error occurs opening the file
100
+     * @throws IOException if an IO Error occurs opening the file
101
+     * @throws IllegalArgumentException if the URL type is not valid
104 102
      */
105
-    public static final ResourceManager getResourceManager(final String type) throws IOException {
106
-        if (type.startsWith("file://")) {
107
-            return new FileResourceManager(type.substring(7));
108
-        } else if (type.startsWith("jar://") || type.startsWith("zip://")) {
109
-            return new ZipResourceManager(type.substring(6));
103
+    public static final ResourceManager getResourceManager(final String url)
104
+            throws IOException, IllegalArgumentException {
105
+        if (url.startsWith("file://")) {
106
+            return new FileResourceManager(url.substring(7));
107
+        } else if (url.startsWith("jar://") || url.startsWith("zip://")) {
108
+            return new ZipResourceManager(url.substring(6));
109
+        } else if (url.startsWith("dmdirc://")) {
110
+            throw new UnsupportedOperationException("Not implemented yet");
111
+        } else if (url.startsWith("theme://")) {
112
+            throw new UnsupportedOperationException("Not implemented yet");
110 113
         } else {
111 114
             throw new IllegalArgumentException("Unknown resource manager type");
112 115
         }
113 116
     }
114 117
     
115
-    /**
116
-     * Returns the working directory for the application.
117
-     * 
118
-     * @return Current working directory
119
-     */
120
-    public static final synchronized String getCurrentWorkingDirectory() {
121
-        final URL resource = Thread.currentThread().getContextClassLoader().
122
-                        getResource("com/dmdirc/Main.class");
123
-        String path = "";
124
-        
125
-        final String protocol = resource.getProtocol();
126
-        
127
-        if ("file".equals(protocol)) {
128
-            path = Thread.currentThread().
129
-                    getContextClassLoader().getResource("").getPath();
130
-        } else if ("jar".equals(protocol)) {
131
-            final String tempPath = resource.getPath();
132
-            if (System.getProperty("os.name").startsWith("Windows")) {
133
-                path = tempPath.substring(6, tempPath.length() - 23);
134
-            } else {
135
-                path = tempPath.substring(5, tempPath.length() - 23);
136
-            }
137
-            path = path.substring(0, path.lastIndexOf('/') + 1);
138
-        }
139
-        
140
-        try {
141
-            path = java.net.URLDecoder.decode(path, "UTF-8");
142
-        } catch (UnsupportedEncodingException ex) {
143
-            Logger.userError(ErrorLevel.MEDIUM, "Unable to decode path");
144
-            path = "";
145
-        }
146
-        return path;
147
-    }
148
-    
149
-    /**
150
-     * Returns the type of the resource manager.
151
-     *
152
-     * @return ResourceManager Type
153
-     */
154
-    public static final synchronized Type getResourceManagerType() {
155
-        if (me == null) {
156
-            getResourceManager();
157
-        }
158
-        
159
-        if (me instanceof ZipResourceManager) {
160
-            return Type.JAR;
161
-        } else if (me instanceof FileResourceManager) {
162
-            return Type.FILE;
163
-        } else {
164
-            return Type.NONE;
165
-        }
166
-    }
167
-    
168 118
     /**
169 119
      * Writes a resource to a file.
170 120
      *
@@ -173,6 +123,7 @@ public abstract class ResourceManager {
173 123
      *
174 124
      * @throws IOException if the write operation fails
175 125
      */
126
+    @Deprecated
176 127
     public final void resourceToFile(final byte[] resource, final File file)
177 128
     throws IOException {
178 129
         final FileOutputStream out = new FileOutputStream(file, false);
@@ -195,6 +146,7 @@ public abstract class ResourceManager {
195 146
      *
196 147
      * @return success of failure of the operation
197 148
      */
149
+    @Deprecated
198 150
     public final boolean extractResource(final String resourceName,
199 151
             final String directory, final boolean usePath) throws IOException {
200 152
         final byte[] resource = getResourceBytes(resourceName);
@@ -241,6 +193,7 @@ public abstract class ResourceManager {
241 193
      *
242 194
      * @throws IOException if the write operation fails
243 195
      */
196
+    @Deprecated
244 197
     public final void extractResources(final String resourcesPrefix,
245 198
             final String directory, final boolean usePath) throws IOException {
246 199
         final Map<String, byte[]> resourcesBytes =
@@ -258,6 +211,7 @@ public abstract class ResourceManager {
258 211
      *
259 212
      * @throws IOException if the write operation fails
260 213
      */
214
+    @Deprecated
261 215
     public final void extractResources(final String resourcesPrefix,
262 216
             final String directory) throws IOException {
263 217
         extractResources(resourcesPrefix, directory, true);
@@ -270,6 +224,7 @@ public abstract class ResourceManager {
270 224
      * 
271 225
      * @return true iif the resource exists
272 226
      */
227
+    @Deprecated
273 228
     public abstract boolean resourceExists(final String resource);
274 229
     
275 230
     /**
@@ -279,6 +234,7 @@ public abstract class ResourceManager {
279 234
      *
280 235
      * @return byte[] for the resource, or an empty byte[] if not found
281 236
      */
237
+    @Deprecated
282 238
     public abstract byte[] getResourceBytes(final String resource);
283 239
     
284 240
     /**
@@ -288,6 +244,7 @@ public abstract class ResourceManager {
288 244
      *
289 245
      * @return InputStream for the resource, or null if not found
290 246
      */
247
+    @Deprecated
291 248
     public abstract InputStream getResourceInputStream(final String resource);
292 249
     
293 250
     /**
@@ -298,6 +255,7 @@ public abstract class ResourceManager {
298 255
      *
299 256
      * @return Map of byte[]s of resources found
300 257
      */
258
+    @Deprecated
301 259
     public abstract Map<String, byte[]> getResourcesStartingWithAsBytes(
302 260
             final String resourcesPrefix);
303 261
     
@@ -309,6 +267,7 @@ public abstract class ResourceManager {
309 267
      *
310 268
      * @return Map of InputStreams of resources found
311 269
      */
270
+    @Deprecated
312 271
     public abstract Map<String, InputStream> getResourcesStartingWithAsInputStreams(
313 272
             final String resourcesPrefix);
314 273
     
@@ -320,5 +279,6 @@ public abstract class ResourceManager {
320 279
      *
321 280
      * @return List of resources found
322 281
      */
282
+    @Deprecated
323 283
     public abstract List<String> getResourcesStartingWith(final String resourcesPrefix);
324 284
 }

Načítá se…
Zrušit
Uložit