Source code: com/aendvari/common/util/ResourceLoader.java
1 /*
2 * ResourceLoader.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.common.util;
11
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.IOException;
15
16 import java.lang.reflect.*;
17
18
19 /**
20 * <p>
21 * This class provides the ability to load a resource using a class as a reference.
22 * </p>
23 *
24 * <p>
25 * A subclass of <code>ResourceLoader</code> must override
26 * the getResourceName() method to specify the name of the resource.
27 * The resource is expected to be placed within the same package as the
28 * subclass.
29 * </p>
30 *
31 * @author Scott Milne
32 *
33 */
34
35 public abstract class ResourceLoader
36 {
37 /**
38 * This method returns the name of the resource.
39 *
40 * @return The name of the resource.
41 *
42 */
43
44 protected abstract String getResourceName();
45
46 /**
47 * Returns an {@link InputStream} for the resource associated with this class.
48 *
49 * @return An {@link InputStream} for the resource.
50 *
51 * @throws Throws {@link IOException} if the resource cannot be located.
52 *
53 */
54
55 public InputStream getResourceAsStream()
56 throws IOException
57 {
58 InputStream in = this.getClass().getResourceAsStream( getResourceName() );
59
60 if (in == null)
61 {
62 throw new IOException( "The resource \"" + getResourceName() + "\" cannot be located.");
63 }
64
65 return in;
66 }
67
68 /**
69 * Returns an {@link InputStream} for the resource associated with the specified class.
70 *
71 * @param resourceClassPath The class path of the resource class.
72 *
73 * @return An {@link InputStream} for the resource.
74 *
75 * @throws Throws {@link IOException} if the resource cannot be located.
76 *
77 */
78
79 public static InputStream getResourceAsStream( String resourceClassPath )
80 throws IOException
81 {
82 // lookup the class object by name
83 Class resourceClass = null;
84
85 try
86 {
87 resourceClass = Class.forName(resourceClassPath);
88 }
89 catch (ClassNotFoundException exception)
90 {
91 throw new IOException(exception.getMessage());
92 }
93
94 return getResourceAsStream( resourceClass );
95 }
96
97 /**
98 * Returns an {@link InputStream} for the resource associated with the specified class.
99 * Get the descriptor input stream from the provided descriptor class.
100 *
101 * @param resourceClass The class of the resource.
102 *
103 * @return An {@link InputStream} of the resource.
104 *
105 * @throws Throws {@link Exception} if the resource cannot be located.
106 *
107 */
108
109 public static InputStream getResourceAsStream( Class resourceClass )
110 throws IOException
111 {
112 try
113 {
114 Class[] listParam = {};
115 Object[] listArgs = {};
116
117 // create an instance of the given resource class
118 Constructor constructor = resourceClass.getConstructor(listParam);
119 ResourceLoader resource = (ResourceLoader)constructor.newInstance(listArgs);
120
121 // get the resource stream from the class
122 // if this fails, and Exception will be thrown.
123 InputStream in = resource.getResourceAsStream();
124
125 return in;
126 }
127 catch (NoSuchMethodException exception)
128 {
129 throw new IOException(exception.getMessage());
130 }
131 catch (InstantiationException exception)
132 {
133 throw new IOException(exception.getMessage());
134 }
135 catch (IllegalAccessException exception)
136 {
137 throw new IOException(exception.getMessage());
138 }
139 catch (InvocationTargetException exception)
140 {
141 throw new IOException(exception.getMessage());
142 }
143 }
144 }
145