Browse Source

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 16 years ago
parent
commit
491f432806

+ 2
- 2
src/com/dmdirc/commandline/CommandLineParser.java View File

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

+ 6
- 13
src/com/dmdirc/updater/components/ClientComponent.java View File

24
 
24
 
25
 import com.dmdirc.Main;
25
 import com.dmdirc.Main;
26
 import com.dmdirc.updater.UpdateComponent;
26
 import com.dmdirc.updater.UpdateComponent;
27
-import com.dmdirc.util.resourcemanager.ResourceManager;
27
+import com.dmdirc.util.resourcemanager.DMDircResourceManager;
28
 
28
 
29
 import java.io.File;
29
 import java.io.File;
30
 
30
 
66
         tmpFile.renameTo(targetFile);
66
         tmpFile.renameTo(targetFile);
67
         
67
         
68
         if (!LauncherComponent.isUsingLauncher()) {
68
         if (!LauncherComponent.isUsingLauncher()) {
69
-            final ResourceManager.Type type = ResourceManager.getResourceManagerType();
70
             final String message;
69
             final String message;
71
-            if (type == ResourceManager.Type.JAR) {
70
+            if (DMDircResourceManager.isRunningFromJar()) {
72
                 message = "A new version of DMDirc has been downloaded, but as you\n"
71
                 message = "A new version of DMDirc has been downloaded, but as you\n"
73
                     + "do not seem to be using the DMDirc launcher, it will\n"
72
                     + "do not seem to be using the DMDirc launcher, it will\n"
74
                     + "not be installed automatically.\n\n"
73
                     + "not be installed automatically.\n\n"
75
                     + "To install this update manually, please replace the\n"
74
                     + "To install this update manually, please replace the\n"
76
                     + "existing DMDirc.jar file, located at:\n"
75
                     + "existing DMDirc.jar file, located at:\n"
77
-                    + " " + ResourceManager.getCurrentWorkingDirectory() + "\n"
76
+                    + " " + DMDircResourceManager.getCurrentWorkingDirectory() + "\n"
78
                     + "with the following file:\n"
77
                     + "with the following file:\n"
79
                     + "  " + targetFile.getAbsolutePath();
78
                     + "  " + targetFile.getAbsolutePath();
80
-            } else if (type == ResourceManager.Type.FILE) {
79
+            } else {
81
                 message = "A new version of DMDirc has been downloaded, but as you\n"
80
                 message = "A new version of DMDirc has been downloaded, but as you\n"
82
                     + "do not seem to be using the DMDirc launcher, it will\n"
81
                     + "do not seem to be using the DMDirc launcher, it will\n"
83
                     + "not be installed automatically.\n\n"
82
                     + "not be installed automatically.\n\n"
85
                     + "new DMDirc.jar file, located at:\n"
84
                     + "new DMDirc.jar file, located at:\n"
86
                     + " " + targetFile.getAbsolutePath() + "\n"
85
                     + " " + targetFile.getAbsolutePath() + "\n"
87
                     + "over your existing DMDirc install located in:\n"
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
             Main.getUI().showMessageDialog("Client update downloaded", message);
90
             Main.getUI().showMessageDialog("Client update downloaded", message);
98
         }
91
         }
99
         
92
         

+ 85
- 0
src/com/dmdirc/util/resourcemanager/DMDircResourceManager.java View File

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 View File

30
 import java.io.IOException;
30
 import java.io.IOException;
31
 import java.io.InputStream;
31
 import java.io.InputStream;
32
 import java.io.UnsupportedEncodingException;
32
 import java.io.UnsupportedEncodingException;
33
-import java.net.URL;
34
 import java.util.List;
33
 import java.util.List;
35
 import java.util.Map;
34
 import java.util.Map;
36
 import java.util.Map.Entry;
35
 import java.util.Map.Entry;
41
 public abstract class ResourceManager {
40
 public abstract class ResourceManager {
42
     
41
     
43
     /** Previously assigned ResourceManager. */
42
     /** Previously assigned ResourceManager. */
43
+    @Deprecated
44
     private static ResourceManager me;
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
      * Returns an appropriate instance of ResourceManager.
47
      * Returns an appropriate instance of ResourceManager.
58
      *
48
      *
59
      * @return ResourceManager implementation
49
      * @return ResourceManager implementation
60
      */
50
      */
51
+    @Deprecated
61
     public static final synchronized ResourceManager getResourceManager() {
52
     public static final synchronized ResourceManager getResourceManager() {
62
         if (me == null) {
53
         if (me == null) {
63
             String path = Thread.currentThread().getContextClassLoader().
54
             String path = Thread.currentThread().getContextClassLoader().
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
         } else {
113
         } else {
111
             throw new IllegalArgumentException("Unknown resource manager type");
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
      * Writes a resource to a file.
119
      * Writes a resource to a file.
170
      *
120
      *
173
      *
123
      *
174
      * @throws IOException if the write operation fails
124
      * @throws IOException if the write operation fails
175
      */
125
      */
126
+    @Deprecated
176
     public final void resourceToFile(final byte[] resource, final File file)
127
     public final void resourceToFile(final byte[] resource, final File file)
177
     throws IOException {
128
     throws IOException {
178
         final FileOutputStream out = new FileOutputStream(file, false);
129
         final FileOutputStream out = new FileOutputStream(file, false);
195
      *
146
      *
196
      * @return success of failure of the operation
147
      * @return success of failure of the operation
197
      */
148
      */
149
+    @Deprecated
198
     public final boolean extractResource(final String resourceName,
150
     public final boolean extractResource(final String resourceName,
199
             final String directory, final boolean usePath) throws IOException {
151
             final String directory, final boolean usePath) throws IOException {
200
         final byte[] resource = getResourceBytes(resourceName);
152
         final byte[] resource = getResourceBytes(resourceName);
241
      *
193
      *
242
      * @throws IOException if the write operation fails
194
      * @throws IOException if the write operation fails
243
      */
195
      */
196
+    @Deprecated
244
     public final void extractResources(final String resourcesPrefix,
197
     public final void extractResources(final String resourcesPrefix,
245
             final String directory, final boolean usePath) throws IOException {
198
             final String directory, final boolean usePath) throws IOException {
246
         final Map<String, byte[]> resourcesBytes =
199
         final Map<String, byte[]> resourcesBytes =
258
      *
211
      *
259
      * @throws IOException if the write operation fails
212
      * @throws IOException if the write operation fails
260
      */
213
      */
214
+    @Deprecated
261
     public final void extractResources(final String resourcesPrefix,
215
     public final void extractResources(final String resourcesPrefix,
262
             final String directory) throws IOException {
216
             final String directory) throws IOException {
263
         extractResources(resourcesPrefix, directory, true);
217
         extractResources(resourcesPrefix, directory, true);
270
      * 
224
      * 
271
      * @return true iif the resource exists
225
      * @return true iif the resource exists
272
      */
226
      */
227
+    @Deprecated
273
     public abstract boolean resourceExists(final String resource);
228
     public abstract boolean resourceExists(final String resource);
274
     
229
     
275
     /**
230
     /**
279
      *
234
      *
280
      * @return byte[] for the resource, or an empty byte[] if not found
235
      * @return byte[] for the resource, or an empty byte[] if not found
281
      */
236
      */
237
+    @Deprecated
282
     public abstract byte[] getResourceBytes(final String resource);
238
     public abstract byte[] getResourceBytes(final String resource);
283
     
239
     
284
     /**
240
     /**
288
      *
244
      *
289
      * @return InputStream for the resource, or null if not found
245
      * @return InputStream for the resource, or null if not found
290
      */
246
      */
247
+    @Deprecated
291
     public abstract InputStream getResourceInputStream(final String resource);
248
     public abstract InputStream getResourceInputStream(final String resource);
292
     
249
     
293
     /**
250
     /**
298
      *
255
      *
299
      * @return Map of byte[]s of resources found
256
      * @return Map of byte[]s of resources found
300
      */
257
      */
258
+    @Deprecated
301
     public abstract Map<String, byte[]> getResourcesStartingWithAsBytes(
259
     public abstract Map<String, byte[]> getResourcesStartingWithAsBytes(
302
             final String resourcesPrefix);
260
             final String resourcesPrefix);
303
     
261
     
309
      *
267
      *
310
      * @return Map of InputStreams of resources found
268
      * @return Map of InputStreams of resources found
311
      */
269
      */
270
+    @Deprecated
312
     public abstract Map<String, InputStream> getResourcesStartingWithAsInputStreams(
271
     public abstract Map<String, InputStream> getResourcesStartingWithAsInputStreams(
313
             final String resourcesPrefix);
272
             final String resourcesPrefix);
314
     
273
     
320
      *
279
      *
321
      * @return List of resources found
280
      * @return List of resources found
322
      */
281
      */
282
+    @Deprecated
323
     public abstract List<String> getResourcesStartingWith(final String resourcesPrefix);
283
     public abstract List<String> getResourcesStartingWith(final String resourcesPrefix);
324
 }
284
 }

Loading…
Cancel
Save