Commit fef0c2b7 authored by michael.simon's avatar michael.simon
Browse files

using guava cache for db message properties. added daos for message

properties.
parent 78b59af7
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.dao;
import edu.kit.scc.webreg.entity.TextPropertyEntity;
public interface TextPropertyDao extends BaseDao<TextPropertyEntity, Long> {
TextPropertyEntity findByKeyAndLang(String key, String language);
}
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.dao.jpa;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.persistence.NoResultException;
import edu.kit.scc.webreg.dao.TextPropertyDao;
import edu.kit.scc.webreg.entity.TextPropertyEntity;
@Named
@ApplicationScoped
public class JpaTextPropertyDao extends JpaBaseDao<TextPropertyEntity, Long> implements TextPropertyDao {
@Override
public TextPropertyEntity findByKeyAndLang(String key, String language) {
try {
return (TextPropertyEntity) em.createQuery("select e from TextPropertyEntity e "
+ "where e.key = :key and e.language = :language")
.setParameter("key", key)
.setParameter("language", language)
.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
@Override
public Class<TextPropertyEntity> getEntityClass() {
return TextPropertyEntity.class;
}
}
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "text_property",
uniqueConstraints = @UniqueConstraint(columnNames = {"text_key", "text_lang"}))
public class TextPropertyEntity extends AbstractBaseEntity {
private static final long serialVersionUID = 1L;
@Column(name = "text_key", nullable = false, length = 256)
private String key;
@Column(name = "text_value", nullable = false, length = 2048)
private String value;
@Column(name = "text_lang", nullable = false, length = 32)
private String language;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
package edu.kit.scc.webreg.res;
public class BundleEntry {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
......@@ -2,33 +2,67 @@ package edu.kit.scc.webreg.res;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import edu.kit.scc.webreg.dao.TextPropertyDao;
import edu.kit.scc.webreg.entity.TextPropertyEntity;
import edu.kit.scc.webreg.util.BeanHelper;
public class DbMessageBundle extends ResourceBundle {
public static final Logger logger = LoggerFactory.getLogger(DbMessageBundle.class);
public static final String BASE_NAME = "msg.messages";
private Map<String, String> messages;
private LoadingCache<String, BundleEntry> messages;
private Locale locale;
private TextPropertyDao textPropertyDao;
public DbMessageBundle() {
Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
ResourceBundle rb = ResourceBundle.getBundle(BASE_NAME, locale);
setParent(rb);
messages = new HashMap<String, String>();
messages.put("server_config", "Server Configuration");
textPropertyDao = BeanHelper.findBean("jpaTextPropertyDao");
messages = CacheBuilder.newBuilder()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(30, TimeUnit.SECONDS)
.build(new CacheLoader<String, BundleEntry>() {
public BundleEntry load(String key) {
BundleEntry be = new BundleEntry();
TextPropertyEntity tpe = textPropertyDao
.findByKeyAndLang(key, locale.getLanguage());
if (tpe != null)
be.setValue(tpe.getValue());
return be;
}
});
}
@Override
protected Object handleGetObject(String key) {
Object o = null;
if (messages != null) {
o = messages.get(key);
BundleEntry be = messages.getUnchecked(key);
if (be != null)
o = be.getValue();
}
if (o == null) {
......@@ -40,6 +74,8 @@ public class DbMessageBundle extends ResourceBundle {
@Override
public Enumeration<String> getKeys() {
return messages != null ? Collections.enumeration(messages.keySet()) : parent.getKeys();
Set<String> rets = messages.asMap().keySet();
rets.addAll(parent.keySet());
return Collections.enumeration(rets);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment