summaryrefslogtreecommitdiff
path: root/js/src/Ice/ObjectAdapterFactory.js
blob: aede99ab19307ddc2da327227596c5d0de7140ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

var Ice = require("../Ice/ModuleRegistry").Ice;
Ice.__M.require(module,
    [
        "../Ice/Class",
        "../Ice/AsyncResultBase",
        "../Ice/LocalException",
        "../Ice/ObjectAdapterI",
        "../Ice/Promise",
        "../Ice/UUID"
    ]);

var AsyncResultBase = Ice.AsyncResultBase;
var ObjectAdapterI = Ice.ObjectAdapterI;
var Promise = Ice.Promise;

//
// Only for use by Instance.
//
var ObjectAdapterFactory = Ice.Class({
    __init__: function(instance, communicator)
    {
        this._instance = instance;
        this._communicator = communicator;
        this._adapters = [];
        this._adapterNamesInUse = [];
        this._shutdownPromise = new Promise();
    },
    shutdown: function()
    {
        //
        // Ignore shutdown requests if the object adapter factory has
        // already been shut down.
        //
        if(this._instance === null)
        {
            return this._shutdownPromise;
        }

        this._instance = null;
        this._communicator = null;
        this._shutdownPromise = Promise.all(
            this._adapters.map(function(adapter)
                               {
                                   return adapter.deactivate();
                               }));
        return this._shutdownPromise;
    },
    waitForShutdown: function()
    {
        var self = this;
        return this._shutdownPromise.then(
            function()
            {
                return Promise.all(self._adapters.map(function(adapter)
                                                      {
                                                          return adapter.waitForDeactivate();
                                                      }));
            });
    },
    isShutdown: function()
    {
        return this._instance === null;
    },
    destroy: function()
    {
        var self = this;
        return this.waitForShutdown().then(
            function()
            {
                return Promise.all(self._adapters.map(function(adapter)
                                                      {
                                                          return adapter.destroy();
                                                      }));
            });
    },
    createObjectAdapter: function(name, router, promise)
    {
        if(this._instance === null)
        {
            throw new Ice.ObjectAdapterDeactivatedException();
        }

        var adapter = null;
        try
        {
            if(name.length === 0)
            {
                var uuid = Ice.generateUUID();
                adapter = new ObjectAdapterI(this._instance, this._communicator, this, uuid, null, true, promise);
            }
            else
            {
                if(this._adapterNamesInUse.indexOf(name) !== -1)
                {
                    throw new Ice.AlreadyRegisteredException("object adapter", name);
                }
                adapter = new ObjectAdapterI(this._instance, this._communicator, this, name, router, false, promise);
                this._adapterNamesInUse.push(name);
            }
            this._adapters.push(adapter);
        }
        catch(ex)
        {
            promise.fail(ex, promise);
        }
    },
    removeObjectAdapter: function(adapter)
    {
        if(this._instance === null)
        {
            return;
        }

        var n = this._adapters.indexOf(adapter);
        if(n !== -1)
        {
            this._adapters.splice(n, 1);
        }

        n = this._adapterNamesInUse.indexOf(adapter.getName());
        if(n !== -1)
        {
            this._adapterNamesInUse.splice(n, 1);
        }
    }
});

Ice.ObjectAdapterFactory = ObjectAdapterFactory;
module.exports.Ice = Ice;