Module: Spider

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

Overview

-- Modified from: ActiveSupport::OrderedHash, Copyright (c) 2005 David Heinemeier Hansson

Defined Under Namespace

Modules: App, CommandLine, Configurable, ConsoleWizard, ContentUtils, ControllerMixin, ControllerMixins, Create, DataType, DataTypes, Dispatcher, EventSource, FirstResponder, Fork, GetText, HTML, HTTP, HashDottedAccess, Helpers, I18n, Inflector, LayoutScene, LessCompiler, Logger, Loggers, Memory, Migrations, Model, Profiling, QueryFuncs, RailsApp, RestModel, SassCompiler, SecureRandom, Setup, TemplateAssets, TemplateBlocks, Test, Utils, WidgetPlugin, WidgetScene Classes: AppManager, 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

VERSION =
File.read(File.dirname(__FILE__)+'/../../VERSION').strip

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.



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

def apps
  @apps
end

.apps_by_pathObject (readonly)

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



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

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).



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

def apps_by_short_name
  @apps_by_short_name
end

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

Returns the value of attribute configuration



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

def configuration
  @configuration
end

.homeObject (readonly)

Current Home



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

def home
  @home
end

.loggerObject (readonly)

An instance of the shared logger.



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

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.



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

def paths
  @paths
end

.resource_typesObject (readonly)

Registered resource types



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

def resource_types
  @resource_types
end

.runmodeObject

The current runmode (test, devel or production).



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

def runmode
  @runmode
end

.siteObject

Main site



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

def site
  @site
end

.spawnerObject

Returns the value of attribute spawner



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

def spawner
  @spawner
end

Class Method Details

._test_setupObject



858
859
860
861
862
# File 'lib/spiderfw/spider.rb', line 858

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

._test_teardownObject



864
865
866
867
868
# File 'lib/spiderfw/spider.rb', line 864

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



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/spiderfw/spider.rb', line 503

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



477
478
479
480
481
# File 'lib/spiderfw/spider.rb', line 477

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

.add_thread(thr) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/spiderfw/spider.rb', line 264

def add_thread(thr)
    @running_threads ||= []
    @threads_mutex ||= Mutex.new
    @threads_mutex.synchronize do
        @running_threads << thr
    end
end

.app?(path_or_name) ⇒ Boolean



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

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

.app_pathsObject



380
381
382
383
384
# File 'lib/spiderfw/spider.rb', line 380

def app_paths
    paths = [$SPIDER_PATHS[:core_apps]]
    paths.unshift(@paths[:apps]) if @paths[:apps]
    paths
end

.apps_load_order(apps, specs) ⇒ Object



522
523
524
525
526
527
528
529
530
# File 'lib/spiderfw/spider.rb', line 522

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.reject{ |a| a.nil? }
end

.conf_alias(name, al = nil) ⇒ Object



342
343
344
# File 'lib/spiderfw/config/configuration.rb', line 342

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

.config_option(*params) ⇒ Object



339
340
341
# File 'lib/spiderfw/config/configuration.rb', line 339

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

.controllerObject

Returns the default controller.



557
558
559
560
# File 'lib/spiderfw/spider.rb', line 557

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

.currentObject



297
298
299
# File 'lib/spiderfw/spider.rb', line 297

def current
    Spider::Request.current
end

.find_all_apps(paths = nil) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/spiderfw/spider.rb', line 445

def find_all_apps(paths=nil)
    paths ||= self.app_paths

    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.



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/spiderfw/spider.rb', line 387

def find_app(name)
    path = nil
    app_paths.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



399
400
401
402
403
404
405
406
# File 'lib/spiderfw/spider.rb', line 399

def find_apps(name)
    app_paths.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



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/spiderfw/spider.rb', line 461

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.



612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/spiderfw/spider.rb', line 612

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.file?(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..2] == '../')
                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 || owner_class == Spider
            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.file?(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



739
740
741
742
# File 'lib/spiderfw/spider.rb', line 739

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

.gem_available?(name, *requirements) ⇒ Boolean

TODO: remove when old RubyGems versions are not a problem anymore



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

def self.gem_available?(name, *requirements)
    if Gem::Specification.respond_to?(:find_by_name)
        begin
            Gem::Specification.find_by_name(name, *requirements)
        rescue Gem::LoadError
            return false
        end
    else
        Gem.available?(name, requirements.first)
    end
end

.get_app_deps(apps, options = {}) ⇒ Object



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

def get_app_deps(apps, options={})
    new_apps = apps.clone
    specs = {}
    init_base
    while !new_apps.empty? && curr = new_apps.pop
        raise "Could not find app #{curr}" unless Spider.home.apps[curr]
        spec = Spider.home.apps[curr][:spec]
        specs[curr] = spec
        new_apps += spec.depends.reject{ |app| specs[app] }
        new_apps += spec.depends_optional.reject{ |app| specs[app] } if options[:optional]
    end
    specs.keys
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



848
849
850
# File 'lib/spiderfw/spider.rb', line 848

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.



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
# File 'lib/spiderfw/spider.rb', line 61

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')
    if File.exists?(ENV['BUNDLE_GEMFILE']) && File.exists?(File.join($SPIDER_RUN_PATH, 'Gemfile.lock'))
        require 'bundler/setup' 
    end
    
    if File.exist?(init_file)
        @home.instance_eval(File.read(init_file), init_file)
    end
    
    init_apps
    @init_done=true
end

.init_appsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/spiderfw/spider.rb', line 88

def init_apps
    @apps.each do |name, mod|
        repos = []
        if File.directory?(File.join(mod.path, 'po'))
            repos <<  FastGettext::TranslationRepository.build(mod.short_name, :path => File.join(mod.path, 'data', 'locale'))
        end
        home_pot = File.join(Spider.paths[:root], 'po', "#{mod.short_name}.pot")
        home_locale = File.join(Spider.paths[:root], 'data', 'locale')
        if File.file?(home_pot) && File.directory?(home_locale)
            repos << FastGettext::TranslationRepository.build(mod.short_name, :path => home_locale)
        end
        unless repos.empty?
            FastGettext.add_text_domain(mod.short_name, :type => :chain, :chain => repos)
        end
    end
    @apps.each do |name, mod|
        mod.app_init if mod.respond_to?(:app_init)
    end
end

.init_base(force = false) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/spiderfw/spider.rb', line 112

def init_base(force=false)
    return if @init_base_done && !force
    l = Spider.locale.to_s
    l = $1 if l =~ /(\w\w)_+/
    FastGettext.locale = l
    
    @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
    @logger = Spider::Logger
    @init_base_done = true
end

.init_debugObject



819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/spiderfw/spider.rb', line 819

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).')
        Spider.output(exc.message)
        Spider.output(msg)
    end
end

.init_done?Boolean



108
109
110
# File 'lib/spiderfw/spider.rb', line 108

def init_done?
    @init_done
end

.interactive?Boolean



870
871
872
# File 'lib/spiderfw/spider.rb', line 870

def interactive?
    !!$SPIDER_INTERACTIVE
end

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



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/spiderfw/spider.rb', line 710

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



439
440
441
442
443
# File 'lib/spiderfw/spider.rb', line 439

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

.load_app(name) ⇒ Object



408
409
410
411
412
413
# File 'lib/spiderfw/spider.rb', line 408

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



415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/spiderfw/spider.rb', line 415

def load_app_at_path(path)
    return if @loaded_apps[path]
    relative_path = path
    if Spider.paths[:root] && 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)) }
end

.load_apps(*l) ⇒ Object



430
431
432
433
434
435
436
437
# File 'lib/spiderfw/spider.rb', line 430

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



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/spiderfw/spider.rb', line 532

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"
                    Spider.output(err, :ERROR)
                else
                    raise
                end
            end
        end
    end
end

.localeObject



836
837
838
839
840
841
842
843
844
845
846
# File 'lib/spiderfw/spider.rb', line 836

def locale
    begin
        @current_locale = Locale.current[0]
    rescue
        # There are problems with subsequent requests on Windows, 
        # so use cached locale if Locale.current fails
        l = @current_locale
        l ||= Locale::Tag.parse(Spider.conf.get('locale')) if Spider.conf.get('locale')
        l ||= Locale::Tag.parse('en')
    end
end

.main_process_shutdownObject



278
279
280
281
282
283
284
285
# File 'lib/spiderfw/spider.rb', line 278

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/spiderfw/spider.rb', line 185

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.output("Monitoring restart.txt")
    else
        Spider.output("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! } unless RUBY_PLATFORM =~ /win32|mingw32/
    
    if @main_process_startup_blocks
        @main_process_startup_blocks.each{ |block| block.call }
    end
    
end

.mutex_requests!Object



314
315
316
# File 'lib/spiderfw/spider.rb', line 314

def mutex_requests!
    @request_mutex = Mutex.new
end

.on_main_process_shutdown(&block) ⇒ Object



287
288
289
290
# File 'lib/spiderfw/spider.rb', line 287

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

.on_main_process_startup(&proc) ⇒ Object



212
213
214
215
# File 'lib/spiderfw/spider.rb', line 212

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

.on_shutdown(&block) ⇒ Object



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

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

.output(str, level = :INFO) ⇒ Object



874
875
876
877
878
879
880
881
882
# File 'lib/spiderfw/spider.rb', line 874

def output(str, level=:INFO)
    use_log = !Spider.interactive? && @logger_started
    if use_log
        @logger.log(level, str)
    else
        str = "#{level}: #{str}" if level == :ERROR
        puts str
    end
end

.pathObject



596
597
598
# File 'lib/spiderfw/spider.rb', line 596

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.



587
588
589
590
591
592
# File 'lib/spiderfw/spider.rb', line 587

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

.relative_pathObject



600
601
602
# File 'lib/spiderfw/spider.rb', line 600

def relative_path
    '/spider'
end

.reload_sourcesObject



772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/spiderfw/spider.rb', line 772

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



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

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

.remove_thread(thr) ⇒ Object



272
273
274
275
276
# File 'lib/spiderfw/spider.rb', line 272

def remove_thread(thr)
    @threads_mutex.synchronize do
        @running_threads.delete(thr)
    end
end

.request_finishedObject



308
309
310
311
312
# File 'lib/spiderfw/spider.rb', line 308

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



318
319
320
# File 'lib/spiderfw/spider.rb', line 318

def request_mutex
    @request_mutex
end

.request_mutex=(val) ⇒ Object



322
323
324
# File 'lib/spiderfw/spider.rb', line 322

def request_mutex=(val)
    @request_mutex = val
end

.request_startedObject



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

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

.resource_search_locations(resource_type, app = nil) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/spiderfw/spider.rb', line 683

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



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/spiderfw/spider.rb', line 787

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

.restart!Object



293
294
295
# File 'lib/spiderfw/spider.rb', line 293

def restart!
    FileUtils.touch(@paths[:restart_file])
end

.route_apps(*apps) ⇒ Object

Sets routes on the #controller for the given apps.



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/spiderfw/spider.rb', line 563

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



147
148
149
150
151
152
153
154
155
# File 'lib/spiderfw/spider.rb', line 147

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).



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/spiderfw/spider.rb', line 363

def setup_paths(root)
    @paths[:root] = root
    @paths[:apps] = File.join(root, 'apps')
    @paths[:core_apps] = $SPIDER_PATHS[:core_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[:restart_file] = File.join(@paths[:tmp], 'restart.txt')
    @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.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/spiderfw/spider.rb', line 227

def shutdown(force=false)
    unless force
        #return unless Thread.current == Thread.main
        return if @shutdown_done
    end
    @shutdown_done = true
    Spider.logger.debug("Shutdown")
    if @running_threads
        begin
            Timeout.timeout(Spider.conf.get('process.shutdown_timeout')) do
                @running_threads.each do |thr|
                    thr.join if thr.alive?
                end
            end
        rescue => exc
            Spider.logger.error(exc)
            @running_threads.each do |thr|
                begin
                    thr.kill
                rescue => exc
                end
            end
        end
    end
    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



260
261
262
# File 'lib/spiderfw/spider.rb', line 260

def shutdown!
    shutdown(true)
end

.sources_in_dir(path) ⇒ Object

Source file management



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

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.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/spiderfw/spider.rb', line 328

def start_loggers(force=false)
    init_base
    return if @logger_started && !force
    @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)
    @logger_started = true
end

.start_loggers!Object



358
359
360
# File 'lib/spiderfw/spider.rb', line 358

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.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/spiderfw/spider.rb', line 159

def startup
    init
    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



217
218
219
# File 'lib/spiderfw/spider.rb', line 217

def startup_done?
    @startup_done
end

.test_setupObject



852
853
# File 'lib/spiderfw/spider.rb', line 852

def test_setup
end

.test_teardownObject



855
856
# File 'lib/spiderfw/spider.rb', line 855

def test_teardown
end

Instance Method Details

#first_found(extensions, path) ⇒ Object

FIXME: security check for allowed paths?



615
616
617
618
619
620
621
622
# File 'lib/spiderfw/spider.rb', line 615

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