Commit 1a516d65 authored by shake's avatar shake
Browse files

Moved settings out of context.xml and into the DB.

git-svn-id: https://subversion.umiacs.umd.edu/ace/trunk@153 f1b3a171-7291-4a19-a512-95ad0ad9394a
parent 82f0d06b
......@@ -30,9 +30,13 @@
// $Id$
package edu.umiacs.ace.driver;
import edu.umiacs.ace.monitor.settings.SettingsParameter;
import edu.umiacs.ace.util.PersistUtil;
import edu.umiacs.util.Strings;
import java.util.Timer;
import java.util.TimerTask;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
......@@ -89,15 +93,22 @@ public class QueryThrottle implements ServletContextListener {
@Override
public void contextInitialized( ServletContextEvent sce ) {
String time = sce.getServletContext().getInitParameter(PARAM_TIME);
System.out.println("[QUERY]");
EntityManager em = PersistUtil.getEntityManager();
Query q = em.createNamedQuery("SettingsParameter.getAttr");
q.setParameter("attr", PARAM_TIME);
SettingsParameter s = (SettingsParameter) q.getSingleResult();
String time = s.getValue();
System.out.println("[QUERY] RESULT " + s.getName() + " " + s.getValue());
if ( Strings.isValidInt(time) ) {
setMinWait(Integer.parseInt(time));
LOG.info("Setting query throttle minwait to " + minWait);
}
String maxBpsString = sce.getServletContext().getInitParameter(PARAM_BPS);
q.setParameter("attr", PARAM_BPS);
s = (SettingsParameter) q.getSingleResult();
String maxBpsString = s.getValue();
if ( Strings.isValidLong(maxBpsString) ) {
setMaxBps(Long.parseLong(maxBpsString));
}
......
......@@ -150,7 +150,8 @@ public class ListContentsServlet extends EntityManagerServlet {
String prefix = ctxPath.substring(
0, ctxPath.lastIndexOf("/Sum"));
line = prefix + "/Path/" + c.getName() + formatPath(rs.getString(1));
os.println(line);
os.write(line.getBytes("UTF-8"));
os.println();
} else if ( TYPE_CHECKM.equals(output) ) {
String digestAlg = checkmDigestAlgFormat(c.getDigestAlgorithm());
line = formatPath(rs.getString(1)) + " | " + digestAlg + " | "
......
......@@ -37,6 +37,7 @@ import edu.umiacs.ace.util.PersistUtil;
import edu.umiacs.ace.monitor.core.Collection;
import edu.umiacs.ace.monitor.core.ConfigConstants;
import edu.umiacs.ace.monitor.core.MonitoredItem;
import edu.umiacs.ace.monitor.settings.SettingsParameter;
import edu.umiacs.util.Strings;
import java.util.List;
import java.util.Timer;
......@@ -71,22 +72,33 @@ public final class AuditConfigurationContext implements ServletContextListener {
@Override
public void contextInitialized( ServletContextEvent arg0 ) {
EntityManager em = PersistUtil.getEntityManager();
Query q = em.createNamedQuery("SettingsParameter.getAttr");
SettingsParameter s = null;
ServletContext ctx = arg0.getServletContext();
// set IMS for audit Thread from server parameter
AuditThreadFactory.setIMS(ctx.getInitParameter(PARAM_IMS));
q.setParameter("attr", PARAM_IMS);
s = (SettingsParameter) q.getSingleResult();
AuditThreadFactory.setIMS(s.getValue());
if ( Strings.isEmpty(AuditThreadFactory.getIMS()) ) {
throw new RuntimeException("IMS is empty");
}
if ( !Strings.isEmpty(ctx.getInitParameter(PARAM_IMS_TOKEN_CLASS)) ) {
String tokenClass = ctx.getInitParameter(PARAM_IMS_TOKEN_CLASS);
q.setParameter("attr", PARAM_IMS_TOKEN_CLASS);
s = (SettingsParameter) q.getSingleResult();
if ( !Strings.isEmpty(s.getValue()) ) {
String tokenClass = s.getValue();
System.out.println("IMS TOKEN CLASS: " + tokenClass);
AuditThreadFactory.setTokenClass(tokenClass);
}
if ( Strings.isValidInt(ctx.getInitParameter(PARAM_IMS_PORT)) ) {
int port = Integer.parseInt(ctx.getInitParameter(PARAM_IMS_PORT));
q.setParameter("attr", PARAM_IMS_PORT);
s = (SettingsParameter) q.getSingleResult();
if ( Strings.isValidInt(s.getValue()) ) {
int port = Integer.parseInt(s.getValue());
System.out.println("IMS PORT: " + port);
if ( port > 1 && port < 32768 ) {
AuditThreadFactory.setImsPort(port);
} else {
......@@ -97,10 +109,14 @@ public final class AuditConfigurationContext implements ServletContextListener {
PauseBean pb = new PauseBean();
ctx.setAttribute(ATTRIBUTE_PAUSE, pb);
String startPaused = ctx.getInitParameter(PARAM_DISABLE_AUDIT);
q.setParameter("attr", PARAM_DISABLE_AUDIT);
s = (SettingsParameter) q.getSingleResult();
String startPaused = s.getValue();
pb.setPaused(Boolean.valueOf(startPaused));
String maxRun = ctx.getInitParameter(PARAM_THROTTLE_MAXAUDIT);
q.setParameter("attr", PARAM_THROTTLE_MAXAUDIT);
s = (SettingsParameter) q.getSingleResult();
String maxRun = s.getValue();
if ( Strings.isValidInt(maxRun) ) {
int audit = Integer.parseInt(maxRun);
if ( audit > 0 ) {
......
......@@ -68,7 +68,8 @@ import javax.persistence.TemporalType;
"SELECT c FROM Collection c ORDER BY c.group"),
@NamedQuery(name = "Collection.getCollectionByName", query =
"SELECT c FROM Collection c WHERE c.name = :name"),
@NamedQuery(name = "Collection.listGroups", query = "SELECT DISTINCT c.group FROM Collection c")
@NamedQuery(name = "Collection.listGroups", query =
"SELECT DISTINCT c.group FROM Collection c")
})
public class Collection implements Serializable {
......
......@@ -80,15 +80,15 @@ import javax.persistence.TemporalType;
"SELECT m FROM MonitoredItem m WHERE m.parentCollection = :coll AND m.parentPath IS NULL"),
@NamedQuery(name = "MonitoredItem.listChildren", query =
"SELECT m FROM MonitoredItem m WHERE m.parentCollection = :coll AND m.parentPath = :parent"),
@NamedQuery(name = "MonitoresItem.countFilesInAllCollections", query =
@NamedQuery(name = "MonitoredItem.countFilesInAllCollections", query =
"SELECT m.parentCollection, count(m) FROM MonitoredItem m WHERE m.directory = false GROUP BY m.parentCollection"),
@NamedQuery(name = "MonitoresItem.countFilesInCollection", query =
@NamedQuery(name = "MonitoredItem.countFilesInCollection", query =
"SELECT count(m) FROM MonitoredItem m WHERE m.directory = false AND m.parentCollection = :coll"),
@NamedQuery(name = "MonitoredItem.listFilesInCollection", query =
"SELECT m FROM MonitoredItem m WHERE m.directory = false AND m.parentCollection = :coll"),
@NamedQuery(name = "MonitoresItem.countDirectoriesInAllCollections", query =
@NamedQuery(name = "MonitoredItem.countDirectoriesInAllCollections", query =
"SELECT m.parentCollection, count(m) FROM MonitoredItem m WHERE m.directory = true GROUP BY m.parentCollection"),
@NamedQuery(name = "MonitoresItem.countDirectoriesCollection", query =
@NamedQuery(name = "MonitoredItem.countDirectoriesCollection", query =
"SELECT count(m) FROM MonitoredItem m WHERE m.directory = true AND m.parentCollection = :coll"),
@NamedQuery(name = "MonitoredItem.countErrorsInCollection", query =
"SELECT count(m) FROM MonitoredItem m WHERE m.state <> 'A' AND m.parentCollection = :coll"),
......
......@@ -30,6 +30,7 @@
// $Id$
package edu.umiacs.ace.monitor.reporting;
import edu.umiacs.ace.monitor.settings.SettingsParameter;
import edu.umiacs.ace.util.PersistUtil;
import edu.umiacs.util.Strings;
import java.text.ParseException;
......@@ -74,9 +75,16 @@ public class SchedulerContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
NDC.push("[schedstartup]");
try {
mailserver = event.getServletContext().getInitParameter(
PARAM_SMTP_SERVER);
mailfrom = event.getServletContext().getInitParameter(PARAM_FROM);
EntityManager em = PersistUtil.getEntityManager();
Query q1 = em.createNamedQuery("SettingsParameter.getAttr");
q1.setParameter("attr", PARAM_SMTP_SERVER);
SettingsParameter s = (SettingsParameter)q1.getSingleResult();
mailserver = s.getValue();
q1.setParameter("attr", PARAM_FROM);
s = (SettingsParameter) q1.getSingleResult();
mailfrom = s.getValue();
if (Strings.isEmpty(mailserver)) {
mailserver = "127.0.0.1";
}
......@@ -86,9 +94,8 @@ public class SchedulerContextListener implements ServletContextListener {
sched.start();
LOG.debug("Starting the scheduler, registering all jobs");
EntityManager em = PersistUtil.getEntityManager();
Query q = em.createNamedQuery("ReportPolicy.listAll");
for (Object o : q.getResultList()) {
Query q2 = em.createNamedQuery("ReportPolicy.listAll");
for (Object o : q2.getResultList()) {
addJob((ReportPolicy) o);
}
} catch (Exception ex) {
......
package edu.umiacs.ace.monitor.settings;
/**
*
* @author shake
*/
public class SettingsConstants {
// Attributes
public static final String PARAM_IMS = "ims";
public static final String PARAM_IMS_PORT = "ims.port";
public static final String PARAM_IMS_TOKEN_CLASS = "ims.tokenclass";
public static final String PARAM_DISABLE_AUDIT = "auto.audit.disable";
public static final String PARAM_THROTTLE_MAXAUDIT = "throttle.maxaudit";
public static final String PARAM_TIME = "throttle.wait";
public static final String PARAM_BPS = "throttle.bps";
public static final String PARAM_SMTP_SERVER = "mail.server";
public static final String PARAM_FROM = "mail.from";
public static final String PARAM_USER_AUTH = "auth.management";
public static final String PARAM_4J_FILE = "log4j.appender.A1.File";
public static final String PARAM_4J_APPENDER = "log4j.appender.A1";
public static final String PARAM_4J_FILE_SIZE =
"log4j.appender.A1.maxFileSize";
public static final String PARAM_4J_BACKUP_INDEX =
"log4j.appender.A1.maxBackupIndex";
public static final String PARAM_4J_ROOT_LOGGER = "log4j.rootLogger";
public static final String PARAM_4J_LAYOUT = "log4j.appender.A1.layout";
public static final String PARAM_4J_CONV_PAT =
"log4j.appender.A1.layout.ConversionPattern";
public static final String PARAM_4J_IRODS = "log4j.logger.edu.umiacs.irods";
public static final String PARAM_4J_CLASS ="log4j.logger.edu.umiacs";
// Default Values
public static final String mailServer = "localhost.localdomain";
public static final String mailFrom = "acemail@localhost";
public static final String maxAudit = "3";
public static final String autoAudit = "true";
public static final String ims = "ims.umiacs.umd.edu";
public static final String imsPort = "8080";
public static final String imsTokenClass = "SHA-256";
public static final String authManagement = "true";
public static final String maxIngestThreads = "4";
public static final String throttleWait = "0";
public static final String throttleBPS= "0";
// Yay log4j
public static final String log4JA1File = "/tmp/aceam.log";
public static final String log4JA1 = "org.apache.log4j.RollingFileAppender";
public static final String log4JA1MaxFileSize = "100000KB";
public static final String log4JA1MaxBackupIndex = "5";
public static final String log4JRootLogger = "FATAL, A1";
public static final String log4JA1Layout = "org.apache.log4j.PatternLayout";
public static final String log4JA1layoutConversationPattern
= "%d{[dd/MMM/yyyy:HH:mm:ss]} %x%m%n";
public static final String log4JLoggerIrods = "ERROR";
public static final String log4JLoggerUMIACS = "TRACE";
}
package edu.umiacs.ace.monitor.settings;
import edu.umiacs.ace.util.PersistUtil;
import edu.umiacs.sql.SQL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
import org.apache.log4j.NDC;
/**
*
* @author shake
*/
public class SettingsMigrationContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce) {
Connection conn = null;
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());
}
} catch (SQLException ex) {
Logger.getLogger(SettingsMigrationContextListener.class.getName())
.log(Level.SEVERE, null, ex);
}
if (conn != null) {
SQL.release(conn);
}
}
public void contextDestroyed(ServletContextEvent sce) {
}
private boolean hasMigrated(Connection conn) throws SQLException {
DatabaseMetaData dmd = conn.getMetaData();
String types[] = {"Table"};
boolean migrated = false;
boolean newTable = false;
ResultSet rs = dmd.getTables(null, null, null, types);
while( rs.next() ) {
String table = rs.getString("TABLE_NAME");
if ( "system_settings".equals(table) ) {
newTable = true;
}
}
if (!newTable) {
//LOG.info("SQL patch to 1.7+ has not been installed, table 'settings' does no exist, shutting down!");
throw new IllegalStateException("SQL patch to 1.7+ has not been installed, table 'settings' does no exist");
}
String search = "SELECT attr, value FROM system_settings";
PreparedStatement pStmt = conn.prepareStatement(search);
rs = pStmt.executeQuery();
migrated = rs.first();
SQL.release(rs);
SQL.release(pStmt);
return migrated;
}
}
package edu.umiacs.ace.monitor.settings;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author shake
*/
@Entity
@Table(name = "system_settings")
@NamedQueries({
@NamedQuery(name = "SettingsParameter.getAttr", query =
"SELECT p FROM SettingsParameter p WHERE p.attr = :attr"),
@NamedQuery(name = "SettingsParameter.getAttrList", query =
"SELECT p FROM SettingsParameter p WHERE p.attr LIKE :attr")
})
public class SettingsParameter implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String attr;
private String value;
public SettingsParameter() {
this.attr = null;
this.value = null;
}
public SettingsParameter(String attr, String value) {
this.attr = attr;
this.value = value;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName(){
return attr;
}
public String getValue() {
return value;
}
public void setAttribute(String attribute) {
this.attr= attribute;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "edu.umiacs.ace.monitor.settings.settingsParameter[id=" + id + "]";
}
}
package edu.umiacs.ace.monitor.settings;
import edu.umiacs.ace.util.EntityManagerServlet;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
/**
*
* @author shake
*/
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>();
ServletFileUpload su = new ServletFileUpload();
try {
FileItemIterator iter = su.getItemIterator(request);
while ( iter.hasNext() ) {
FileItemStream item = iter.next();
InputStream stream = item.openStream();
if ( item.isFormField() ) {
String name = item.getFieldName();
String value = Streams.asString(stream);
if ( paramSet.contains(name) && !value.isEmpty()) {
settings.put(name, value);
}else {
System.out.println("Ignoring " + name + " :: " + value);
}
}
}
} catch (FileUploadException ex) {
Logger.getLogger(SettingsServlet.class.getName()).log(Level.SEVERE, null, ex);
}
SettingsUtil.updateSettings(settings);
response.sendRedirect("Status");
}
}
package edu.umiacs.ace.monitor.settings;
import edu.umiacs.ace.util.PersistUtil;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;
/**
*
* @author shake
*/
public class SettingsUtil {
public static SettingsParameter getItemByAttr( String attr ) {
EntityManager em = PersistUtil.getEntityManager();
Query q = em.createNamedQuery("SettingsParameter.getAttr");
q.setParameter("attr", attr);
try {
return (SettingsParameter) q.getSingleResult();
} catch (NoResultException ex) {
// zzz
}
return null;
}
public static void updateSettings(Map<String, String> settings) {
EntityManager em = PersistUtil.getEntityManager();
EntityTransaction trans = em.getTransaction();
trans.begin();
for ( String attr : settings.keySet() ) {
SettingsParameter item = getItemByAttr(attr);
if ( item == null ) {
System.out.println("Item es nool");
em.persist(new SettingsParameter(attr, settings.get(attr)));
} else {
item.setValue(settings.get(attr));
em.merge(item);
}
}
trans.commit();
em.clear();
}
public static Set<String> getParamSet() {
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);
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,