View Javadoc

1   package net.sf.cantina.datasource;
2   
3   import net.sf.cantina.webgui.StrutsAction;
4   import net.sf.cantina.system.ChangeDocumentAction;
5   import net.sf.cantina.DataSource;
6   import net.sf.cantina.SearchEngine;
7   import org.apache.log4j.Logger;
8   import org.apache.struts.action.*;
9   import org.apache.struts.validator.DynaValidatorActionForm;
10  import org.apache.struts.upload.FormFile;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  import java.io.InputStream;
15  import java.io.OutputStream;
16  import java.io.FileOutputStream;
17  
18  /***
19   * @author Stephane JAIS
20   */
21  public class ImportBackupAction
22    extends StrutsAction
23  {
24    private static final Logger logger = Logger.getLogger(ChangeDocumentAction.class);
25  
26    /***
27     * This action is synchronized, it will perform poorly if accessed simultaneously by many users.
28     */
29  
30    public synchronized ActionForward execute(ActionMapping mapping,
31                                 ActionForm form,
32                                 HttpServletRequest request,
33                                 HttpServletResponse response)
34      throws Exception
35    {
36      String workPath = getServlet().getServletContext().getRealPath("/WEB-INF/tmp");
37      String hypersonicPath = workPath+"/import";
38      String scriptPath = hypersonicPath+".script";
39      ActionMessages messages = new ActionMessages();
40      ActionErrors errors = new ActionErrors();
41      DynaValidatorActionForm dynaform = (DynaValidatorActionForm)form;
42      try
43      {
44        InputStream is = ((FormFile)dynaform.get("data")).getInputStream();
45        OutputStream os = new FileOutputStream(scriptPath);
46        byte[] buf = new byte[1024];
47        int i=0;
48        while((i=is.read(buf)) != -1)
49        {
50          os.write(buf,0,i);
51        }
52        is.close();
53        os.close();
54        DataSource hypersonicBackup = new HypersonicDataSource(hypersonicPath,false);
55        BackupUtils.importDataSource(hypersonicBackup,DataSource.getInstance());
56        SearchEngine.getInstance().indexAllDocuments(hypersonicBackup);
57        hypersonicBackup.release();
58      } catch (Exception e)
59      {
60        errors.add("data",new ActionError("data.file.invalid"));
61        logger.error("Could not import backup",e);
62        saveErrors(request,errors);
63        return mapping.findForward(MAPPING_INPUT);
64      }
65      messages.add("data", new ActionMessage("data.imported.successfully"));
66      saveMessages(request,messages);
67      return mapping.findForward(MAPPING_SUCCESS);
68    }
69  }