Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
upliveapp committed Jan 9, 2019
1 parent f04ae0f commit 990f425
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,30 @@
public class Configure {

private static final Logger LOGGER = LoggerFactory.getLogger(Configure.class);
private final int port;
private final String databaseName;
private final String databaseUser;
private final String databasePass;
private final String databaseHost;
private final int databasePort;
private final String databaseOpts;
private int port;
private String databaseName;
private String databaseUser;
private String databasePass;
private String databaseHost;
private int databasePort;
private String databaseOpts;

public Configure(Properties properties) {
init(properties);
}

private void init(Properties properties) {
properties.entrySet().forEach((entry) -> {
LOGGER.info("configure key = {}, value = {}", entry.getKey(), entry.getValue());
});
this.port = Integer.valueOf(properties.getProperty("switcher.port", "12306"));
this.databaseName = properties.getProperty("switcher.database.name", "switcher");
this.databaseUser = properties.getProperty("switcher.database.user", "root");
this.databasePass = properties.getProperty("switcher.database.pass", "");
this.databaseHost = properties.getProperty("switcher.database.host", "127.0.0.1");
this.databasePort = Integer.valueOf(properties.getProperty("switcher.database.port", "3306"));
this.databaseOpts = properties.getProperty("switcher.database.opts", "useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true");
}

/**
*
Expand All @@ -47,16 +64,7 @@ public Configure(String filename) {
} catch (IOException ioe) {
throw new FileNotFoundException(String.format("%s the file cannot be found in the root of the class", filename), ioe);
}
properties.entrySet().forEach((entry) -> {
LOGGER.info("configure key = {}, value = {}", entry.getKey(), entry.getValue());
});
this.port = Integer.valueOf(properties.getProperty("switcher.port", "12306"));
this.databaseName = properties.getProperty("switcher.database.name", "switcher");
this.databaseUser = properties.getProperty("switcher.database.user", "root");
this.databasePass = properties.getProperty("switcher.database.pass", "");
this.databaseHost = properties.getProperty("switcher.database.host", "127.0.0.1");
this.databasePort = Integer.valueOf(properties.getProperty("switcher.database.port", "3306"));
this.databaseOpts = properties.getProperty("switcher.database.opts", "useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true");
init(properties);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.xincao9.jswitcher.service.method.switcher.OnMethodImpl;
import com.github.xincao9.jswitcher.service.method.switcher.SetMethodImpl;
import com.github.xincao9.jswitcher.service.service.SwitcherServiceImpl;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -48,28 +49,38 @@ public SwitcherServer() {
this(DEFAULT_CONFIG_FILE);
}

public SwitcherServer(Properties properties) {
LOGGER.warn("switcher-server turning on!");
this.configure = new Configure(properties);
init();
}

/**
*
* @param filename
*/
public SwitcherServer(String filename) {
LOGGER.warn("switcher-server turning on!");
configure = new Configure(filename);
SwitcherDAO switcherDAO = new SwitcherDAO(configure);
switcherService = new SwitcherServiceImpl(configure, switcherDAO);
CheckMethodImpl checkMethodImpl = new CheckMethodImpl(switcherService);
OnMethodImpl onMethodImpl = new OnMethodImpl(switcherService);
OffMethodImpl offMethodImpl = new OffMethodImpl(switcherService);
SetMethodImpl setMethodImpl = new SetMethodImpl(switcherService);
ListMethodImpl listMethodImpl = new ListMethodImpl(switcherService);
this.configure = new Configure(filename);
init();
}

private void init() {
SwitcherDAO switcherDAO = new SwitcherDAO(this.configure);
this.switcherService = new SwitcherServiceImpl(this.configure, switcherDAO);
CheckMethodImpl checkMethodImpl = new CheckMethodImpl(this.switcherService);
OnMethodImpl onMethodImpl = new OnMethodImpl(this.switcherService);
OffMethodImpl offMethodImpl = new OffMethodImpl(this.switcherService);
SetMethodImpl setMethodImpl = new SetMethodImpl(this.switcherService);
ListMethodImpl listMethodImpl = new ListMethodImpl(this.switcherService);
try {
jsonRPCServer = JsonRPCServer.defaultJsonRPCServer(configure.getPort(), 1, 1);
jsonRPCServer.register(checkMethodImpl);
jsonRPCServer.register(onMethodImpl);
jsonRPCServer.register(offMethodImpl);
jsonRPCServer.register(setMethodImpl);
jsonRPCServer.register(listMethodImpl);
jsonRPCServer.start();
this.jsonRPCServer = JsonRPCServer.defaultJsonRPCServer(this.configure.getPort(), 1, 1);
this.jsonRPCServer.register(checkMethodImpl);
this.jsonRPCServer.register(onMethodImpl);
this.jsonRPCServer.register(offMethodImpl);
this.jsonRPCServer.register(setMethodImpl);
this.jsonRPCServer.register(listMethodImpl);
this.jsonRPCServer.start();
} catch (Throwable e) {
throw new SwitcherServerException("JsonRPCServer abnormal", e);
}
Expand All @@ -85,11 +96,11 @@ public void run() {
*
*/
public void close() {
if (jsonRPCServer != null) {
if (this.jsonRPCServer != null) {
try {
LOGGER.warn("switcher-server closing!");
jsonRPCServer.shutdown();
jsonRPCServer = null;
this.jsonRPCServer.shutdown();
this.jsonRPCServer = null;
} catch (Throwable e) {
throw new SwitcherServerException("JsonRPCServer abnormal", e);
}
Expand All @@ -101,7 +112,7 @@ public void close() {
* @return
*/
public Configure getConfigure() {
return configure;
return this.configure;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CheckMethodImpl(SwitcherService switcherService) {
@Override
public Boolean exec(Request request) {
List<Object> params = request.getParams();
return switcherService.check((String) params.get(0));
return this.switcherService.check((String) params.get(0));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OffMethodImpl(SwitcherService switcherService) {
@Override
public Void exec(Request request) {
List<Object> params = request.getParams();
switcherService.off((String) params.get(0));
this.switcherService.off((String) params.get(0));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OnMethodImpl(SwitcherService switcherService) {
@Override
public Void exec(Request request) {
List<Object> params = request.getParams();
switcherService.on((String) params.get(0));
this.switcherService.on((String) params.get(0));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SetMethodImpl(SwitcherService switcherService) {
@Override
public Void exec(Request request) {
List<Object> params = request.getParams();
switcherService.set((String) params.get(0), (Boolean) params.get(1));
this.switcherService.set((String) params.get(0), (Boolean) params.get(1));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ public SwitcherServiceImpl(Configure configre, SwitcherDAO switcherDAO) {
@Override
public void register(String key, Boolean open, String describe, QoS qos) {
String k = String.valueOf(key);
if (!keyAndSwitcher.containsKey(k)) {
if (!this.keyAndSwitcher.containsKey(k)) {
Switcher switcher = getSwitcherByKey(k);
if (switcher == null) {
if (!keyAndSwitcher.containsKey(k)) {
if (!this.keyAndSwitcher.containsKey(k)) {
switcher = new Switcher();
switcher.setKey(k);
switcher.setOpen(open);
switcher.setDescribe(describe);
switcher.setQos(qos);
keyAndSwitcher.put(k, switcher);
this.keyAndSwitcher.put(k, switcher);
LOGGER.warn("new registration switch {}", switcher.toString());
}
} else {
keyAndSwitcher.put(k, switcher);
this.keyAndSwitcher.put(k, switcher);
LOGGER.warn("load switch information {}", switcher.toString());
}
}
Expand All @@ -86,8 +86,8 @@ public void register(String key, Boolean open, String describe, QoS qos) {
@Override
public Boolean isOpen(String key) {
String k = String.valueOf(key);
if (keyAndSwitcher.containsKey(k)) {
return keyAndSwitcher.get(k).getOpen();
if (this.keyAndSwitcher.containsKey(k)) {
return this.keyAndSwitcher.get(k).getOpen();
}
LOGGER.warn("this switch has not been registered in the application. key = {}", k);
return false;
Expand Down Expand Up @@ -116,10 +116,10 @@ public Boolean check(String key) {
if (StringUtils.isBlank(k)) {
throw new ParameterInvalidException("key can not be empty!");
}
if (!keyAndSwitcher.containsKey(k)) {
if (!this.keyAndSwitcher.containsKey(k)) {
throw new KeyNotFoundException(String.format("key = %s can't find!", k));
}
return keyAndSwitcher.get(k).getOpen();
return this.keyAndSwitcher.get(k).getOpen();
}

/**
Expand All @@ -133,10 +133,10 @@ public void on(String key) {
if (StringUtils.isBlank(k)) {
throw new ParameterInvalidException("key can not be empty!");
}
if (!keyAndSwitcher.containsKey(k)) {
if (!this.keyAndSwitcher.containsKey(k)) {
throw new KeyNotFoundException(String.format("key = %s can't find!", k));
}
keyAndSwitcher.get(k).setOpen(true);
this.keyAndSwitcher.get(k).setOpen(true);
}

/**
Expand All @@ -150,10 +150,10 @@ public void off(String key) {
if (StringUtils.isBlank(k)) {
throw new ParameterInvalidException("key can not be empty!");
}
if (!keyAndSwitcher.containsKey(k)) {
if (!this.keyAndSwitcher.containsKey(k)) {
throw new KeyNotFoundException(String.format("key = %s can't find!", k));
}
keyAndSwitcher.get(k).setOpen(false);
this.keyAndSwitcher.get(k).setOpen(false);
}

/**
Expand All @@ -168,15 +168,15 @@ public void set(String key, Boolean open) {
if (StringUtils.isBlank(k)) {
throw new ParameterInvalidException("key can not be empty!");
}
if (!keyAndSwitcher.containsKey(k)) {
if (!this.keyAndSwitcher.containsKey(k)) {
throw new KeyNotFoundException(String.format("key = %s can't find!", k));
}
keyAndSwitcher.get(k).setOpen(open);
this.keyAndSwitcher.get(k).setOpen(open);
Switcher switcher = getSwitcherByKey(k);
if (switcher != null) {
switcherDAO.changeStatusByKey(k, !open, open);
this.switcherDAO.changeStatusByKey(k, !open, open);
} else {
switcherDAO.insert(keyAndSwitcher.get(k)); // resolve repeated insertion problems by using key as a unique index
this.switcherDAO.insert(this.keyAndSwitcher.get(k)); // resolve repeated insertion problems by using key as a unique index
}
}

Expand All @@ -192,13 +192,13 @@ public List<Switcher> list() {
}

/**
*
*
* @param key
* @return
* @return
*/
public Switcher getSwitcherByKey(String key) {
if (StringUtils.isNotEmpty(key)) {
return switcherDAO.selectByKey(key);
return this.switcherDAO.selectByKey(key);
}
return null;
}
Expand Down

0 comments on commit 990f425

Please sign in to comment.