forked from woggioni/rbcs
temporary commit
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
import net.woggioni.gbcs.api.CacheProvider;
|
||||
import net.woggioni.gbcs.url.ClasspathUrlStreamHandlerFactoryProvider;
|
||||
import net.woggioni.gbcs.cache.FileSystemCacheProvider;
|
||||
|
||||
open module net.woggioni.gbcs {
|
||||
// exports net.woggioni.gbcs.cache to net.woggioni.gbcs.test;
|
||||
// exports net.woggioni.gbcs.configuration to net.woggioni.gbcs.test;
|
||||
// exports net.woggioni.gbcs.url to net.woggioni.gbcs.test;
|
||||
// exports net.woggioni.gbcs to net.woggioni.gbcs.test;
|
||||
// opens net.woggioni.gbcs.schema to net.woggioni.gbcs.test;
|
||||
|
||||
requires java.sql;
|
||||
requires java.xml;
|
||||
requires java.logging;
|
||||
@@ -14,10 +22,16 @@ open module net.woggioni.gbcs {
|
||||
requires io.netty.codec;
|
||||
requires org.slf4j;
|
||||
requires net.woggioni.jwo;
|
||||
requires net.woggioni.gbcs.base;
|
||||
requires net.woggioni.gbcs.api;
|
||||
|
||||
exports net.woggioni.gbcs;
|
||||
exports net.woggioni.gbcs.url;
|
||||
// exports net.woggioni.gbcs;
|
||||
// exports net.woggioni.gbcs.url;
|
||||
// opens net.woggioni.gbcs to net.woggioni.envelope;
|
||||
provides java.net.URLStreamHandlerFactory with ClasspathUrlStreamHandlerFactoryProvider;
|
||||
uses java.net.URLStreamHandlerFactory;
|
||||
// uses net.woggioni.gbcs.api.Cache;
|
||||
uses CacheProvider;
|
||||
|
||||
provides CacheProvider with FileSystemCacheProvider;
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package net.woggioni.gbcs;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
public class NettyPingServer {
|
||||
private final int port;
|
||||
|
||||
public NettyPingServer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
// Create event loop groups for handling incoming connections and processing
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
|
||||
try {
|
||||
// Create server bootstrap configuration
|
||||
ServerBootstrap bootstrap = new ServerBootstrap()
|
||||
.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
ch.pipeline().addLast(
|
||||
new StringDecoder(CharsetUtil.UTF_8),
|
||||
new StringEncoder(CharsetUtil.UTF_8),
|
||||
new PingServerHandler()
|
||||
);
|
||||
}
|
||||
})
|
||||
.option(ChannelOption.SO_BACKLOG, 128)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true);
|
||||
|
||||
// Bind and start the server
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
System.out.println("Ping Server started on port: " + port);
|
||||
try(final var handle = new GradleBuildCacheServer.ServerHandle(future, bossGroup, workerGroup)) {
|
||||
Thread.sleep(5000);
|
||||
future.channel().close();
|
||||
// Wait until the server socket is closed
|
||||
future.channel().closeFuture().sync();
|
||||
}
|
||||
} finally {
|
||||
// Shutdown event loop groups
|
||||
// workerGroup.shutdownGracefully();
|
||||
// bossGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
// Custom handler for processing ping requests
|
||||
private static class PingServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
||||
// Check if the received message is a ping request
|
||||
if ("ping".equalsIgnoreCase(msg.trim())) {
|
||||
// Respond with "pong"
|
||||
ctx.writeAndFlush("pong\n");
|
||||
System.out.println("Received ping, sent pong");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
// Log and close the connection in case of any errors
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Main method to start the server
|
||||
public static void main(String[] args) throws Exception {
|
||||
int port = 8080; // Default port
|
||||
if (args.length > 0) {
|
||||
port = Integer.parseInt(args[0]);
|
||||
}
|
||||
|
||||
new NettyPingServer(port).start();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,47 @@
|
||||
package net.woggioni.gbcs.url;
|
||||
|
||||
import net.woggioni.jwo.Fun;
|
||||
import net.woggioni.jwo.LazyValue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.net.URLStreamHandlerFactory;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ClasspathUrlStreamHandlerFactoryProvider implements URLStreamHandlerFactory {
|
||||
|
||||
private static final AtomicBoolean installed = new AtomicBoolean(false);
|
||||
public static void install() {
|
||||
if(!installed.getAndSet(true)) {
|
||||
URL.setURLStreamHandlerFactory(new ClasspathUrlStreamHandlerFactoryProvider());
|
||||
}
|
||||
}
|
||||
|
||||
private static final LazyValue<Map<String, List<Module>>> packageMap = LazyValue.of(() ->
|
||||
ClasspathUrlStreamHandlerFactoryProvider.class.getModule().getLayer()
|
||||
.modules()
|
||||
.stream()
|
||||
.flatMap(m -> m.getPackages().stream().map(p -> Map.entry(p, m)))
|
||||
.collect(
|
||||
Collectors.groupingBy(
|
||||
Map.Entry::getKey,
|
||||
Collectors.mapping(
|
||||
Map.Entry::getValue,
|
||||
Collectors.toUnmodifiableList()
|
||||
)
|
||||
)
|
||||
),
|
||||
LazyValue.ThreadSafetyMode.NONE
|
||||
);
|
||||
|
||||
|
||||
private static class Handler extends URLStreamHandler {
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
@@ -24,10 +55,17 @@ public class ClasspathUrlStreamHandlerFactoryProvider implements URLStreamHandle
|
||||
|
||||
@Override
|
||||
protected URLConnection openConnection(URL u) throws IOException {
|
||||
final URL resourceUrl = classLoader.getResource(u.getPath());
|
||||
return Optional.ofNullable(resourceUrl)
|
||||
.map((Fun<URL, URLConnection>) URL::openConnection)
|
||||
.orElseThrow(IOException::new);
|
||||
return Optional.ofNullable(getClass().getModule())
|
||||
.filter(m -> m.getLayer() != null)
|
||||
.map(m -> {
|
||||
final var path = u.getPath();
|
||||
final var i = path.lastIndexOf('/');
|
||||
final var packageName = path.substring(0, i).replace('/', '.');
|
||||
final var modules = packageMap.get().get(packageName);
|
||||
return (URLConnection) new ModuleResourceURLConnection(u, modules);
|
||||
})
|
||||
.or(() -> Optional.of(classLoader).map(cl -> cl.getResource(u.getPath())).map((Fun<URL, URLConnection>) URL::openConnection))
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +81,24 @@ public class ClasspathUrlStreamHandlerFactoryProvider implements URLStreamHandle
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static final class ModuleResourceURLConnection extends URLConnection {
|
||||
private final List<Module> modules;
|
||||
|
||||
ModuleResourceURLConnection(URL url, List<Module> modules) {
|
||||
super(url);
|
||||
this.modules = modules;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
for(final var module : modules) {
|
||||
final var result = module.getResourceAsStream(getURL().getPath());
|
||||
if(result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user