Skip to content

Commit

Permalink
feat: Add config options viw system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuJLund committed Dec 8, 2024
1 parent 5646381 commit e3abfa0
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ private HibernateUtil() {

static {
try {
sessionFactory = new Configuration().configure()
String hibernateConfig = System.getProperty("hibernate.config",
"hibernate.cfg.xml");
sessionFactory = new Configuration().configure(hibernateConfig)
.buildSessionFactory();
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ public static synchronized AppDataService getInstance() {
}

private AppDataServiceImpl() {
var message = new Message(MockDataGenerator.createMessage(),
LocalDateTime.now());
messageDao.updateMessage(message);
logger.info("Generated mock app data");
var env = System.getProperty("generate.data");
if (env == null || env.equals("true")) {
var message = new Message(MockDataGenerator.createMessage(),
LocalDateTime.now());
messageDao.updateMessage(message);
logger.info("Generated mock app data");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ public class ProductDataServiceImpl implements ProductDataService {
private static ProductDataServiceImpl instance;
private ProductDao productDao = new ProductDao();
private DraftDao draftDao = new DraftDao();
Random random = new Random();
private Random random = new Random();
private boolean slow = false;

private ProductDataServiceImpl() {
var categories = MockDataGenerator.createCategories();
categories.forEach(cat -> productDao.updateCategory(cat));
var savedCategories = productDao.getAllCategories().stream()
.collect(Collectors.toList());
var products = MockDataGenerator.createProducts(savedCategories);
products.forEach(prod -> productDao.updateProduct(prod));
logger.info("Generated mock product data");
var backendMode = System.getProperty("backend.mode");
if (backendMode != null && backendMode.equals("slow")) {
slow = true;
}
var env = System.getProperty("generate.data");
if (env == null || env.equals("true")) {
var categories = MockDataGenerator.createCategories();
categories.forEach(cat -> productDao.updateCategory(cat));
var savedCategories = productDao.getAllCategories().stream()
.collect(Collectors.toList());
var products = MockDataGenerator.createProducts(savedCategories);
products.forEach(prod -> productDao.updateProduct(prod));
logger.info("Generated mock product data");
}
}

public static synchronized ProductDataService getInstance() {
Expand Down Expand Up @@ -81,6 +89,7 @@ public synchronized Collection<Category> getAllCategories() {
@Override
public synchronized void deleteCategory(Integer id) {
Objects.requireNonNull(id, ID_CANT_BE_NULL);
randomWait(2);
var category = productDao.getCategory(id);
if (category == null) {
throw new IllegalArgumentException("Category not found");
Expand Down Expand Up @@ -111,6 +120,7 @@ public synchronized Category updateCategory(Category category) {
@Override
public void saveDraft(User user, Product draftProduct) {
Objects.requireNonNull(user, "user can't be null");
randomWait(1);
logger.info("Saving draft for user '{}'", user.getName());
if (draftProduct == null) {
draftDao.deleteDraft(user);
Expand All @@ -130,6 +140,9 @@ public Product findDraft(User user) {

@SuppressWarnings("java:S2142")
private void randomWait(int count) {
if (!slow) {
return;
}
int wait = 20 + random.nextInt(40);
try {
Thread.sleep(wait * (long) count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class UserServiceImpl implements UserService {

private UserDao userDao = new UserDao();
private Random random = new Random();
private boolean slow = false;

private static UserServiceImpl instance;

Expand All @@ -27,9 +28,16 @@ public static synchronized UserService getInstance() {
}

private UserServiceImpl() {
var users = MockDataGenerator.createUsers();
users.forEach(user -> userDao.updateUser(user));
logger.info("Generated mock user data");
var backendMode = System.getProperty("backend.mode");
if (backendMode != null && backendMode.equals("slow")) {
slow = true;
}
var env = System.getProperty("generate.data");
if (env == null || env.equals("true")) {
var users = MockDataGenerator.createUsers();
users.forEach(user -> userDao.updateUser(user));
logger.info("Generated mock user data");
}
}

@Override
Expand Down Expand Up @@ -74,6 +82,9 @@ public synchronized java.util.List<User> getAllUsers() {

@SuppressWarnings("java:S2142")
private void randomWait(int count) {
if (!slow) {
return;
}
int wait = 10 + random.nextInt(20);
try {
Thread.sleep(wait * (long) count);
Expand Down
1 change: 0 additions & 1 deletion vaadincreate-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@
</plugins>
</build>
</profile>

</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.vaadin.tatu.vaadincreate;

import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -38,6 +39,8 @@
import com.vaadin.annotations.Viewport;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.CustomizedSystemMessages;
import com.vaadin.server.DefaultDeploymentConfiguration;
import com.vaadin.server.DeploymentConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinResponse;
import com.vaadin.server.VaadinServlet;
Expand Down Expand Up @@ -292,6 +295,19 @@ public static class Servlet extends VaadinServlet {

final AtomicInteger exceptionCount = new AtomicInteger(1);

@Override
protected DeploymentConfiguration createDeploymentConfiguration(
Properties initParameters) {
var env = System.getProperty("vaadin.productionMode");
var productionMode = "false";
if (env != null) {
productionMode = env.equals("true") ? "true" : "false";
}
initParameters.put("productionMode", productionMode);
return new DefaultDeploymentConfiguration(getClass(),
initParameters);
}

@Override
protected void servletInitialized() {
// Disable session expired notification and redirect to login view
Expand Down

0 comments on commit e3abfa0

Please sign in to comment.