View Javadoc
1   package com.atlassian.activeobjects.testplugin;
2   
3   import com.atlassian.activeobjects.external.ActiveObjects;
4   import com.atlassian.activeobjects.spi.Backup;
5   import com.atlassian.activeobjects.spi.NullBackupProgressMonitor;
6   import com.atlassian.activeobjects.spi.NullRestoreProgressMonitor;
7   import com.atlassian.activeobjects.test.model.Author;
8   import com.atlassian.activeobjects.test.model.Model;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  import org.springframework.beans.factory.InitializingBean;
12  
13  import javax.servlet.ServletException;
14  import javax.servlet.http.HttpServlet;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.io.ByteArrayInputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.io.UnsupportedEncodingException;
22  
23  import static com.google.common.base.Preconditions.checkNotNull;
24  
25  public class BackupTestServlet extends HttpServlet implements InitializingBean {
26      public static final String CREATE = "create";
27      public static final String BACKUP = "backup";
28      public static final String DELETE = "delete";
29  
30      private final ActiveObjects ao;
31      private final Backup backup;
32  
33      private static Logger log = LoggerFactory.getLogger(BackupTestServlet.class);
34  
35      public BackupTestServlet(ActiveObjects ao, Backup backup) {
36          this.ao = checkNotNull(ao);
37          this.backup = checkNotNull(backup);
38      }
39  
40      @Override
41      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
42          if (Boolean.valueOf(req.getParameter(CREATE))) {
43              new Model(ao).createData();
44          } else if (Boolean.valueOf(req.getParameter(DELETE))) {
45              new Model(ao).emptyDatabase();
46          }
47  
48          resp.setContentType("application/xml");
49          resp.setCharacterEncoding("UTF-8");
50  
51          final OutputStream os = resp.getOutputStream();
52  
53          backup.save(os, NullBackupProgressMonitor.INSTANCE);
54  
55          os.flush();
56          os.close();
57      }
58  
59      @Override
60      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
61          backup.restore(newInputStream(req.getParameter(BACKUP)), NullRestoreProgressMonitor.INSTANCE);
62      }
63  
64      private InputStream newInputStream(String backupString) {
65          try {
66              return new ByteArrayInputStream(backupString.getBytes("UTF-8"));
67          } catch (UnsupportedEncodingException e) {
68              throw new RuntimeException(e);
69          }
70      }
71  
72      @Override
73      protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
74          new Model(ao).emptyDatabase();
75      }
76  
77      @Override
78      public void afterPropertiesSet() throws Exception {
79          //accessing active objects during plugin initialization used to blow up : AO-330
80          Model model = new Model(ao);
81          try {
82  
83              model.migrateEntities();
84              model.createData();
85          } catch (Exception ex) {
86              throw new RuntimeException("Failed to access active objects during plugin initialization", ex);
87          }
88  
89          // can't use checkXXX methods as junit is not bundled in the plugin
90          int authorCount = ao.count(Author.class);
91          if (authorCount <= 0) {
92              throw new IllegalStateException("Should have found some data " + authorCount);
93          }
94  
95          model.emptyDatabase();
96  
97          log.info("succeeded in accessing active objects whilst initializing a plugin");
98      }
99  }