All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ratpack.server.RatpackServerBuilder Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2013 the original author or authors.
 *
 * 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://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.
 */

package ratpack.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import ratpack.handling.Handler;
import ratpack.handling.internal.FactoryHandler;
import ratpack.launch.HandlerFactory;
import ratpack.launch.LaunchConfig;
import ratpack.launch.LaunchException;
import ratpack.launch.internal.LaunchConfigInternal;
import ratpack.reload.internal.ClassUtil;
import ratpack.reload.internal.ReloadableFileBackedFactory;
import ratpack.server.internal.NettyRatpackServer;
import ratpack.server.internal.RatpackChannelInitializer;
import ratpack.func.Factory;
import ratpack.func.Transformer;

import java.io.File;
import java.nio.file.Path;

/**
 * Builds a {@link RatpackServer}.
 */
public abstract class RatpackServerBuilder {

  private RatpackServerBuilder() {
  }

  /**
   * Constructs a new server based on the builder's state.
   * 

* The returned server has not been started. * * @param launchConfig The server's configuration * @return A new, not yet started, Ratpack server. */ public static RatpackServer build(LaunchConfig launchConfig) { Transformer> channelInitializer = buildChannelInitializer(launchConfig); LaunchConfigInternal launchConfigInternal = (LaunchConfigInternal) launchConfig; return new NettyRatpackServer(launchConfigInternal, channelInitializer); } private static Transformer> buildChannelInitializer(final LaunchConfig launchConfig) { return new Transformer>() { @Override public ChannelInitializer transform(Stopper stopper) { return new RatpackChannelInitializer(launchConfig, createHandler(launchConfig), stopper); } }; } private static Handler createHandler(final LaunchConfig launchConfig) { final HandlerFactory handlerFactory = launchConfig.getHandlerFactory(); if (launchConfig.isReloadable()) { File classFile = ClassUtil.getClassFile(handlerFactory); if (classFile != null) { Factory factory = new ReloadableFileBackedFactory<>(classFile.toPath(), true, new ReloadableFileBackedFactory.Producer() { @Override public Handler produce(Path file, ByteBuf bytes) { return createHandler(launchConfig, handlerFactory); } }); return new FactoryHandler(factory); } } return createHandler(launchConfig, handlerFactory); } private static Handler createHandler(LaunchConfig launchConfig, HandlerFactory handlerFactory) { try { return handlerFactory.create(launchConfig); } catch (Exception e) { throw new LaunchException("Could not create handler via handler factory: " + handlerFactory.getClass().getName(), e); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy