Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Swim ServerLoader
package swim.basic;
import java.io.IOException;
import swim.api.service.ServiceDef;
import swim.api.service.ServiceFactory;
import swim.api.space.SpaceDef;
import swim.auth.AuthenticatorKernel;
import swim.fabric.FabricKernel;
import swim.java.JavaKernel;
import swim.kernel.BootKernel;
import swim.kernel.Kernel;
import swim.kernel.KernelException;
import swim.kernel.KernelLoader;
import swim.remote.RemoteKernel;
import swim.service.ServiceKernel;
import swim.service.web.WebServiceDef;
import swim.service.web.WebServiceKernel;
import swim.store.mem.MemStoreKernel;
import swim.structure.Item;
import swim.structure.Value;
public final class MyServerLoader {
private MyServerLoader () {
// static
}
public static Kernel loadServer() {
return loadServer(KernelLoader.class.getClassLoader());
}
public static Kernel loadServer(ClassLoader classLoader) {
try {
Value kernelConfig = KernelLoader.loadConfig(classLoader);
if (kernelConfig == null) {
kernelConfig = KernelLoader.loadConfigResource(classLoader, "server.recon");
}
if (kernelConfig == null) {
kernelConfig = Value.absent();
}
final Kernel kernel = loadServerStack(classLoader, kernelConfig);
loadSpaces(kernel, kernelConfig, classLoader);
loadServices(kernel, kernelConfig, classLoader);
return kernel;
} catch (IOException cause) {
throw new KernelException(cause);
}
}
public static Kernel loadServerStack() {
return loadServerStack(KernelLoader.class.getClassLoader());
}
public static Kernel loadServerStack(ClassLoader classLoader) {
return injectServerStack(classLoader, null);
}
public static Kernel loadServerStack(ClassLoader classLoader, Value kernelConfig) {
Kernel kernel = KernelLoader.loadKernelStack(classLoader, kernelConfig);
kernel = injectServerStack(classLoader, kernel);
return kernel;
}
public static Kernel injectServerStack(ClassLoader classLoader, Kernel kernel) {
if (kernel == null) {
kernel = new BootKernel();
} else if (kernel.unwrapKernel(BootKernel.class) == null) {
kernel = kernel.injectKernel(new BootKernel());
}
if (kernel.unwrapKernel(MemStoreKernel.class) == null) {
kernel = kernel.injectKernel(new MemStoreKernel());
}
if (kernel.unwrapKernel(RemoteKernel.class) == null) {
kernel = kernel.injectKernel(new RemoteKernel());
}
if (kernel.unwrapKernel(ServiceKernel.class) == null) {
kernel = kernel.injectKernel(new ServiceKernel());
}
if (kernel.unwrapKernel(WebServiceKernel.class) == null) {
kernel = kernel.injectKernel(new WebServiceKernel());
}
if (kernel.unwrapKernel(AuthenticatorKernel.class) == null) {
kernel = kernel.injectKernel(new AuthenticatorKernel());
}
if (kernel.unwrapKernel(FabricKernel.class) == null) {
kernel = kernel.injectKernel(new FabricKernel());
}
if (kernel.unwrapKernel(JavaKernel.class) == null) {
kernel = kernel.injectKernel(new JavaKernel());
}
return kernel;
}
public static void loadSpaces(Kernel kernel, Value kernelConfig, ClassLoader classLoader) {
for (int i = 0, n = kernelConfig.length(); i < n; i += 1) {
final Item item = kernelConfig.getItem(i);
final SpaceDef spaceDef = kernel.defineSpace(item);
if (spaceDef != null) {
kernel.openSpace(spaceDef);
}
}
}
public static void loadServices(Kernel kernel, Value kernelConfig, ClassLoader classLoader) {
for (int i = 0, n = kernelConfig.length(); i < n; i += 1) {
final Item item = kernelConfig.getItem(i);
final ServiceDef serviceDef = kernel.defineService(item);
if (serviceDef != null) {
if (serviceDef instanceof WebServiceDef) {
final WebServiceDef oldDef = ((WebServiceDef) serviceDef);
int port = Integer.parseInt(System.getProperty("port", "9002"));
final WebServiceDef newDef= oldDef.port(port);
System.out.println("Will bind to port " + port);
final ServiceFactory<?> serviceFactory = kernel.createServiceFactory(newDef, classLoader);
kernel.openService(serviceDef.serviceName(), serviceFactory);
} else {
final ServiceFactory<?> serviceFactory = kernel.createServiceFactory(serviceDef, classLoader);
kernel.openService(serviceDef.serviceName(), serviceFactory);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.