1 16 17 22 package org.ditchnet.jsp.taglib.tabs.listener; 23 24 import java.util.List ; 25 import java.util.Iterator ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.io.File ; 29 import java.io.Reader ; 30 import java.io.InputStream ; 31 import java.io.BufferedInputStream ; 32 import java.io.OutputStream ; 33 import java.io.BufferedOutputStream ; 34 import java.io.FileOutputStream ; 35 import java.net.URL ; 36 import javax.servlet.ServletContext ; 37 import javax.servlet.ServletContextEvent ; 38 import javax.servlet.ServletContextListener ; 39 40 56 public class TabServletContextListener implements ServletContextListener { 57 58 private static final String DEST_FOLDER_NAME = "/org.ditchnet.taglib/"; 59 public static final String SCRIPT_URI = DEST_FOLDER_NAME + "tabs.js"; 60 public static final String STYLE_URI = DEST_FOLDER_NAME + "tabstyle.jsp"; 61 62 private static final List DEST_PATHS = Arrays.asList( 63 new String [] { SCRIPT_URI, STYLE_URI }); 64 65 private static final List RESOURCE_PATHS = Arrays.asList( 66 new String [] {"/lib/js/tabs.js", "/lib/style/tabs.css" }); 67 68 private static final String IMAGE_RESOURCE_FOLDER = "/lib/images/"; 69 70 private static final List IMAGE_RESOURCE_NAMES = Arrays.asList( 71 new String [] {"default_tab_bg_left.gif","default_tab_bg_right.gif", 72 "default_tab_bg_white_left.gif","default_tab_bg_white_right.gif"}); 73 74 79 public void contextInitialized(final ServletContextEvent evt) { 80 ServletContext servletContext = evt.getServletContext(); 81 createDitchnetDir(servletContext); 82 String sourcePath,destPath,fileName; 83 URL sourceURL; 84 int i = 0; 85 for (Iterator iter = RESOURCE_PATHS.iterator(); iter.hasNext();) { 86 sourcePath = (String )iter.next(); 87 sourceURL = getClass().getResource(sourcePath); 88 destPath = (String )DEST_PATHS.get(i); 89 destPath = servletContext.getRealPath(destPath); 90 writeFile(sourceURL,destPath,servletContext); 91 i++; 92 } 93 for (Iterator iter = IMAGE_RESOURCE_NAMES.iterator(); iter.hasNext();) { 94 fileName = (String )iter.next(); 95 sourcePath = IMAGE_RESOURCE_FOLDER + fileName; 96 sourceURL = getClass().getResource(sourcePath); 97 destPath = DEST_FOLDER_NAME + fileName; 98 destPath = servletContext.getRealPath(destPath); 99 writeFile(sourceURL,destPath,servletContext); 100 } 101 } 102 103 public void contextDestroyed(final ServletContextEvent evt) { } 104 105 private void createDitchnetDir(final ServletContext servletContext) { 106 String dirPath = servletContext.getRealPath(DEST_FOLDER_NAME); 107 File dir = null; 108 try { 109 dir = new File (dirPath); 110 dir.mkdir(); 111 } catch (Exception e) { 112 } 114 } 115 116 private void writeFile(final URL fromURL, 117 final String toPath, 118 final ServletContext servletContext) { 119 InputStream in = null; 120 OutputStream out = null; 121 try { 122 in = new BufferedInputStream (fromURL.openStream()); 123 out = new BufferedOutputStream (new FileOutputStream (toPath)); 124 int len; 125 byte [] buffer = new byte [4096]; 126 while ((len = in.read(buffer,0,buffer.length)) != -1) { 127 out.write(buffer,0,len); 128 } 129 out.flush(); 130 } catch (Exception e) { 131 } finally { 133 try { in.close(); out.close(); } catch (Exception e) { } 134 } 135 } 136 137 } 138 | Popular Tags |