1 15 package org.apache.tapestry.asset; 16 17 import java.io.BufferedInputStream ; 18 import java.io.BufferedOutputStream ; 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.net.URL ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import javax.servlet.ServletContext ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.hivemind.ApplicationRuntimeException; 32 import org.apache.hivemind.ClassResolver; 33 import org.apache.tapestry.Tapestry; 34 import org.apache.tapestry.engine.IPropertySource; 35 import org.apache.tapestry.util.StringSplitter; 36 37 87 88 public class AssetExternalizerImpl implements AssetExternalizer 89 { 90 91 private Log _log; 92 93 private ClassResolver _resolver; 94 95 96 private IPropertySource _propertySource; 97 98 private File _assetDir; 99 100 private String _URL; 101 102 105 106 private Map _resources = new HashMap (); 107 108 private static final int BUFFER_SIZE = 2048; 109 110 public void initializeService() 111 { 112 String directory = _propertySource.getPropertyValue("org.apache.tapestry.asset.dir"); 113 114 if (directory == null) 115 return; 116 117 _URL = _propertySource.getPropertyValue("org.apache.tapestry.asset.URL"); 118 119 if (_URL == null) 120 return; 121 122 _assetDir = new File (directory); 123 124 _log.debug("Initialized with directory " + _assetDir + " mapped to " + _URL); 125 } 126 127 protected void externalize(String resourcePath) throws IOException 128 { 129 if (_log.isDebugEnabled()) 130 _log.debug("Externalizing " + resourcePath); 131 132 File file = _assetDir; 133 134 136 StringSplitter splitter = new StringSplitter('/'); 137 138 String [] path = splitter.splitToArray(resourcePath); 139 140 143 for (int i = 0; i < path.length - 1; i++) 144 { 145 147 file = new File (file, path[i]); 148 } 149 150 152 file.mkdirs(); 153 154 file = new File (file, path[path.length - 1]); 155 156 160 if (file.exists()) 161 return; 162 163 165 URL inputURL = _resolver.getResource(resourcePath); 166 if (inputURL == null) 167 throw new IOException (Tapestry.format("missing-resource", resourcePath)); 168 169 InputStream in = null; 170 OutputStream out = null; 171 172 try 173 { 174 in = new BufferedInputStream (inputURL.openStream()); 175 176 out = new BufferedOutputStream (new FileOutputStream (file)); 177 178 byte[] buffer = new byte[BUFFER_SIZE]; 179 180 while (true) 181 { 182 int bytesRead = in.read(buffer, 0, BUFFER_SIZE); 183 if (bytesRead < 0) 184 break; 185 186 out.write(buffer, 0, bytesRead); 187 } 188 } 189 finally 190 { 191 close(in); 192 close(out); 193 } 194 195 } 197 198 private void close(InputStream in) 199 { 200 if (in != null) 201 try 202 { 203 in.close(); 204 } 205 catch (IOException ex) 206 { 207 } 209 } 210 211 private void close(OutputStream out) 212 { 213 if (out != null) 214 215 try 216 { 217 out.close(); 218 } 219 catch (IOException ex) 220 { 221 } 223 } 224 225 241 242 public String getURL(String resourcePath) 243 { 244 if (_assetDir == null) 245 return null; 246 247 synchronized (_resources) 248 { 249 String result = (String ) _resources.get(resourcePath); 250 251 if (result != null) 252 return result; 253 254 try 255 { 256 externalize(resourcePath); 257 } 258 catch (IOException ex) 259 { 260 throw new ApplicationRuntimeException(Tapestry.format( 261 "AssetExternalizer.externalize-failure", 262 resourcePath, 263 _assetDir), ex); 264 } 265 266 result = _URL + resourcePath; 267 268 _resources.put(resourcePath, result); 269 270 return result; 271 } 272 } 273 274 275 public void setLog(Log log) 276 { 277 _log = log; 278 } 279 280 281 public void setClassResolver(ClassResolver resolver) 282 { 283 _resolver = resolver; 284 } 285 286 287 public void setPropertySource(IPropertySource propertySource) 288 { 289 _propertySource = propertySource; 290 } 291 } | Popular Tags |