1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jdo.impl.enhancer.util;
19
20 import java.util.Set;
21 import java.util.Map;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Iterator;
25
26 import java.io.IOException;
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.PrintWriter;
30 import java.io.InputStream;
31 import java.io.FileInputStream;
32
33 import java.net.URL;
34
35
36 /***
37 * Searches resources among a set of files.
38 */
39 public class ListResourceLocator
40 extends ResourceLocatorBase
41 implements ResourceLocator
42 {
43 /***
44 * The map of jdo files.
45 */
46 final Map files = new HashMap();
47
48 /***
49 * Creates an intsance.
50 */
51 public ListResourceLocator(PrintWriter out,
52 boolean verbose,
53 List fileNames)
54 throws IOException
55 {
56 super(out, verbose);
57 affirm(fileNames != null);
58
59
60 for (Iterator i = fileNames.iterator(); i.hasNext();) {
61 final String s = (String)i.next();
62
63
64 final File file = new File(s).getCanonicalFile();
65 final URL url = file.toURL();
66 final String canonicalName = url.toString();
67 affirm(canonicalName != null);
68
69
70 if (!file.canRead()) {
71 final String msg
72 = getI18N("enhancer.cannot_read_resource",
73 file.toString());
74 throw new IOException(msg);
75 }
76
77
78 files.put(canonicalName, file);
79 printMessage(getI18N("enhancer.using_file",
80 canonicalName));
81 }
82 }
83
84 /***
85 * Finds a resource with a given name.
86 */
87 public InputStream getInputStreamForResource(String resourceName)
88 {
89
90
91 affirm(resourceName != null);
92
93 final Set entries = files.entrySet();
94 for (Iterator i = entries.iterator(); i.hasNext();) {
95 final Map.Entry entry = (Map.Entry)i.next();
96 final String fileName = (String)entry.getKey();
97 if (!fileName.endsWith(resourceName)) {
98 continue;
99 }
100 final File file = (File)entry.getValue();
101
102 final InputStream stream;
103 try {
104 stream = new FileInputStream(file);
105 } catch (FileNotFoundException ex) {
106
107
108 final String msg
109 = getI18N("enhancer.io_error_while_reading_resource",
110 file.toString(), ex.getMessage());
111 throw new RuntimeException(msg);
112 }
113 affirm(stream != null);
114 printMessage(getI18N("enhancer.found_resource", resourceName));
115 return stream;
116 }
117 printMessage(getI18N("enhancer.not_found_resource", resourceName));
118 return null;
119 }
120 }