KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > config > CachedResource


1 /*
2  * $Id: CachedResource.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.spring.config;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17
18 import org.springframework.core.io.AbstractResource;
19
20 /**
21  * Spring 2.x is picky about open/closed input streams, as it requires a closed
22  * stream (fully read resource) to enable automatic validation detection (DTD or
23  * XSD). Otherwise, a caller has to specify the mode explicitly. <p/> Code relying on
24  * Spring 1.2.x behavior may now break with
25  * {@link org.springframework.beans.factory.BeanDefinitionStoreException}. This
26  * class is called in to remedy this and should be used instead of, e.g.
27  * {@link org.springframework.core.io.InputStreamResource}. <p/> The resource is
28  * fully stored in memory.
29  */

30 public class CachedResource extends AbstractResource
31 {
32
33     private static final String JavaDoc DEFAULT_DESCRIPTION = "cached in-memory resource";
34
35     private final byte[] buffer;
36     private final String JavaDoc description;
37
38     public CachedResource(byte[] source)
39     {
40         this(source, null);
41     }
42
43     public CachedResource(String JavaDoc source, String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc
44     {
45         this(source.trim().getBytes(encoding), DEFAULT_DESCRIPTION);
46     }
47
48     public CachedResource(byte[] source, String JavaDoc description)
49     {
50         this.buffer = source;
51         this.description = description;
52     }
53
54     public String JavaDoc getDescription()
55     {
56         return (description == null) ? "" : description;
57     }
58
59     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
60     {
61         // This HAS to be a new InputStream, otherwise SAX
62
// parser breaks with 'Premature end of file at line -1"
63
// This behavior is not observed with Spring pre-2.x
64
return new ByteArrayInputStream JavaDoc(buffer);
65     }
66 }
67
Popular Tags