Question

I'm working on an HTTP Server and I'm using Indy's TIdHTTPWebBrokerBridge with a TWebModule. I'm encapsulating all the Server functionality into a single Component, and this includes the Indy Server component and its corresponding Web Module. However, I'm having some issues figuring out how to make the Web Module know the Component from which it was called.

Suppose I have this component:

type
  TMyComponent = class(TComponent)
  private
    FServer: TIdHTTPWebBrokerBridge;
  end;

implementation

uses
  MyWebModule;

I do know I have to initialize this by setting the Request Handler, and I've taken care of it by adding an initialization section to this unit:

initialization
  if WebRequestHandler <> nil then
    WebRequestHandler.WebModuleClass:= WebModuleClass;

Then I have the Web Module class in a separate unit:

uses
  MyWebServer;

type
  TMyWebModule = class(TWebModule)
  private
    FOwner: TMyComponent;
  end;

Note the private field I have in this web module FOwner: TMyComponent. This is where I'm having trouble figuring out where to go. How do I assign this to its appropriate owner? The Web Module is automatically created and managed by the Indy HTTP Server, so as far as I know, I don't have any control to set such a thing.

The reason why I need to have access to its owner is because I have properties set there which the Web Module needs to be able to read. For example, one property I have on my component is RootDir which is a root directory where to read/write files. I need to be able to read this property from within the Web Module.

How can I make the Web Module able to read its owner component's properties? Or in general, how can I assign this private field FOwner to the instance of my component from where it was instantiated?

Was it helpful?

Solution

I discovered my answer a while after asking this question

Since it's not advised to create multiple instances of TIdHTTPWebBrokerBridge, you don't necessarily need to worry about the existence of multiple different instances of TMyComponent. You do however need to perform your own checking to make sure multiple instances don't exist in the first place. But since you only have one instance of this Component, you can feel safe declaring a global variable in your component's unit and exposing it to your web module.

Don't just declare a global var variable directly in the interface of your unit. Instead, you should protect this...

function MyComponent: TMyComponent;

implementation

uses
  MyWebModule;

var
  _MyComponent: TMyComponent;

function MyComponent: TMyComponent;
begin
  Result:= _MyComponent;
end;

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;
  _MyComponent:= Self;
end;

destructor TMyComponent.Destroy;
begin
  _MyComponent:= nil;
  inherited;
end;

initialization
  _MyComponent:= nil;
  if WebRequestHandler <> nil then
    WebRequestHandler.WebModuleClass:= WebModuleClass;
end.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top