1 18 19 package org.apache.struts.examples.mailreader.memory; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.InputStream ; 26 import javax.servlet.ServletException ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.action.ActionServlet; 30 import org.apache.struts.action.PlugIn; 31 import org.apache.struts.config.ModuleConfig; 32 import org.apache.struts.examples.mailreader.Constants; 33 34 50 51 public final class MemoryDatabasePlugIn implements PlugIn { 52 53 54 56 57 60 private MemoryUserDatabase database = null; 61 62 63 66 private Log log = LogFactory.getLog(this.getClass()); 67 68 69 72 private ActionServlet servlet = null; 73 74 75 77 78 82 private String pathname = "/WEB-INF/database.xml"; 83 84 public String getPathname() { 85 return (this.pathname); 86 } 87 88 public void setPathname(String pathname) { 89 this.pathname = pathname; 90 } 91 92 93 95 96 100 public void destroy() { 101 102 log.info("Finalizing memory database plug in"); 103 104 if (database != null) { 105 try { 106 database.close(); 107 } catch (Exception e) { 108 log.error("Closing memory database", e); 109 } 110 } 111 112 servlet.getServletContext().removeAttribute(Constants.DATABASE_KEY); 113 database = null; 114 servlet = null; 115 database = null; 116 } 117 118 119 127 public void init(ActionServlet servlet, ModuleConfig config) 128 throws ServletException { 129 130 log.info("Initializing memory database plug in from '" + 131 pathname + "'"); 132 133 this.servlet = servlet; 135 136 database = new MemoryUserDatabase(); 138 try { 139 String path = calculatePath(); 140 if (log.isDebugEnabled()) { 141 log.debug(" Loading database from '" + path + "'"); 142 } 143 database.setPathname(path); 144 database.open(); 145 } catch (Exception e) { 146 log.error("Opening memory database", e); 147 throw new ServletException ("Cannot load database from '" + 148 pathname + "'", e); 149 } 150 151 servlet.getServletContext().setAttribute(Constants.DATABASE_KEY, 153 database); 154 155 } 156 157 158 160 161 163 164 166 167 173 private String calculatePath() throws Exception { 174 175 String path = servlet.getServletContext().getRealPath(pathname); 177 if (path != null) { 178 return (path); 179 } 180 181 File dir = (File ) 183 servlet.getServletContext().getAttribute 184 ("javax.servlet.context.tempdir"); 185 File file = new File (dir, "struts-example-database.xml"); 186 if (file.exists()) { 187 return (file.getAbsolutePath()); 188 } 189 190 InputStream is = 192 servlet.getServletContext().getResourceAsStream(pathname); 193 BufferedInputStream bis = new BufferedInputStream (is, 1024); 194 FileOutputStream os = 195 new FileOutputStream (file); 196 BufferedOutputStream bos = new BufferedOutputStream (os, 1024); 197 byte buffer[] = new byte[1024]; 198 while (true) { 199 int n = bis.read(buffer); 200 if (n <= 0) { 201 break; 202 } 203 bos.write(buffer, 0, n); 204 } 205 bos.close(); 206 bis.close(); 207 return (file.getAbsolutePath()); 208 209 } 210 211 212 } 213 | Popular Tags |