1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Collections;
|
21
|
|
using System.Collections.Specialized;
|
22
|
|
using System.Runtime.Remoting.Messaging;
|
23
|
|
using System.Web;
|
24
|
|
using System.Web.SessionState;
|
25
|
|
using Seasar.Framework.Container.Util;
|
26
|
|
|
27
|
|
namespace Seasar.Framework.Container.Impl
|
28
|
|
{
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
public class S2ContainerImpl : IS2Container
|
33
|
|
{
|
34
|
|
private Hashtable componentDefTable_ = new Hashtable();
|
35
|
|
private IList componentDefList_ = new ArrayList();
|
36
|
|
private string namespace_;
|
37
|
|
private string path_;
|
38
|
|
private IList children_ = new ArrayList();
|
39
|
|
private Hashtable descendants_ = CollectionsUtil.CreateCaseInsensitiveHashtable();
|
40
|
|
private IS2Container root_;
|
41
|
|
private MetaDefSupport metaDefSupport_;
|
42
|
|
private bool inited_ = false;
|
43
|
|
|
44
|
250
|
public S2ContainerImpl()
|
45
|
|
{
|
46
|
250
|
root_ = this;
|
47
|
250
|
IComponentDef componentDef = new SimpleComponentDef(this,ContainerConstants.INSTANCE_PROTOTYPE);
|
48
|
250
|
componentDefTable_.Add(ContainerConstants.CONTAINER_NAME,componentDef);
|
49
|
250
|
componentDefTable_.Add(typeof(IS2Container),componentDef);
|
50
|
|
|
51
|
250
|
IComponentDef requestCd = new RequestComponentDef(this);
|
52
|
250
|
componentDefTable_.Add(ContainerConstants.REQUEST_NAME, requestCd);
|
53
|
250
|
componentDefTable_.Add(typeof(HttpRequest), requestCd);
|
54
|
|
|
55
|
250
|
IComponentDef sessionCd = new SessionComponentDef(this);
|
56
|
250
|
componentDefTable_.Add(ContainerConstants.SESSION_NAME, sessionCd);
|
57
|
250
|
componentDefTable_.Add(typeof(HttpSessionState), sessionCd);
|
58
|
|
|
59
|
250
|
IComponentDef responseCd = new ResponseComponentDef(this);
|
60
|
250
|
componentDefTable_.Add(ContainerConstants.RESPONSE_NAME, responseCd);
|
61
|
250
|
componentDefTable_.Add(typeof(HttpResponse), responseCd);
|
62
|
|
|
63
|
250
|
IComponentDef applicationCd = new HttpApplicationComponentDef(this);
|
64
|
250
|
componentDefTable_.Add(ContainerConstants.HTTP_APPLICATION_NAME, applicationCd);
|
65
|
250
|
componentDefTable_.Add(typeof(HttpApplication), applicationCd);
|
66
|
|
|
67
|
250
|
metaDefSupport_ = new MetaDefSupport(this);
|
68
|
|
}
|
69
|
|
|
70
|
|
#region S2Container メンバ
|
71
|
|
|
72
|
667
|
public Object GetComponent(object componentKey)
|
73
|
|
{
|
74
|
667
|
IComponentDef cd = this.GetComponentDef(componentKey);
|
75
|
664
|
if(componentKey is Type)
|
76
|
|
{
|
77
|
418
|
return cd.GetComponent((Type) componentKey);
|
78
|
|
}
|
79
|
|
else
|
80
|
|
{
|
81
|
246
|
return cd.GetComponent();
|
82
|
|
}
|
83
|
|
}
|
84
|
|
|
85
|
1
|
public void InjectDependency(Object outerComponent)
|
86
|
|
{
|
87
|
|
|
88
|
1
|
this.InjectDependency(outerComponent,outerComponent.GetType());
|
89
|
|
}
|
90
|
|
|
91
|
2
|
public void InjectDependency(Object outerComponent, Type componentType)
|
92
|
|
{
|
93
|
|
|
94
|
2
|
this.GetComponentDef(componentType).InjectDependency(outerComponent);
|
95
|
|
}
|
96
|
|
|
97
|
2
|
public void InjectDependency(Object outerComponent, string componentName)
|
98
|
|
{
|
99
|
|
|
100
|
2
|
this.GetComponentDef(componentName).InjectDependency(outerComponent);
|
101
|
|
}
|
102
|
|
|
103
|
0
|
public void Register(Object component)
|
104
|
|
{
|
105
|
|
|
106
|
|
this.Register(new SimpleComponentDef(component));
|
107
|
|
}
|
108
|
|
|
109
|
3
|
public void Register(Object component, string componentName)
|
110
|
|
{
|
111
|
|
|
112
|
3
|
this.Register(new SimpleComponentDef(component,componentName));
|
113
|
|
|
114
|
|
}
|
115
|
|
|
116
|
27
|
public void Register(Type componentType)
|
117
|
|
{
|
118
|
|
|
119
|
27
|
this.Register(new ComponentDefImpl(componentType));
|
120
|
|
}
|
121
|
|
|
122
|
3
|
public void Register(Type componentType, string componentName)
|
123
|
|
{
|
124
|
|
|
125
|
3
|
this.Register(new ComponentDefImpl(componentType,componentName));
|
126
|
|
}
|
127
|
|
|
128
|
588
|
public void Register(IComponentDef componentDef)
|
129
|
|
{
|
130
|
|
|
131
|
588
|
lock(this)
|
132
|
|
{
|
133
|
588
|
this.Register0(componentDef);
|
134
|
588
|
componentDefList_.Add(componentDef);
|
135
|
|
}
|
136
|
|
}
|
137
|
|
|
138
|
588
|
private void Register0(IComponentDef componentDef)
|
139
|
|
{
|
140
|
588
|
if(componentDef.Container == null)
|
141
|
|
{
|
142
|
588
|
componentDef.Container = this;
|
143
|
|
}
|
144
|
588
|
this.RegisterByType(componentDef);
|
145
|
588
|
this.RegisterByName(componentDef);
|
146
|
|
}
|
147
|
|
|
148
|
588
|
private void RegisterByType(IComponentDef componentDef)
|
149
|
|
{
|
150
|
588
|
Type[] types = GetAssignableTypes(componentDef.ComponentType);
|
151
|
588
|
foreach(Type type in types)
|
152
|
|
{
|
153
|
1590
|
this.RegisterTable(type,componentDef);
|
154
|
|
}
|
155
|
|
}
|
156
|
|
|
157
|
588
|
private void RegisterByName(IComponentDef componentDef)
|
158
|
|
{
|
159
|
588
|
string componentName = componentDef.ComponentName;
|
160
|
588
|
if(componentName != null)
|
161
|
402
|
this.RegisterTable(componentName,componentDef);
|
162
|
|
}
|
163
|
|
|
164
|
2063
|
private void RegisterTable(object key,IComponentDef componentDef)
|
165
|
|
{
|
166
|
2063
|
if(componentDefTable_.ContainsKey(key))
|
167
|
|
{
|
168
|
546
|
this.ProcessTooManyRegistration(key,componentDef);
|
169
|
|
}
|
170
|
|
else
|
171
|
|
{
|
172
|
1517
|
componentDefTable_[key] = componentDef;
|
173
|
|
}
|
174
|
|
}
|
175
|
|
|
176
|
|
public int ComponentDefSize
|
177
|
|
{
|
178
|
341
|
get
|
179
|
|
{
|
180
|
|
|
181
|
341
|
return componentDefList_.Count;
|
182
|
|
}
|
183
|
|
}
|
184
|
|
|
185
|
320
|
public IComponentDef GetComponentDef(int index)
|
186
|
|
{
|
187
|
|
|
188
|
320
|
lock(this)
|
189
|
|
{
|
190
|
320
|
return (IComponentDef) componentDefList_[index];
|
191
|
|
}
|
192
|
|
}
|
193
|
|
|
194
|
871
|
public IComponentDef GetComponentDef(object key)
|
195
|
|
{
|
196
|
|
|
197
|
871
|
IComponentDef cd = this.GetComponentDef0(key);
|
198
|
871
|
if(cd == null) throw new ComponentNotFoundRuntimeException(key);
|
199
|
868
|
return cd;
|
200
|
|
}
|
201
|
|
|
202
|
1933
|
private IComponentDef GetComponentDef0(object key)
|
203
|
|
{
|
204
|
1933
|
lock(this)
|
205
|
|
{
|
206
|
1933
|
IComponentDef cd = (IComponentDef) componentDefTable_[key];
|
207
|
1933
|
if(cd != null) return cd;
|
208
|
843
|
if(key is string)
|
209
|
|
{
|
210
|
659
|
string name = (string) key;
|
211
|
659
|
int index = name.IndexOf(ContainerConstants.NS_SEP);
|
212
|
659
|
if(index > 0)
|
213
|
|
{
|
214
|
277
|
string ns = name.Substring(0,index);
|
215
|
277
|
if(this.HasComponentDef(ns))
|
216
|
|
{
|
217
|
81
|
IS2Container child = (IS2Container) this.GetComponent(ns);
|
218
|
81
|
name = name.Substring(index + 1);
|
219
|
81
|
if(child.HasComponentDef(name))
|
220
|
|
{
|
221
|
77
|
return child.GetComponentDef(name);
|
222
|
|
}
|
223
|
|
}
|
224
|
|
}
|
225
|
|
}
|
226
|
839
|
for(int i = 0; i < this.ChildSize; ++i)
|
227
|
|
{
|
228
|
180
|
IS2Container child = this.GetChild(i);
|
229
|
180
|
if(child.HasComponentDef(key)) return child.GetComponentDef(key);
|
230
|
|
}
|
231
|
659
|
return null;
|
232
|
|
}
|
233
|
|
}
|
234
|
|
|
235
|
1062
|
public bool HasComponentDef(object componentKey)
|
236
|
|
{
|
237
|
|
|
238
|
1062
|
return this.GetComponentDef0(componentKey) != null;
|
239
|
|
}
|
240
|
|
|
241
|
68
|
public bool HasDescendant(string path)
|
242
|
|
{
|
243
|
|
|
244
|
68
|
lock(this)
|
245
|
|
{
|
246
|
68
|
return descendants_.ContainsKey(path);
|
247
|
|
}
|
248
|
|
}
|
249
|
|
|
250
|
4
|
public IS2Container GetDescendant(string path)
|
251
|
|
{
|
252
|
|
|
253
|
4
|
lock(this)
|
254
|
|
{
|
255
|
4
|
IS2Container descendant = (IS2Container) descendants_[path];
|
256
|
4
|
if(descendant != null)
|
257
|
|
{
|
258
|
4
|
return descendant;
|
259
|
|
}
|
260
|
|
else
|
261
|
|
{
|
262
|
0
|
throw new ContainerNotRegisteredRuntimeException(path);
|
263
|
|
}
|
264
|
|
}
|
265
|
|
}
|
266
|
|
|
267
|
64
|
public void RegisterDescendant(IS2Container descendant)
|
268
|
|
{
|
269
|
|
|
270
|
64
|
lock(this)
|
271
|
|
{
|
272
|
64
|
descendants_[descendant.Path] = descendant;
|
273
|
|
}
|
274
|
|
}
|
275
|
|
|
276
|
83
|
public void Include(IS2Container child)
|
277
|
|
{
|
278
|
|
|
279
|
83
|
lock(this)
|
280
|
|
{
|
281
|
83
|
child.Root = this.Root;
|
282
|
83
|
children_.Add(child);
|
283
|
83
|
string ns = child.Namespace;
|
284
|
83
|
if(ns!=null)
|
285
|
71
|
this.RegisterTable(ns,new S2ContainerComponentDef(child,ns));
|
286
|
|
}
|
287
|
|
}
|
288
|
|
|
289
|
|
public int ChildSize
|
290
|
|
{
|
291
|
1015
|
get
|
292
|
|
{
|
293
|
|
|
294
|
1015
|
lock(this)
|
295
|
|
{
|
296
|
1015
|
return children_.Count;
|
297
|
|
}
|
298
|
|
}
|
299
|
|
}
|
300
|
|
|
301
|
255
|
public IS2Container GetChild(int index)
|
302
|
|
{
|
303
|
|
|
304
|
255
|
lock(this)
|
305
|
|
{
|
306
|
255
|
return (IS2Container) children_[index];
|
307
|
|
}
|
308
|
|
}
|
309
|
|
|
310
|
127
|
public void Init()
|
311
|
|
{
|
312
|
|
|
313
|
127
|
if(inited_) return;
|
314
|
104
|
for(int i = 0;i < this.ChildSize; ++i)
|
315
|
|
{
|
316
|
20
|
this.GetChild(i).Init();
|
317
|
|
}
|
318
|
267
|
for(int i = 0; i < this.ComponentDefSize; ++i)
|
319
|
|
{
|
320
|
183
|
this.GetComponentDef(i).Init();
|
321
|
|
}
|
322
|
84
|
inited_ = true;
|
323
|
|
}
|
324
|
|
|
325
|
|
public string Namespace
|
326
|
|
{
|
327
|
84
|
get
|
328
|
|
{
|
329
|
|
|
330
|
84
|
return namespace_;
|
331
|
|
}
|
332
|
116
|
set
|
333
|
|
{
|
334
|
|
|
335
|
116
|
lock(this)
|
336
|
|
{
|
337
|
116
|
if(namespace_ != null && componentDefTable_.Contains(namespace_))
|
338
|
0
|
componentDefTable_.Remove(namespace_);
|
339
|
116
|
namespace_ = value;
|
340
|
116
|
componentDefTable_[namespace_] =
|
341
|
|
new SimpleComponentDef(this,namespace_);
|
342
|
|
}
|
343
|
|
}
|
344
|
|
}
|
345
|
|
|
346
|
|
public string Path
|
347
|
|
{
|
348
|
65
|
get
|
349
|
|
{
|
350
|
|
|
351
|
65
|
return path_;
|
352
|
|
}
|
353
|
145
|
set
|
354
|
|
{
|
355
|
|
|
356
|
145
|
path_ = value;
|
357
|
|
}
|
358
|
|
}
|
359
|
|
|
360
|
|
public IS2Container Root
|
361
|
|
{
|
362
|
220
|
get
|
363
|
|
{
|
364
|
|
|
365
|
220
|
return root_;
|
366
|
|
}
|
367
|
147
|
set
|
368
|
|
{
|
369
|
|
|
370
|
147
|
root_ = value;
|
371
|
|
}
|
372
|
|
}
|
373
|
|
|
374
|
155
|
public void Destroy()
|
375
|
|
{
|
376
|
155
|
if (!inited_) return;
|
377
|
209
|
for(int i = this.ComponentDefSize - 1; 0 <= i; --i)
|
378
|
|
{
|
379
|
137
|
try
|
380
|
|
{
|
381
|
137
|
this.GetComponentDef(i).Destroy();
|
382
|
|
}
|
383
|
|
catch(Exception ex)
|
384
|
|
{
|
385
|
0
|
Console.WriteLine(ex.Message);
|
386
|
0
|
Console.WriteLine(ex.StackTrace);
|
387
|
|
}
|
388
|
|
}
|
389
|
127
|
for(int i = this.ChildSize - 1; 0 <= i; --i)
|
390
|
|
{
|
391
|
55
|
this.GetChild(i).Destroy();
|
392
|
|
}
|
393
|
72
|
inited_ = false;
|
394
|
|
}
|
395
|
|
|
396
|
0
|
public HttpApplication HttpApplication
|
397
|
|
{
|
398
|
|
get
|
399
|
|
{
|
400
|
|
if(this.HttpContext == null)
|
401
|
|
{
|
402
|
|
return null;
|
403
|
|
}
|
404
|
|
else
|
405
|
|
{
|
406
|
|
return this.HttpContext.ApplicationInstance;
|
407
|
|
}
|
408
|
|
}
|
409
|
|
}
|
410
|
|
|
411
|
0
|
public HttpResponse Response
|
412
|
|
{
|
413
|
|
get
|
414
|
|
{
|
415
|
|
if(this.HttpContext == null)
|
416
|
|
{
|
417
|
|
return null;
|
418
|
|
}
|
419
|
|
else
|
420
|
|
{
|
421
|
|
return this.HttpContext.Response;
|
422
|
|
}
|
423
|
|
}
|
424
|
|
}
|
425
|
|
|
426
|
0
|
public HttpRequest Request
|
427
|
|
{
|
428
|
|
get
|
429
|
|
{
|
430
|
|
if(this.HttpContext == null)
|
431
|
|
{
|
432
|
|
return null;
|
433
|
|
}
|
434
|
|
else
|
435
|
|
{
|
436
|
|
return this.HttpContext.Request;
|
437
|
|
}
|
438
|
|
}
|
439
|
|
}
|
440
|
|
|
441
|
0
|
public HttpSessionState Session
|
442
|
|
{
|
443
|
|
get
|
444
|
|
{
|
445
|
|
if(this.HttpContext == null)
|
446
|
|
{
|
447
|
|
return null;
|
448
|
|
}
|
449
|
|
else
|
450
|
|
{
|
451
|
|
return this.HttpContext.Session;
|
452
|
|
}
|
453
|
|
}
|
454
|
|
}
|
455
|
|
|
456
|
|
public HttpContext HttpContext
|
457
|
|
{
|
458
|
4
|
set
|
459
|
|
{
|
460
|
4
|
CallContext.SetData(ContainerConstants.HTTP_CONTEXT_NAME, value);
|
461
|
|
}
|
462
|
5
|
get
|
463
|
|
{
|
464
|
5
|
return (HttpContext) CallContext.GetData(ContainerConstants.HTTP_CONTEXT_NAME);
|
465
|
|
}
|
466
|
|
}
|
467
|
|
|
468
|
|
#endregion
|
469
|
|
|
470
|
|
#region IMetaDefAware メンバ
|
471
|
|
|
472
|
1
|
public void AddMetaDef(IMetaDef metaDef)
|
473
|
|
{
|
474
|
|
|
475
|
1
|
metaDefSupport_.AddMetaDef(metaDef);
|
476
|
|
}
|
477
|
|
|
478
|
|
public int MetaDefSize
|
479
|
|
{
|
480
|
1
|
get
|
481
|
|
{
|
482
|
|
|
483
|
1
|
return metaDefSupport_.MetaDefSize;
|
484
|
|
}
|
485
|
|
}
|
486
|
|
|
487
|
0
|
public IMetaDef GetMetaDef(int index)
|
488
|
|
{
|
489
|
|
|
490
|
|
return metaDefSupport_.GetMetaDef(index);
|
491
|
|
}
|
492
|
|
|
493
|
1
|
public IMetaDef GetMetaDef(string name)
|
494
|
|
{
|
495
|
|
|
496
|
1
|
return metaDefSupport_.GetMetaDef(name);
|
497
|
|
}
|
498
|
|
|
499
|
0
|
public IMetaDef[] GetMetaDefs(string name)
|
500
|
|
{
|
501
|
|
|
502
|
|
return metaDefSupport_.GetMetaDefs(name);
|
503
|
|
}
|
504
|
|
|
505
|
|
#endregion
|
506
|
|
|
507
|
588
|
private static Type[] GetAssignableTypes(Type componentType)
|
508
|
|
{
|
509
|
588
|
ArrayList types = new ArrayList();
|
510
|
1397
|
for(Type type = componentType; type != typeof(object)
|
511
|
|
&& type != null; type = type.BaseType)
|
512
|
|
{
|
513
|
809
|
AddAssignableTypes(types,type);
|
514
|
|
}
|
515
|
588
|
return (Type[]) types.ToArray(typeof(Type));
|
516
|
|
}
|
517
|
|
|
518
|
2000
|
private static void AddAssignableTypes(IList types,Type type)
|
519
|
|
{
|
520
|
2000
|
if(types.Contains(type)) return;
|
521
|
1590
|
types.Add(type);
|
522
|
1590
|
Type[] interfaces = type.GetInterfaces();
|
523
|
1590
|
foreach(Type interfaceTemp in interfaces)
|
524
|
|
{
|
525
|
1191
|
AddAssignableTypes(types,interfaceTemp);
|
526
|
|
}
|
527
|
|
}
|
528
|
|
|
529
|
546
|
private void ProcessTooManyRegistration(object key,IComponentDef componentDef)
|
530
|
|
{
|
531
|
546
|
IComponentDef cd = (IComponentDef) componentDefTable_[key];
|
532
|
546
|
if(cd is TooManyRegistrationComponentDef)
|
533
|
|
{
|
534
|
301
|
((TooManyRegistrationComponentDef) cd)
|
535
|
|
.AddComponentType(componentDef.ComponentType);
|
536
|
|
}
|
537
|
|
else
|
538
|
|
{
|
539
|
245
|
TooManyRegistrationComponentDef tmrcf =
|
540
|
|
new TooManyRegistrationComponentDef(key);
|
541
|
245
|
tmrcf.AddComponentType(cd.ComponentType);
|
542
|
245
|
tmrcf.AddComponentType(componentDef.ComponentType);
|
543
|
245
|
componentDefTable_[key] = tmrcf;
|
544
|
|
}
|
545
|
|
}
|
546
|
|
|
547
|
|
}
|
548
|
|
}
|
549
|
|
|