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.BufferedOutputStream;
016: import java.io.File;
017: import java.io.FileOutputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.io.Serializable;
022: import java.net.URL;
023: import java.net.URLClassLoader;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import org.jfree.io.IOUtils;
028: import org.pentaho.core.repository.ISolutionRepository;
029: import org.pentaho.core.session.IPentahoSession;
030: import org.pentaho.core.solution.IActionResource;
031: import org.pentaho.core.system.IApplicationContext;
032: import org.pentaho.core.system.PentahoSystem;
033: import org.pentaho.messages.Messages;
034:
035: /**
036: * Creation-Date: 07.07.2006, 15:49:35
037: *
038: * @author Thomas Morgner
039: */
040: public class ReportUtils {
041: private static class ClassLoaderEntry implements Serializable {
042: private static final long serialVersionUID = 8925334939030498948L;
043:
044: private transient ClassLoader entry;
045:
046: public ClassLoaderEntry(final ClassLoader entry) {
047: this .entry = entry;
048: }
049:
050: public ClassLoader getEntry() {
051: return entry;
052: }
053: }
054:
055: private ReportUtils() {
056: }
057:
058: public static synchronized File getTempDirectory(
059: IPentahoSession session) {
060: IApplicationContext ctx = PentahoSystem.getApplicationContext();
061: if (ctx != null) {
062: final String fileOutputPath = ctx
063: .getFileOutputPath("system/tmp/"); //$NON-NLS-1$
064: final File tempDir = new File(fileOutputPath);
065: final String id = session.getId();
066:
067: final File userTempDir;
068: if (id == null) {
069: // typical sloopy programming! Someone forgot to check null values.
070: userTempDir = tempDir;
071: } else {
072: userTempDir = new File(tempDir, id);
073: }
074:
075: // this operation silently fails if the dir already exists.
076: userTempDir.mkdir();
077: return userTempDir;
078: }
079: throw new IllegalStateException(
080: Messages
081: .getString("ReportUtils.ERROR_0036_PENTAHO_SYSTEM_NOT_OK")); //$NON-NLS-1$
082: }
083:
084: public static ClassLoader createJarLoader(IPentahoSession session,
085: IActionResource resource) {
086:
087: // todo: We cant clean the temp directory ...
088: // session.addListener (,..) is needed
089: synchronized (session) {
090: try {
091: final URL url = getURL(session, resource, true);
092: if (url == null) {
093: return null;
094: }
095:
096: final Map cache = getClassLoaderCache(session);
097: ClassLoaderEntry entry = (ClassLoaderEntry) cache
098: .get(url);
099: if (entry != null) {
100: if (entry.getEntry() != null) {
101: return entry.getEntry();
102: }
103: }
104:
105: // now wrap the beast into a jar URL ...
106: final URL jarURL = new URL(
107: "jar:" + url.toExternalForm() + "!/"); //$NON-NLS-1$ //$NON-NLS-2$
108: final URLClassLoader urlClassLoader = URLClassLoader
109: .newInstance(new URL[] { jarURL });
110: cache.put(url, new ClassLoaderEntry(urlClassLoader));
111: return urlClassLoader;
112: } catch (IOException e) {
113: // something went wrong ..
114: return null;
115: }
116: }
117: }
118:
119: private static Map getClassLoaderCache(IPentahoSession session) {
120: synchronized (session) {
121: Object maybeMap = session
122: .getAttribute("-x-pentaho-classloaders"); //$NON-NLS-1$
123: if (maybeMap instanceof Map) {
124: return (Map) maybeMap;
125: }
126: Map map = new HashMap();
127: session.setAttribute("-x-pentaho-classloaders", map); //$NON-NLS-1$
128: return map;
129: }
130: }
131:
132: private static URL getURL(IPentahoSession session,
133: IActionResource resource, boolean create)
134: throws IOException {
135: if (resource.getSourceType() == IActionResource.URL_RESOURCE) {
136: return new URL(resource.getAddress());
137: }
138: if (resource.getSourceType() == IActionResource.FILE_RESOURCE) {
139: File file = new File(resource.getAddress());
140: if (file.exists() && file.canRead()) {
141: return file.toURL();
142: }
143: }
144: if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE) {
145: String reportJarPath = PentahoSystem
146: .getApplicationContext().getSolutionPath(
147: resource.getAddress());
148: File file = new File(reportJarPath);
149: if (file.exists() && file.canRead()) {
150: return file.toURL();
151: }
152: }
153:
154: if (create) {
155: // ok, fall back to copy the file into the temp dir and to load it from
156: // there...
157: File temp = getTempDirectory(session);
158: File tempFile = File.createTempFile(
159: "loaded-jar-", ".jar", temp); //$NON-NLS-1$ //$NON-NLS-2$
160: // if that fails, we dont have to waste our time on copying the stuff ..
161: final URL url = tempFile.toURL();
162:
163: final ISolutionRepository solutionRepository = PentahoSystem
164: .getSolutionRepository(session);
165: final InputStream in = solutionRepository
166: .getResourceInputStream(resource, true);
167: final OutputStream out = new BufferedOutputStream(
168: new FileOutputStream(tempFile));
169: IOUtils.getInstance().copyStreams(in, out);
170: in.close();
171: out.close();
172: return url;
173: } else {
174: return null;
175: }
176: }
177:
178: public static URL getURL(IPentahoSession session,
179: IActionResource resource) throws IOException {
180: return getURL(session, resource, false);
181: }
182: }
|