Skip to content

Commit

Permalink
SAK-42403 MessageBundleManager turned on by default (sakaiproject#7294)
Browse files Browse the repository at this point in the history
  • Loading branch information
ern authored and adrianfish committed Aug 29, 2019
1 parent 4eae5bd commit eeaa91b
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@
# version.sakai=2.9-SNAPSHOT

# Message bundle management
# DEFAULT: false
# load.bundles.from.db=true
# DEFAULT: true
# load.bundles.from.db=false

# Property added to control the number of milliseconds before rechecking database for updates, the default is 30 secs.
# DEFAULT: 30000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public interface MessageBundleService {
*/
public int SORT_FIELD_BASENAME = 5;

/**
* If the service should store bundle info in the database
*/
public boolean isEnabled();

/**
*
* @param search - text to search for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.messagebundle.api.MessageBundleProperty;
import org.sakaiproject.messagebundle.api.MessageBundleService;
import org.springframework.beans.BeanUtils;
Expand All @@ -52,6 +53,7 @@
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -89,18 +91,23 @@ public class MessageBundleServiceImpl implements MessageBundleService {
*/
private List<SaveOrUpdateCall> queue = Collections.synchronizedList(new ArrayList<>());

@Setter
private TransactionTemplate transactionTemplate;
@Getter private boolean enabled = false;

@Setter
private SessionFactory sessionFactory;
@Setter private ServerConfigurationService serverConfigurationService;
@Setter private SessionFactory sessionFactory;
@Setter private TransactionTemplate transactionTemplate;

public void init() {
timer.schedule(new SaveOrUpdateTask(), 0, scheduleDelay);
if (serverConfigurationService.getBoolean("load.bundles.from.db", true)) {
enabled = true;
timer.schedule(new SaveOrUpdateTask(), 0, scheduleDelay);
}
}

public void destroy() {
timer.cancel();
if (enabled) {
timer.cancel();
}
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -150,14 +157,12 @@ public int getSearchCount(String searchQuery, String module, String baseName, St
*/
@Transactional
public void saveOrUpdate(String baseName, String moduleName, ResourceBundle newBundle, Locale loc) {
if (StringUtils.isBlank(baseName) || StringUtils.isBlank(moduleName) || loc == null || newBundle == null) {
return;
}

if (scheduleSaves) {
queueBundle(baseName, moduleName, convertResourceBundleToMap(newBundle), loc);
} else {
saveOrUpdateInternal(baseName, moduleName, convertResourceBundleToMap(newBundle), loc);
if (enabled && newBundle != null && loc != null && (StringUtils.isNotBlank(baseName) && StringUtils.isNotBlank(moduleName))) {
if (scheduleSaves) {
queueBundle(baseName, moduleName, convertResourceBundleToMap(newBundle), loc);
} else {
saveOrUpdateInternal(baseName, moduleName, convertResourceBundleToMap(newBundle), loc);
}
}
}

Expand Down Expand Up @@ -287,22 +292,20 @@ public MessageBundleProperty getProperty(MessageBundleProperty mbp) {
public Map<String, String> getBundle(String baseName, String moduleName, Locale loc) {
Map<String, String> map = new HashMap<>();

if (StringUtils.isBlank(baseName) || StringUtils.isBlank(moduleName) || loc == null) {
return map;
}
if (enabled && loc != null && StringUtils.isNotBlank(baseName) && StringUtils.isNotBlank(moduleName)) {
Query query = sessionFactory.getCurrentSession().getNamedQuery("findPropertyWithNullValue");
query.setString("basename", baseName);
query.setString("module", moduleName);
query.setString("locale", loc.toString());
List<MessageBundleProperty> results = (List<MessageBundleProperty>) query.list();

Query query = sessionFactory.getCurrentSession().getNamedQuery("findPropertyWithNullValue");
query.setString("basename", baseName);
query.setString("module", moduleName);
query.setString("locale", loc.toString());
List<MessageBundleProperty> results = (List<MessageBundleProperty>) query.list();
for (MessageBundleProperty mbp : results) {
map.put(mbp.getPropertyName(), mbp.getValue());
}

for (MessageBundleProperty mbp : results) {
map.put(mbp.getPropertyName(), mbp.getValue());
if (map.isEmpty() && log.isDebugEnabled())
log.debug("can't find any values for: " + getIndexKeyName(baseName, moduleName, loc.toString()));
}

if (map.isEmpty() && log.isDebugEnabled())
log.debug("can't find any values for: " + getIndexKeyName(baseName, moduleName, loc.toString()));
return map;
}

Expand All @@ -327,6 +330,7 @@ public int getAllPropertiesCount() {
public List<MessageBundleProperty> getAllProperties(String locale, String module) {

Criteria query = sessionFactory.getCurrentSession().createCriteria(MessageBundleProperty.class);
query.setCacheable(true);

if (StringUtils.isNotEmpty(locale)) {
query.add(Restrictions.eq("locale", locale));
Expand Down Expand Up @@ -373,9 +377,9 @@ public List<String> getAllModuleNames() {
Criteria query = sessionFactory.getCurrentSession().createCriteria(MessageBundleProperty.class)
.setProjection(Projections.distinct(Projections.property("moduleName")))
.addOrder(Order.asc("moduleName"));
query.setCacheable(true);

List<String> results = (List<String>) query.list();
Hibernate.initialize(results);
return results;
}

Expand All @@ -385,7 +389,6 @@ public List<String> getAllBaseNames() {
.setProjection(Projections.distinct(Projections.property("baseName")))
.addOrder(Order.asc("baseName"));
List<String> results = (List<String>) query.list();
Hibernate.initialize(results);
return results;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<context:annotation-config/>
<tx:annotation-driven transaction-manager="org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager"/>

<bean id="sakai.messagebundle.mappings"
<bean id="org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappings.messagebundle"
class="org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappingsImpl">
<property name="annotatedClasses">
<list>
Expand All @@ -29,6 +29,7 @@
<property name="scheduleSaves" value="true"/>
<property name="scheduleDelay" value="5000"/>
<property name="sessionFactory" ref="org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory"/>
<property name="serverConfigurationService" ref="org.sakaiproject.component.api.ServerConfigurationService"/>
<property name="transactionTemplate">
<bean class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
import java.util.Map;
import java.util.ResourceBundle;

import org.hibernate.SessionFactory;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sakaiproject.messagebundle.api.MessageBundleProperty;
import org.sakaiproject.messagebundle.api.MessageBundleService;
import org.sakaiproject.messagebundle.impl.MessageBundleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
Expand All @@ -47,21 +45,19 @@
* To change this template use File | Settings | File Templates.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/spring-hibernate.xml"})
@ContextConfiguration(classes = {MessageBundleTestConfiguration.class})
@FixMethodOrder(NAME_ASCENDING)
public class MessageBundleTest extends AbstractTransactionalJUnit4SpringContextTests {

@Autowired
private MessageBundleService messageBundleService;

static ResourceBundle resourceBundleEN;
static ResourceBundle resourceBundleFr;

static Locale localeEn;
static Locale localeFr;

static String baseName;
static String moduleName;
private static ResourceBundle resourceBundleEN;
private static ResourceBundle resourceBundleFr;
private static Locale localeEn;
private static Locale localeFr;
private static String baseName;
private static String moduleName;

@BeforeTransaction
public void beforeTransaction() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.sakaiproject.messagebundle.impl.test;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Properties;

import javax.sql.DataSource;
import javax.transaction.TransactionManager;

import org.hibernate.SessionFactory;
import org.hibernate.dialect.HSQLDialect;
import org.hsqldb.jdbcDriver;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.hibernate.AssignableUUIDGenerator;
import org.sakaiproject.messagebundle.api.MessageBundleProperty;
import org.sakaiproject.messagebundle.api.MessageBundleService;
import org.sakaiproject.messagebundle.impl.MessageBundleServiceImpl;
import org.sakaiproject.springframework.orm.hibernate.AdditionalHibernateMappings;
import org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappingsImpl;
import org.sakaiproject.tool.api.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:/hibernate.properties")
public class MessageBundleTestConfiguration {
@Autowired
private Environment environment;

@Bean(name = "org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappings.messagebundle")
public AdditionalHibernateMappings hibernateMappings() {
Class[] annotatedClasses = new Class[] {MessageBundleProperty.class};
AdditionalHibernateMappings mappings = new AdditionalHibernateMappingsImpl();
mappings.setAnnotatedClasses(annotatedClasses);
return mappings;
}

@Bean(name = "org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory")
public SessionFactory sessionFactory(Properties hibernateProperties, AdditionalHibernateMappings mappings) {
LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(dataSource());
try {
mappings.processAdditionalMappings(sfb);
} catch (IOException e) {
e.printStackTrace();
}
sfb.addProperties(hibernateProperties);
sfb.getIdentifierGeneratorFactory().register("uuid2", AssignableUUIDGenerator.class);
return sfb.buildSessionFactory();
}

@Bean(name = "javax.sql.DataSource")
public DataSource dataSource() {
DriverManagerDataSource db = new DriverManagerDataSource();
db.setDriverClassName(environment.getProperty(org.hibernate.cfg.Environment.DRIVER, jdbcDriver.class.getName()));
db.setUrl(environment.getProperty(org.hibernate.cfg.Environment.URL, "jdbc:hsqldb:mem:test"));
db.setUsername(environment.getProperty(org.hibernate.cfg.Environment.USER, "sa"));
db.setPassword(environment.getProperty(org.hibernate.cfg.Environment.PASS, ""));
return db;
}

@Bean
public Properties hibernateProperties() {
return new Properties() {
{
setProperty(org.hibernate.cfg.Environment.DIALECT, environment.getProperty(org.hibernate.cfg.Environment.DIALECT, HSQLDialect.class.getName()));
setProperty(org.hibernate.cfg.Environment.HBM2DDL_AUTO, environment.getProperty(org.hibernate.cfg.Environment.HBM2DDL_AUTO));
setProperty(org.hibernate.cfg.Environment.ENABLE_LAZY_LOAD_NO_TRANS, environment.getProperty(org.hibernate.cfg.Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true"));
setProperty(org.hibernate.cfg.Environment.CACHE_REGION_FACTORY, environment.getProperty(org.hibernate.cfg.Environment.CACHE_REGION_FACTORY));
}
};
}

@Bean(name = "org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager")
public PlatformTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}

@Bean(name = "org.sakaiproject.component.api.ServerConfigurationService")
public ServerConfigurationService serverConfigurationService() {
ServerConfigurationService scs = mock(ServerConfigurationService.class);
when(scs.getBoolean(eq("load.bundles.from.db"), eq(true))).thenReturn(true);
return scs;
}

@Bean(name = "org.sakaiproject.messagebundle.api.MessageBundleService")
public MessageBundleService messageBundleService(ServerConfigurationService serverConfigurationService, SessionFactory sessionFactory, PlatformTransactionManager transactionManager) {
MessageBundleServiceImpl messageBundleService = new MessageBundleServiceImpl();
messageBundleService.setServerConfigurationService(serverConfigurationService);
messageBundleService.setSessionFactory(sessionFactory);
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
messageBundleService.setTransactionTemplate(transactionTemplate);
messageBundleService.setScheduleSaves(false);
messageBundleService.init();
return messageBundleService;
}
}
61 changes: 0 additions & 61 deletions kernel/kernel-impl/src/test/java/spring-hibernate.xml

This file was deleted.

Loading

0 comments on commit eeaa91b

Please sign in to comment.