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