1 16 17 18 package org.apache.struts.webapp.example2.memory; 19 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 java.util.ArrayList ; 27 import javax.servlet.ServletException ; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.action.ActionServlet; 31 import org.apache.struts.action.PlugIn; 32 import org.apache.struts.config.ModuleConfig; 33 import org.apache.struts.util.LabelValueBean; 34 import org.apache.struts.webapp.example2.Constants; 35 36 53 54 public final class MemoryDatabasePlugIn implements PlugIn { 55 56 57 59 60 63 private ModuleConfig config = null; 64 65 66 69 private MemoryUserDatabase database = null; 70 71 72 75 private Log log = LogFactory.getLog(this.getClass()); 76 77 78 81 private ActionServlet servlet = null; 82 83 84 86 87 91 private String pathname = "/WEB-INF/database.xml"; 92 93 public String getPathname() { 94 return (this.pathname); 95 } 96 97 public void setPathname(String pathname) { 98 this.pathname = pathname; 99 } 100 101 102 104 105 109 public void destroy() { 110 111 log.info("Finalizing memory database plug in"); 112 113 if (database != null) { 114 try { 115 database.close(); 116 } catch (Exception e) { 117 log.error("Closing memory database", e); 118 } 119 } 120 121 servlet.getServletContext().removeAttribute(Constants.DATABASE_KEY); 122 database = null; 123 servlet = null; 124 database = null; 125 config = null; 126 127 } 128 129 130 138 public void init(ActionServlet servlet, ModuleConfig config) 139 throws ServletException { 140 141 log.info("Initializing memory database plug in from '" + 142 pathname + "'"); 143 144 this.config = config; 146 this.servlet = servlet; 147 148 database = new MemoryUserDatabase(); 150 try { 151 String path = calculatePath(); 152 if (log.isDebugEnabled()) { 153 log.debug(" Loading database from '" + path + "'"); 154 } 155 database.setPathname(path); 156 database.open(); 157 } catch (Exception e) { 158 log.error("Opening memory database", e); 159 throw new ServletException ("Cannot load database from '" + 160 pathname + "'", e); 161 } 162 163 servlet.getServletContext().setAttribute(Constants.DATABASE_KEY, 165 database); 166 167 setupCache(servlet, config); 169 170 } 171 172 173 175 176 178 179 186 protected void setupCache(ActionServlet servlet, ModuleConfig config) { 187 188 ArrayList serverTypes = new ArrayList (); 190 serverTypes.add(new LabelValueBean("IMAP Protocol", "imap")); 191 serverTypes.add(new LabelValueBean("POP3 Protocol", "pop3")); 192 servlet.getServletContext().setAttribute("serverTypes", serverTypes); 193 194 } 195 196 197 198 199 201 202 208 private String calculatePath() throws Exception { 209 210 String path = servlet.getServletContext().getRealPath(pathname); 212 if (path != null) { 213 return (path); 214 } 215 216 File dir = (File ) 218 servlet.getServletContext().getAttribute 219 ("javax.servlet.context.tempdir"); 220 File file = new File (dir, "struts-example-database.xml"); 221 if (file.exists()) { 222 return (file.getAbsolutePath()); 223 } 224 225 InputStream is = 227 servlet.getServletContext().getResourceAsStream(pathname); 228 BufferedInputStream bis = new BufferedInputStream (is, 1024); 229 FileOutputStream os = 230 new FileOutputStream (file); 231 BufferedOutputStream bos = new BufferedOutputStream (os, 1024); 232 byte buffer[] = new byte[1024]; 233 while (true) { 234 int n = bis.read(buffer); 235 if (n <= 0) { 236 break; 237 } 238 bos.write(buffer, 0, n); 239 } 240 bos.close(); 241 bis.close(); 242 return (file.getAbsolutePath()); 243 244 } 245 246 247 } 248 | Popular Tags |