1 13 package org.pentaho.plugin.jfreereport.helper; 14 15 import java.io.BufferedOutputStream ; 16 import java.io.File ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.io.Serializable ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.HashMap ; 25 import java.util.Map ; 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 40 public class ReportUtils { 41 private static class ClassLoaderEntry implements Serializable { 42 private static final long serialVersionUID = 8925334939030498948L; 43 44 private transient ClassLoader entry; 45 46 public ClassLoaderEntry(final ClassLoader entry) { 47 this.entry = entry; 48 } 49 50 public ClassLoader getEntry() { 51 return entry; 52 } 53 } 54 55 private ReportUtils() { 56 } 57 58 public static synchronized File getTempDirectory(IPentahoSession session) { 59 IApplicationContext ctx = PentahoSystem.getApplicationContext(); 60 if (ctx != null) { 61 final String fileOutputPath = ctx.getFileOutputPath("system/tmp/"); final File tempDir = new File (fileOutputPath); 63 final String id = session.getId(); 64 65 final File userTempDir; 66 if (id == null) { 67 userTempDir = tempDir; 69 } else { 70 userTempDir = new File (tempDir, id); 71 } 72 73 userTempDir.mkdir(); 75 return userTempDir; 76 } 77 throw new IllegalStateException (Messages.getString("ReportUtils.ERROR_0036_PENTAHO_SYSTEM_NOT_OK")); } 79 80 public static ClassLoader createJarLoader(IPentahoSession session, IActionResource resource) { 81 82 synchronized (session) { 85 try { 86 final URL url = getURL(session, resource, true); 87 if (url == null) { 88 return null; 89 } 90 91 final Map 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 final URL jarURL = new URL ("jar:" + url.toExternalForm() + "!/"); final URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL [] { jarURL }); 102 cache.put(url, new ClassLoaderEntry(urlClassLoader)); 103 return urlClassLoader; 104 } catch (IOException e) { 105 return null; 107 } 108 } 109 } 110 111 private static Map getClassLoaderCache(IPentahoSession session) { 112 synchronized (session) { 113 Object maybeMap = session.getAttribute("-x-pentaho-classloaders"); if (maybeMap instanceof Map ) { 115 return (Map ) maybeMap; 116 } 117 Map map = new HashMap (); 118 session.setAttribute("-x-pentaho-classloaders", map); return map; 120 } 121 } 122 123 private static URL getURL(IPentahoSession session, IActionResource resource, boolean create) throws IOException { 124 if (resource.getSourceType() == IActionResource.URL_RESOURCE) { 125 return new URL (resource.getAddress()); 126 } 127 if (resource.getSourceType() == IActionResource.FILE_RESOURCE) { 128 File file = new File (resource.getAddress()); 129 if (file.exists() && file.canRead()) { 130 return file.toURL(); 131 } 132 } 133 if (resource.getSourceType() == IActionResource.SOLUTION_FILE_RESOURCE) { 134 String reportJarPath = PentahoSystem.getApplicationContext().getSolutionPath(resource.getAddress()); 135 File file = new File (reportJarPath); 136 if (file.exists() && file.canRead()) { 137 return file.toURL(); 138 } 139 } 140 141 if (create) { 142 File temp = getTempDirectory(session); 145 File tempFile = File.createTempFile("loaded-jar-", ".jar", temp); final URL url = tempFile.toURL(); 148 149 final ISolutionRepository solutionRepository = PentahoSystem.getSolutionRepository(session); 150 final InputStream in = solutionRepository.getResourceInputStream(resource); 151 final OutputStream out = new BufferedOutputStream (new FileOutputStream (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 getURL(IPentahoSession session, IActionResource resource) throws IOException { 162 return getURL(session, resource, false); 163 } 164 } 165 | Popular Tags |