001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.plugin.jfreereport.helper;
014:
015: import java.io.FileNotFoundException;
016: import java.io.InputStream;
017:
018: import org.jfree.resourceloader.FactoryParameterKey;
019: import org.jfree.resourceloader.ResourceData;
020: import org.jfree.resourceloader.ResourceKey;
021: import org.jfree.resourceloader.ResourceLoadingException;
022: import org.jfree.resourceloader.ResourceManager;
023: import org.jfree.resourceloader.loader.AbstractResourceData;
024: import org.pentaho.core.runtime.IRuntimeContext;
025: import org.pentaho.core.solution.IActionResource;
026: import org.pentaho.core.solution.ActionResource;
027:
028: /**
029: * This class is implemented to support loading solution files
030: * from the pentaho repository into JFreeReport
031: *
032: * @author Will Gorman
033: */
034: public class PentahoResourceData extends AbstractResourceData {
035:
036: private static final long serialVersionUID = 1806026106310340013L;
037:
038: public static final String PENTAHO_RUNTIME_CONTEXT_KEY = "pentahoRuntimeContext"; //$NON-NLS-1$
039:
040: private String filename;
041:
042: private ResourceKey key;
043:
044: private IRuntimeContext runtimecontext;
045:
046: /**
047: * constructor which takes a resource key for data loading specifics
048: *
049: * @param key resource key
050: */
051: public PentahoResourceData(final ResourceKey key) {
052: if (key == null) {
053: throw new NullPointerException();
054: }
055:
056: this .key = key;
057: this .filename = (String) key.getIdentifier();
058: try {
059: this .runtimecontext = (IRuntimeContext) key
060: .getFactoryParameters().get(
061: new FactoryParameterKey(
062: PENTAHO_RUNTIME_CONTEXT_KEY));
063: } catch (Throwable t) {
064: t.printStackTrace();
065: }
066: if (runtimecontext == null) {
067: throw new InstantiationError(
068: "PentahoResourceData: Failed to retrieve runtime context from resource key"); //$NON-NLS-1$
069: }
070: }
071:
072: /**
073: * gets a resource stream from the runtime context.
074: *
075: * @param caller resource manager
076: * @return input stream
077: */
078: public InputStream getResourceAsStream(ResourceManager caller)
079: throws ResourceLoadingException {
080: IActionResource resource = new ActionResource(
081: "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", (String) key.getIdentifier()); //$NON-NLS-1$ //$NON-NLS-2$
082: try {
083: return runtimecontext.getResourceInputStream(resource);
084: } catch (FileNotFoundException e) {
085: throw new ResourceLoadingException(e.getLocalizedMessage(),
086: e);
087: }
088: }
089:
090: /**
091: * returns a requested attribute, currently only supporting filename.
092: *
093: * @param key attribute requested
094: * @return attribute value
095: */
096: public Object getAttribute(String lookupKey) {
097: if (lookupKey.equals(ResourceData.FILENAME)) {
098: return filename;
099: }
100: return null;
101: }
102:
103: /**
104: * return the version number. We don't have access to file dates or versions
105: * so return 0
106: *
107: * @param caller resource manager
108: *
109: * @return version
110: */
111: public long getVersion(ResourceManager caller)
112: throws ResourceLoadingException {
113: return 0;
114: }
115:
116: /**
117: * get the resource key
118: *
119: * @return resource key
120: */
121: public ResourceKey getKey() {
122: return key;
123: }
124: }
|