Archived
0

new module: single server

This commit is contained in:
2016-08-08 01:03:20 +03:00
parent eb85c47111
commit f845d82a79
6 changed files with 298 additions and 2 deletions

58
SingleServer/pom.xml Normal file
View File

@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>Single Server</name>
<artifactId>singleserver</artifactId>
<version>0.16</version>
<packaging>bundle</packaging>
<parent>
<groupId>asys</groupId>
<artifactId>asys</artifactId>
<version>0.5</version>
</parent>
<dependencies>
<dependency>
<groupId>asys</groupId>
<artifactId>core</artifactId>
<version>0.18</version>
</dependency>
</dependencies>
<build>
<finalName>${project.groupId}.${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Name>ASys: ${project.name}</Bundle-Name>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Activator>asys.singleserver.SingleServerActivator</Bundle-Activator>
<Import-Package>asys.core.api, *</Import-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Service-Provide>asys.core.api.IServerManager</Service-Provide>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,32 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-08-07
*/
package asys.singleserver;
import asys.core.api.IServerManager;
import org.apache.felix.service.command.Descriptor;
import java.util.Dictionary;
import java.util.Hashtable;
public class Commands {
private IServerManager serverManager;
public static Dictionary<String, Object> getDictionary() {
Hashtable<String, Object> dictionary = new Hashtable<>();
dictionary.put("osgi.command.scope", "asys.singleserver");
dictionary.put("osgi.command.function", new String[] { "startserver" });
return dictionary;
}
public Commands(IServerManager serverManager) {
this.serverManager = serverManager;
}
@Descriptor("Server start")
public void startserver() {
serverManager.getServer(null).start();
}
}

View File

@@ -0,0 +1,72 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-07-30
*/
package asys.singleserver;
import asys.core.api.IMinecraftServer;
import java.io.*;
import java.nio.file.Path;
import java.util.List;
import java.util.Vector;
public class MinecraftWinServer implements IMinecraftServer {
private ProcessBuilder processBuilder;
/** запуск через скрипт */
public MinecraftWinServer(Path directory, Path script) {
processBuilder = new ProcessBuilder();
processBuilder.directory(directory.toFile());
processBuilder.command("cmd.exe", "/C", "start", script.toString());
}
/** запуск через asys */
public MinecraftWinServer(Path directory, Path mainJar, String jvmArgs, String params) {
processBuilder = new ProcessBuilder();
processBuilder.directory(directory.toFile());
List<String> commandLine = new Vector<>();
commandLine.add("cmd.exe");
commandLine.add("/C");
commandLine.add("start");
commandLine.add("java");
if (jvmArgs != null && !jvmArgs.trim().isEmpty()) {
commandLine.add(jvmArgs);
}
commandLine.add("-jar");
commandLine.add(mainJar.toString());
if (params != null && !params.trim().isEmpty()) {
commandLine.add(params);
}
processBuilder.command(commandLine);
}
@Override
public void start() {
try {
processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void stop() {
sendCommand("stop");
}
@Override
public void forceStop() {
//TODO need code
}
@Override
public boolean isAlive() {
return false; //TODO need code
}
@Override
public void sendCommand(String command) {
//TODO need code
}
}

View File

@@ -0,0 +1,86 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-07-30
*/
package asys.singleserver;
import asys.core.api.ICore;
import asys.core.api.IMinecraftServer;
import asys.core.api.IServerManager;
import org.osgi.framework.BundleContext;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
public class SingleServer implements IServerManager {
private IMinecraftServer mcServer;
private BundleContext bundleContext;
public SingleServer(BundleContext bundleContext) {
this.bundleContext = bundleContext;
Path serverPath = Paths.get(getProperty("asys.singleserver.server.path"));
if (Files.notExists(serverPath)) {
throw new RuntimeException(String.format("Server path \"%s\" not found", serverPath.toString()));
}
if (getProperty("asys.singleserver.runscript.use", "false").equalsIgnoreCase("true")) {
Path scriptFile = Paths.get(getProperty("asys.singleserver.runscript.file"));
if (Files.notExists(scriptFile)) {
throw new RuntimeException(String.format("Runscript \"%s\" not found", scriptFile.toString()));
}
if (!Files.isExecutable(scriptFile)) {
throw new RuntimeException(String.format("Runscript \"%s\" is not executable", scriptFile.toString()));
}
mcServer = new MinecraftWinServer(serverPath, scriptFile);
} else {
Path mainJar = serverPath.resolve(getProperty("asys.singleserver.server.mainjar"));
if (Files.notExists(mainJar)) {
throw new RuntimeException(String.format("Main jar \"%s\" not found", mainJar.toString()));
}
mcServer = new MinecraftWinServer(serverPath, mainJar,
getProperty("asys.singleserver.server.jvm.args", null),
getProperty("asys.singleserver.server.params", null));
}
}
@Override
public IMinecraftServer developServer(String serverName) {
return mcServer;
}
@Override
public IMinecraftServer getServer(String serverName) {
return mcServer;
}
void loadState() {
ICore core = SingleServerActivator.instance.getCore();
IMinecraftServer testMcServer = (IMinecraftServer) core.loadObject(SingleServer.class.getName() + "#mcServer");
if (testMcServer != null) {
mcServer = testMcServer;
}
}
void saveState() {
ICore core = SingleServerActivator.instance.getCore();
core.saveObject(SingleServer.class.getName() + "#mcServer", mcServer);
}
private String getProperty(String name) {
return getProperty(name, null);
}
private String getProperty(String name, String defValue) {
try {
return bundleContext.getProperty(name);
} catch (NullPointerException e) {
return defValue;
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2016-07-30
*/
package asys.singleserver;
import asys.core.api.ICore;
import asys.core.api.IServerManager;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;
public class SingleServerActivator implements BundleActivator {
static SingleServerActivator instance;
private ServiceRegistration<?> service, cmdService;
private ServiceTracker<?, ICore> tracker;
private SingleServer singleServer;
@Override
public void start(BundleContext bundleContext) throws Exception {
instance = this;
tracker = new ServiceTracker<>(bundleContext, ICore.class.getName(), null);
tracker.open();
singleServer = new SingleServer(bundleContext);
singleServer.loadState();
service = bundleContext.registerService(IServerManager.class.getName(), singleServer, null);
// Регистрация комманд для Gogo Shell
cmdService = bundleContext.registerService(
Commands.class.getName(), new Commands(singleServer), Commands.getDictionary());
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
cmdService.unregister();
singleServer.saveState();
tracker.close();
service.unregister();
instance = null;
}
ICore getCore() {
return tracker.getService();
}
}

View File

@@ -14,7 +14,6 @@
</developers> </developers>
<properties> <properties>
<asys.version>0.2</asys.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<slf4j.version>1.7.20</slf4j.version> <slf4j.version>1.7.20</slf4j.version>
@@ -23,10 +22,11 @@
<groupId>asys</groupId> <groupId>asys</groupId>
<artifactId>asys</artifactId> <artifactId>asys</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>${asys.version}</version> <version>0.5</version>
<modules> <modules>
<module>core</module> <module>core</module>
<module>singleserver</module>
</modules> </modules>
<dependencies> <dependencies>