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



61
62
63
# File 'lib/seasar/container/s2application-context.rb', line 61

def initialize
  self.init
end

Class Method Details

.instanceObject

  • args

    • none

  • return

    • Seasar::Container::S2ApplicationContext



39
40
41
42
43
44
# File 'lib/seasar/container/s2application-context.rb', line 39

def instance
  if @@instance.nil?
    @@instance = self.new
  end
  return @@instance;
end

.instance=(obj) ⇒ Object

  • args

    1. Seasar::Container::S2ApplicationContext

  • return

    • nil



52
# File 'lib/seasar/container/s2application-context.rb', line 52

def instance=(obj); @@instance = obj; end

Instance Method Details

#aspect_infosObject

  • args

    • none

  • return

    • Array : aspect information list.



104
105
106
107
# File 'lib/seasar/container/s2application-context.rb', line 104

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.



93
94
95
96
# File 'lib/seasar/container/s2application-context.rb', line 93

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 constructor block

  • return

    • Seasar::Container::S2Container



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/seasar/container/s2application-context.rb', line 246

def create(namespaces = [], &procedure)
  namespaces = [namespaces] unless namespaces.is_a?(Array)
  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|
    if component_info.procedure.nil?
      cd = ComponentDef.new(component_info.component_class, component_info.component_name)
    else
      procedure = component_info.procedure
      cd = ComponentDef.new(component_info.component_class, component_info.component_name, &procedure)
    end
    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(namespaces) ⇒ Object

  • args

    1. Array namespaces String namespaces

  • return

    • String



230
231
232
233
234
235
236
237
# File 'lib/seasar/container/s2application-context.rb', line 230

def create_singleton(namespaces)
  namespaces = [namespaces] unless namespaces.is_a?(Array)
  key = namespaces.sort
  if not @singletons.key?(key)
    @singletons[key] = self.create(namespaces)
  end
  return key
end

#exclude(pattern) ⇒ Object

  • args

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

  • return

    • nil



442
443
444
# File 'lib/seasar/container/s2application-context.rb', line 442

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



203
204
205
206
# File 'lib/seasar/container/s2application-context.rb', line 203

def get_component(key, namespaces = [], &procedure)
  namespace_key = self.create_singleton(namespaces)
  return @singletons[namespace_key].get_component(key, &procedure)
end

#get_component_infos(namespaces) ⇒ Object

  • args

    1. Array namespaces String namespaces

  • return

    • Array



350
351
352
353
354
355
# File 'lib/seasar/container/s2application-context.rb', line 350

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



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/seasar/container/s2application-context.rb', line 305

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



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

def has_component_def(key, namespaces = [])
  namespace_key = self.create_singleton(namespaces)
  return @singletons[namespace_key].has_component_def(key)
end

#include(pattern) ⇒ Object

  • args

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

  • return

    • nil



432
433
434
# File 'lib/seasar/container/s2application-context.rb', line 432

def include(pattern)
  @include_patterns << pattern
end

#init(options = {}) ⇒ Object

  • args

    • none

  • return

    • nil



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/seasar/container/s2application-context.rb', line 71

def init(options = {})
  if options[:force] == true
    @@static_component_infos = []
    @@static_aspect_infos    = []
    @@instance = nil
  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



163
164
165
166
167
168
169
170
171
# File 'lib/seasar/container/s2application-context.rb', line 163

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
end

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

  • args

    1. Hash options aspect information

    2. Proc procedure interceptor block

  • return

    • nil



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/seasar/container/s2application-context.rb', line 180

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
end

#select(&procedure) ⇒ Object

  • args

    1. Proc procedure filter block

  • return

    • nil



422
423
424
# File 'lib/seasar/container/s2application-context.rb', line 422

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



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

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



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/seasar/container/s2application-context.rb', line 384

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



405
406
407
408
409
410
411
412
413
414
# File 'lib/seasar/container/s2application-context.rb', line 405

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)


332
333
334
335
336
337
338
339
340
341
342
# File 'lib/seasar/container/s2application-context.rb', line 332

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



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/seasar/container/s2application-context.rb', line 284

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



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

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)


115
116
117
118
119
120
121
# File 'lib/seasar/container/s2application-context.rb', line 115

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



152
153
154
# File 'lib/seasar/container/s2application-context.rb', line 152

def snapshot_aspect_infos=(infos)
  @snapshot_aspect_infos = infos
end

#snapshot_component_infos=(infos) ⇒ Object

  • args

    1. Array infos

  • return

    • nil



142
143
144
# File 'lib/seasar/container/s2application-context.rb', line 142

def snapshot_component_infos=(infos)
  @snapshot_component_infos = infos
end