Skip to content

Commit

Permalink
Refactor database restore to extend PostProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
th-schwarz committed Jul 7, 2024
1 parent 20465c3 commit 4db7513
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Slf4j
public abstract class PostProcessor implements BeanPostProcessor {

private final Collection<Class> wanted;
private final Collection<Class<?>> wanted;
private final Collection<Object> initialized = new HashSet<>();
private boolean processed;

Expand All @@ -28,7 +28,7 @@ public PostProcessor() {
Collections.addAll(wanted, getWanted());
}

public abstract Class[] getWanted();
public abstract Class<?>[] getWanted();

public abstract void process(Collection<Object> wantedBeans) throws Exception;

Expand All @@ -53,7 +53,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName)
@Nullable
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Collection<Class> toBeRemoved = new HashSet<>();
Collection<Class<?>> toBeRemoved = new HashSet<>();
for (Class<?> wantedBean : wanted) {
if (wantedBean.isInstance(bean) && !initialized.contains(bean)) {
initialized.add(bean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,21 @@ public void restore() throws Exception {
assert restorePath != null;
assert restorePathBak != null;
populate(ds);
try {
Files.move(restorePath, restorePathBak, StandardCopyOption.REPLACE_EXISTING);
log.info("Database restored successful, restore dump has moved to: {}", restorePathBak);
} catch (IOException e) {
throw new RuntimeException(e);
}
renameDump();
}
} else {
log.info("Embedded database is empty, try restore it from the last dump!");
populate(ds);
renameDump();
}
}

private void renameDump() {
try {
Files.move(restorePath, restorePathBak, StandardCopyOption.REPLACE_EXISTING);
log.info("Database restored successful, restore dump has moved to: {}", restorePathBak);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand All @@ -113,7 +118,7 @@ private void populate(DataSource ds) {
}

@Override
public Class[] getWanted() {
return new Class[] {AppConfig.class, DataSource.class};
public Class<?>[] getWanted() {
return new Class<?>[] {AppConfig.class, DataSource.class};
}
}

0 comments on commit 4db7513

Please sign in to comment.