View Javadoc

1   /*
2    * $Id: ReaderResource.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config;
12  
13  import org.mule.MuleManager;
14  import org.mule.util.IOUtils;
15  import org.mule.util.StringUtils;
16  
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.io.InputStreamReader;
20  import java.io.Reader;
21  
22  /**
23   * <code>ReaderResource</code> is a reader with a description associated with it.
24   * This is useful for error resolution as the reader description can be included when
25   * reporting errors during reading the resource.
26   */
27  public class ReaderResource
28  {
29  
30      private String description;
31      private Reader reader;
32  
33      public ReaderResource(String description, Reader reader)
34      {
35          this.description = description;
36          this.reader = reader;
37      }
38  
39      public String getDescription()
40      {
41          return description;
42      }
43  
44      public Reader getReader()
45      {
46          return reader;
47      }
48  
49      public static ReaderResource[] parseResources(String configResources, String encoding) throws IOException
50      {
51          String[] resources = StringUtils.splitAndTrim(configResources, ",");
52          MuleManager.getConfiguration().setConfigResources(resources);
53          ReaderResource[] readers = new ReaderResource[resources.length];
54          for (int i = 0; i < resources.length; i++)
55          {
56              InputStream is = IOUtils.getResourceAsStream(resources[i].trim(), ReaderResource.class);
57              if (is == null)
58              {
59                  throw new IOException("could not load resource: " + resources[i].trim());
60              }
61              readers[i] = new ReaderResource(resources[i].trim(), new InputStreamReader(is, encoding));
62          }
63          return readers;
64      }
65  
66      public static ReaderResource[] parseResources(String configResources) throws IOException
67      {
68          return parseResources(configResources, MuleManager.getConfiguration().getEncoding());
69      }
70  }