Skip to content

Commit

Permalink
for sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jun 5, 2017
1 parent 3583dc4 commit 993c3c9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private Properties getProperties() {
try (FileInputStream fileInputStream = new FileInputStream(PROPERTIES_PATH)) {
result.load(fileInputStream);
} catch (final IOException ex) {
log.warn("Can not load properties file from path: '{}'.", PROPERTIES_PATH);
}
setPropertiesByEnv(result);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ public Collection<JobContext> getAllEligibleJobContexts() {
continue;
}
List<Integer> assignedShardingItems = getAssignedShardingItems(each, taskIdList, assignedTasks);
if (!assignedShardingItems.isEmpty()) {
if (jobConfig.isPresent()) {
result.add(new JobContext(jobConfig.get(), assignedShardingItems, ExecutionType.FAILOVER));
}
if (!assignedShardingItems.isEmpty() && jobConfig.isPresent()) {
result.add(new JobContext(jobConfig.get(), assignedShardingItems, ExecutionType.FAILOVER));
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
@Slf4j
public final class JobRunningStatisticJob extends AbstractStatisticJob {

private static final StatisticInterval EXECUTE_INTERVAL = StatisticInterval.MINUTE;

private RunningService runningService;

private StatisticRdbRepository repository;

private final StatisticInterval execInterval = StatisticInterval.MINUTE;

/**
* 构造函数.
* @param registryCenter 注册中心
Expand All @@ -78,7 +78,7 @@ public JobDetail buildJobDetail() {
public Trigger buildTrigger() {
return TriggerBuilder.newTrigger()
.withIdentity(getTriggerName())
.withSchedule(CronScheduleBuilder.cronSchedule(execInterval.getCron())
.withSchedule(CronScheduleBuilder.cronSchedule(EXECUTE_INTERVAL.getCron())
.withMisfireHandlingInstructionDoNothing()).build();
}

Expand All @@ -102,7 +102,7 @@ private void statisticJob(final int runningCount) {
if (latestOne.isPresent()) {
fillBlankIfNeeded(latestOne.get());
}
JobRunningStatistics jobRunningStatistics = new JobRunningStatistics(runningCount, StatisticTimeUtils.getCurrentStatisticTime(execInterval));
JobRunningStatistics jobRunningStatistics = new JobRunningStatistics(runningCount, StatisticTimeUtils.getCurrentStatisticTime(EXECUTE_INTERVAL));
log.debug("Add jobRunningStatistics, runningCount is:{}", runningCount);
repository.add(jobRunningStatistics);
}
Expand All @@ -112,7 +112,7 @@ private void statisticTask(final int runningCount) {
if (latestOne.isPresent()) {
fillBlankIfNeeded(latestOne.get());
}
TaskRunningStatistics taskRunningStatistics = new TaskRunningStatistics(runningCount, StatisticTimeUtils.getCurrentStatisticTime(execInterval));
TaskRunningStatistics taskRunningStatistics = new TaskRunningStatistics(runningCount, StatisticTimeUtils.getCurrentStatisticTime(EXECUTE_INTERVAL));
log.debug("Add taskRunningStatistics, runningCount is:{}", runningCount);
repository.add(taskRunningStatistics);
}
Expand All @@ -136,7 +136,7 @@ private int getTaskRunningCount(final Map<String, Set<TaskContext>> allRunningTa
}

private void fillBlankIfNeeded(final JobRunningStatistics latestOne) {
List<Date> blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), execInterval);
List<Date> blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), EXECUTE_INTERVAL);
if (!blankDateRange.isEmpty()) {
log.debug("Fill blank range of jobRunningStatistics, range is:{}", blankDateRange);
}
Expand All @@ -146,7 +146,7 @@ private void fillBlankIfNeeded(final JobRunningStatistics latestOne) {
}

private void fillBlankIfNeeded(final TaskRunningStatistics latestOne) {
List<Date> blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), execInterval);
List<Date> blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), EXECUTE_INTERVAL);
if (!blankDateRange.isEmpty()) {
log.debug("Fill blank range of taskRunningStatistics, range is:{}", blankDateRange);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public RestfulServer addFilter(final Class<? extends Filter> filterClass, final
}

private ServletContextHandler buildServletContextHandler() {
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
return servletContextHandler;
ServletContextHandler result = new ServletContextHandler(ServletContextHandler.SESSIONS);
result.setContextPath("/");
return result;
}

private ServletHolder getServletHolder(final String packages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,48 @@

package com.dangdang.ddframe.job.security;

import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.servlet.*;
import com.google.common.base.Strings;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

/**
* 认证过滤器.
*
* @author zhangliang
*/
@Slf4j
public final class WwwAuthFilter implements Filter {

private static final String FILE_SEPARATOR = System.getProperty("file.separator");

private static final String AUTH_PREFIX = "Basic ";

private static final String GUEST = "guest";

private static final String ROOT = "root";


private static final String ROOT_IDENTIFY = "root";

private static final String ROOT_DEFAULT_USERNAME = "root";

private static final String ROOT_DEFAULT_PASSWORD = "root";

private static final String GUEST_IDENTIFY = "guest";

private static final String GUEST_DEFAULT_USERNAME = "guest";

private static final String GUEST_DEFAULT_PASSWORD = "guest";


private String rootUsername;

private String rootPassword;
Expand All @@ -47,26 +69,20 @@ public final class WwwAuthFilter implements Filter {

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
String fileSeparator = System.getProperty("file.separator");
String configFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + fileSeparator + "conf" + fileSeparator + "auth.properties";
Properties props = new Properties();
try {
props.load(new FileInputStream(configFilePath));
} catch (final IOException ex) {
log.warn("Cannot found auth config file, use default auth config.");
}
if (Strings.isNullOrEmpty(props.getProperty("root.username"))) {
rootUsername = "root";
} else {
rootUsername = props.getProperty("root.username");
}
if (Strings.isNullOrEmpty(props.getProperty("guest.username"))) {
guestUsername = "guest";
} else {
guestUsername = props.getProperty("guest.username");
URL classLoaderURL = Thread.currentThread().getContextClassLoader().getResource("");
if (null != classLoaderURL) {
String configFilePath = Joiner.on(FILE_SEPARATOR).join(classLoaderURL.getPath(), "conf", "auth.properties");
try {
props.load(new FileInputStream(configFilePath));
} catch (final IOException ex) {
log.warn("Cannot found auth config file, use default auth config.");
}
}
rootPassword = props.getProperty("root.password", "root");
guestPassword = props.getProperty("guest.password", "guest");
rootUsername = props.getProperty("root.username", ROOT_DEFAULT_USERNAME);
rootPassword = props.getProperty("root.password", ROOT_DEFAULT_PASSWORD);
guestUsername = props.getProperty("guest.username", GUEST_DEFAULT_USERNAME);
guestPassword = props.getProperty("guest.password", GUEST_DEFAULT_PASSWORD);
}

@Override
Expand All @@ -83,22 +99,22 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
authenticateSuccess(httpResponse, true);
chain.doFilter(httpRequest, httpResponse);
} else {
needAuthenticate(httpRequest, httpResponse);
needAuthenticate(httpResponse);
}
} else {
needAuthenticate(httpRequest, httpResponse);
needAuthenticate(httpResponse);
}
}

private void authenticateSuccess(final HttpServletResponse response, boolean isGuest) {
private void authenticateSuccess(final HttpServletResponse response, final boolean isGuest) {
response.setStatus(200);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
response.setHeader("identify", true == isGuest ? GUEST : ROOT);
response.setHeader("identify", isGuest ? GUEST_IDENTIFY : ROOT_IDENTIFY);
}

private void needAuthenticate(final HttpServletRequest request, final HttpServletResponse response) {
private void needAuthenticate(final HttpServletResponse response) {
response.setStatus(401);
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
Expand Down

0 comments on commit 993c3c9

Please sign in to comment.