diff --git a/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/env/BootstrapEnvironment.java b/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/env/BootstrapEnvironment.java index 1abe3cd238..42d6a667c6 100644 --- a/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/env/BootstrapEnvironment.java +++ b/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/env/BootstrapEnvironment.java @@ -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; diff --git a/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/state/failover/FailoverService.java b/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/state/failover/FailoverService.java index dfaa62b9f7..8b6b9de9b0 100644 --- a/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/state/failover/FailoverService.java +++ b/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/state/failover/FailoverService.java @@ -105,10 +105,8 @@ public Collection getAllEligibleJobContexts() { continue; } List 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; diff --git a/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/statistics/job/JobRunningStatisticJob.java b/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/statistics/job/JobRunningStatisticJob.java index d1c908685c..32006d558b 100644 --- a/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/statistics/job/JobRunningStatisticJob.java +++ b/elastic-job-cloud/elastic-job-cloud-scheduler/src/main/java/com/dangdang/ddframe/job/cloud/scheduler/statistics/job/JobRunningStatisticJob.java @@ -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 注册中心 @@ -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(); } @@ -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); } @@ -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); } @@ -136,7 +136,7 @@ private int getTaskRunningCount(final Map> allRunningTa } private void fillBlankIfNeeded(final JobRunningStatistics latestOne) { - List blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), execInterval); + List blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), EXECUTE_INTERVAL); if (!blankDateRange.isEmpty()) { log.debug("Fill blank range of jobRunningStatistics, range is:{}", blankDateRange); } @@ -146,7 +146,7 @@ private void fillBlankIfNeeded(final JobRunningStatistics latestOne) { } private void fillBlankIfNeeded(final TaskRunningStatistics latestOne) { - List blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), execInterval); + List blankDateRange = findBlankStatisticTimes(latestOne.getStatisticsTime(), EXECUTE_INTERVAL); if (!blankDateRange.isEmpty()) { log.debug("Fill blank range of taskRunningStatistics, range is:{}", blankDateRange); } diff --git a/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/restful/RestfulServer.java b/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/restful/RestfulServer.java index 8fe8842b74..bf932e87e5 100644 --- a/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/restful/RestfulServer.java +++ b/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/restful/RestfulServer.java @@ -97,9 +97,9 @@ public RestfulServer addFilter(final Class 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) { diff --git a/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/security/WwwAuthFilter.java b/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/security/WwwAuthFilter.java index 9861ddbfeb..6f23a788c5 100644 --- a/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/security/WwwAuthFilter.java +++ b/elastic-job-common/elastic-job-common-restful/src/main/java/com/dangdang/ddframe/job/security/WwwAuthFilter.java @@ -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; @@ -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 @@ -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);