Skip to content

Commit

Permalink
Add global configuration filter to load activated registry center and…
Browse files Browse the repository at this point in the history
… event trace data source.
  • Loading branch information
haocao committed Apr 7, 2017
1 parent af710b9 commit 47b00c2
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.dangdang.ddframe.job.lite.console;

import com.dangdang.ddframe.job.lite.console.filter.GlobalConfigurationFilter;
import com.dangdang.ddframe.job.lite.console.restful.JobOperationRestfulApi;
import com.dangdang.ddframe.job.restful.RestfulServer;
import com.dangdang.ddframe.job.security.WwwAuthFilter;
Expand Down Expand Up @@ -45,7 +46,8 @@ public final class ConsoleBootstrap {
public static void main(final String[] args) throws Exception {
//CHECKSTYLE:ON
RestfulServer restfulServer = new RestfulServer(PORT);
restfulServer.addFilter(WwwAuthFilter.class, "/")
restfulServer.addFilter(GlobalConfigurationFilter.class, "*.html")
.addFilter(WwwAuthFilter.class, "/")
.addFilter(WwwAuthFilter.class, "*.html")
.start(JobOperationRestfulApi.class.getPackage().getName(), Optional.of(CONSOLE_PATH));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.job.lite.console.filter;

import com.dangdang.ddframe.job.lite.console.domain.EventTraceDataSourceConfiguration;
import com.dangdang.ddframe.job.lite.console.domain.EventTraceDataSourceFactory;
import com.dangdang.ddframe.job.lite.console.domain.RegistryCenterConfiguration;
import com.dangdang.ddframe.job.lite.console.service.EventTraceDataSourceConfigurationService;
import com.dangdang.ddframe.job.lite.console.service.RegistryCenterConfigurationService;
import com.dangdang.ddframe.job.lite.console.service.impl.EventTraceDataSourceConfigurationServiceImpl;
import com.dangdang.ddframe.job.lite.console.service.impl.RegistryCenterConfigurationServiceImpl;
import com.dangdang.ddframe.job.lite.console.util.SessionEventTraceDataSourceConfiguration;
import com.dangdang.ddframe.job.lite.console.util.SessionRegistryCenterConfiguration;
import com.dangdang.ddframe.job.lite.lifecycle.internal.reg.RegistryCenterFactory;
import com.dangdang.ddframe.job.reg.exception.RegException;
import com.google.common.base.Optional;

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.HttpSession;
import java.io.IOException;

import static com.dangdang.ddframe.job.lite.console.restful.config.EventTraceDataSourceRestfulApi.DATA_SOURCE_CONFIG_KEY;
import static com.dangdang.ddframe.job.lite.console.restful.config.RegistryCenterRestfulApi.REG_CENTER_CONFIG_KEY;

public class GlobalConfigurationFilter implements Filter {

private final RegistryCenterConfigurationService regCenterService = new RegistryCenterConfigurationServiceImpl();

private final EventTraceDataSourceConfigurationService rdbService = new EventTraceDataSourceConfigurationServiceImpl();

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpSession httpSession = httpRequest.getSession();
if (null == httpSession.getAttribute(REG_CENTER_CONFIG_KEY)) {
loadActivatedRegCenter(httpSession);
}
if (null == httpSession.getAttribute(DATA_SOURCE_CONFIG_KEY)) {
loadActivatedEventTraceDataSource(httpSession);
}
filterChain.doFilter(servletRequest, servletResponse);
}

private void loadActivatedRegCenter(final HttpSession httpSession) {
Optional<RegistryCenterConfiguration> config = regCenterService.loadActivated();
if (config.isPresent()) {
String configName = config.get().getName();
boolean isConnected = setRegistryCenterNameToSession(regCenterService.find(configName, regCenterService.loadAll()), httpSession);
if (isConnected) {
regCenterService.load(configName);
}
}
}

private boolean setRegistryCenterNameToSession(final RegistryCenterConfiguration regCenterConfig, final HttpSession session) {
session.setAttribute(REG_CENTER_CONFIG_KEY, regCenterConfig);
try {
RegistryCenterFactory.createCoordinatorRegistryCenter(regCenterConfig.getZkAddressList(), regCenterConfig.getNamespace(), Optional.fromNullable(regCenterConfig.getDigest()));
SessionRegistryCenterConfiguration.setRegistryCenterConfiguration((RegistryCenterConfiguration) session.getAttribute(REG_CENTER_CONFIG_KEY));
} catch (final RegException ex) {
return false;
}
return true;
}

private void loadActivatedEventTraceDataSource(final HttpSession httpSession) {
Optional<EventTraceDataSourceConfiguration> config = rdbService.loadActivated();
if (config.isPresent()) {
String configName = config.get().getName();
boolean isConnected = setEventTraceDataSourceNameToSession(rdbService.find(configName, rdbService.loadAll()), httpSession);
if (isConnected) {
rdbService.load(configName);
}
}
}

private boolean setEventTraceDataSourceNameToSession(final EventTraceDataSourceConfiguration dataSourceConfig, final HttpSession session) {
session.setAttribute(DATA_SOURCE_CONFIG_KEY, dataSourceConfig);
try {
EventTraceDataSourceFactory.createEventTraceDataSource(dataSourceConfig.getDriver(), dataSourceConfig.getUrl(),
dataSourceConfig.getUsername(), Optional.fromNullable(dataSourceConfig.getPassword()));
SessionEventTraceDataSourceConfiguration.setDataSourceConfiguration((EventTraceDataSourceConfiguration) session.getAttribute(DATA_SOURCE_CONFIG_KEY));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
return false;
}
return true;
}

@Override
public void destroy() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ <h4>历史作业执行状态 </h4>
<label for="state">状态:</label>
<input type="text" class="form-control" id="state" placeholder="">
</div>
<br/>
<div class="form-group toolbar">
<label>创建时间:</label>
<input type="text" class="form-control pull-right custom-datepicker" id="start-time">
Expand Down

0 comments on commit 47b00c2

Please sign in to comment.