Class: Seasar::Container::S2ApplicationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/seasar/container/s2application-context.rb

Overview

The main functions of S2ApplicationContext become the next.

  • Generate S2Container which contains the registerd class as a component

  • Automatic setup of Aspect

  • Singleton S2Container instance management

Constant Summary collapse

@@static_component_infos =
[]
@@static_aspect_infos =
[]
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeS2ApplicationContext

  • args

    • none

  • return

    • nil



76
77
78
# File 'lib/seasar/container/s2application-context.rb', line 76

def initialize
  self.init
end

Class Method Details

.instanceObject

  • args

    • none

  • return

    • Seasar::Container::S2ApplicationContext



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/seasar/container/s2application-context.rb', line 39

def instance
  if Seasar::Container.use_thread_local?
    require 'thread'
    th = Thread.current
    if th[:S2ApplicationContext].nil?
      th[:S2ApplicationContext] = self.new
    end
    return th[:S2ApplicationContext]
  end
  if @@instance.nil?
    @@instance = self.new
  end
  return @@instance;
end

.instance=(val) ⇒ Object

  • args

    1. Boolean

  • return

    • nil



60
61
62
63
64
65
66
67
# File 'lib/seasar/container/s2application-context.rb', line 60

def instance=(val)
  if Seasar::Container.use_thread_local?
    require 'thread'
    Thread.current[:S2ApplicationContext] = val
  else
    @@instance = val
  end
end

Instance Method Details

#aspect_infosObject

  • args

    • none

  • return

    • Array : aspect information list.



119
120
121
122
# File 'lib/seasar/container/s2application-context.rb', line 119

def aspect_infos
  return @snapshot_aspect_infos if self.snapshot?
  return @aspect_infos + @@static_aspect_infos
end

#component_infosObject

  • args

    • none

  • return

    • Array : component information list.



108
109
110
111
# File 'lib/seasar/container/s2application-context.rb', line 108

def component_infos
  return @snapshot_component_infos if self.snapshot?
  return @component_infos + @@static_component_infos
end

#create(namespaces = [], &procedure) ⇒ Object

  • args

    1. Array namespaces String namespaces

    2. Proc procedure component selector block

  • return

    • Seasar::Container::S2Container



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/seasar/container/s2application-context.rb', line 279

def create(namespaces = [], &procedure)
  namespaces = [namespaces] if namespaces.is_a?(String)
  container = S2Container.new
  component_defs = []
  component_infos = self.get_component_infos(namespaces)
  component_infos = self.select_component_infos(component_infos, &procedure)
  component_infos.each {|component_info|
    cd = ComponentDef.new(component_info.component_class, component_info.component_name)
    cd.constructor = component_info.constructor
    cd.destructor  = component_info.destructor

    if not component_info.instance.nil?
      cd.instance_def = Seasar::Container::Deployer::InstanceDefFactory.get_instance_def(component_info.instance)
    end

    if not component_info.autobinding.nil?
      cd.autobinding_def = Seasar::Container::Assembler::AutoBindingDefFactory.get_autobinding_def(component_info.autobinding)
    end

    component_defs << cd
    if component_info.namespace.nil?
      container.register(cd)
    else
      self.get_container(container, component_info.namespace).register(cd)
    end
  }
  component_defs.each{|cd|
    self.setup_auto_aspect(cd)
  }
  return container
end

#create_singleton_container(namespaces = [], &procedure) ⇒ Object Also known as: get_singleton_container

  • args

    1. Array namespaces String namespaces

    2. Proc procedure component selector block

  • return

    • String



246
247
248
249
250
251
252
253
# File 'lib/seasar/container/s2application-context.rb', line 246

def create_singleton_container(namespaces = [], &procedure)
  namespaces = [namespaces] if namespaces.is_a?(String)
  key = namespaces.sort
  unless @singletons.key?(key)
    @singletons[key] = self.create(namespaces, &procedure)
  end
  return @singletons[key]
end

#destroy_singleton_containers(namespaces = nil) ⇒ Object

  • args

    1. Array|nil namespaces String namespaces

  • return

    • nil



262
263
264
265
266
267
268
269
270
# File 'lib/seasar/container/s2application-context.rb', line 262

def destroy_singleton_containers(namespaces = nil)
  namespaces = [namespaces] if namespaces.is_a?(String)
  key = namespaces.sort unless namespaces.nil?
  if namespaces.nil?
    @singletons.each {|key, container| container.destroy}
  elsif @singletons.key?(key)
    @singletons[key].destroy
  end
end

#exclude(pattern) ⇒ Object

  • args

    1. String|Symbol|Class|Module|Regexp pattern

  • return

    • nil



475
476
477
# File 'lib/seasar/container/s2application-context.rb', line 475

def exclude(pattern)
  @exclude_patterns << pattern
end

#get_component(key, namespaces = [], &procedure) ⇒ Object Also known as: component, get, []

  • args

    1. String|Symbol key component key

    2. Array namespaces String namespaces

    3. Proc procedure constructor block

  • return

    • Object



220
221
222
# File 'lib/seasar/container/s2application-context.rb', line 220

def get_component(key, namespaces = [], &procedure)
  return self.get_singleton_container(namespaces).get_component(key, &procedure)
end

#get_component_infos(namespaces) ⇒ Object

  • args

    1. Array namespaces String namespaces

  • return

    • Array



383
384
385
386
387
388
# File 'lib/seasar/container/s2application-context.rb', line 383

def get_component_infos(namespaces)
  component_infos = self.select_component_info_by_namespace(self.component_infos, namespaces)
  component_infos = self.select_component_info_by_include(component_infos)
  component_infos = self.select_component_info_by_exclude(component_infos)
  return component_infos
end

#get_container(container, namespace) ⇒ Object

  • args

    1. Seasar::Container::S2Container container

    2. String namespace

  • return

    • Seasar::Container::S2Container



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/seasar/container/s2application-context.rb', line 338

def get_container(container, namespace)
  items = namespace.split('.', 2)
  if container.has_component_def(items[0])
    if items.length == 1
      return container.get_component(items[0])
    else
      return self.get_container(container.get_component(items[0]), items[1])
    end
  else
    child = S2Container.new
    child.namespace = items[0]
    container.include(child)
    if items.length == 1
      return child
    else
      return self.get_container(child, items[1])
    end
  end
end

#has_component_def(key, namespaces = []) ⇒ Object Also known as: component_def?

  • args

    1. String|Symbol key component key

    2. Array namespaces String namespaces

  • return

    • Boolean



234
235
236
# File 'lib/seasar/container/s2application-context.rb', line 234

def has_component_def(key, namespaces = [])
  return self.get_singleton_container(namespaces).has_component_def(key)
end

#include(pattern) ⇒ Object

  • args

    1. String|Symbol|Class|Module|Regexp pattern

  • return

    • nil



465
466
467
# File 'lib/seasar/container/s2application-context.rb', line 465

def include(pattern)
  @include_patterns << pattern
end

#init(options = {}) ⇒ Object

  • args

    • none

  • return

    • nil



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/seasar/container/s2application-context.rb', line 86

def init(options = {})
  if options[:force] == true
    @@static_component_infos = []
    @@static_aspect_infos    = []
    Seasar::Beans::BeanDescFactory.init
  end
  @include_patterns = []
  @exclude_patterns = []
  @singletons       = {}
  @selectors        = []
  @component_infos  = []
  @aspect_infos     = []
  @snapshot_component_infos = nil
  @snapshot_aspect_infos    = nil
end

#register(options = {}, &procedure) ⇒ Object

  • args

    1. Hash options component information

    2. Proc procedure constructor block of component

  • return

    • nil



178
179
180
181
182
183
184
185
186
187
# File 'lib/seasar/container/s2application-context.rb', line 178

def register(options = {}, &procedure)
  raise "can not register component_info to snapshot application context." if self.snapshot?
  info = ComponentInfoDef.new(options, &procedure)
  if options[:static] == true
    @@static_component_infos << info
  else
    @component_infos << info
  end
  return info
end

#register_aspect(options, &procedure) ⇒ Object Also known as: aspect

  • args

    1. Hash options aspect information

    2. Proc procedure interceptor block

  • return

    • nil



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/seasar/container/s2application-context.rb', line 196

def register_aspect(options, &procedure)
  raise "can not register aspect_info to snapshot application context." if self.snapshot?
  if block_given?
    options[:interceptor] = procedure
  end

  info = AspectInfoDef.new(options)
  if options[:static] == true
    @@static_aspect_infos << info
  else
    @aspect_infos << info
  end
  return info
end

#select(&procedure) ⇒ Object

  • args

    1. Proc procedure filter block

  • return

    • nil



455
456
457
# File 'lib/seasar/container/s2application-context.rb', line 455

def select(&procedure)
  @selectors << procedure if not procedure.nil?
end

#select_component_info_by_exclude(component_infos) ⇒ Object

  • args

    1. Array component_infos Array of Seasar::Container::ComponentInfoDef

  • return

    • Array



396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/seasar/container/s2application-context.rb', line 396

def select_component_info_by_exclude(component_infos)
  return component_infos if @exclude_patterns.size == 0
  result = []
  component_infos.each {|info|
    @exclude_patterns.each {|pattern|
      if info.match(pattern)
        next
      end
      result << info
      break
    }
  }
  return result
end

#select_component_info_by_include(component_infos) ⇒ Object

  • args

    1. Array component_infos Array of Seasar::Container::ComponentInfoDef

  • return

    • Array



417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/seasar/container/s2application-context.rb', line 417

def select_component_info_by_include(component_infos)
  return component_infos if @include_patterns.size == 0
  result = []
  component_infos.each {|info|
    @include_patterns.each {|pattern|
      if info.match(pattern)
        result << info
        break
      end
    }
  }
  return result
end

#select_component_info_by_namespace(component_infos, namespaces) ⇒ Object

  • args

    1. Array component_infos Array of Seasar::Container::ComponentInfoDef

    2. Array namespaces String namespaces

  • return

    • Array



438
439
440
441
442
443
444
445
446
447
# File 'lib/seasar/container/s2application-context.rb', line 438

def select_component_info_by_namespace(component_infos, namespaces)
  return component_infos if namespaces.size == 0
  reg_namespaces = namespaces.map {|v| /^#{v.gsub(/\./, '\.')}\./ }
  return component_infos.select {|info|
    next false if info.namespace.nil?
    next true if namespaces.member?(info.namespace)
    next true if nil != reg_namespaces.find {|reg| info.namespace.match(reg)}
    next false
    }
end

#select_component_infos(component_infos, &procedure) ⇒ Object

  • args

    1. Array component_infos Array of Seasar::Container::ComponentInfoDef

    2. Proc procedure filter block

  • return

    • Array

Raises:

  • (TypeError)


365
366
367
368
369
370
371
372
373
374
375
# File 'lib/seasar/container/s2application-context.rb', line 365

def select_component_infos(component_infos, &procedure)
  @selectors.each {|selector|
    component_infos = selector.call(component_infos)
    raise TypeError.new("result of component selector must be Array") if not component_infos.is_a?(Array)
  }
  if not procedure.nil?
    component_infos = procedure.call(component_infos)
  end
  raise TypeError.new("result of component selector must be Array") if not component_infos.is_a?(Array)
  return component_infos
end

#setup_auto_aspect(component_def) ⇒ Object

  • args

    1. Seasar::Container::ComponentDef component_def

  • return

    • nil



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/seasar/container/s2application-context.rb', line 317

def setup_auto_aspect(component_def)
  self.aspect_infos.each {|aspect_info|
    if aspect_info.applicable?(component_def)
      aspect_def = AspectDef.new(aspect_info.pointcut)
      if component_def.container.has_component_def(aspect_info.interceptor)
        aspect_def.child_component_def = component_def.container.get_component_def(aspect_info.interceptor)
      else
        aspect_def.value = aspect_info.interceptor
      end
      component_def.add_aspect_def(aspect_def)
    end
  }
end

#snapshotObject

  • args

    • none

  • return

    • S2ApplicationContext



144
145
146
147
148
149
# File 'lib/seasar/container/s2application-context.rb', line 144

def snapshot
  app = S2ApplicationContext.new
  app.snapshot_component_infos = self.component_infos
  app.snapshot_aspect_infos    = self.aspect_infos
  return app
end

#snapshot?Boolean

  • args

    • none

  • return

    • Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
# File 'lib/seasar/container/s2application-context.rb', line 130

def snapshot?
  if @snapshot_component_infos.nil? && @snapshot_aspect_infos.nil?
    return false
  else
    return true
  end
end

#snapshot_aspect_infos=(infos) ⇒ Object

  • args

    1. Array infos

  • return

    • nil



167
168
169
# File 'lib/seasar/container/s2application-context.rb', line 167

def snapshot_aspect_infos=(infos)
  @snapshot_aspect_infos = infos
end

#snapshot_component_infos=(infos) ⇒ Object

  • args

    1. Array infos

  • return

    • nil



157
158
159
# File 'lib/seasar/container/s2application-context.rb', line 157

def snapshot_component_infos=(infos)
  @snapshot_component_infos = infos
end