RESTEasy内核分析

以下是RESTEasy的核心:

RESTEasy内核分析_第1张图片

如上图所示,ResteasyBootstrap是容器入口;ResteasyDeployment是核心,它包含ResteasyProviderFactory,RESTEasyContextParameters等启动时需要加载的类。

确切地讲ResteasyBootstrap是Servlet容器入口。我们还可以使用其它的容器,比如TJWSServletContainer或是org.jboss.netty.bootstrap.ServerBootstrap。如果有兴趣可以看看RESTEasy源代码中的"server-adapters"子项目:

https://github.com/resteasy/Resteasy/tree/master/jaxrs/server-adapters

RESTEasyProviderFactory是RESTEasy的容器,启动时需要加载的东西基本都装在里面,在启动时由RESTEasyDeployment将其包含在内:

RESTEasy内核分析_第2张图片

RESTEasyContextParameters定义了RESTEasy的启动项:

RESTEasy内核分析_第3张图片

ResteasyDeployment并不依赖于servlet容器,因此加载入口start()可由servlet容器(如tomcat),javaee容器(如wildfly),nio容器(如netty),或是标准容器(sun jdk http server或tjws)加载,未来还可以接入更多容器。ResteasyDeployment中包含ResteasyProvider并对其初始化。以下是ResteasyDeployment的加载过程,start方法是入口:

RESTEasy内核分析_第4张图片


在ResteasyDeployment启动过程中,通过调用createApplication()来加载ResteasyProviderFactory:

RESTEasy内核分析_第5张图片

我们可以在RESTEasy的源代码中找到一些RESTEasy启动过程的源代码:


package org.jboss.resteasy.test.smoke;

import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.test.EmbeddedContainer;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.jboss.resteasy.test.TestPortProvider.generateBaseUrl;

/**
 * Simple smoke test
 *
 * @author <a href="mailto:[email protected]">Bill Burke</a>
 * @version $Revision: 1 $
 */
public class TestClient
{

   private static Dispatcher dispatcher;

   @BeforeClass
   public static void before() throws Exception
   {
      dispatcher = EmbeddedContainer.start().getDispatcher();
   }

   @AfterClass
   public static void after() throws Exception
   {
      EmbeddedContainer.stop();
   }

   @Test
   public void testNoDefaultsResource() throws Exception
   {
      int oldSize = dispatcher.getRegistry().getSize();
      dispatcher.getRegistry().addPerRequestResource(SimpleResource.class);
      Assert.assertTrue(oldSize < dispatcher.getRegistry().getSize());

      SimpleClient client = ProxyFactory.create(SimpleClient.class, generateBaseUrl());

      Assert.assertEquals("basic", client.getBasic());
      client.putBasic("hello world");
      Assert.assertEquals("hello world", client.getQueryParam("hello world"));
      Assert.assertEquals(1234, client.getUriParam(1234));

      dispatcher.getRegistry().removeRegistrations(SimpleResource.class);
      Assert.assertEquals(oldSize, dispatcher.getRegistry().getSize());
   }

}


其中EmbeddedContainer是TJWS容器,是RESTEasy支持的多种Bootstraper中的一种:

package org.jboss.resteasy.test;
...

/**
 * @author <a href="mailto:[email protected]">Bill Burke</a>
 * @version $Revision: 1 $
 */
public class EmbeddedContainer
{
   private static Class<?> bootstrap = TJWSServletContainer.class;

...

   public static void setBootstrap(Class bootstrap)
   {
      EmbeddedContainer.bootstrap = bootstrap;
   }

   public static void start(ResteasyDeployment deployment) throws Exception
   {
      Method start = bootstrap.getMethod("start", ResteasyDeployment.class);
      start.invoke(null, deployment);

   }

   public static void stop() throws Exception
   {
      Method stop = bootstrap.getMethod("stop");
      stop.invoke(null);
   }
}


我们从上面的TestCase中可以看到在ResteasyDeployment中还包含一个重要的对象Dispatcher,从Dispatcher中可以获取Registry:

dispatcher.getRegistry().addPerRequestResource(SimpleResource.class);


Dispatcher负责将http请求按照jax-rs spec映射至用户的resources class,而Registry负责保存用户的resources class:

RESTEasy内核分析_第6张图片

可以说Dispatcher是RESTEasy的引擎。

你可能感兴趣的:(java,jboss,resteasy)