View Javadoc
1   package common;
2   
3   import org.flywaydb.core.Flyway;
4   import org.flywaydb.core.api.configuration.FluentConfiguration;
5   import org.flywaydb.core.api.output.MigrateResult;
6   import usecase.auth.Pbkdf2PasswordHash;
7   
8   import javax.annotation.Resource;
9   import javax.ejb.TransactionManagement;
10  import javax.ejb.TransactionManagementType;
11  import javax.enterprise.context.ApplicationScoped;
12  import javax.enterprise.context.Initialized;
13  import javax.enterprise.event.Observes;
14  import javax.sql.DataSource;
15  import java.io.File;
16  import java.io.FileOutputStream;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.nio.file.Files;
20  import java.nio.file.Path;
21  import java.sql.Connection;
22  import java.sql.PreparedStatement;
23  import java.sql.SQLException;
24  import java.util.Map;
25  import java.util.zip.ZipEntry;
26  import java.util.zip.ZipInputStream;
27  
28  @TransactionManagement(TransactionManagementType.BEAN)
29  class FlywayMigrator {
30  
31      private Path uploadRoot = Path.of(System.getProperty("openejb.home"), "uploads");
32  
33      @Resource(name = "shareboardDB")
34      private DataSource dataSource;
35  
36      public void postConstruct(@Observes @Initialized(ApplicationScoped.class) Object o) throws IOException, SQLException {
37          if (dataSource == null) {
38              throw new RuntimeException("Cannot migrate, no datasource configured!");
39          }
40          FluentConfiguration config = Flyway.configure()
41                  .dataSource(dataSource)
42                  .validateOnMigrate(false)
43                  .locations("classpath:db/migration");
44          Flyway flyway = new Flyway(config);
45          MigrateResult migrate = flyway.migrate();
46          if(!migrate.migrations.isEmpty()){
47              /*add pictures*/
48              Files.createDirectories(uploadRoot);
49  
50              InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("sample/pictures.zip");
51              if(resourceAsStream == null){
52                  throw new RuntimeException("pictures.zip not found");
53              }
54  
55              try(ZipInputStream zipStream = new ZipInputStream(resourceAsStream)){
56                  ZipEntry entry = zipStream.getNextEntry();
57                  byte[] buffer = new byte[1024];
58                  while (entry != null) {
59                      File newFile = new File(uploadRoot.toFile(), entry.getName());
60                      // fix for Windows-created archives
61                      File parent = newFile.getParentFile();
62                      if (!parent.isDirectory() && !parent.mkdirs()) {
63                          throw new IOException("Failed to create directory " + parent);
64                      }
65  
66                      // write file content
67                      try(FileOutputStream fos = new FileOutputStream(newFile)){
68                          int len;
69                          while ((len = zipStream.read(buffer)) > 0) {
70                              fos.write(buffer, 0, len);
71                          }
72                          entry = zipStream.getNextEntry();
73                      }
74                  }
75              }
76  
77              /*set passwords (sql script scrambles the binary fields, TODO fix)*/
78              try(Connection con = dataSource.getConnection()){
79                  Map<String, String> credentials = Map.of(
80                          "test","test",
81                          "test2","test",
82                          "Nibiru","informatica",
83                          "admin","admin");
84                  for (Map.Entry<String, String> entry : credentials.entrySet()) {
85                      String name = entry.getKey();
86                      String pass = entry.getValue();
87                      try (PreparedStatement ps = con.prepareStatement("UPDATE user SET password=?, salt=? WHERE username = ?")) {
88                          Pbkdf2PasswordHash.HashedPassword hashAndSalt = new Pbkdf2PasswordHash().generate(pass);
89                          ps.setBytes(1,hashAndSalt.getPassword());
90                          ps.setBytes(2, hashAndSalt.getSalt());
91                          ps.setString(3,name);
92                          ps.executeUpdate();
93                      }
94                  }
95  
96              }
97          }
98      }
99  }