zoukankan      html  css  js  c++  java
  • 读DataSnap源代码(三)

     1 function TWebRequestHandler.HandleRequest(Request: TWebRequest;
     2   Response: TWebResponse): Boolean;
     3 var
     4   I: Integer;
     5   LWebModule: TComponent;
     6   LWebAppServices: IWebAppServices;
     7   LGetWebAppServices: IGetWebAppServices;
     8   LComponent: TComponent;
     9 begin
    10   Result := False;
    11   LWebModule := ActivateWebModules;
    12   if Assigned(LWebModule) then
    13   try
    14     try
    15       if Supports(IInterface(LWebModule), IGetWebAppServices, LGetWebAppServices) then
    16         LWebAppServices := LGetWebAppServices.GetWebAppServices;
    17       if LWebAppServices = nil then
    18         for I := 0 to LWebModule.ComponentCount - 1 do
    19         begin
    20           LComponent := LWebModule.Components[I];
    21           if Supports(LComponent, IWebAppServices, LWebAppServices) then
    22             if LWebAppServices.Active then
    23               break
    24             else
    25               LWebAppServices := nil;
    26         end;
    27       if LWebAppServices = nil then
    28         LWebAppServices := TDefaultWebAppServices.Create;
    29       LWebAppServices.InitContext(LWebModule, Request, Response);
    30       try
    31         try
    32           Result := LWebAppServices.HandleRequest;
    33         except
    34           ApplicationHandleException(LWebAppServices.ExceptionHandler);
    35         end;
    36       finally
    37         LWebAppServices.FinishContext;
    38       end;
    39       if Result and not Response.Sent then
    40         Response.SendResponse;
    41     except
    42       ApplicationHandleException(LWebAppServices.ExceptionHandler);
    43     end;
    44   finally
    45     DeactivateWebModules(LWebModule);
    46   end;
    47 end;


    第11行代码,先得到一个激活的WebModule,如何没有,就会创建一个再返回。

    第14行到第26行代码,判断WebModule中是否实现了 IGetWebAppServices 接口的,具体做什么先略过。因为WebModule没有继承这个接口。

    第27行,创建一个默认的WebAppServices

    第32行,执行默认操作。

     1 function TDefaultWebAppServices.HandleRequest: Boolean;
     2 begin
     3   Result := InvokeDispatcher;
     4 end;
     5 
     6 function TDefaultWebAppServices.InvokeDispatcher: Boolean;
     7 begin
     8   if RequestHandler <> nil then
     9   begin
    10     Result := RequestHandler.HandleRequest(Request, Response);
    11   end
    12   else
    13     raise EWebBrokerException.CreateRes(@sNoDispatcherComponent);
    14 end;

    上面的RequestHandler是

        property RequestHandler: IWebRequestHandler read GetRequestHandler;

    function TDefaultWebAppServices.GetRequestHandler: IWebRequestHandler;
    begin
      if FRequestHandler = nil then
        FRequestHandler := FindRequestHandler;
      Result := FRequestHandler;
    end;
    
    function TDefaultWebAppServices.FindRequestHandler: IWebRequestHandler;
    var
      Component: TComponent;
    begin
      Result := nil;
      Component := FindWebDispatcher;
      if Component <> nil then
        if not Supports(Component, IWebRequestHandler, Result) then
          Assert(False, 'Expect support for IWebRequestHandler');  { do not localize }
    end;

    function TDefaultWebAppServices.FindWebDispatcher: TComponent;
    var
      J: Integer;
    begin
      Result := nil;
      if WebModule is TCustomWebDispatcher then
        Result := WebModule
      else
        for J := 0 to WebModule.ComponentCount - 1 do
          if WebModule.Components[J] is TCustomWebDispatcher then
          begin
            Result := WebModule.Components[J];
            break;
          end;
    end;


    上面红色标记的代码,最终会返回 WebModule.

      TWebModule = class(TCustomWebDispatcher)

    所以,上面绿色标记的代码,是调用TCustomWebDispatcher.HandleRequest方法, 内部代码如下:

    function TCustomWebDispatcher.HandleRequest(
      Request: TWebRequest; Response: TWebResponse): Boolean;
    begin
      FRequest := Request;
      FResponse := Response;
      Result := DispatchAction(Request, Response);
    end;
  • 相关阅读:
    第七章11
    第七章10
    第七章9
    第七章8
    第七章7
    第七章6
    第七章5
    第七章例7-13
    第七章例7-12
    第七章例7-11
  • 原文地址:https://www.cnblogs.com/Jiaojiawang/p/4818756.html
Copyright © 2011-2022 走看看