Permalink
Browse files

Saving custom settings to the database and loading them when the appl…

…ication starts
  • Loading branch information...
1 parent d944595 commit dfd0a318e9111bee5a663a47e69e9599c94315c0 Rafael Steil committed Feb 12, 2012
@@ -10,8 +10,6 @@
*/
package net.jforum.controllers;
-import java.io.FileInputStream;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
@@ -21,7 +19,6 @@
import net.jforum.actions.helpers.Domain;
import net.jforum.core.SecurityConstraint;
-import net.jforum.core.exceptions.ForumException;
import net.jforum.security.AdministrationRule;
import net.jforum.services.ConfigService;
import net.jforum.util.JForumConfig;
@@ -35,48 +32,34 @@
*/
@Resource
@Path(Domain.CONFIG_ADMIN)
-// @InterceptedBy(ActionSecurityInterceptor.class)
@SecurityConstraint(value = AdministrationRule.class, displayLogin = true)
public class ConfigController {
private final ConfigService service;
private final HttpServletRequest request;
private final JForumConfig config;
private final Result result;
- public ConfigController(ConfigService service, HttpServletRequest request,
- JForumConfig config, Result result) {
+ public ConfigController(ConfigService service, HttpServletRequest request, JForumConfig config, Result result) {
this.service = service;
this.request = request;
this.config = config;
this.result = result;
}
- public void list() {
+ public void list() throws Exception {
this.result.include("locales", this.loadLocaleNames());
this.result.include("config", this.config);
}
- public void save() {
+ public void save() throws Exception {
this.service.save(this.request);
this.result.redirectTo(this).list();
}
- private List<String> loadLocaleNames() {
+ private List<String> loadLocaleNames() throws Exception {
Properties locales = new Properties();
- FileInputStream fis = null;
-
- try {
- locales.load(this.getClass().getResourceAsStream(
- "/jforumConfig/languages/locales.properties"));
- } catch (IOException e) {
- throw new ForumException(e);
- } finally {
- if (fis != null) {
- try { fis.close(); }
- catch (Exception e) { }
- }
- }
+ locales.load(this.getClass().getResourceAsStream("/jforumConfig/languages/locales.properties"));
List<String> localesList = new ArrayList<String>();
@@ -23,6 +23,7 @@
@ApplicationScoped
public class PostFormatters extends ArrayList<Formatter> {
public PostFormatters(List<Formatter> list) {
+ clear();
organizeOrder(list);
}
@@ -27,8 +27,7 @@ public ConfigService(JForumConfig config, ConfigRepository repository, I18n i18n
}
public void save(HttpServletRequest request) {
- for (Enumeration<?> e = request.getParameterNames(); e
- .hasMoreElements();) {
+ for (Enumeration<?> e = request.getParameterNames(); e.hasMoreElements();) {
String key = (String) e.nextElement();
if (key.startsWith("p_")) {
@@ -51,7 +50,6 @@ public void save(HttpServletRequest request) {
}
}
- this.i18n.changeBoardDefaultLanguage(this.config
- .getValue(ConfigKeys.I18N_DEFAULT));
+ this.i18n.changeBoardDefaultLanguage(this.config.getValue(ConfigKeys.I18N_DEFAULT));
}
}
@@ -1,43 +0,0 @@
-/*
- * Copyright (c) JForum Team. All rights reserved.
- *
- * The software in this package is published under the terms of the LGPL
- * license a copy of which has been included with this distribution in the
- * license.txt file.
- *
- * The JForum Project
- * http://www.jforum.net
- */
-package net.jforum.util;
-
-import org.hibernate.SessionFactory;
-
-/**
- * @author Rafael Steil
- */
-public class HibernateAwareTask {
- private final SessionFactory sessionFactory;
-
- public HibernateAwareTask(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- public void execute(HibernateRunnable runnable) {
- // TODO
- throw new RuntimeException("Not implemented");
- /*
- try {
- TransactionSynchronizationManager.bindResource(this.sessionFactory,
- new SessionHolder(this.sessionFactory.openSession()));
- this.sessionFactory.getCurrentSession().beginTransaction();
-
- runnable.run();
-
- this.sessionFactory.getCurrentSession().getTransaction().commit();
- }
- finally {
- TransactionSynchronizationManager.unbindResource(this.sessionFactory);
- }
- */
- }
-}
@@ -1,18 +0,0 @@
-/*
- * Copyright (c) JForum Team. All rights reserved.
- *
- * The software in this package is published under the terms of the LGPL
- * license a copy of which has been included with this distribution in the
- * license.txt file.
- *
- * The JForum Project
- * http://www.jforum.net
- */
-package net.jforum.util;
-
-/**
- * @author Rafael Steil
- */
-public interface HibernateRunnable {
- public void run();
-}
@@ -24,6 +24,9 @@
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
+import org.apache.log4j.Logger;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
@@ -35,32 +38,18 @@
@Component
@ApplicationScoped
public class JForumConfig extends PropertiesConfiguration {
- private ConfigRepository configRepository;
+ private static final Logger logger = Logger.getLogger(JForumConfig.class);
+ private final SessionFactory sessionFactory;
- public JForumConfig(ServletContext servletContext /*ConfigRepository configRepository, HibernateAwareTask hibernateTask*/) {
+ public JForumConfig(ServletContext servletContext, SessionFactory sessionFactory) {
+ this.sessionFactory = sessionFactory;
this.setReloadingStrategy(new FileChangedReloadingStrategy());
this.setDelimiterParsingDisabled(true);
try {
loadProps();
setProperty(ConfigKeys.APPLICATION_PATH, servletContext.getRealPath(""));
-
- /*
- //in test environment, hibernateTask could be null
- if(hibernateTask != null){
- hibernateTask.execute(new HibernateRunnable() {
- @Override
- public void run() {
- try {
- loadDatabaseProperties();
- }
- catch (Exception e) {
- throw new ForumException(e);
- }
- }
- });
- }
- */
+ loadDatabaseProperties();
}
catch (Exception e) {
throw new ForumException(e);
@@ -87,15 +76,27 @@ private void loadCustomProperties() throws Exception {
}
}
- private void loadDatabaseProperties() throws Exception{
- if (configRepository != null) {
- List<Config> databasesProperties = this.configRepository.getAll();
+ private void loadDatabaseProperties() {
+ Session session = null;
+
+ try {
+ session = sessionFactory.openSession();
+
+ ConfigRepository repository = new ConfigRepository(session);
+ List<Config> databasesProperties = repository.getAll();
for (Config config : databasesProperties) {
this.clearProperty(config.getName());
this.addProperty(config.getName(), config.getValue());
}
}
+ catch (Exception e) {
+ logger.error("Error while trying to load custom settings from the database: " + e.getMessage(), e);
+ }
+ finally {
+ try { session.close(); }
+ catch (Exception e) {}
+ }
}
/**
@@ -138,13 +138,13 @@ username.max.length = 25
#####################
#setting for avatar allowd image info
-avatar.maxHeight = 250
-avatar.maxWidth = 200
+avatar.maxHeight = 80
+avatar.maxWidth = 80
avatar.minWidth = 20
avatar.minHeight = 20
# in bytes
-avatar.maxSize = 61440
+avatar.maxSize = 25600
# How many columns of avatars do you wish to display to the user
# when selecting a pre-defined avatar?
@@ -60,13 +60,11 @@
</tr>
<tr>
- <td class="row1">
- <p><a id="tags" class="genmed" href="<jforum:url address='/adminTag/list'/>" target="main"><jforum:i18n key="Admin.Tags"/></a></p>
- </td>
- </tr>
+ <!--
<tr>
<td class="row1"><p><a id="avatars" class="genmed" href="<jforum:url address='/adminAvatar/list'/>" target="main"><jforum:i18n key="Admin.avatars"/></a></p></td>
</tr>
+ -->
<tr>
<td class="row1"><p><a id="smilies" class="genmed" href="<jforum:url address='/adminSearchStats/list'/>" target="main"><jforum:i18n key="Admin.search"/></a></p></td>
@@ -48,7 +48,7 @@ function testEmail()
}
</script>
-<form action="<jforum:url address='/jforum'/>?module=adminConfig&action=save" method="post" name="form" id="form">
+<form action="<jforum:url address='/adminConfig/save'/>" method="post" name="form" id="form">
<table class="forumline" cellspacing="1" cellpadding="3" width="100%" border="0">
<tr>
@@ -129,7 +129,7 @@ function testEmail()
<tr>
<td class="row1" width="38%"><span class="gen"><jforum:i18n key="Config.Form.htmlTags"/></span></td>
- <td class="row1" width="38%"><span class="gen"><input type="text" size="50" name="p_html.tags.welcome" value="${config.getListAsString$1["html.tags.welcome"]}" /></span></td>
+ <td class="row1" width="38%"><span class="gen"><input type="text" size="50" name="p_html.tags.welcome" value="${config.getValue$1["html.tags.welcome"]}" /></span></td>
</tr>
<tr>

0 comments on commit dfd0a31

Please sign in to comment.