| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package no.sesat.search.view.velocity; |
| 20 | |
|
| 21 | |
import org.apache.velocity.runtime.resource.ResourceManagerImpl; |
| 22 | |
import org.apache.velocity.runtime.resource.Resource; |
| 23 | |
import org.apache.velocity.exception.ResourceNotFoundException; |
| 24 | |
import org.apache.velocity.exception.ParseErrorException; |
| 25 | |
import org.apache.log4j.Logger; |
| 26 | |
import org.apache.commons.lang.time.StopWatch; |
| 27 | |
|
| 28 | |
import java.util.concurrent.ExecutorService; |
| 29 | |
import java.util.concurrent.Executors; |
| 30 | |
import java.text.MessageFormat; |
| 31 | |
import java.util.concurrent.ThreadPoolExecutor; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | public final class QuickResourceManagerImpl extends ResourceManagerImpl { |
| 45 | |
|
| 46 | 0 | private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool(); |
| 47 | |
|
| 48 | |
private static final String RESOURCE_NOT_FOUND = "ResourceManager :'{0}'' not found in any resource loader."; |
| 49 | |
private static final String RESOURCE_PARSE_EXCEPTION = "ResourceManager.getResource() parse exception"; |
| 50 | |
private static final String RESOURCE_EXCEPTION = "ResourceManager.getResource() exception new"; |
| 51 | |
private static final String LOADED_VELOCITY_RESOURCE = "Loaded velocity resource {0} in {1}"; |
| 52 | |
private static final String CHECKED_MODIFICATION = "Checked modification of velocity resource {0} in {1}"; |
| 53 | |
|
| 54 | 0 | private static final Logger LOG = Logger.getLogger(QuickResourceManagerImpl.class); |
| 55 | |
private static final String DEBUG_POOL_COUNT = "Pool size: "; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public Resource getResource(final String name, final int type, final String encoding) throws Exception { |
| 62 | |
|
| 63 | 0 | final Resource resource = globalCache.get(type + name); |
| 64 | |
|
| 65 | 0 | if (resource != null) { |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 0 | if (resource.requiresChecking()) { |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 0 | resource.touch(); |
| 74 | 0 | EXECUTOR.submit(new Loader(resource, name, type, encoding)); |
| 75 | |
} |
| 76 | |
|
| 77 | 0 | return resource; |
| 78 | |
|
| 79 | |
} else { |
| 80 | |
|
| 81 | 0 | return new Loader(resource, name, type, encoding).load(); |
| 82 | |
} |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | 0 | private final class Loader implements Runnable { |
| 89 | |
|
| 90 | |
private final String name; |
| 91 | |
private final int type; |
| 92 | |
private final String enc; |
| 93 | |
private final String key; |
| 94 | |
private final Resource oldResource; |
| 95 | 0 | private boolean modified = false; |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | 0 | private Loader(final Resource oldResource, final String name, final int type, final String enc) { |
| 108 | |
|
| 109 | 0 | this.oldResource = oldResource; |
| 110 | 0 | this.name = name; |
| 111 | 0 | this.type = type; |
| 112 | 0 | this.enc = enc; |
| 113 | |
|
| 114 | 0 | key = type + name; |
| 115 | |
|
| 116 | 0 | if(LOG.isDebugEnabled() && EXECUTOR instanceof ThreadPoolExecutor){ |
| 117 | 0 | final ThreadPoolExecutor tpe = (ThreadPoolExecutor)EXECUTOR; |
| 118 | 0 | LOG.debug(DEBUG_POOL_COUNT + tpe.getActiveCount() + '/' + tpe.getPoolSize()); |
| 119 | |
} |
| 120 | 0 | } |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
private Resource load() throws Exception { |
| 131 | |
|
| 132 | 0 | final StopWatch stopWatch = new StopWatch(); |
| 133 | |
|
| 134 | |
try { |
| 135 | 0 | stopWatch.start(); |
| 136 | 0 | modified = oldResource == null || oldResource.isSourceModified(); |
| 137 | 0 | stopWatch.split(); |
| 138 | |
|
| 139 | 0 | if (modified) { |
| 140 | 0 | final Resource newResource = loadResource(name, type, enc); |
| 141 | |
|
| 142 | 0 | if (newResource.getResourceLoader().isCachingOn()) { |
| 143 | 0 | globalCache.put(key, newResource); |
| 144 | |
} |
| 145 | 0 | return newResource; |
| 146 | |
} else { |
| 147 | 0 | return oldResource; |
| 148 | |
} |
| 149 | 0 | } catch (ResourceNotFoundException rnfe) { |
| 150 | 0 | log.error(MessageFormat.format(RESOURCE_NOT_FOUND, name)); |
| 151 | 0 | throw rnfe; |
| 152 | 0 | } catch (ParseErrorException pee) { |
| 153 | 0 | log.error(RESOURCE_PARSE_EXCEPTION, pee); |
| 154 | 0 | throw pee; |
| 155 | 0 | } catch (RuntimeException re) { |
| 156 | 0 | throw re; |
| 157 | 0 | } catch (Exception e) { |
| 158 | 0 | log.error(RESOURCE_EXCEPTION, e); |
| 159 | 0 | throw e; |
| 160 | |
} finally { |
| 161 | |
|
| 162 | 0 | stopWatch.stop(); |
| 163 | |
|
| 164 | 0 | if (oldResource != null && LOG.isInfoEnabled()) { |
| 165 | 0 | LOG.info(MessageFormat.format(CHECKED_MODIFICATION, key, stopWatch.toSplitString())); |
| 166 | |
} |
| 167 | 0 | if (modified && LOG.isDebugEnabled()) { |
| 168 | 0 | LOG.debug(MessageFormat.format(LOADED_VELOCITY_RESOURCE, key, stopWatch.toString())); |
| 169 | |
} |
| 170 | |
} |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public void run() { |
| 177 | |
|
| 178 | |
try { |
| 179 | 0 | load(); |
| 180 | |
|
| 181 | 0 | } catch (ResourceNotFoundException rnfe) { |
| 182 | 0 | log.error(MessageFormat.format(RESOURCE_NOT_FOUND, name)); |
| 183 | 0 | } catch (ParseErrorException pee) { |
| 184 | 0 | log.error(RESOURCE_PARSE_EXCEPTION, pee); |
| 185 | 0 | } catch (RuntimeException re) { |
| 186 | 0 | throw re; |
| 187 | 0 | } catch (Exception e) { |
| 188 | 0 | log.error(RESOURCE_EXCEPTION, e); |
| 189 | 0 | } |
| 190 | 0 | } |
| 191 | |
} |
| 192 | |
} |