Skip to content

Commit 2cfede7

Browse files
Merge pull request #392 from michaelJustin/377-disallow-contexts-with-the-same-name
added check for duplicates
2 parents 4d32e9e + 681030e commit 2cfede7

9 files changed

Lines changed: 53 additions & 115 deletions

source/djContextHandlerCollection.pas

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,32 @@
3131
interface
3232

3333
uses
34-
djHandlerList, djContextMap
35-
{$IFDEF DARAJA_LOGGING},djLogAPI, djLoggerFactory{$ENDIF DARAJA_LOGGING}
36-
;
34+
{$IFDEF DARAJA_LOGGING}
35+
djLogAPI, djLoggerFactory,
36+
{$ENDIF DARAJA_LOGGING}
37+
djHandlerList,
38+
Classes;
3739

3840
type
3941
{ TdjContextHandlerCollection }
4042

4143
{*
42-
* Multiple contexts may have the same context path and they are
43-
* called in order until one handles the request.
44+
* Multiple contexts with the same name are not allowed.
4445
*}
4546
TdjContextHandlerCollection = class(TdjHandlerList)
4647
private
4748
{$IFDEF DARAJA_LOGGING}
4849
Logger: ILogger;
4950
{$ENDIF DARAJA_LOGGING}
50-
51-
ContextMap: TdjContextMap;
52-
53-
procedure Trace(const S: string);
54-
5551
public
5652
{*
5753
* Create a collection of context handlers.
5854
*}
5955
constructor Create; override;
60-
6156
{*
6257
* Destructor.
6358
*}
6459
destructor Destroy; override;
65-
6660
end;
6761

6862
implementation
@@ -76,27 +70,11 @@ constructor TdjContextHandlerCollection.Create;
7670
{$IFDEF DARAJA_LOGGING}
7771
Logger := TdjLoggerFactory.GetLogger('dj.' + TdjContextHandlerCollection.ClassName);
7872
{$ENDIF DARAJA_LOGGING}
79-
80-
ContextMap := TdjContextMap.Create;
81-
82-
Trace('Created');
8373
end;
8474

8575
destructor TdjContextHandlerCollection.Destroy;
8676
begin
87-
ContextMap.Free;
88-
8977
inherited;
9078
end;
9179

92-
procedure TdjContextHandlerCollection.Trace(const S: string);
93-
begin
94-
{$IFDEF DARAJA_LOGGING}
95-
if Logger.IsTraceEnabled then
96-
begin
97-
Logger.Trace(S);
98-
end;
99-
{$ENDIF DARAJA_LOGGING}
100-
end;
101-
10280
end.

source/djContextMap.pas

Lines changed: 0 additions & 49 deletions
This file was deleted.

source/djHandlerCollection.pas

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
interface
3232

3333
uses
34+
{$IFDEF DARAJA_LOGGING}
35+
djLogAPI, djLoggerFactory,
36+
{$ENDIF DARAJA_LOGGING}
3437
djInterfaces, djAbstractHandlerContainer, djServerContext,
35-
{$IFDEF DARAJA_LOGGING}djLogAPI, djLoggerFactory,{$ENDIF DARAJA_LOGGING}
3638
djTypes;
3739

3840
type
@@ -100,19 +102,13 @@ constructor TdjHandlerCollection.Create;
100102

101103
destructor TdjHandlerCollection.Destroy;
102104
begin
103-
{$IFDEF LOG_DESTROY}
104-
Trace('Destroy');
105-
{$ENDIF}
106-
107105
FHandlers.Free;
108106

109107
inherited;
110108
end;
111109

112110
procedure TdjHandlerCollection.AddHandler(const Handler: IHandler);
113111
begin
114-
// Trace(Name + ' AddHandler');
115-
116112
FHandlers.Add(Handler);
117113

118114
if Started then
@@ -123,8 +119,6 @@ procedure TdjHandlerCollection.AddHandler(const Handler: IHandler);
123119

124120
procedure TdjHandlerCollection.RemoveHandler(const Handler: IHandler);
125121
begin
126-
// Trace('RemoveHandler');
127-
128122
if Handler.IsStarted then
129123
begin
130124
Handler.Stop;
@@ -147,8 +141,6 @@ procedure TdjHandlerCollection.DoStart;
147141
var
148142
H: IHandler;
149143
begin
150-
// Trace('Start ' + FName);
151-
152144
for H in FHandlers do
153145
begin
154146
try
@@ -170,8 +162,6 @@ procedure TdjHandlerCollection.DoStop;
170162
var
171163
H: IHandler;
172164
begin
173-
// Trace('Stop ' + FName);
174-
175165
try
176166
inherited DoStop;
177167
except

source/djHandlerList.pas

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ interface
3434

3535
uses
3636
djServerContext, djHandlerCollection,
37-
{$IFDEF DARAJA_LOGGING}djLogAPI, djLoggerFactory,{$ENDIF DARAJA_LOGGING}
37+
{$IFDEF DARAJA_LOGGING}
38+
djLogAPI, djLoggerFactory,
39+
{$ENDIF DARAJA_LOGGING}
3840
djTypes;
3941

4042
type
@@ -117,12 +119,12 @@ procedure TdjHandlerList.Handle(const Target: string; Context: TdjServerContext;
117119

118120
procedure TdjHandlerList.Trace(const S: string);
119121
begin
120-
{$IFDEF DARAJA_LOGGING}
122+
{$IFDEF DARAJA_LOGGING}
121123
if Logger.IsTraceEnabled then
122124
begin
123125
Logger.Trace(S);
124126
end;
125-
{$ENDIF DARAJA_LOGGING}
127+
{$ENDIF DARAJA_LOGGING}
126128
end;
127129

128130
end.

source/djServer.pas

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface
3939
{$IFDEF FPC}
4040
LazUTF8,
4141
{$ENDIF}
42-
Generics.Collections;
42+
Classes, Generics.Collections;
4343

4444
const
4545
DEFAULT_BINDING_PORT = 8080;
@@ -90,7 +90,7 @@ TdjServer = class(TdjServerBase)
9090
ConnectorMap: TObjectDictionary<string, IConnector>;
9191
ConnectorList: TdjStrings;
9292
ContextHandlers: IHandlerContainer;
93-
93+
ContextNames: TStrings;
9494
procedure Trace(const S: string);
9595
procedure StartConnectors;
9696
procedure StopConnectors;
@@ -147,6 +147,7 @@ TdjServer = class(TdjServerBase)
147147
* Add a new context.
148148
*
149149
* @param Context the context handler.
150+
* @throws EWebComponentException if an error occurs that interferes with the component's normal operation.
150151
*}
151152
procedure Add(Context: TdjWebComponentContextHandler);
152153

@@ -162,7 +163,7 @@ TdjServer = class(TdjServerBase)
162163
implementation /// \cond
163164

164165
uses
165-
Generics.Defaults, SysUtils, Classes;
166+
Generics.Defaults, SysUtils;
166167

167168
{ TdjServer }
168169

@@ -183,12 +184,9 @@ constructor TdjServer.Create;
183184
ConnectorList := TdjStrings.Create;
184185

185186
ContextHandlers := TdjContextHandlerCollection.Create;
187+
ContextNames := TStringList.Create;
186188

187189
AddHandler(ContextHandlers);
188-
189-
{$IFDEF LOG_CREATE}
190-
Trace('Created');
191-
{$ENDIF}
192190
end;
193191

194192
constructor TdjServer.Create(const AHost: string;
@@ -209,10 +207,6 @@ constructor TdjServer.Create(const APort: Integer);
209207

210208
destructor TdjServer.Destroy;
211209
begin
212-
{$IFDEF LOG_DESTROY}
213-
Trace('Destroy');
214-
{$ENDIF}
215-
216210
if IsStarted then
217211
begin
218212
Stop;
@@ -221,6 +215,8 @@ destructor TdjServer.Destroy;
221215
ConnectorMap.Free;
222216
ConnectorList.Free;
223217

218+
ContextNames.Free;
219+
224220
inherited;
225221
end;
226222

@@ -263,6 +259,16 @@ procedure TdjServer.Add(Context: TdjWebComponentContextHandler);
263259
begin
264260
Trace('Add context ' + Context.ContextPath);
265261

262+
if ContextNames.IndexOf(Context.ContextPath) < 0 then
263+
begin
264+
ContextNames.Add(Context.ContextPath);
265+
end else begin
266+
raise EWebComponentException.CreateFmt('Context path "%s" is already registered.',
267+
[Context.ContextPath]);
268+
269+
Context.Free; // avoid leak
270+
end;
271+
266272
ContextHandlers.AddHandler(Context);
267273
end;
268274

@@ -320,7 +326,6 @@ procedure TdjServer.Trace(const S: string);
320326
{$ENDIF DARAJA_LOGGING}
321327
end;
322328

323-
324329
procedure TdjServer.DoStart;
325330
begin
326331
CheckStarted;

test/unittests/ConfigAPITests.pas

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TAPIConfigTests = class(THTTPTestCase)
4949

5050
// Multiple contexts may have the same context path and they are
5151
// called in order until one handles the request.
52-
procedure AddTwoContextWithSameName;
52+
procedure AddTwoContextWithSameNameRaisesException;
5353

5454
procedure ConfigTwoContexts;
5555

@@ -270,7 +270,7 @@ procedure TAPIConfigTests.TestDefaultHandlerInContext;
270270
end;
271271
end;
272272

273-
procedure TAPIConfigTests.AddTwoContextWithSameName;
273+
procedure TAPIConfigTests.AddTwoContextWithSameNameRaisesException;
274274
var
275275
Server: TdjServer;
276276
Context: TdjWebAppContext;
@@ -280,13 +280,18 @@ procedure TAPIConfigTests.AddTwoContextWithSameName;
280280
Context := TdjWebAppContext.Create('foo');
281281
Context.Add(TExamplePage, '/bar');
282282
Server.Add(Context);
283+
283284
Context := TdjWebAppContext.Create('foo');
284285
Context.Add(TExamplePage, '/bar2');
286+
287+
{$IFDEF FPC}
288+
ExpectException(EWebComponentException, 'Context path "foo" is already registered.');
289+
{$ELSE}
290+
ExpectedException := EWebComponentException;
291+
{$ENDIF}
292+
285293
Server.Add(Context);
286-
Server.Start;
287294

288-
CheckGETResponseEquals('example', '/foo/bar');
289-
CheckGETResponseEquals('example', '/foo/bar2');
290295

291296
finally
292297
Server.Free;

test/unittests/Unittests.dpr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ uses
3636
djContextConfig in '..\..\source\djContextConfig.pas',
3737
djContextHandler in '..\..\source\djContextHandler.pas',
3838
djContextHandlerCollection in '..\..\source\djContextHandlerCollection.pas',
39-
djContextMap in '..\..\source\djContextMap.pas',
4039
djDefaultHandler in '..\..\source\optional\djDefaultHandler.pas',
4140
djDefaultWebComponent in '..\..\source\optional\djDefaultWebComponent.pas',
4241
djGenericHolder in '..\..\source\djGenericHolder.pas',

test/unittests/Unittests.lpi

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<PackageName Value="FCL"/>
3939
</Item3>
4040
</RequiredPackages>
41-
<Units Count="12">
41+
<Units Count="14">
4242
<Unit0>
4343
<Filename Value="Unittests.lpr"/>
4444
<IsPartOfProject Value="True"/>
@@ -84,9 +84,17 @@
8484
<IsPartOfProject Value="True"/>
8585
</Unit10>
8686
<Unit11>
87-
<Filename Value="..\..\source\djAbstractHolder.pas"/>
87+
<Filename Value="..\..\source\djInterfaces.pas"/>
8888
<IsPartOfProject Value="True"/>
8989
</Unit11>
90+
<Unit12>
91+
<Filename Value="ConfigAPITests.pas"/>
92+
<IsPartOfProject Value="True"/>
93+
</Unit12>
94+
<Unit13>
95+
<Filename Value="..\..\source\optional\djDefaultWebComponent.pas"/>
96+
<IsPartOfProject Value="True"/>
97+
</Unit13>
9098
</Units>
9199
</ProjectOptions>
92100
<CompilerOptions>
@@ -114,7 +122,7 @@
114122
</Linking>
115123
<Other>
116124
<CompilerMessages>
117-
<IgnoredMessages idx6058="True" idx5091="True" idx5071="True" idx5062="True" idx5024="True" idx4046="True" idx3124="True" idx3123="True"/>
125+
<IgnoredMessages idx6058="True" idx5093="True" idx5091="True" idx5071="True" idx5062="True" idx5024="True" idx4046="True" idx3124="True" idx3123="True"/>
118126
</CompilerMessages>
119127
</Other>
120128
</CompilerOptions>

test/unittests/Unittests.lpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
djLogAPI, djLogOverSimpleLogger, SimpleLogger,
3636
Forms,
3737
Interfaces,
38-
djGlobal,
38+
djGlobal, djInterfaces, djDefaultWebComponent,
3939
djPathMapTests,
4040
djWebAppContextTests,
4141
djWebComponentHolderTests,

0 commit comments

Comments
 (0)