From 336e76f53624f6d14be26adc5c68141caeeb2ffe Mon Sep 17 00:00:00 2001 From: Joern Muehlencord Date: Wed, 1 May 2019 13:52:39 +0200 Subject: [PATCH] added caching and exception handling --- .../account/boundary/ApiKeyService.java | 3 ++- .../config/boundary/ConfigService.java | 23 ++++++++++++------- .../instance/boundary/StartupBean.java | 3 ++- .../mail/entity/MailTemplateEntity.java | 22 ++++++++++++++---- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java b/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java index 087b405..8210736 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/ApiKeyService.java @@ -15,6 +15,7 @@ */ package de.muehlencord.shared.account.business.account.boundary; +import de.muehlencord.shared.account.business.ControllerException; import de.muehlencord.shared.account.business.account.control.AccountControl; import de.muehlencord.shared.account.business.account.entity.AccountEntity; import de.muehlencord.shared.account.business.account.entity.ApiKeyEntity; @@ -89,7 +90,7 @@ public class ApiKeyService implements Serializable { password = configService.getConfigValue("rest.password"); issuer = configService.getConfigValue("rest.issuer"); expirationInMinutes = Short.parseShort(configService.getConfigValue("rest.expiration_in_minutes", "120", true)); - } catch (ConfigException | NumberFormatException ex) { + } catch (ConfigException | NumberFormatException | ControllerException ex) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(ex.toString(), ex); } else { diff --git a/account/src/main/java/de/muehlencord/shared/account/business/config/boundary/ConfigService.java b/account/src/main/java/de/muehlencord/shared/account/business/config/boundary/ConfigService.java index f3d93fd..9831b72 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/config/boundary/ConfigService.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/config/boundary/ConfigService.java @@ -15,6 +15,7 @@ */ package de.muehlencord.shared.account.business.config.boundary; +import de.muehlencord.shared.account.business.ControllerException; import de.muehlencord.shared.account.business.account.entity.Account; import de.muehlencord.shared.account.business.account.entity.AccountEntity; import de.muehlencord.shared.account.business.application.entity.ApplicationEntity; @@ -107,14 +108,14 @@ public class ConfigService implements Serializable { @Lock(LockType.READ) @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) - public String getConfigValue(String configKey, String defaultValue) throws ConfigException { + public String getConfigValue(String configKey, String defaultValue) throws ConfigException, ControllerException { return getConfigValue(configKey, defaultValue, false); } @Transactional @Lock(LockType.WRITE) @TransactionAttribute(REQUIRES_NEW) - public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue) throws ConfigException { + public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue) throws ConfigException, ControllerException { // get configValue as usual String configValue = getConfigValue(configKey); @@ -160,7 +161,7 @@ public class ConfigService implements Serializable { @Transactional @Lock(LockType.WRITE) @TransactionAttribute(REQUIRES_NEW) - public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue, Account account, boolean fallbackToSystem) throws ConfigException { + public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue, Account account, boolean fallbackToSystem) throws ConfigException, ControllerException { String configValue = getConfigValue(configKey, account, fallbackToSystem); if (configValue == null) { @@ -179,7 +180,7 @@ public class ConfigService implements Serializable { @Transactional @Lock(LockType.WRITE) @TransactionAttribute(REQUIRES_NEW) - public boolean updateConfigValue(String configKey, String configValue) throws ConfigException { + public boolean updateConfigValue(String configKey, String configValue) throws ConfigException, ControllerException { Account account = getAccount("system"); return updateConfigValue(configKey, account, configValue); } @@ -187,7 +188,7 @@ public class ConfigService implements Serializable { @Transactional @Lock(LockType.WRITE) @TransactionAttribute(REQUIRES_NEW) - public boolean updateConfigValue(String configKey, String accountName, String configValue) { + public boolean updateConfigValue(String configKey, String accountName, String configValue) throws ControllerException { Account account = getAccount(accountName); if (accountName == null) { return false; @@ -202,15 +203,14 @@ public class ConfigService implements Serializable { @Transactional @Lock(LockType.WRITE) @TransactionAttribute(REQUIRES_NEW) - public boolean updateConfigValue(String configKey, Account account, String configValue) { + public boolean updateConfigValue(String configKey, Account account, String configValue) throws ControllerException { if ((configKey == null) || (configKey.equals(""))) { // null or empty key return false; } if (account == null) { - LOGGER.error("Account must not be null, not updating"); - return false; + throw new ControllerException(ControllerException.CAUSE_CANNOT_PERSIST, "Account must not be null, not updating"); } AccountEntity accountEntity = getAccount(account.getUsername()); @@ -251,4 +251,11 @@ public class ConfigService implements Serializable { return accountList.get(0); } } + + @TransactionAttribute(TransactionAttributeType.REQUIRED) + @Transactional + @Lock(LockType.WRITE) + public void delete(ConfigEntity config) throws ControllerException { + em.remove(em.merge(config)); + } } diff --git a/account/src/main/java/de/muehlencord/shared/account/business/instance/boundary/StartupBean.java b/account/src/main/java/de/muehlencord/shared/account/business/instance/boundary/StartupBean.java index 18dc909..de4eced 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/instance/boundary/StartupBean.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/instance/boundary/StartupBean.java @@ -15,6 +15,7 @@ */ package de.muehlencord.shared.account.business.instance.boundary; +import de.muehlencord.shared.account.business.ControllerException; import de.muehlencord.shared.account.business.application.entity.ApplicationEntity; import de.muehlencord.shared.account.business.config.boundary.ConfigService; import de.muehlencord.shared.account.business.config.entity.ConfigException; @@ -55,7 +56,7 @@ public class StartupBean { configService.getConfigValue("account.maxFailedLogins", "5", true); LOGGER.info("Application startup complete"); - } catch (ConfigException ex) { + } catch (ConfigException | ControllerException ex) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(ex.toString(), ex); } else { diff --git a/account/src/main/java/de/muehlencord/shared/account/business/mail/entity/MailTemplateEntity.java b/account/src/main/java/de/muehlencord/shared/account/business/mail/entity/MailTemplateEntity.java index fffb08b..d199819 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/mail/entity/MailTemplateEntity.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/mail/entity/MailTemplateEntity.java @@ -17,11 +17,13 @@ package de.muehlencord.shared.account.business.mail.entity; import java.io.Serializable; import javax.persistence.Basic; +import javax.persistence.Cacheable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; +import javax.persistence.QueryHint; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @@ -32,16 +34,26 @@ import javax.xml.bind.annotation.XmlRootElement; * @author joern.muehlencord */ @Entity +@Cacheable @Table(name = "mail_template") @XmlRootElement @NamedQueries({ - @NamedQuery(name = "MailTemplateEntity.findAll", query = "SELECT m FROM MailTemplateEntity m"), - @NamedQuery(name = "MailTemplateEntity.findByTemplateName", query = "SELECT m FROM MailTemplateEntity m WHERE m.templateName = :templateName"), - @NamedQuery(name = "MailTemplateEntity.findByTemplateValue", query = "SELECT m FROM MailTemplateEntity m WHERE m.templateValue = :templateValue")}) + @NamedQuery(name = "MailTemplateEntity.findAll", query = "SELECT m FROM MailTemplateEntity m", + hints = { + @QueryHint(name = "org.hibernate.cacheable", value = "true"), + @QueryHint(name = "org.hibernate.cacheRegion", value = "Queries")}), + @NamedQuery(name = "MailTemplateEntity.findByTemplateName", query = "SELECT m FROM MailTemplateEntity m WHERE m.templateName = :templateName", + hints = { + @QueryHint(name = "org.hibernate.cacheable", value = "true"), + @QueryHint(name = "org.hibernate.cacheRegion", value = "Queries")}), + @NamedQuery(name = "MailTemplateEntity.findByTemplateValue", query = "SELECT m FROM MailTemplateEntity m WHERE m.templateValue = :templateValue", + hints = { + @QueryHint(name = "org.hibernate.cacheable", value = "true"), + @QueryHint(name = "org.hibernate.cacheRegion", value = "Queries")})}) public class MailTemplateEntity implements Serializable { private static final long serialVersionUID = 4527399247302581555L; - + @Id @Basic(optional = false) @NotNull @@ -106,5 +118,5 @@ public class MailTemplateEntity implements Serializable { public String toString() { return "de.muehlencord.shared.account.entity.MailTemplate[ templateName=" + templateName + " ]"; } - + }