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 com.google.common.base.Throwables;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import ratpack.file.BaseDirRequiredException;
import ratpack.func.Factory;
import ratpack.func.Function;
import ratpack.handling.Handler;
import ratpack.handling.internal.FactoryHandler;
import ratpack.launch.HandlerFactory;
import ratpack.launch.LaunchConfig;
import ratpack.launch.LaunchException;
import ratpack.reload.internal.ClassUtil;
import ratpack.reload.internal.ReloadableFileBackedFactory;
import ratpack.server.internal.NettyRatpackServer;
import ratpack.server.internal.RatpackChannelInitializer;

import java.io.File;

/**
 * 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) { Function> channelInitializer = buildChannelInitializer(launchConfig); return new NettyRatpackServer(launchConfig, channelInitializer); } private static Function> buildChannelInitializer(final LaunchConfig launchConfig) { return stopper -> new RatpackChannelInitializer(launchConfig, createHandler(launchConfig), stopper); } private static Handler createHandler(final LaunchConfig launchConfig) { final HandlerFactory handlerFactory = launchConfig.getHandlerFactory(); if (launchConfig.isDevelopment()) { File classFile = ClassUtil.getClassFile(handlerFactory); if (classFile != null) { Factory factory = new ReloadableFileBackedFactory<>(classFile.toPath(), true, (file, bytes) -> 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) { Throwables.propagateIfInstanceOf(e, BaseDirRequiredException.class); throw new LaunchException("Could not create handler via handler factory: " + handlerFactory.getClass().getName(), e); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy