01: /*
02: * Copyright 2005 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
07: * BI Platform. The Initial Developer is Pentaho Corporation.
08: *
09: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11: * the license for the specific language governing your rights and limitations.
12: *
13: * Created Mar 29, 2006
14: * @author wseyler
15: */
16: package com.pentaho.repository.dbbased.solution;
17:
18: import java.io.ByteArrayInputStream;
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.util.HashMap;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25: import org.pentaho.core.repository.SolutionRepositoryBase;
26: import org.pentaho.messages.Messages;
27:
28: public class DbRepositoryClassLoader extends ClassLoader {
29: protected static final Log logger = LogFactory
30: .getLog(SolutionRepositoryBase.class);
31:
32: private static HashMap resourceMap = new HashMap();
33:
34: String path;
35:
36: SolutionRepository repository;
37:
38: public DbRepositoryClassLoader(String inPath,
39: SolutionRepository inRepository) {
40: path = inPath;
41: repository = inRepository;
42: }
43:
44: /**
45: * Returns the requested resource as an InputStream.
46: * @param name The resource name
47: * @retruns An input stream for reading the resource, or <code>null</code> if the resource could not be found
48: */
49: public InputStream getResourceAsStream(String name) {
50: String key = path + RepositoryFile.SEPARATOR + name;
51: byte[] bytes = (byte[]) resourceMap.get(key);
52: if (bytes == null) {
53: try {
54: bytes = repository.getResourceAsBytes(key, false);
55: resourceMap.put(key, bytes);
56: } catch (IOException ignored) {
57: // This situation indicates the resource could not be found. This is a common and correct situation
58: // and this exception should be ignored.
59: if (logger.isTraceEnabled()) {
60: logger
61: .trace(Messages
62: .getString(
63: "DbRepositoryClassLoader.RESOURCE_NOT_FOUND", name)); //$NON-NLS-1$
64: }
65:
66: // Return null to indicate that the resource could not be found (and this is ok)
67: return null;
68: }
69: }
70: return new ByteArrayInputStream(bytes);
71: }
72:
73: public static void clearResourceCache() {
74: resourceMap.clear();
75: }
76:
77: }
|