KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreereport > helper > ReportUtils


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * 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 package org.pentaho.plugin.jfreereport.helper;
14
15 import java.io.BufferedOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.jfree.io.IOUtils;
28 import org.pentaho.core.repository.ISolutionRepository;
29 import org.pentaho.core.session.IPentahoSession;
30 import org.pentaho.core.solution.IActionResource;
31 import org.pentaho.core.system.IApplicationContext;
32 import org.pentaho.core.system.PentahoSystem;
33 import org.pentaho.messages.Messages;
34
35 /**
36  * Creation-Date: 07.07.2006, 15:49:35
37  *
38  * @author Thomas Morgner
39  */

40 public class ReportUtils {
41     private static class ClassLoaderEntry implements Serializable JavaDoc {
42         private static final long serialVersionUID = 8925334939030498948L;
43
44         private transient ClassLoader JavaDoc entry;
45
46         public ClassLoaderEntry(final ClassLoader JavaDoc entry) {
47             this.entry = entry;
48         }
49
50         public ClassLoader JavaDoc getEntry() {
51             return entry;
52         }
53     }
54
55     private ReportUtils() {
56     }
57
58     public static synchronized File JavaDoc getTempDirectory(IPentahoSession session) {
59         IApplicationContext ctx = PentahoSystem.getApplicationContext();
60         if (ctx != null) {
61             final String JavaDoc fileOutputPath = ctx.getFileOutputPath("system/tmp/"); //$NON-NLS-1$
62
final File JavaDoc tempDir = new File JavaDoc(fileOutputPath);
63             final String JavaDoc id = session.getId();
64
65             final File JavaDoc userTempDir;
66             if (id == null) {
67                 // typical sloopy programming! Someone forgot to check null values.
68
userTempDir = tempDir;
69             } else {
70                 userTempDir = new File JavaDoc(tempDir, id);
71             }
72
73             // this operation silently fails if the dir already exists.
74
userTempDir.mkdir();
75             return userTempDir;
76         }
77         throw new IllegalStateException JavaDoc(Messages.getString("ReportUtils.ERROR_0036_PENTAHO_SYSTEM_NOT_OK")); //$NON-NLS-1$
78
}
79
80     public static ClassLoader JavaDoc createJarLoader(IPentahoSession session, IActionResource resource) {
81
82         // todo: We cant clean the temp directory ...
83
// session.addListener (,..) is needed
84
synchronized (session) {
85             try {
86                 final URL JavaDoc url = getURL(session, resource, true);
87                 if (url == null) {
88                     return null;
89                 }
90
91                 final Map JavaDoc cache = getClassLoaderCache(session);
92                 ClassLoaderEntry entry = (ClassLoaderEntry) cache.get(url);
93                 if (entry != null) {
94                     if (entry.getEntry() != null) {
95                         return entry.getEntry();
96                     }
97                 }
98
99                 // now wrap the beast into a jar URL ...
100
final URL JavaDoc jarURL = new URL JavaDoc("jar:" + url.toExternalForm() + "!/"); //$NON-NLS-1$ //$NON-NLS-2$
101
final URLClassLoader JavaDoc urlClassLoader = URLClassLoader.newInstance(new URL JavaDoc[] { jarURL });
102                 cache.put(url, new ClassLoaderEntry(urlClassLoader));
103                 return urlClassLoader;
104             } catch (IOException JavaDoc e) {
105                 // something went wrong ..
106
return null;
107             }
108         }
109     }
110
111     private static Map JavaDoc getClassLoaderCache(IPentahoSession session) {
112         synchronized (session) {
113             Object JavaDoc maybeMap = session.getAttribute("-x-pentaho-classloaders"); //$NON-NLS-1$
114
if (maybeMap instanceof Map JavaDoc) {
115                 return (Map JavaDoc) maybeMap;
116             }
117             Map JavaDoc map = new HashMap JavaDoc();
118             session.setAttribute("-x-pentaho-classloaders", map); //$NON-NLS-1$
119
return map;
120         }
121     }
122
123     private static URL JavaDoc getURL(IPentahoSession session, IActionResource resource, boolean create) throws IOException JavaDoc {
124         if (resource.getSourceType() == IActionResource.URL_RESOURCE) {
125             return new URL JavaDoc(resource.getAddress());
126         }
127         if (resource.getSourceType() == IActionResource.FILE_RESOURCE) {
128             File JavaDoc file = new File JavaDoc(resource.getAddress());
129             if (file.exists() && file.canRead()) {
130                 return file.toURL();
131             }
132         }
133         if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE) {
134             String JavaDoc reportJarPath = PentahoSystem.getApplicationContext().getSolutionPath(resource.getAddress());
135             File JavaDoc file = new File JavaDoc(reportJarPath);
136             if (file.exists() && file.canRead()) {
137                 return file.toURL();
138             }
139         }
140
141         if (create) {
142             // ok, fall back to copy the file into the temp dir and to load it from
143
// there...
144
File JavaDoc temp = getTempDirectory(session);
145             File JavaDoc tempFile = File.createTempFile("loaded-jar-", ".jar", temp); //$NON-NLS-1$ //$NON-NLS-2$
146
// if that fails, we dont have to waste our time on copying the stuff ..
147
final URL JavaDoc url = tempFile.toURL();
148
149             final ISolutionRepository solutionRepository = PentahoSystem.getSolutionRepository(session);
150             final InputStream JavaDoc in = solutionRepository.getResourceInputStream(resource);
151             final OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(tempFile));
152             IOUtils.getInstance().copyStreams(in, out);
153             in.close();
154             out.close();
155             return url;
156         } else {
157             return null;
158         }
159     }
160
161     public static URL JavaDoc getURL(IPentahoSession session, IActionResource resource) throws IOException JavaDoc {
162         return getURL(session, resource, false);
163     }
164 }
165
Popular Tags