| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PluginResourceMaintainer |
|
| 4.333333333333333;4.333 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | package net.sourceforge.pebble.web.view; | |
| 33 | ||
| 34 | import org.apache.commons.io.IOUtils; | |
| 35 | import org.apache.commons.logging.Log; | |
| 36 | import org.apache.commons.logging.LogFactory; | |
| 37 | ||
| 38 | import javax.servlet.ServletContext; | |
| 39 | import java.io.*; | |
| 40 | import java.net.URISyntaxException; | |
| 41 | import java.util.Map; | |
| 42 | import java.util.concurrent.ConcurrentHashMap; | |
| 43 | ||
| 44 | /** | |
| 45 | * Maintains JSPs from plugins in the WAR directory | |
| 46 | * | |
| 47 | * @author James Roper | |
| 48 | */ | |
| 49 | 0 | public class PluginResourceMaintainer { |
| 50 | 0 | private static final Log log = LogFactory.getLog(PluginResourceMaintainer.class); |
| 51 | ||
| 52 | public static final String PLUGINS_JSP = "plugins-jsp"; | |
| 53 | public static final String WEB_INF_PLUGINS_JSP = "WEB-INF/plugins-jsp"; | |
| 54 | ||
| 55 | 0 | private final static Map<String, Boolean> resourceStateCache = new ConcurrentHashMap<String, Boolean>(); |
| 56 | 0 | private final static Object lock = new Object(); |
| 57 | ||
| 58 | /** | |
| 59 | * Ensure that the given resource is deployed to somewhere where tomcat can serve it and is up to date, copying it | |
| 60 | * across if it's not | |
| 61 | * | |
| 62 | * @param classPathResource The classpath resource | |
| 63 | * @param deployLocation The directory to deploy the resource to | |
| 64 | * @param sourceClass A class that comes from the same JAR as the resource, for modification time checking | |
| 65 | * @param servletContext The servlet context | |
| 66 | */ | |
| 67 | public static void checkResource(String classPathResource, String deployLocation, Class sourceClass, | |
| 68 | ServletContext servletContext) { | |
| 69 | 0 | String cacheKey = classPathResource + "|" + deployLocation; |
| 70 | 0 | if (!resourceStateCache.containsKey(cacheKey)) { |
| 71 | 0 | synchronized (lock) { |
| 72 | 0 | if (!resourceStateCache.containsKey(cacheKey)) { |
| 73 | 0 | String realPath = servletContext.getRealPath("/"); |
| 74 | 0 | if (realPath != null) { |
| 75 | File deployTo; | |
| 76 | 0 | if (deployLocation != null) { |
| 77 | 0 | deployTo = new File(realPath, deployLocation); |
| 78 | } else { | |
| 79 | 0 | deployTo = new File(realPath); |
| 80 | } | |
| 81 | 0 | String resourceName = classPathResource; |
| 82 | 0 | if (classPathResource.contains("/")) { |
| 83 | 0 | resourceName = classPathResource.substring(classPathResource.lastIndexOf("/") + 1); |
| 84 | } | |
| 85 | 0 | File destResource = new File(deployTo, resourceName); |
| 86 | 0 | if (destResource.exists()) { |
| 87 | // Check if the resource has been updated | |
| 88 | try { | |
| 89 | 0 | if (destResource.lastModified() < new File(sourceClass.getProtectionDomain().getCodeSource().getLocation() |
| 90 | .toURI()).lastModified()) { | |
| 91 | // Update the resource | |
| 92 | 0 | updateResource(destResource, classPathResource, sourceClass); |
| 93 | } | |
| 94 | 0 | } catch (URISyntaxException e) { |
| 95 | 0 | log.error("Bad code source", e); |
| 96 | 0 | } |
| 97 | } else { | |
| 98 | 0 | updateResource(destResource, classPathResource, sourceClass); |
| 99 | } | |
| 100 | } | |
| 101 | 0 | resourceStateCache.put(cacheKey, true); |
| 102 | } | |
| 103 | 0 | } |
| 104 | } | |
| 105 | 0 | } |
| 106 | ||
| 107 | /** | |
| 108 | * Ensure that the given classpath JSP is in the WEB-INF/plugins-jsp directory and up to date, copying it across if | |
| 109 | * it's not | |
| 110 | * | |
| 111 | * @param classPathJsp The classpath JSP | |
| 112 | * @param viewClass The class the JSP comes from, used to find the jar to do modification time based checking | |
| 113 | * @param servletContext The servlet context | |
| 114 | */ | |
| 115 | public static void checkJsp(String classPathJsp, Class viewClass, ServletContext servletContext) { | |
| 116 | 0 | checkResource(classPathJsp, WEB_INF_PLUGINS_JSP, viewClass, servletContext); |
| 117 | 0 | } |
| 118 | ||
| 119 | private static void updateResource(File dest, String classpathResource, Class viewClass) { | |
| 120 | 0 | if (!dest.getParentFile().exists()) { |
| 121 | //noinspection ResultOfMethodCallIgnored | |
| 122 | 0 | dest.getParentFile().mkdirs(); |
| 123 | } | |
| 124 | 0 | InputStream is = null; |
| 125 | 0 | OutputStream os = null; |
| 126 | try { | |
| 127 | 0 | os = new FileOutputStream(dest); |
| 128 | 0 | is = viewClass.getClassLoader().getResourceAsStream(classpathResource); |
| 129 | 0 | IOUtils.copy(is, os); |
| 130 | 0 | } catch (IOException e) { |
| 131 | 0 | log.error("Error copying resource (" + classpathResource + ") from classpath to " + dest.getAbsolutePath(), e); |
| 132 | } finally { | |
| 133 | 0 | IOUtils.closeQuietly(is); |
| 134 | 0 | IOUtils.closeQuietly(os); |
| 135 | 0 | } |
| 136 | 0 | } |
| 137 | ||
| 138 | } |