Module: Spider

Defined in:
lib/spiderfw/model/element.rb,
lib/spiderfw/app.rb,
lib/spiderfw/home.rb,
lib/spiderfw/site.rb,
lib/spiderfw/test.rb,
lib/spiderfw/create.rb,
lib/spiderfw/spider.rb,
lib/spiderfw/cmd/cmd.rb,
lib/spiderfw/tag/tag.rb,
lib/spiderfw/version.rb,
lib/spiderfw/autoload.rb,
lib/spiderfw/resource.rb,
lib/spiderfw/http/http.rb,
lib/spiderfw/i18n/cldr.rb,
lib/spiderfw/i18n/i18n.rb,
lib/spiderfw/test/unit.rb,
lib/spiderfw/i18n/rails.rb,
lib/spiderfw/model/sync.rb,
lib/spiderfw/model/type.rb,
lib/spiderfw/utils/fork.rb,
lib/spiderfw/http/server.rb,
lib/spiderfw/model/model.rb,
lib/spiderfw/model/query.rb,
lib/spiderfw/utils/rails.rb,
lib/spiderfw/utils/logger.rb,
lib/spiderfw/utils/memory.rb,
lib/spiderfw/content_utils.rb,
lib/spiderfw/i18n/provider.rb,
lib/spiderfw/model/request.rb,
lib/spiderfw/model/storage.rb,
lib/spiderfw/widget/widget.rb,
lib/spiderfw/model/junction.rb,
lib/spiderfw/model/condition.rb,
lib/spiderfw/model/data_type.rb,
lib/spiderfw/model/datatypes.rb,
lib/spiderfw/model/query_set.rb,
lib/spiderfw/utils/inflector.rb,
lib/spiderfw/utils/profiling.rb,
lib/spiderfw/utils/rails_app.rb,
lib/spiderfw/utils/sanitizer.rb,
lib/spiderfw/controller/scene.rb,
lib/spiderfw/model/base_model.rb,
lib/spiderfw/model/model_hash.rb,
lib/spiderfw/setup/setup_task.rb,
lib/spiderfw/templates/layout.rb,
lib/spiderfw/test/page_object.rb,
lib/spiderfw/controller/cookie.rb,
lib/spiderfw/controller/router.rb,
lib/spiderfw/http/adapters/cgi.rb,
lib/spiderfw/i18n/shtml_parser.rb,
lib/spiderfw/model/mixins/list.rb,
lib/spiderfw/model/mixins/tree.rb,
lib/spiderfw/model/proxy_model.rb,
lib/spiderfw/model/query_funcs.rb,
lib/spiderfw/model/query_funcs.rb,
lib/spiderfw/setup/app_manager.rb,
lib/spiderfw/utils/http_client.rb,
lib/spiderfw/widget/rest_model.rb,
lib/spiderfw/controller/cookies.rb,
lib/spiderfw/controller/request.rb,
lib/spiderfw/controller/session.rb,
lib/spiderfw/http/adapters/fcgi.rb,
lib/spiderfw/http/adapters/rack.rb,
lib/spiderfw/http/adapters/thin.rb,
lib/spiderfw/model/datatypes/pk.rb,
lib/spiderfw/model/inline_model.rb,
lib/spiderfw/model/unit_of_work.rb,
lib/spiderfw/templates/template.rb,
lib/spiderfw/utils/ordered_hash.rb,
lib/spiderfw/utils/shared_store.rb,
lib/spiderfw/config/configurable.rb,
lib/spiderfw/controller/response.rb,
lib/spiderfw/model/active_record.rb,
lib/spiderfw/model/mixins/mixins.rb,
lib/spiderfw/model/storage/db/db.rb,
lib/spiderfw/templates/blocks/if.rb,
lib/spiderfw/utils/secure_random.rb,
lib/spiderfw/cache/template_cache.rb,
lib/spiderfw/config/configuration.rb,
lib/spiderfw/model/datatypes/bool.rb,
lib/spiderfw/model/datatypes/text.rb,
lib/spiderfw/model/datatypes/uuid.rb,
lib/spiderfw/model/mappers/mapper.rb,
lib/spiderfw/model/storage/schema.rb,
lib/spiderfw/templates/blocks/run.rb,
lib/spiderfw/templates/blocks/tag.rb,
lib/spiderfw/widget/widget_plugin.rb,
lib/spiderfw/config/options/spider.rb,
lib/spiderfw/controller/controller.rb,
lib/spiderfw/controller/dispatcher.rb,
lib/spiderfw/http/adapters/mongrel.rb,
lib/spiderfw/http/adapters/webrick.rb,
lib/spiderfw/model/datatypes/email.rb,
lib/spiderfw/model/identity_mapper.rb,
lib/spiderfw/model/mappers/mappers.rb,
lib/spiderfw/templates/blocks/each.rb,
lib/spiderfw/templates/blocks/html.rb,
lib/spiderfw/templates/blocks/pass.rb,
lib/spiderfw/templates/blocks/text.rb,
lib/spiderfw/utils/periodic_runner.rb,
lib/spiderfw/i18n/javascript_parser.rb,
lib/spiderfw/model/datatypes/binary.rb,
lib/spiderfw/model/mixins/converted.rb,
lib/spiderfw/model/mixins/versioned.rb,
lib/spiderfw/templates/blocks/yield.rb,
lib/spiderfw/test/stubs/mapper_stub.rb,
lib/spiderfw/utils/multi_level_hash.rb,
lib/spiderfw/controller/formats/html.rb,
lib/spiderfw/model/datatypes/decimal.rb,
lib/spiderfw/model/mappers/db_mapper.rb,
lib/spiderfw/setup/app_server_client.rb,
lib/spiderfw/templates/blocks/lambda.rb,
lib/spiderfw/templates/blocks/output.rb,
lib/spiderfw/templates/blocks/render.rb,
lib/spiderfw/templates/blocks/tag_if.rb,
lib/spiderfw/templates/blocks/widget.rb,
lib/spiderfw/test/stubs/storage_stub.rb,
lib/spiderfw/controller/controller_io.rb,
lib/spiderfw/controller/mixins/visual.rb,
lib/spiderfw/model/datatypes/password.rb,
lib/spiderfw/model/integrated_element.rb,
lib/spiderfw/templates/blocks/attr_if.rb,
lib/spiderfw/templates/blocks/comment.rb,
lib/spiderfw/templates/blocks/recurse.rb,
lib/spiderfw/templates/resources/less.rb,
lib/spiderfw/widget/widget_attributes.rb,
lib/spiderfw/model/datatypes/file_path.rb,
lib/spiderfw/model/datatypes/time_span.rb,
lib/spiderfw/model/mappers/hash_mapper.rb,
lib/spiderfw/model/mixins/synchronized.rb,
lib/spiderfw/setup/spider_setup_wizard.rb,
lib/spiderfw/templates/blocks/debugger.rb,
lib/spiderfw/templates/template_blocks.rb,
lib/spiderfw/utils/events/event_source.rb,
lib/spiderfw/controller/first_responder.rb,
lib/spiderfw/controller/home_controller.rb,
lib/spiderfw/controller/http_controller.rb,
lib/spiderfw/controller/page_controller.rb,
lib/spiderfw/model/mappers/proxy_mapper.rb,
lib/spiderfw/model/mixins/state_machine.rb,
lib/spiderfw/model/storage/base_storage.rb,
lib/spiderfw/model/storage/db/db_schema.rb,
lib/spiderfw/model/storage/db/reflector.rb,
lib/spiderfw/model/storage/null_storage.rb,
lib/spiderfw/config/configuration_editor.rb,
lib/spiderfw/controller/controller_mixin.rb,
lib/spiderfw/model/storage/db/db_storage.rb,
lib/spiderfw/controller/mixins/http_mixin.rb,
lib/spiderfw/controller/spider_controller.rb,
lib/spiderfw/controller/session/flash_hash.rb,
lib/spiderfw/model/extended_models/managed.rb,
lib/spiderfw/model/mappers/document_mapper.rb,
lib/spiderfw/model/storage/connection_pool.rb,
lib/spiderfw/model/storage/db/db_connector.rb,
lib/spiderfw/templates/blocks/layout_assets.rb,
lib/spiderfw/controller/session/file_session.rb,
lib/spiderfw/model/storage/db/adapters/mssql.rb,
lib/spiderfw/model/storage/db/adapters/mysql.rb,
lib/spiderfw/model/storage/document/document.rb,
lib/spiderfw/templates/blocks/parent_context.rb,
lib/spiderfw/controller/controller_exceptions.rb,
lib/spiderfw/controller/helpers/widget_helper.rb,
lib/spiderfw/controller/mixins/static_content.rb,
lib/spiderfw/model/storage/db/adapters/oracle.rb,
lib/spiderfw/model/storage/db/adapters/sqlite.rb,
lib/spiderfw/model/storage/db/connectors/jdbc.rb,
lib/spiderfw/model/storage/db/connectors/oci8.rb,
lib/spiderfw/model/storage/db/connectors/odbc.rb,
lib/spiderfw/controller/session/memory_session.rb,
lib/spiderfw/controller/session/transient_hash.rb,
lib/spiderfw/model/datatypes/serialized_object.rb,
lib/spiderfw/utils/loggers/apache_commons_logger.rb,
lib/spiderfw/utils/shared_store/file_shared_store.rb,
lib/spiderfw/utils/shared_store/memory_shared_store.rb,
lib/spiderfw/model/storage/db/connectors/jdbc_oracle.rb,
lib/spiderfw/model/storage/db/dialects/no_total_rows.rb,
lib/spiderfw/model/storage/document/adapters/mongodb.rb,
lib/spiderfw/model/storage/document/document_storage.rb

Overview

– Modified from: ActiveSupport::OrderedHash, Copyright © 2005 David Heinemeier Hansson

Defined Under Namespace

Modules: App, AppManager, CommandLine, Configurable, ConsoleWizard, ContentUtils, ControllerMixin, ControllerMixins, Create, DataType, DataTypes, Dispatcher, EventSource, FirstResponder, Fork, HTML, HTTP, HashDottedAccess, Helpers, I18n, Inflector, LayoutScene, Logger, Loggers, Memory, Model, Profiling, QueryFuncs, RailsApp, RestModel, SecureRandom, Setup, TemplateAssets, TemplateBlocks, TemplateResources, Test, Utils, WidgetPlugin, WidgetScene Classes: AppServerClient, CompiledTemplate, Configuration, ConfigurationEditor, ConfigurationException, Controller, ControllerIO, Cookie, Cookies, DocType, FileSession, FlashHash, FullSanitizer, HTTPController, Home, HomeController, Layout, LinkSanitizer, MemorySession, MultiLevelHash, MySQLSetupWizard, OracleSetupWizard, OrderedHash, PageController, PeriodicRunner, Rails, Request, Resource, Response, Router, Sanitizer, Scene, Session, SetupTask, Site, SpiderController, SpiderSetupWizard, Tag, Template, TemplateCache, TemplateCompileError, TestCase, TransientHash, UploadedFile, WhiteListSanitizer, Widget, WidgetAttributes, Wizard

Constant Summary collapse

VERSION =
File.read(File.dirname(__FILE__)+'/../../VERSION').strip
Bool =
Spider::DataTypes::Bool
Text =
Spider::DataTypes::Text

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.appsObject (readonly)

An hash of registered Spider::App, indexed by name.



31
32
33
# File 'lib/spiderfw/spider.rb', line 31

def apps
  @apps
end

.apps_by_pathObject (readonly)

An hash of registred Spider::App modules, indexed by path.



33
34
35
# File 'lib/spiderfw/spider.rb', line 33

def apps_by_path
  @apps_by_path
end

.apps_by_short_nameObject (readonly)

An hash of registred Spider::App modules, indexed by short name (name without namespace).



35
36
37
# File 'lib/spiderfw/spider.rb', line 35

def apps_by_short_name
  @apps_by_short_name
end

.configurationObject (readonly) Also known as: config, conf

Returns the value of attribute configuration.



330
331
332
# File 'lib/spiderfw/config/configuration.rb', line 330

def configuration
  @configuration
end

.homeObject (readonly)

Current Home



51
52
53
# File 'lib/spiderfw/spider.rb', line 51

def home
  @home
end

.loggerObject (readonly)

An instance of the shared logger.



29
30
31
# File 'lib/spiderfw/spider.rb', line 29

def logger
  @logger
end

.pathsObject (readonly)

An hash of runtime paths.

:root

The base runtime path.

:apps

Apps folder.

:core_apps

Spider apps folder.

:config

Config folder.

:views

Runtime views folder.

:var

Var folder. Must be writable. Contains cache, logs, and other files written by the server.

:data

Data folder. Holds static and dynamic files. Some subdirs may have to be writable.

:certs

Certificates folder.

::tmp

Temp folder. Must be writable.

::log

Log location.



49
50
51
# File 'lib/spiderfw/spider.rb', line 49

def paths
  @paths
end

.resource_typesObject (readonly)

Registered resource types



53
54
55
# File 'lib/spiderfw/spider.rb', line 53

def resource_types
  @resource_types
end

.runmodeObject

The current runmode (test, devel or production).



37
38
39
# File 'lib/spiderfw/spider.rb', line 37

def runmode
  @runmode
end

.siteObject

Main site



55
56
57
# File 'lib/spiderfw/spider.rb', line 55

def site
  @site
end

.spawnerObject

Returns the value of attribute spawner.



56
57
58
# File 'lib/spiderfw/spider.rb', line 56

def spawner
  @spawner
end

Class Method Details

._test_setupObject



780
781
782
783
784
# File 'lib/spiderfw/spider.rb', line 780

def _test_setup
    @apps.each do |name, mod|
        mod.test_setup if mod.respond_to?(:test_setup)
    end
end

._test_teardownObject



786
787
788
789
790
# File 'lib/spiderfw/spider.rb', line 786

def _test_teardown
    @apps.each do |name, mod|
        mod.test_teardown if mod.respond_to?(:test_teardown)
    end
end

.activate_apps(apps, specs = nil) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/spiderfw/spider.rb', line 425

def activate_apps(apps, specs=nil)
    require 'spiderfw/config/configuration_editor'
    init_base
    unless specs
        specs = {}
        Spider.home.apps.each do |k, v|
            specs[k] = v[:spec] if apps.include?(k)
        end
    end
    editor = Spider::ConfigurationEditor.new
    Spider.config.loaded_files.each do |f|
        editor.load(f)
    end
    c_apps = Spider.config.get('apps')
    c_apps = (c_apps + apps).uniq
    editor.set('apps', Spider.apps_load_order(c_apps, specs))
    editor.save
end

.add_app(mod) ⇒ Object



413
414
415
416
417
# File 'lib/spiderfw/spider.rb', line 413

def add_app(mod)
    @apps[mod.name] = mod
    @apps_by_path[mod.relative_path] = mod
    @apps_by_short_name[mod.short_name] = mod
end

.app?(path_or_name) ⇒ Boolean

Returns:

  • (Boolean)


419
420
421
422
423
# File 'lib/spiderfw/spider.rb', line 419

def app?(path_or_name)
    return true if @apps_by_path[path_or_name]
    return true if @apps_by_short_name[path_or_name]
    return false
end

.apps_load_order(apps, specs) ⇒ Object



444
445
446
447
448
449
450
451
452
# File 'lib/spiderfw/spider.rb', line 444

def apps_load_order(apps, specs)
    # TODO
    require 'spiderfw/app'
    sort = Spider::App::RuntimeSort.new
    apps.each do |a|
        sort.add(specs[a] ? specs[a] : a)
    end
    sort.tsort
end

.conf_alias(name, al = nil) ⇒ Object



337
338
339
# File 'lib/spiderfw/config/configuration.rb', line 337

def self.conf_alias(name, al=nil)
    @configuration.conf_alias(name, al)
end

.config_option(*params) ⇒ Object



334
335
336
# File 'lib/spiderfw/config/configuration.rb', line 334

def self.config_option(*params)
    @configuration.config_option(*params)
end

.controllerObject

Returns the default controller.



483
484
485
486
# File 'lib/spiderfw/spider.rb', line 483

def controller
    require 'spiderfw/controller/spider_controller'
    SpiderController
end

.currentObject



241
242
243
# File 'lib/spiderfw/spider.rb', line 241

def current
    Spider::Request.current
end

.find_all_apps(paths = nil) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/spiderfw/spider.rb', line 382

def find_all_apps(paths=nil)
    paths ||= [@paths[:core_apps], @paths[:apps]]
    app_paths = []
    Find.find(*paths) do |path|
        if (File.basename(path) == '_init.rb')
            app_paths << File.dirname(path)
            Find.prune
        elsif File.exist?(File.join(path, '_init.rb'))
            app_paths << path
            Find.prune
        end
    end
    return app_paths
end

.find_app(name) ⇒ Object

Finds an app by name, looking in paths and paths. Returns the found path.



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/spiderfw/spider.rb', line 323

def find_app(name)
    path = nil
    [@paths[:apps], @paths[:core_apps]].each do |base|
        test = File.join(base, name)
        if File.exist?(File.join(test, '_init.rb'))
            path = test
            break
        end
    end
    return path
end

.find_apps(name) ⇒ Object



335
336
337
338
339
340
341
342
# File 'lib/spiderfw/spider.rb', line 335

def find_apps(name)
    [@paths[:apps], @paths[:core_apps]].each do |base|
        test = File.join(base, name)
        if File.exist?(test)
            return find_apps_in_folder(test)
        end
    end
end

.find_apps_in_folder(path) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/spiderfw/spider.rb', line 397

def find_apps_in_folder(path)
    return unless File.directory?(path)
    return [path] if File.exist?(File.join(path, '_init.rb'))
    found = []
    Dir.new(path).each do |f|
        next if f[0].chr == '.'
        found_path = File.join(path, f)
        if File.exist?(File.join(found_path, '/_init.rb'))
            found << found_path
        else
            found += find_apps_in_folder(found_path)
        end
    end
    return found
end

.find_resource(resource_type, path, cur_path = nil, owner_classes = nil, search_paths = []) ⇒ Object

Returns the full path of a resource. resource_type may be :views, or any other type registered with #register_resource_type path is the path of the resource, relative to the resource folder cur_path, if provided, is the current working path owner_class, if provided, must respond to app

Will look for the resource in the runtime root first, than in the app’s :“#resource_type_path”, and finally in the spider folder.



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/spiderfw/spider.rb', line 538

def find_resource(resource_type, path, cur_path=nil, owner_classes=nil, search_paths=[])
    owner_classes = [owner_classes] unless owner_classes.is_a?(Enumerable)
    # FIXME: security check for allowed paths?
    def first_found(extensions, path)
        extensions.each do |ext|
            full = path
            full += '.'+ext if ext
            return full if (File.exist?(full))
        end
        return nil
    end
    
    search_paths ||= []
    owner_classes.each do |owner_class| # FIXME: refactor
        next if owner_class.is_a?(Spider::Home) # home is already checked for other owner_classes
                                                # FIXME: maybe it shouldn't get here?
        owner_class = nil if owner_class == NilClass
        resource_config = @resource_types[resource_type]
        raise "Unknown resource type #{resource_type}" unless resource_config
        resource_rel_path = resource_config[:path]
        extensions = [nil] + resource_config[:extensions]
        path.strip!
        if (path[0..3] == 'ROOT')
            path.sub!(/^ROOT/, Spider.paths[:root])
            return Resource.new(path, @home)
        elsif (path[0..5] == 'SPIDER')
            path.sub!(/^SPIDER/, $SPIDER_PATH)
            return Resource.new(path, self)
        elsif (cur_path)
            if (path[0..1] == './')
                return Resource.new(first_found(extensions, File.dirname(cur_path)+path[1..-1]), owner_class)
            elsif (path[0..1] == '../')
                return Resource.new(first_found(extensions, File.dirname(File.dirname(cur_path))+path[2..-1]), owner_class)
            end
        end
        app = nil
        path_app = nil
        if (path[0].chr == '/')
            first_part = path[1..-1].split('/')[0]
            Spider.apps_by_path.each do |p, a|
                if path.index(p+'/') == 1 # FIXME: might not be correct
                #if first_part == p
                    path_app = a
                    path = path[p.length+2..-1]
                    break
                end
            end
            app = path_app
        elsif owner_class <= Spider::App
            app = owner_class
        else
            app = owner_class.app if (owner_class && owner_class.app)
        end
        return Resource.new(cur_path+'/'+path, owner_class) if cur_path && File.exist?(cur_path+'/'+path) # !app
        raise "Can't find owner app for resource #{path}" unless app
        search_locations = resource_search_locations(resource_type, app)
        search_paths.each do |p|
            p = [p, owner_class] unless p.is_a?(Array)
            search_locations << p
        end
        search_locations.each do |p|
            found = first_found(extensions, p[0]+'/'+path)
            next if found == cur_path
            definer = path_app || p[1]
            return Resource.new(found, definer) if found
        end
    end
    resource_type, path, cur_path=nil, owner_classes=nil, search_paths
    return Resource.new(path)
end

.find_resource_path(resource_type, path, cur_path = nil, owner_classes = nil, search_paths = []) ⇒ Object



665
666
667
668
# File 'lib/spiderfw/spider.rb', line 665

def find_resource_path(resource_type, path, cur_path=nil, owner_classes=nil, search_paths=[])
    res = find_resource(resource_type, path, cur_path, owner_classes, search_paths)
    return res ? res.path : nil
end

.fork(&proc) ⇒ Object

Calls Spider::Fork.fork



36
37
38
# File 'lib/spiderfw/utils/fork.rb', line 36

def self.fork(&proc)
    Spider::Fork.fork(&proc)
end

.http_clientObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/spiderfw/utils/http_client.rb', line 5

def self.http_client
    uri = URI.parse('http://www.test.com')
    proxy = uri.find_proxy
    klass = nil
    if proxy
        proxy_user, proxy_pass = nil
        proxy_user, proxy_pass = proxy.userinfo.split(/:/) if proxy.userinfo
        Net::HTTP::Proxy(proxy.host, proxy.port, proxy_user, proxy_pass)
    else
        Net::HTTP
    end
end

.i18n(l = self.locale) ⇒ Object



770
771
772
# File 'lib/spiderfw/spider.rb', line 770

def i18n(l = self.locale)
    Spider::I18n.provider(l)
end

.init(force = false) ⇒ Object

Initializes the runtime environment. This method is called when spider is required. Apps may implement an app_init method, that will be called after Spider::init is done.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spiderfw/spider.rb', line 60

def init(force=false)
    return if @init_done && !force
    
    init_base(force)
    
    start_loggers

#            @controller = Controller
    @paths[:spider] = $SPIDER_PATH

    if ($SPIDER_CONFIG_SETS)
        $SPIDER_CONFIG_SETS.each{ |set| @configuration.include_set(set) }
    end
    init_file = File.join($SPIDER_RUN_PATH, 'init.rb')
    ENV['BUNDLE_GEMFILE'] ||= File.join($SPIDER_RUN_PATH, 'Gemfile')
    require 'bundler/setup' if File.exists? ENV['BUNDLE_GEMFILE']
    
    if File.exist?(init_file)
        @home.instance_eval(File.read(init_file), init_file)
    end
    
    @apps.each do |name, mod|
        mod.app_init if mod.respond_to?(:app_init)
    end
    GetText::LocalePath.memoize_clear # since new paths have been added to GetText
    @apps.each do |name, mod|
        if File.directory?(File.join(mod.path, 'po'))
            GetText.bindtextdomain(mod.short_name)
        end
    end
    @init_done=true
end

.init_base(force = false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/spiderfw/spider.rb', line 97

def init_base(force=false)
    return if @init_base_done && !force
    
    @apps_to_load = []
    @root = $SPIDER_RUN_PATH
    @home = Home.new(@root)
    
    require 'spiderfw/config/options/spider.rb'

    setup_paths(@root)
    all_apps = find_all_apps
    all_apps.each do |path|
        load_configuration(File.join(path, 'config'))
    end
    @runmode = nil
    self.runmode = $SPIDER_RUNMODE if $SPIDER_RUNMODE
    
    load_configuration File.join($SPIDER_PATH, 'config')
    load_configuration File.join(@root, 'config')
    Locale.default = Spider.conf.get('i18n.default_locale')
    setup_env
    @init_base_done = true
end

.init_debugObject



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/spiderfw/spider.rb', line 745

def init_debug
    begin
        require 'ruby-debug'
        if File.exists?(File.join($SPIDER_RUN_PATH,'tmp', 'debug.txt'))
            Debugger.wait_connection = true
            Debugger.start_remote
            File.delete(File.join($SPIDER_RUN_PATH,'tmp', 'debug.txt'))
        else
            Debugger.start
        end
    rescue LoadError, RuntimeError => exc
        msg = _('Unable to start debugger. Ensure ruby-debug is installed (or set debugger.start to false).')
        if Spider.logger
            Spider.logger.warn(exc.message)
            Spider.logger.warn(msg) 
        else
            puts msg
        end
    end
end

.init_done?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/spiderfw/spider.rb', line 93

def init_done?
    @init_done
end

.list_resources(resource_type, owner_class = nil, start = nil, search_paths = []) ⇒ Object



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/spiderfw/spider.rb', line 636

def list_resources(resource_type, owner_class=nil, start=nil, search_paths = [])
    app = nil
    if owner_class <= Spider::App
        app = owner_class
    else
        app = owner_class.app if (owner_class && owner_class.app)
    end
    search_locations = resource_search_locations(resource_type, app)
    resource_config = @resource_types[resource_type]
    extensions = resource_config[:extensions]
    search_paths.each do |p|
        p = [p, owner_class] unless p.is_a?(Array)
        search_locations << p
    end
    res = []
    search_locations.reverse.each do |p|
        pname = Pathname.new(p[0])
        base = p[0]
        base = File.join(base, start) if start
        extensions.each do |ext|
            Dir.glob(File.join(base, "*.#{ext}")).each do |f|
                res << (Pathname.new(f).relative_path_from(pname)).to_s
            end
        end
    end
    res.uniq
    
end

.load_all_appsObject



376
377
378
379
380
# File 'lib/spiderfw/spider.rb', line 376

def load_all_apps
    find_all_apps.each do |path|
        load_app_at_path(path)
    end
end

.load_app(name) ⇒ Object



344
345
346
347
348
349
# File 'lib/spiderfw/spider.rb', line 344

def load_app(name)
    paths = find_apps(name)
    paths.each do |path|
        load_app_at_path(path)
    end
end

.load_app_at_path(path) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/spiderfw/spider.rb', line 351

def load_app_at_path(path)
    return if @loaded_apps[path]
    relative_path = path
    if path.index(Spider.paths[:root])
        home = Pathname.new(Spider.paths[:root])
        pname = Pathname.new(path)
        relative_path = pname.relative_path_from(home).to_s
    end
    @loaded_apps[path] = true
    last_name = File.basename(path)
    app_files = ['_init.rb', last_name+'.rb', 'cmd.rb']
    app_files.each{ |f| require File.join(relative_path, f) if File.exist?(File.join(path, f)) }
    GetText::LocalePath.add_default_rule(File.join(path, "data/locale/%{lang}/LC_MESSAGES/%{name}.mo"))
end

.load_apps(*l) ⇒ Object



367
368
369
370
371
372
373
374
# File 'lib/spiderfw/spider.rb', line 367

def load_apps(*l)
    if l.empty?
        l = Spider.conf.get('apps')
    end
    l.each do |app|
        load_app(app)
    end
end

.load_configuration(path) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/spiderfw/spider.rb', line 454

def load_configuration(path)
    return unless File.directory?(path)
    opts = File.join(path, 'options.rb')
    require opts if File.exist?(opts)
    Dir.new(path).each do |f|
        f.untaint # FIXME: security parse
        case f
        when /^\./
            next
        when /\.(yaml|yml)$/
            begin
                @configuration.load_yaml(File.join(path, f))
            rescue ConfigurationException => exc
                if (exc.type == :yaml)
                    err = "Configuration file #{path+f} is not valid YAML"
                    if @logger
                        @logger.error(err)
                    else
                        puts err
                    end
                else
                    raise
                end
            end
        end
    end
end

.localeObject



766
767
768
# File 'lib/spiderfw/spider.rb', line 766

def locale
    Locale.current[0]
end

.main_process_shutdownObject



227
228
229
230
231
232
233
234
# File 'lib/spiderfw/spider.rb', line 227

def main_process_shutdown
    if startup_done?
        shutdown!
    end
    if @main_process_shutdown_blocks
        @main_process_shutdown_blocks.each{ |b| b.call }
    end
end

.main_process_startupObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/spiderfw/spider.rb', line 165

def main_process_startup
    if Object.const_defined?(:FSSM)
        monitor = FSSM::Monitor.new

        monitor.path(Spider.paths[:tmp], 'restart.txt') do
            create { |base, relative| Process.kill 'HUP', $$ }
            update { |base, relative| Process.kill 'HUP', $$ }            

        end

        @fssm_thread = Thread.new do
            monitor.run
        end
        Spider.logger.debug("Monitoring restart.txt")
    else
        Spider.logger.debug("FSSM not installed, unable to monitor restart.txt")
    end
    trap('TERM'){ Spider.main_process_shutdown; exit }
    trap('INT'){ Spider.main_process_shutdown; exit }
    trap('HUP'){ Spider.respawn! }
    
    if @main_process_startup_blocks
        @main_process_startup_blocks.each{ |block| block.call }
    end
    
end

.mutex_requests!Object



258
259
260
# File 'lib/spiderfw/spider.rb', line 258

def mutex_requests!
    @request_mutex = Mutex.new
end

.on_main_process_shutdown(&block) ⇒ Object



236
237
238
239
# File 'lib/spiderfw/spider.rb', line 236

def on_main_process_shutdown(&block)
    @main_process_shutdown_blocks ||= []
    @main_process_shutdown_blocks << block
end

.on_main_process_startup(&proc) ⇒ Object



192
193
194
195
# File 'lib/spiderfw/spider.rb', line 192

def on_main_process_startup(&proc)
    @main_process_startup_blocks ||= []
    @main_process_startup_blocks << proc
end

.on_shutdown(&block) ⇒ Object



201
202
203
204
# File 'lib/spiderfw/spider.rb', line 201

def on_shutdown(&block)
    @shutdown_blocks ||= []
    @shutdown_blocks << block
end

.pathObject



522
523
524
# File 'lib/spiderfw/spider.rb', line 522

def path
    $SPIDER_PATH
end

.register_resource_type(name, options = {}) ⇒ Object

Adds a resource type name must be a symbol, extensions an array of extensions (strings, without the dot) for this resource. Options may be: :extensions an array of possible extensions. If given, find_resource will try appending the extensions

when looking for the file.

:path the path of the resource relative to resource root; if not given, name will be used.



513
514
515
516
517
518
# File 'lib/spiderfw/spider.rb', line 513

def register_resource_type(name, options={})
    @resource_types[name] = {
        :extensions => options[:extensions],
        :path => options[:path] || name.to_s
    }
end

.relative_pathObject



526
527
528
# File 'lib/spiderfw/spider.rb', line 526

def relative_path
    '/spider'
end

.reload_sourcesObject



698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/spiderfw/spider.rb', line 698

def reload_sources
    logger.debug("Reloading sources")
    crit = Thread.critical
    Thread.critical = true
    $".each do |file|
        if file =~ /^(#{$SPIDER_RUN_PATH}|apps)/ 
         #   logger.debug("RELOADING #{file}")
            load(file)
        else
        #    logger.debug("SKIPPING #{file}")
        end
    end
    Thread.critical = crit
end

.reload_sources_in_dir(dir) ⇒ Object



692
693
694
695
696
# File 'lib/spiderfw/spider.rb', line 692

def reload_sources_in_dir(dir)
    self.sources_in_dir(dir).each do |file|
        load(file)
    end
end

.request_finishedObject



252
253
254
255
256
# File 'lib/spiderfw/spider.rb', line 252

def request_finished
    # Spider.logger.info("Done in #{(Time.now - Spider::Request.current[:_start])*1000}ms")
    Spider::Request.reset_current
    @request_mutex.unlock if (@request_mutex)
end

.request_mutexObject



262
263
264
# File 'lib/spiderfw/spider.rb', line 262

def request_mutex
    @request_mutex
end

.request_mutex=(val) ⇒ Object



266
267
268
# File 'lib/spiderfw/spider.rb', line 266

def request_mutex=(val)
    @request_mutex = val
end

.request_startedObject



245
246
247
248
249
250
# File 'lib/spiderfw/spider.rb', line 245

def request_started
    @request_mutex.lock if (@request_mutex)
    Spider::Request.current = {
        :_start => Time.now
    }
end

.resource_search_locations(resource_type, app = nil) ⇒ Object



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/spiderfw/spider.rb', line 609

def resource_search_locations(resource_type, app=nil)
    resource_config = @resource_types[resource_type]
    resource_rel_path = resource_config[:path]
    app_rel_path = app && app.respond_to?(:relative_path) ? app.relative_path : nil
    search_locations = []
    unless Spider.conf.get('resources.disable_custom')
        root_search = File.join(Spider.paths[:root], resource_rel_path)
        root_search = File.join(root_search, app_rel_path) if app_rel_path
        # unless cur_path && cur_path == File.join(root_search, path)
        search_locations = [[root_search, @home]]
        # end
    end
    if app
        if app.respond_to?("#{resource_type}_path")
            search_locations << [app.send("#{resource_type}_path"), app]
        else
            search_locations << [File.join(app.path, resource_rel_path), app]
        end
        if Spider.runmode == 'test'
            search_locations << [File.join(app.path, 'test', resource_rel_path), app]
        end
    end
    spider_path = File.join($SPIDER_PATH, resource_rel_path)
    search_locations << [spider_path, self]
    search_locations
end

.respawn!Object



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/spiderfw/spider.rb', line 713

def respawn!
    require 'rbconfig'
    Spider.logger.info("Restarting")
    ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']).sub(/.*\s.*/m, '"\&"')
    Spider.main_process_shutdown
    return if $SPIDER_NO_RESPAWN
    cmd = $SPIDER_SCRIPT || $0
    args = $SPIDER_PROC_ARGS || ARGV
    Spider.logger.debug("CWD: #{Dir.pwd}")
    if RUBY_PLATFORM =~ /win32|mingw32/
        start_cmd = "start cmd /C #{ruby} #{cmd} #{args.join(' ')}"
        Spider.logger.debug(start_cmd)
        IO.popen(start_cmd)
        sleep 5
    else
        start_cmd = "#{ruby} #{cmd} #{args.join(' ')}"
        Spider.logger.debug(start_cmd)
        exec(start_cmd)
    end
end

.route_apps(*apps) ⇒ Object

Sets routes on the #controller for the given apps.



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/spiderfw/spider.rb', line 489

def route_apps(*apps)
    options = {}
    if apps[-1].is_a?(Hash)
        options = apps.pop
    end
    @route_apps = apps.empty? ? true : apps
    if (@route_apps)
        apps_to_route = @route_apps == true ? self.apps.values : @route_apps.map{ |name| self.apps[name] }
    end
    if options[:except]
        apps_to_route.reject{ |app| options[:except].include?(app) }
    end
    if (apps_to_route)
        apps_to_route.each{ |app| @home.controller.route_app(app) }
    end
end

.setup_envObject

def stop

@apps.each do |name, mod|
    mod.app_stop if mod.respond_to?(:app_stop)
end

end



128
129
130
131
132
133
134
135
136
# File 'lib/spiderfw/spider.rb', line 128

def setup_env
    unless File.exists?(File.join(Spider.paths[:root], 'init.rb'))
        raise "This command must be run from the root directory"
    end
    FileUtils.mkdir_p(Spider.paths[:tmp])
    FileUtils.mkdir_p(Spider.paths[:var])
    FileUtils.mkdir_p(File.join(Spider.paths[:var], 'memory'))
    
end

.setup_paths(root) ⇒ Object

Sets the default paths (see #paths).



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/spiderfw/spider.rb', line 306

def setup_paths(root)
    @paths[:root] = root
    @paths[:apps] = File.join(root, 'apps')
    @paths[:core_apps] = File.join($SPIDER_PATH, 'apps')
    @paths[:config] = File.join(root, 'config')
    @paths[:layouts] = File.join(root, 'layouts')
    @paths[:var] = File.join(root, 'var')
    @paths[:certs] = File.join(@paths[:config], 'certs')
    @paths[:tmp] = File.join(root, 'tmp')
    @paths[:data] = File.join(root, 'data')
    @paths[:log] = File.join(@paths[:var], 'log')
    @paths.each do |k, path|
        @paths[k] = File.expand_path(File.readlink(path)) if File.symlink?(path)
    end
end

.shutdown(force = false) ⇒ Object

Invoked when a server is shutdown. Apps may implement the app_shutdown method, that will be called.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/spiderfw/spider.rb', line 207

def shutdown(force=false)
    unless force
        #return unless Thread.current == Thread.main
        return if @shutdown_done
    end
    @shutdown_done = true
    Spider.logger.debug("Shutdown")
    Debugger.post_mortem = false if Object.const_defined?(:Debugger) && Debugger.post_mortem?
    @apps.each do |name, mod|
        mod.app_shutdown if mod.respond_to?(:app_shutdown)
    end
    if @shutdown_blocks
        @shutdown_blocks.each{ |b| b.call }
    end
end

.shutdown!Object



223
224
225
# File 'lib/spiderfw/spider.rb', line 223

def shutdown!
    shutdown(true)
end

.sources_in_dir(path) ⇒ Object

Source file management



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/spiderfw/spider.rb', line 673

def sources_in_dir(path)
    loaded = []
    $".each do |file|
        basename = File.basename(file)
        next if (basename == 'spider.rb' || basename == 'options.rb')
        if (file[0..path.length-1] == path)
           loaded.push(file)
        else
            $:.each do |dir|
                file_path = File.join(dir, file)
                if (file_path =~ /^#{path}/)  # FileTest.exists?(file_path) && 
                    loaded.push(file_path)
                end
            end
        end
    end
    return loaded
end

.start_loggers(force = false) ⇒ Object

Closes any open loggers, and opens new ones based on configured settings.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/spiderfw/spider.rb', line 272

def start_loggers(force=false)
    return if @logger && !force
    @logger ||= Spider::Logger
    @logger.close_all
    @logger.open(STDERR, Spider.conf.get('log.console')) if Spider.conf.get('log.console')
    begin
        FileUtils.mkdir(@paths[:log]) unless File.exist?(@paths[:log])
    rescue => exc
        @logger.error("Unable to create log folder") if File.exist?(File.dirname(@paths[:log]))
    end
    if @paths[:log] && File.exist?(@paths[:log])
        @logger.open(File.join(@paths[:log], 'error.log'), :ERROR) if Spider.conf.get('log.errors')
        if Spider.conf.get('log.level')
            @logger.open(File.join(@paths[:log], Spider.conf.get('log.file_name')), Spider.conf.get('log.level'))
        end
    end
    if RUBY_PLATFORM =~ /java/ && Spider.conf.get('log.apache_commons')
        begin
            require 'spiderfw/utils/loggers/apache_commons_logger'
            l = Spider::Loggers::ApacheCommonsLogger.new
            @logger.add('apache_commons_logger', l)
        rescue NameError
            $stderr << "Warning: Unable to load Java class org.apache.commons.logging.LogFactory\n"
        end
    end
    $LOG = @logger
    Object.const_set(:LOGGER, @logger)
end

.start_loggers!Object



301
302
303
# File 'lib/spiderfw/spider.rb', line 301

def start_loggers!
    start_loggers(false)
end

.startupObject

Invoked before a long running service started. Apps may implement the app_startup method, that will be called.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/spiderfw/spider.rb', line 140

def startup
    setup_env
    if Spider.conf.get('template.cache.reload_on_restart')
        FileUtils.touch("#{Spider.paths[:tmp]}/templates_reload.txt")
    end
    unless Spider.runmode == 'test'
        if domain = Spider.conf.get('site.domain')
            ssl_port = Spider.conf.get('site.ssl') ? Spider.conf.get('site.ssl_port') : nil
            Spider.site = Site.new(domain, Spider.conf.get('site.port'), ssl_port)
        elsif File.exists?(Site.cache_file)
            Spider.site = Site.load_cache
        end
    end
    if Spider.conf.get('request.mutex')
        mutex_requests!
    end
    @apps.each do |name, mod|
        mod.app_startup if mod.respond_to?(:app_startup)
    end
    @startup_done = true
    at_exit do
        Spider.shutdown
    end
end

.startup_done?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/spiderfw/spider.rb', line 197

def startup_done?
    @startup_done
end

.test_setupObject



774
775
# File 'lib/spiderfw/spider.rb', line 774

def test_setup
end

.test_teardownObject



777
778
# File 'lib/spiderfw/spider.rb', line 777

def test_teardown
end

Instance Method Details

#first_found(extensions, path) ⇒ Object

FIXME: security check for allowed paths?



541
542
543
544
545
546
547
548
# File 'lib/spiderfw/spider.rb', line 541

def first_found(extensions, path)
    extensions.each do |ext|
        full = path
        full += '.'+ext if ext
        return full if (File.exist?(full))
    end
    return nil
end