• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / ibatis-2 / 556

26 May 2025 03:04AM UTC coverage: 65.124% (-0.02%) from 65.146%
556

push

github

web-flow
Merge pull request #277 from hazendaz/master

Some NIO and remove old ibatis.com dtd handling as it no longer exists

1609 of 2820 branches covered (57.06%)

11 of 13 new or added lines in 3 files covered. (84.62%)

5051 of 7756 relevant lines covered (65.12%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.38
/src/main/java/com/ibatis/common/resources/Resources.java
1
/*
2
 * Copyright 2004-2025 the original author or authors.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.ibatis.common.resources;
17

18
import com.ibatis.common.beans.ClassInfo;
19

20
import java.io.*;
21
import java.net.URL;
22
import java.net.URLConnection;
23
import java.nio.charset.Charset;
24
import java.nio.file.Path;
25
import java.util.Properties;
26

27
/**
28
 * A class to simplify access to resources through the classloader.
29
 */
30
public class Resources extends Object {
31

32
  /** The default class loader. */
33
  private static ClassLoader defaultClassLoader;
34

35
  /**
36
   * Charset to use when calling getResourceAsReader. null means use the system default.
37
   */
38
  private static Charset charset;
39

40
  /**
41
   * Instantiates a new resources.
42
   */
43
  private Resources() {
44
  }
45

46
  /**
47
   * Returns the default classloader (may be null).
48
   *
49
   * @return The default classloader
50
   */
51
  public static ClassLoader getDefaultClassLoader() {
52
    return defaultClassLoader;
1✔
53
  }
54

55
  /**
56
   * Sets the default classloader.
57
   *
58
   * @param defaultClassLoader
59
   *          - the new default ClassLoader
60
   */
61
  public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
62
    Resources.defaultClassLoader = defaultClassLoader;
1✔
63
  }
1✔
64

65
  /**
66
   * Returns the URL of the resource on the classpath.
67
   *
68
   * @param resource
69
   *          The resource to find
70
   *
71
   * @return The resource
72
   *
73
   * @throws IOException
74
   *           If the resource cannot be found or read
75
   */
76
  public static URL getResourceURL(String resource) throws IOException {
77
    return getResourceURL(getClassLoader(), resource);
1✔
78
  }
79

80
  /**
81
   * Returns the URL of the resource on the classpath.
82
   *
83
   * @param loader
84
   *          The classloader used to load the resource
85
   * @param resource
86
   *          The resource to find
87
   *
88
   * @return The resource
89
   *
90
   * @throws IOException
91
   *           If the resource cannot be found or read
92
   */
93
  public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
94
    URL url = null;
1✔
95
    if (loader != null)
1!
96
      url = loader.getResource(resource);
1✔
97
    if (url == null)
1✔
98
      url = ClassLoader.getSystemResource(resource);
1✔
99
    if (url == null)
1✔
100
      throw new IOException("Could not find resource " + resource);
1✔
101
    return url;
1✔
102
  }
103

104
  /**
105
   * Returns a resource on the classpath as a Stream object.
106
   *
107
   * @param resource
108
   *          The resource to find
109
   *
110
   * @return The resource
111
   *
112
   * @throws IOException
113
   *           If the resource cannot be found or read
114
   */
115
  public static InputStream getResourceAsStream(String resource) throws IOException {
116
    return getResourceAsStream(getClassLoader(), resource);
1✔
117
  }
118

119
  /**
120
   * Returns a resource on the classpath as a Stream object.
121
   *
122
   * @param loader
123
   *          The classloader used to load the resource
124
   * @param resource
125
   *          The resource to find
126
   *
127
   * @return The resource
128
   *
129
   * @throws IOException
130
   *           If the resource cannot be found or read
131
   */
132
  public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
133
    InputStream in = null;
1✔
134
    if (loader != null)
1!
135
      in = loader.getResourceAsStream(resource);
1✔
136
    if (in == null)
1✔
137
      in = ClassLoader.getSystemResourceAsStream(resource);
1✔
138
    if (in == null)
1✔
139
      throw new IOException("Could not find resource " + resource);
1✔
140
    return in;
1✔
141
  }
142

143
  /**
144
   * Returns a resource on the classpath as a Properties object.
145
   *
146
   * @param resource
147
   *          The resource to find
148
   *
149
   * @return The resource
150
   *
151
   * @throws IOException
152
   *           If the resource cannot be found or read
153
   */
154
  public static Properties getResourceAsProperties(String resource) throws IOException {
155
    Properties props = new Properties();
1✔
156
    InputStream in = null;
1✔
157
    String propfile = resource;
1✔
158
    in = getResourceAsStream(propfile);
1✔
159
    props.load(in);
1✔
160
    in.close();
1✔
161
    return props;
1✔
162
  }
163

164
  /**
165
   * Returns a resource on the classpath as a Properties object.
166
   *
167
   * @param loader
168
   *          The classloader used to load the resource
169
   * @param resource
170
   *          The resource to find
171
   *
172
   * @return The resource
173
   *
174
   * @throws IOException
175
   *           If the resource cannot be found or read
176
   */
177
  public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
178
    Properties props = new Properties();
1✔
179
    InputStream in = null;
1✔
180
    String propfile = resource;
1✔
181
    in = getResourceAsStream(loader, propfile);
1✔
182
    props.load(in);
1✔
183
    in.close();
1✔
184
    return props;
1✔
185
  }
186

187
  /**
188
   * Returns a resource on the classpath as a Reader object.
189
   *
190
   * @param resource
191
   *          The resource to find
192
   *
193
   * @return The resource
194
   *
195
   * @throws IOException
196
   *           If the resource cannot be found or read
197
   */
198
  public static Reader getResourceAsReader(String resource) throws IOException {
199
    Reader reader;
200
    if (charset == null) {
1!
201
      reader = new InputStreamReader(getResourceAsStream(resource));
1✔
202
    } else {
203
      reader = new InputStreamReader(getResourceAsStream(resource), charset);
×
204
    }
205

206
    return reader;
1✔
207
  }
208

209
  /**
210
   * Returns a resource on the classpath as a Reader object.
211
   *
212
   * @param loader
213
   *          The classloader used to load the resource
214
   * @param resource
215
   *          The resource to find
216
   *
217
   * @return The resource
218
   *
219
   * @throws IOException
220
   *           If the resource cannot be found or read
221
   */
222
  public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
223
    Reader reader;
224
    if (charset == null) {
×
225
      reader = new InputStreamReader(getResourceAsStream(loader, resource));
×
226
    } else {
227
      reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
×
228
    }
229

230
    return reader;
×
231
  }
232

233
  /**
234
   * Returns a resource on the classpath as a File object.
235
   *
236
   * @param resource
237
   *          The resource to find
238
   *
239
   * @return The resource
240
   *
241
   * @throws IOException
242
   *           If the resource cannot be found or read
243
   */
244
  public static File getResourceAsFile(String resource) throws IOException {
NEW
245
    return Path.of(getResourceURL(resource).toString()).toFile();
×
246
  }
247

248
  /**
249
   * Returns a resource on the classpath as a File object.
250
   *
251
   * @param loader
252
   *          - the classloader used to load the resource
253
   * @param resource
254
   *          - the resource to find
255
   *
256
   * @return The resource
257
   *
258
   * @throws IOException
259
   *           If the resource cannot be found or read
260
   */
261
  public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
NEW
262
    return Path.of(getResourceURL(loader, resource).toString()).toFile();
×
263
  }
264

265
  /**
266
   * Gets a URL as an input stream.
267
   *
268
   * @param urlString
269
   *          - the URL to get
270
   *
271
   * @return An input stream with the data from the URL
272
   *
273
   * @throws IOException
274
   *           If the resource cannot be found or read
275
   */
276
  public static InputStream getUrlAsStream(String urlString) throws IOException {
277
    URL url = new URL(urlString);
×
278
    URLConnection conn = url.openConnection();
×
279
    return conn.getInputStream();
×
280
  }
281

282
  /**
283
   * Gets a URL as a Reader.
284
   *
285
   * @param urlString
286
   *          - the URL to get
287
   *
288
   * @return A Reader with the data from the URL
289
   *
290
   * @throws IOException
291
   *           If the resource cannot be found or read
292
   */
293
  public static Reader getUrlAsReader(String urlString) throws IOException {
294
    return new InputStreamReader(getUrlAsStream(urlString));
×
295
  }
296

297
  /**
298
   * Gets a URL as a Properties object.
299
   *
300
   * @param urlString
301
   *          - the URL to get
302
   *
303
   * @return A Properties object with the data from the URL
304
   *
305
   * @throws IOException
306
   *           If the resource cannot be found or read
307
   */
308
  public static Properties getUrlAsProperties(String urlString) throws IOException {
309
    Properties props = new Properties();
×
310
    InputStream in = null;
×
311
    String propfile = urlString;
×
312
    in = getUrlAsStream(propfile);
×
313
    props.load(in);
×
314
    in.close();
×
315
    return props;
×
316
  }
317

318
  /**
319
   * Loads a class.
320
   *
321
   * @param className
322
   *          - the class to load
323
   *
324
   * @return The loaded class
325
   *
326
   * @throws ClassNotFoundException
327
   *           If the class cannot be found (duh!)
328
   */
329
  public static Class classForName(String className) throws ClassNotFoundException {
330
    Class clazz = null;
1✔
331
    try {
332
      clazz = getClassLoader().loadClass(className);
1✔
333
    } catch (Exception e) {
×
334
      // Ignore. Failsafe below.
335
    }
1✔
336
    if (clazz == null) {
1!
337
      clazz = Class.forName(className);
×
338
    }
339
    return clazz;
1✔
340
  }
341

342
  /**
343
   * Creates an instance of a class.
344
   *
345
   * @param className
346
   *          - the class to create
347
   *
348
   * @return An instance of the class
349
   *
350
   * @throws ClassNotFoundException
351
   *           If the class cannot be found (duh!)
352
   * @throws InstantiationException
353
   *           If the class cannot be instantiaed
354
   * @throws IllegalAccessException
355
   *           If the class is not public, or other access problems arise
356
   */
357
  public static Object instantiate(String className)
358
      throws ClassNotFoundException, InstantiationException, IllegalAccessException {
359
    return instantiate(classForName(className));
1✔
360
  }
361

362
  /**
363
   * Creates an instance of a class.
364
   *
365
   * @param clazz
366
   *          - the class to create
367
   *
368
   * @return An instance of the class
369
   *
370
   * @throws InstantiationException
371
   *           If the class cannot be instantiaed
372
   * @throws IllegalAccessException
373
   *           If the class is not public, or other access problems arise
374
   */
375
  public static Object instantiate(Class clazz) throws InstantiationException, IllegalAccessException {
376
    try {
377
      return ClassInfo.getInstance(clazz).instantiateClass();
1✔
378
    } catch (Exception e) {
×
379
      // Try alternative...theoretically should fail for the exact same
380
      // reason, but in case of a weird security manager, this will help
381
      // some cases.
382
      // return clazz.newInstance();
383
      return clazz.newInstance();
×
384
    }
385
  }
386

387
  /**
388
   * Gets the class loader.
389
   *
390
   * @return the class loader
391
   */
392
  private static ClassLoader getClassLoader() {
393
    if (defaultClassLoader != null) {
1!
394
      return defaultClassLoader;
×
395
    } else {
396
      return Thread.currentThread().getContextClassLoader();
1✔
397
    }
398
  }
399

400
  /**
401
   * Gets the charset.
402
   *
403
   * @return the charset
404
   */
405
  public static Charset getCharset() {
406
    return charset;
×
407
  }
408

409
  /**
410
   * Use this method to set the Charset to be used when calling the getResourceAsReader methods. This will allow iBATIS
411
   * to function properly when the system default encoding doesn't deal well with unicode (IBATIS-340, IBATIS-349)
412
   *
413
   * @param charset
414
   *          the new charset
415
   */
416
  public static void setCharset(Charset charset) {
417
    Resources.charset = charset;
×
418
  }
×
419

420
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc