Commit 81f12eb7 authored by shake's avatar shake
Browse files

Changed how some methods work for updating/getting settings

git-svn-id: https://subversion.umiacs.umd.edu/ace/trunk@160 f1b3a171-7291-4a19-a512-95ad0ad9394a
parent ddeb8ffe
...@@ -8,7 +8,9 @@ package edu.umiacs.ace.monitor.settings; ...@@ -8,7 +8,9 @@ package edu.umiacs.ace.monitor.settings;
import edu.umiacs.ace.util.EntityManagerServlet; import edu.umiacs.ace.util.EntityManagerServlet;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -27,25 +29,30 @@ import org.apache.commons.fileupload.util.Streams; ...@@ -27,25 +29,30 @@ import org.apache.commons.fileupload.util.Streams;
public class AddSettingServlet extends EntityManagerServlet{ public class AddSettingServlet extends EntityManagerServlet{
@Override @Override
protected void processRequest(HttpServletRequest request, HttpServletResponse response, EntityManager em) throws ServletException, IOException { protected void processRequest(HttpServletRequest request, HttpServletResponse
HashMap <String, String> customSettings = new HashMap<String, String>(); response, EntityManager em) throws ServletException, IOException {
List<SettingsParameter> customSettings = new ArrayList<SettingsParameter>();
ServletFileUpload su = new ServletFileUpload(); ServletFileUpload su = new ServletFileUpload();
try { try {
FileItemIterator iter = su.getItemIterator(request); FileItemIterator iter = su.getItemIterator(request);
while ( iter.hasNext() ) { while ( iter.hasNext() ) {
FileItemStream name = iter.next(); FileItemStream name = iter.next();
InputStream nameStream = name.openStream(); InputStream nameStream = name.openStream();
String paramName = Streams.asString(nameStream); String paramName = Streams.asString(nameStream);
FileItemStream value = null; FileItemStream value = null;
// Our form has pairs of 2 (attribute name & value),
// so grab the next item as well
if ( iter.hasNext() ) { if ( iter.hasNext() ) {
value = iter.next(); value = iter.next();
InputStream valueStream = value.openStream(); InputStream valueStream = value.openStream();
if ( name.isFormField() && value.isFormField()) { if ( name.isFormField() && value.isFormField()) {
String paramValue = Streams.asString(valueStream); String paramValue = Streams.asString(valueStream);
customSettings.put(paramName, paramValue); customSettings.add(new SettingsParameter(paramName,
paramValue, true));
} }
} }
...@@ -54,7 +61,7 @@ public class AddSettingServlet extends EntityManagerServlet{ ...@@ -54,7 +61,7 @@ public class AddSettingServlet extends EntityManagerServlet{
// Logger.getLogger(SettingsServlet.class.getName()).log(Level.SEVERE, null, ex); // Logger.getLogger(SettingsServlet.class.getName()).log(Level.SEVERE, null, ex);
} }
SettingsUtil.updateSettings(customSettings, true); SettingsUtil.updateSettings(customSettings);
RequestDispatcher dispatcher = request.getRequestDispatcher("UpdateSettings"); RequestDispatcher dispatcher = request.getRequestDispatcher("UpdateSettings");
dispatcher.forward(request, response); dispatcher.forward(request, response);
......
...@@ -25,16 +25,13 @@ public class SettingsMigrationContextListener implements ServletContextListener{ ...@@ -25,16 +25,13 @@ public class SettingsMigrationContextListener implements ServletContextListener{
boolean migrated; boolean migrated;
NDC.push("[MIGRATION]"); NDC.push("[MIGRATION]");
//System.out.println("[MIGRATION] Started");
DataSource ds = PersistUtil.getDataSource(); DataSource ds = PersistUtil.getDataSource();
try { try {
conn = ds.getConnection(); conn = ds.getConnection();
migrated = hasMigrated(conn); migrated = hasMigrated(conn);
//System.out.println("[MIGRATION] " + migrated);
if ( !migrated ) { if ( !migrated ) {
//System.out.println("[MIGRATION] Inserting tables"); SettingsUtil.updateSettings(SettingsUtil.getDefaultSettings());
SettingsUtil.updateSettings(SettingsUtil.getDefaultMap(), false);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
Logger.getLogger(SettingsMigrationContextListener.class.getName()) Logger.getLogger(SettingsMigrationContextListener.class.getName())
...@@ -56,6 +53,7 @@ public class SettingsMigrationContextListener implements ServletContextListener{ ...@@ -56,6 +53,7 @@ public class SettingsMigrationContextListener implements ServletContextListener{
boolean newTable = false; boolean newTable = false;
ResultSet rs = dmd.getTables(null, null, null, types); ResultSet rs = dmd.getTables(null, null, null, types);
// Search for the new table
while( rs.next() ) { while( rs.next() ) {
String table = rs.getString("TABLE_NAME"); String table = rs.getString("TABLE_NAME");
if ( "system_settings".equals(table) ) { if ( "system_settings".equals(table) ) {
...@@ -68,6 +66,7 @@ public class SettingsMigrationContextListener implements ServletContextListener{ ...@@ -68,6 +66,7 @@ public class SettingsMigrationContextListener implements ServletContextListener{
throw new IllegalStateException("SQL patch to 1.7+ has not been installed, table 'settings' does no exist"); throw new IllegalStateException("SQL patch to 1.7+ has not been installed, table 'settings' does no exist");
} }
// Find out if there are any elements in the table
String search = "SELECT attr, value FROM system_settings"; String search = "SELECT attr, value FROM system_settings";
PreparedStatement pStmt = conn.prepareStatement(search); PreparedStatement pStmt = conn.prepareStatement(search);
......
...@@ -45,6 +45,12 @@ public class SettingsParameter implements Serializable { ...@@ -45,6 +45,12 @@ public class SettingsParameter implements Serializable {
this.custom = custom; this.custom = custom;
} }
// Custom will default to false when added to DB unless otherwise specified
public SettingsParameter(String attr, String value) {
this.attr = attr;
this.value = value;
}
public Long getId() { public Long getId() {
return id; return id;
} }
......
...@@ -4,6 +4,7 @@ import edu.umiacs.ace.util.EntityManagerServlet; ...@@ -4,6 +4,7 @@ import edu.umiacs.ace.util.EntityManagerServlet;
import edu.umiacs.util.Strings; import edu.umiacs.util.Strings;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -32,8 +33,8 @@ public class SettingsServlet extends EntityManagerServlet { ...@@ -32,8 +33,8 @@ public class SettingsServlet extends EntityManagerServlet {
@Override @Override
protected void processRequest(HttpServletRequest request, HttpServletResponse response, protected void processRequest(HttpServletRequest request, HttpServletResponse response,
EntityManager em) throws ServletException, IOException { EntityManager em) throws ServletException, IOException {
Set<String> paramSet = SettingsUtil.getParamSet(); Set<String> paramSet = SettingsUtil.getParamNames();
HashMap<String, String> settings = new HashMap<String, String>(); List<SettingsParameter> settings = new ArrayList<SettingsParameter>();
boolean update = false; boolean update = false;
// See if we have a multipart/form and take care of it // See if we have a multipart/form and take care of it
...@@ -49,7 +50,7 @@ public class SettingsServlet extends EntityManagerServlet { ...@@ -49,7 +50,7 @@ public class SettingsServlet extends EntityManagerServlet {
String name = item.getFieldName(); String name = item.getFieldName();
String value = Streams.asString(stream); String value = Streams.asString(stream);
if ( paramSet.contains(name) && !value.isEmpty() ) { if ( paramSet.contains(name) && !value.isEmpty() ) {
settings.put(name, value); settings.add(new SettingsParameter(name, value));
} }
if ( name.equals("update") ) { if ( name.equals("update") ) {
...@@ -62,9 +63,9 @@ public class SettingsServlet extends EntityManagerServlet { ...@@ -62,9 +63,9 @@ public class SettingsServlet extends EntityManagerServlet {
} }
if ( update ) { if ( update ) {
SettingsUtil.updateSettings(settings, false); SettingsUtil.updateSettings(settings);
} else { } else {
SettingsUtil.updateSettings(SettingsUtil.getDefaultMap(), false); SettingsUtil.updateSettings(SettingsUtil.getDefaultSettings());
} }
} }
......
package edu.umiacs.ace.monitor.settings; package edu.umiacs.ace.monitor.settings;
import edu.umiacs.ace.util.PersistUtil; import edu.umiacs.ace.util.PersistUtil;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
...@@ -17,6 +18,7 @@ import javax.persistence.Query; ...@@ -17,6 +18,7 @@ import javax.persistence.Query;
*/ */
public class SettingsUtil { public class SettingsUtil {
// Get an item by its attribute name
public static SettingsParameter getItemByAttr( String attr ) { public static SettingsParameter getItemByAttr( String attr ) {
EntityManager em = PersistUtil.getEntityManager(); EntityManager em = PersistUtil.getEntityManager();
Query q = em.createNamedQuery("SettingsParameter.getAttr"); Query q = em.createNamedQuery("SettingsParameter.getAttr");
...@@ -31,6 +33,7 @@ public class SettingsUtil { ...@@ -31,6 +33,7 @@ public class SettingsUtil {
return null; return null;
} }
// Return a list of all custom settings
public static List<SettingsParameter> getCustomSettings() { public static List<SettingsParameter> getCustomSettings() {
EntityManager em = PersistUtil.getEntityManager(); EntityManager em = PersistUtil.getEntityManager();
Query q = em.createNamedQuery("SettingsParameter.getCustomSettings"); Query q = em.createNamedQuery("SettingsParameter.getCustomSettings");
...@@ -38,6 +41,7 @@ public class SettingsUtil { ...@@ -38,6 +41,7 @@ public class SettingsUtil {
return q.getResultList(); return q.getResultList();
} }
// Return a list of all settings
public static List<SettingsParameter> getCurrentSettings() { public static List<SettingsParameter> getCurrentSettings() {
EntityManager em = PersistUtil.getEntityManager(); EntityManager em = PersistUtil.getEntityManager();
Query q = em.createNamedQuery("SettingsParameter.getCurrentSettings"); Query q = em.createNamedQuery("SettingsParameter.getCurrentSettings");
...@@ -45,24 +49,28 @@ public class SettingsUtil { ...@@ -45,24 +49,28 @@ public class SettingsUtil {
return q.getResultList(); return q.getResultList();
} }
public static void updateSettings(Map<String, String> settings, boolean isCustom) { // Update settings based on their name and value, and add new settings
public static void updateSettings(List<SettingsParameter> settings) {
EntityManager em = PersistUtil.getEntityManager(); EntityManager em = PersistUtil.getEntityManager();
EntityTransaction trans = em.getTransaction(); EntityTransaction trans = em.getTransaction();
trans.begin(); trans.begin();
for ( String name : settings.keySet() ) { for ( SettingsParameter setting : settings ) {
// Skip any empty strings // Skip any empty settings
if (name.trim().isEmpty() || settings.get(name).trim().isEmpty() ) { if ( setting.getName().trim().isEmpty() ||
setting.getValue().trim().isEmpty() ) {
continue; continue;
} }
SettingsParameter old = getItemByAttr(setting.getName());
SettingsParameter item = getItemByAttr(name); // If there is no item, persist the new setting
if ( item == null ) { if ( old == null ) {
em.persist(new SettingsParameter(name, settings.get(name), isCustom)); em.persist(setting);
} else { } else {
item.setValue(settings.get(name)); // Else update and merge the old item
em.merge(item); old.setValue(setting.getValue());
em.merge(old);
} }
} }
...@@ -70,73 +78,61 @@ public class SettingsUtil { ...@@ -70,73 +78,61 @@ public class SettingsUtil {
em.clear(); em.clear();
} }
public static Set<String> getParamSet() { // Get the names of all curent settings
public static Set<String> getParamNames() {
List<SettingsParameter> settings = getCurrentSettings();
Set<String> paramSet = new HashSet<String>(); Set<String> paramSet = new HashSet<String>();
paramSet.add(SettingsConstants.PARAM_4J_APPENDER); for ( SettingsParameter s : settings ) {
paramSet.add(SettingsConstants.PARAM_4J_BACKUP_INDEX); paramSet.add(s.getName());
paramSet.add(SettingsConstants.PARAM_4J_CLASS); }
paramSet.add(SettingsConstants.PARAM_4J_CONV_PAT);
paramSet.add(SettingsConstants.PARAM_4J_FILE);
paramSet.add(SettingsConstants.PARAM_4J_FILE_SIZE);
paramSet.add(SettingsConstants.PARAM_4J_IRODS);
paramSet.add(SettingsConstants.PARAM_4J_LAYOUT);
paramSet.add(SettingsConstants.PARAM_4J_ROOT_LOGGER);
paramSet.add(SettingsConstants.PARAM_BPS);
paramSet.add(SettingsConstants.PARAM_DISABLE_AUDIT);
paramSet.add(SettingsConstants.PARAM_FROM);
paramSet.add(SettingsConstants.PARAM_IMS);
paramSet.add(SettingsConstants.PARAM_IMS_PORT);
paramSet.add(SettingsConstants.PARAM_IMS_TOKEN_CLASS);
paramSet.add(SettingsConstants.PARAM_SMTP_SERVER);
paramSet.add(SettingsConstants.PARAM_THROTTLE_MAXAUDIT);
paramSet.add(SettingsConstants.PARAM_TIME);
paramSet.add(SettingsConstants.PARAM_USER_AUTH);
return paramSet; return paramSet;
} }
public static Map<String, String> getDefaultMap() {
Map<String, String> paramMap = new HashMap<String, String>(); public static List<SettingsParameter> getDefaultSettings() {
List<SettingsParameter> defaults = new ArrayList<SettingsParameter>();
paramMap.put(SettingsConstants.PARAM_IMS,
SettingsConstants.ims); defaults.add(new SettingsParameter(SettingsConstants.PARAM_IMS,
paramMap.put(SettingsConstants.PARAM_IMS_PORT, SettingsConstants.ims,false));
SettingsConstants.imsPort); defaults.add(new SettingsParameter(SettingsConstants.PARAM_IMS_PORT,
paramMap.put(SettingsConstants.PARAM_IMS_TOKEN_CLASS, SettingsConstants.imsPort,false));
SettingsConstants.imsTokenClass); defaults.add(new SettingsParameter(SettingsConstants.PARAM_IMS_TOKEN_CLASS,
paramMap.put(SettingsConstants.PARAM_DISABLE_AUDIT, SettingsConstants.imsTokenClass,false));
SettingsConstants.autoAudit); defaults.add(new SettingsParameter(SettingsConstants.PARAM_DISABLE_AUDIT,
paramMap.put(SettingsConstants.PARAM_THROTTLE_MAXAUDIT, SettingsConstants.autoAudit,false));
SettingsConstants.maxAudit); defaults.add(new SettingsParameter(SettingsConstants.PARAM_THROTTLE_MAXAUDIT,
paramMap.put(SettingsConstants.PARAM_TIME, SettingsConstants.maxAudit,false));
SettingsConstants.throttleWait); defaults.add(new SettingsParameter(SettingsConstants.PARAM_TIME,
paramMap.put(SettingsConstants.PARAM_BPS, SettingsConstants.throttleWait,false));
SettingsConstants.throttleBPS); defaults.add(new SettingsParameter(SettingsConstants.PARAM_BPS,
paramMap.put(SettingsConstants.PARAM_SMTP_SERVER, SettingsConstants.throttleBPS,false));
SettingsConstants.mailServer); defaults.add(new SettingsParameter(SettingsConstants.PARAM_SMTP_SERVER,
paramMap.put(SettingsConstants.PARAM_FROM, SettingsConstants.mailServer,false));
SettingsConstants.mailFrom); defaults.add(new SettingsParameter(SettingsConstants.PARAM_FROM,
paramMap.put(SettingsConstants.PARAM_USER_AUTH, SettingsConstants.mailFrom,false));
SettingsConstants.authManagement); defaults.add(new SettingsParameter(SettingsConstants.PARAM_USER_AUTH,
paramMap.put(SettingsConstants.PARAM_4J_APPENDER, SettingsConstants.authManagement,false));
SettingsConstants.log4JA1); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_APPENDER,
paramMap.put(SettingsConstants.PARAM_4J_BACKUP_INDEX, SettingsConstants.log4JA1,false));
SettingsConstants.log4JA1MaxBackupIndex); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_BACKUP_INDEX,
paramMap.put(SettingsConstants.PARAM_4J_CLASS, SettingsConstants.log4JA1MaxBackupIndex,false));
SettingsConstants.log4JLoggerUMIACS); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_CLASS,
paramMap.put(SettingsConstants.PARAM_4J_CONV_PAT, SettingsConstants.log4JLoggerUMIACS,false));
SettingsConstants.log4JA1layoutConversationPattern); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_CONV_PAT,
paramMap.put(SettingsConstants.PARAM_4J_FILE, SettingsConstants.log4JA1layoutConversationPattern,false));
SettingsConstants.log4JA1File); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_FILE,
paramMap.put(SettingsConstants.PARAM_4J_FILE_SIZE, SettingsConstants.log4JA1File,false));
SettingsConstants.log4JA1MaxFileSize); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_FILE_SIZE,
paramMap.put(SettingsConstants.PARAM_4J_IRODS, SettingsConstants.log4JA1MaxFileSize,false));
SettingsConstants.log4JLoggerIrods); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_IRODS,
paramMap.put(SettingsConstants.PARAM_4J_LAYOUT, SettingsConstants.log4JLoggerIrods,false));
SettingsConstants.log4JA1Layout); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_LAYOUT,
paramMap.put(SettingsConstants.PARAM_4J_ROOT_LOGGER, SettingsConstants.log4JA1Layout,false));
SettingsConstants.log4JRootLogger); defaults.add(new SettingsParameter(SettingsConstants.PARAM_4J_ROOT_LOGGER,
SettingsConstants.log4JRootLogger,false));
return paramMap; return defaults;
} }
}
}
\ No newline at end of file
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