Commit d9b61d59 authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

Organize admin controllers 27.02.2015

parent f4d85f95
......@@ -17,19 +17,10 @@
package org.genesys2.server.servlet.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.monitor.LocalMapStats;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.service.*;
......@@ -40,15 +31,11 @@ import org.genesys2.server.service.worker.ITPGRFAStatusUpdater;
import org.genesys2.server.service.worker.InstituteUpdater;
import org.genesys2.server.service.worker.SGSVUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -56,7 +43,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Controller
@RequestMapping("/admin")
......@@ -93,23 +83,12 @@ public class AdminController {
@Autowired
private GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
private MappingService mappingService;
@Autowired
private CacheManager cacheManager;
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private GenesysFilterService filterService;
@Value("${list.logger.pagination.size}")
int size;
private Logger logger;
@RequestMapping("/")
public String root(Model model) {
return "/admin/index";
......@@ -201,95 +180,6 @@ public class AdminController {
return "redirect:/admin/";
}
@RequestMapping(method = RequestMethod.GET, value = "/adjustLogger")
public String adjustLogger(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page){
int size = this.size;
//first element on page
int firstelement = (page==null)?0:(page-1) * size;
List<Logger> allLoggers = getAllLoggers();
int totalPage = allLoggers.size()/size + 1;
List<Logger> loggers = new ArrayList<>();
if(size > (allLoggers.size() - firstelement)){
size = allLoggers.size() - firstelement;
}
//copy part of array
for(int i = 0; i<size; i++){
loggers.add(allLoggers.get(firstelement+i));
}
model.addAttribute("loggers", loggers);
model.addAttribute("pageNumber", page);
model.addAttribute("totalPage", totalPage);
return "/admin/adjust-logger/loggersList";
}
@RequestMapping(method = RequestMethod.GET, value = "/adjustLoggerPage/{loggerName}")
public String adjustLoggerPage(Model model, @PathVariable(value = "loggerName")String loggerName){
if (loggerName != null && "root".equalsIgnoreCase(loggerName))
logger = LogManager.getRootLogger();
else
logger = LogManager.getLogger(loggerName);
model.addAttribute("logger", logger);
model.addAttribute("appenders", logger.getRootLogger().getAllAppenders());
return "/admin/adjust-logger/adjustLoggerPage";
}
@RequestMapping(method = RequestMethod.POST, value = "/changeLoger")
public String changeLogger(@RequestParam(value = "loggerLevel") String loggerLevel,
@RequestParam(value = "loggerName") String loggerName){
if (loggerName != null && "root".equalsIgnoreCase(loggerName))
logger = LogManager.getRootLogger();
else
logger = LogManager.getLogger(loggerName);
if (logger != null) {
LOG.debug("Got logger: " + logger.getName());
logger.setLevel(loggerLevel == null ? null : Level.toLevel(loggerLevel));
}
return "redirect:/admin/adjustLoggerPage/" + loggerName+".";
}
private List<Logger> getAllLoggers(){
Enumeration<Logger> en = LogManager.getCurrentLoggers();
List<Logger> loggers = new ArrayList<>();
while (en.hasMoreElements()) {
loggers.add(en.nextElement());
}
loggers.add(LogManager.getRootLogger());
Collections.sort(loggers, new Comparator<Logger>() {
@Override
public int compare(Logger o1, Logger o2) {
// root logger is on the top
if (LogManager.getRootLogger() == o1){
return -1;
}
if (LogManager.getRootLogger() == o2){
return 1;
}
// otherwise sort by name
return o1.getName().compareTo(o2.getName());
}
});
return loggers;
}
@RequestMapping(method = RequestMethod.POST, value = "/addLoger")
public String addLogger(@RequestParam(value = "nameNewLogger") String nameNewLogger, Model model){
Logger.getLogger(nameNewLogger);
return "redirect:/admin/adjustLogger";
}
@RequestMapping(method = RequestMethod.POST, value = "/convertNames")
public String convertNames() {
// Convert {@link AllAccenames} to Aliases
......@@ -439,110 +329,4 @@ public class AdminController {
return "redirect:/admin/";
}
@RequestMapping(method = RequestMethod.POST, value = "/clearTilesCache")
public String clearTilesCache() {
final Cache tileServerCache = cacheManager.getCache("tileserver");
System.err.println("tileServerCache=" + tileServerCache.getNativeCache());
@SuppressWarnings("rawtypes")
final IMap hazelCache = (IMap) tileServerCache.getNativeCache();
LOG.info("Tiles cache size=" + hazelCache.size());
int count = 0;
for (final Object key : hazelCache.keySet()) {
LOG.info("\tkey=" + key);
if (++count > 20) {
break;
}
}
mappingService.clearCache();
LOG.info("Tiles cache size=" + hazelCache.size());
return "redirect:/admin/";
}
@RequestMapping(method = RequestMethod.POST, value = "/clearCaches")
public String clearCaches() {
for (String cacheName : cacheManager.getCacheNames()) {
final Cache cache = cacheManager.getCache(cacheName);
LOG.info("Clearing cache " + cacheName);
cache.clear();
}
return "redirect:/admin/cache";
}
@RequestMapping("/cache")
public String cacheStats(Model model) {
List<CacheStats> cacheMaps = new ArrayList<CacheStats>();
List<Object> cacheOther = new ArrayList<Object>();
Set<HazelcastInstance> instances = Hazelcast.getAllHazelcastInstances();
for (HazelcastInstance hz : instances) {
if (LOG.isDebugEnabled())
LOG.debug("\n\nCache stats Instance: " + hz.getName());
for (DistributedObject o : hz.getDistributedObjects()) {
if (o instanceof IMap) {
IMap imap = (IMap) o;
cacheMaps.add(new CacheStats(imap));
// System.out.println(imap.getServiceName() + ": " +
// imap.getName() + " " + imap.getPartitionKey());
// LocalMapStats localMapStats = imap.getLocalMapStats();
// System.out.println("created: " +
// localMapStats.getCreationTime());
// System.out.println("owned entries: " +
// localMapStats.getOwnedEntryCount());
// System.out.println("backup entries: " +
// localMapStats.getBackupEntryCount());
// System.out.println("locked entries: " +
// localMapStats.getLockedEntryCount());
// System.out.println("dirty entries: " +
// localMapStats.getDirtyEntryCount());
// System.out.println("hits: " + localMapStats.getHits());
// System.out.println("puts: " +
// localMapStats.getPutOperationCount());
// System.out.println("last update: " +
// localMapStats.getLastUpdateTime());
// System.out.println("last access:" +
// localMapStats.getLastAccessTime());
} else {
if (LOG.isDebugEnabled())
LOG.debug(o.getClass() + " " + o);
cacheOther.add(o);
}
}
}
model.addAttribute("cacheMaps", cacheMaps);
model.addAttribute("cacheOther", cacheOther);
return "/admin/cache";
}
public static final class CacheStats {
private String serviceName;
private String name;
private LocalMapStats mapStats;
public CacheStats(IMap<?, ?> imap) {
this.serviceName = imap.getServiceName();
this.name = imap.getName();
this.mapStats = imap.getLocalMapStats();
}
public String getServiceName() {
return serviceName;
}
public String getName() {
return name;
}
public LocalMapStats getMapStats() {
return mapStats;
}
}
}
package org.genesys2.server.servlet.controller.admin;
import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.monitor.LocalMapStats;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.servlet.controller.AdminController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Controller
@RequestMapping("/cache")
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class CacheController {
public static final Log LOG = LogFactory.getLog(AdminController.class);
@Autowired
private MappingService mappingService;
@Autowired
private CacheManager cacheManager;
@RequestMapping(method = RequestMethod.POST, value = "/clearTilesCache")
public String clearTilesCache() {
final Cache tileServerCache = cacheManager.getCache("tileserver");
System.err.println("tileServerCache=" + tileServerCache.getNativeCache());
@SuppressWarnings("rawtypes")
final IMap hazelCache = (IMap) tileServerCache.getNativeCache();
LOG.info("Tiles cache size=" + hazelCache.size());
int count = 0;
for (final Object key : hazelCache.keySet()) {
LOG.info("\tkey=" + key);
if (++count > 20) {
break;
}
}
mappingService.clearCache();
LOG.info("Tiles cache size=" + hazelCache.size());
return "redirect:/admin/";
}
@RequestMapping(method = RequestMethod.POST, value = "/clearCaches")
public String clearCaches() {
for (String cacheName : cacheManager.getCacheNames()) {
final Cache cache = cacheManager.getCache(cacheName);
LOG.info("Clearing cache " + cacheName);
cache.clear();
}
return "redirect:/cache/cache";
}
@RequestMapping("/cache")
public String cacheStats(Model model) {
List<CacheStats> cacheMaps = new ArrayList<CacheStats>();
List<Object> cacheOther = new ArrayList<Object>();
Set<HazelcastInstance> instances = Hazelcast.getAllHazelcastInstances();
for (HazelcastInstance hz : instances) {
if (LOG.isDebugEnabled())
LOG.debug("\n\nCache stats Instance: " + hz.getName());
for (DistributedObject o : hz.getDistributedObjects()) {
if (o instanceof IMap) {
IMap imap = (IMap) o;
cacheMaps.add(new CacheStats(imap));
// System.out.println(imap.getServiceName() + ": " +
// imap.getName() + " " + imap.getPartitionKey());
// LocalMapStats localMapStats = imap.getLocalMapStats();
// System.out.println("created: " +
// localMapStats.getCreationTime());
// System.out.println("owned entries: " +
// localMapStats.getOwnedEntryCount());
// System.out.println("backup entries: " +
// localMapStats.getBackupEntryCount());
// System.out.println("locked entries: " +
// localMapStats.getLockedEntryCount());
// System.out.println("dirty entries: " +
// localMapStats.getDirtyEntryCount());
// System.out.println("hits: " + localMapStats.getHits());
// System.out.println("puts: " +
// localMapStats.getPutOperationCount());
// System.out.println("last update: " +
// localMapStats.getLastUpdateTime());
// System.out.println("last access:" +
// localMapStats.getLastAccessTime());
} else {
if (LOG.isDebugEnabled())
LOG.debug(o.getClass() + " " + o);
cacheOther.add(o);
}
}
}
model.addAttribute("cacheMaps", cacheMaps);
model.addAttribute("cacheOther", cacheOther);
return "/admin/cache";
}
public static final class CacheStats {
private String serviceName;
private String name;
private LocalMapStats mapStats;
public CacheStats(IMap<?, ?> imap) {
this.serviceName = imap.getServiceName();
this.name = imap.getName();
this.mapStats = imap.getLocalMapStats();
}
public String getServiceName() {
return serviceName;
}
public String getName() {
return name;
}
public LocalMapStats getMapStats() {
return mapStats;
}
}
}
package org.genesys2.server.servlet.controller.admin;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.genesys2.server.servlet.controller.AdminController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.*;
@Controller
@RequestMapping("/logger")
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class LoggerController {
public static final Log LOG = LogFactory.getLog(AdminController.class);
@Value("${list.logger.pagination.size}")
int size;
private Logger logger;
@RequestMapping(method = RequestMethod.GET, value = "/adjustLogger")
public String adjustLogger(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page){
int size = this.size;
//first element on page
int firstelement = (page==null)?0:(page-1) * size;
List<Logger> allLoggers = getAllLoggers();
int totalPage = allLoggers.size()/size + 1;
List<Logger> loggers = new ArrayList<>();
if(size > (allLoggers.size() - firstelement)){
size = allLoggers.size() - firstelement;
}
//copy part of array
for(int i = 0; i<size; i++){
loggers.add(allLoggers.get(firstelement+i));
}
model.addAttribute("loggers", loggers);
model.addAttribute("pageNumber", page);
model.addAttribute("totalPage", totalPage);
return "/admin/adjust-logger/loggersList";
}
@RequestMapping(method = RequestMethod.GET, value = "/adjustLoggerPage/{loggerName}")
public String adjustLoggerPage(Model model, @PathVariable(value = "loggerName")String loggerName){
if (loggerName != null && "root".equalsIgnoreCase(loggerName))
logger = LogManager.getRootLogger();
else
logger = LogManager.getLogger(loggerName);
model.addAttribute("logger", logger);
model.addAttribute("appenders", logger.getRootLogger().getAllAppenders());
return "/admin/adjust-logger/adjustLoggerPage";
}
@RequestMapping(method = RequestMethod.POST, value = "/changeLoger")
public String changeLogger(@RequestParam(value = "loggerLevel") String loggerLevel,
@RequestParam(value = "loggerName") String loggerName){
if (loggerName != null && "root".equalsIgnoreCase(loggerName))
logger = LogManager.getRootLogger();
else
logger = LogManager.getLogger(loggerName);
if (logger != null) {
LOG.debug("Got logger: " + logger.getName());
logger.setLevel(loggerLevel == null ? null : Level.toLevel(loggerLevel));
}
return "redirect:/logger/adjustLoggerPage/" + loggerName+".";
}
private List<Logger> getAllLoggers(){
Enumeration<Logger> en = LogManager.getCurrentLoggers();
List<Logger> loggers = new ArrayList<>();
while (en.hasMoreElements()) {
loggers.add(en.nextElement());
}
loggers.add(LogManager.getRootLogger());
Collections.sort(loggers, new Comparator<Logger>() {
@Override
public int compare(Logger o1, Logger o2) {
// root logger is on the top
if (LogManager.getRootLogger() == o1) {
return -1;
}
if (LogManager.getRootLogger() == o2) {
return 1;
}
// otherwise sort by name
return o1.getName().compareTo(o2.getName());
}
});
return loggers;
}
@RequestMapping(method = RequestMethod.POST, value = "/addLoger")
public String addLogger(@RequestParam(value = "nameNewLogger") String nameNewLogger, Model model){
Logger.getLogger(nameNewLogger);
return "redirect:/admin/adjustLogger";
}
}
......@@ -32,7 +32,7 @@
<div class="change-level-label">Change level</div>
<form method="post" class="form-change-level form-inline" action=<c:url value="/admin/changeLoger"/> >
<form method="post" class="form-change-level form-inline" action=<c:url value="/logger/changeLoger"/> >
<input type="hidden" name="loggerName" value="${logger.name}"/>
......@@ -54,7 +54,7 @@
</div>
<div class="form-group">
<a class="btn btn-default" href="<c:url value="/admin/adjustLogger" />">Cancel</a>
<a class="btn btn-default" href="<c:url value="/logger/adjustLogger" />">Cancel</a>
</div>
</form>
......
......@@ -24,7 +24,7 @@
<div class="change-level-label">Add logger</div>
<form method="post" class="form-change-level form-inline" action=<c:url value="/admin/addLoger"/> >
<form method="post" class="form-change-level form-inline" action=<c:url value="/logger/addLoger"/> >
<div class="form-group col-sm-3">
<input class="form-control" type="text" name="nameNewLogger" placeholder="com.example.package.Class"/>
......@@ -49,7 +49,7 @@
<tbody>
<c:forEach items="${loggers}" var="logger" varStatus="status">
<tr class="${status.count % 2 == 0 ? 'even' : 'odd'} particular-logger">
<td><a class="adjust-logger-config" href="<c:url value="/admin/adjustLoggerPage/${logger.name}." />">${logger.name}</a></td>
<td><a class="adjust-logger-config" href="<c:url value="/logger/adjustLoggerPage/${logger.name}." />">${logger.name}</a></td>
</tr>
</c:forEach>
</tbody>
......