All files / src/Ice ObjectAdapter.js

82.11% Statements 372/453
85.33% Branches 64/75
74.28% Functions 26/35
82.11% Lines 372/453

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 184 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 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 259 260 261 262 263 264 265 266 267 268 269 270 271 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 45441x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 26x 26x 12x 12x 12x 12x 12x 12x 12x 26x 26x 26x 30x 30x 30x 21x 21x 21x 21x 21x 9x 9x 9x 9x 9x 9x 9x 9x 30x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 8x                   6x 6x 6x 6x 6x 6x 6x 6x 6x 8x       8x   6x 6x 6x 6x 6x 6x 30x     6x 8x 4x 4x 4x 4x 4x 4x 4x           4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 2x 2x 6x 6x 6x 4x 4x 6x 6x 2x 2x 6x 6x 8x       30x 41x 41x 505x 505x 41x 41x 847x 847x 41x 41x 26x 26x 26x 26x 3x 3x 3x 3x 3x 3x 26x 26x 26x 26x 26x 26x 26x 41x 41x 29x     29x 29x 29x 41x 41x 23x 23x 41x 41x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 41x 41x 5x 5x 41x 41x 6x 6x 41x 41x 2x 2x 2x 2x 2x 41x 41x 2x 2x 41x 41x 4x 4x 4x 4x 4x 41x 41x 3x 3x 3x 3x 3x 41x 41x 1x 1x 1x 1x 41x 41x     41x 41x         41x 41x         41x 41x         41x 41x 4x 4x 4x 41x 41x 941x 16x 16x 15x 15x 15x 16x 16x 941x 941x 41x 41x 6x 6x 6x 41x 41x 2x 2x 2x 41x 41x       41x 41x 9x 9x 9x 9x 41x 41x     41x 41x     41x 41x 7x 7x 41x 41x 2x 2x 1x 1x 1x 2x 41x 41x     41x 41x     41x 41x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 41x 41x 102x 1x 1x 102x 41x 41x 66x 1x 1x 65x 66x     66x 41x 41x 40x 1x 1x 40x 41x 41x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x                                             1x 1x 1x 1x 1x     1x 1x 2x 4x 4x 1x 1x 1x 1x 4x 4x 41x  
// Copyright (c) ZeroC, Inc.
 
import { ArrayUtil } from "./ArrayUtil.js";
import { Ice as Ice_Identity } from "./Identity.js";
const { Identity } = Ice_Identity;
import { generateUUID } from "./UUID.js";
import {
    AlreadyRegisteredException,
    InitializationException,
    ObjectAdapterDestroyedException,
    ParseException,
} from "./LocalExceptions.js";
import { Ice as Ice_Router } from "./Router.js";
const { RouterPrx } = Ice_Router;
import { Promise } from "./Promise.js";
import { PropertyNames } from "./PropertyNames.js";
import { ServantManager } from "./ServantManager.js";
import { StringUtil } from "./StringUtil.js";
import { identityToString } from "./IdentityToString.js";
import { ObjectPrx } from "./ObjectPrx.js";
import { Logger } from "./Logger.js";
import { LoggerMiddleware } from "./LoggerMiddleware.js";
import { Properties } from "./Properties.js";
 
//
// Only for use by IceInternal.ObjectAdapterFactory
//
export class ObjectAdapter {
    constructor(instance, communicator, objectAdapterFactory, name, router, noConfig, promise) {
        this._instance = instance;
        this._communicator = communicator;
        this._objectAdapterFactory = objectAdapterFactory;
        this._servantManager = new ServantManager(instance, name);
        this._name = name;
        this._publishedEndpoints = [];
        this._routerInfo = null;
        this._isDestroyed = false;
        this._noConfig = noConfig;
 
        this._dispatchPipeline = null;
        this._middlewareStack = [];
 
        // Install default middleware depending on the communicator's configuration.
        const logger = instance.initializationData().logger;
        if (logger instanceof Logger) {
            const warningLevel = instance.initializationData().properties.getIcePropertyAsInt("Ice.Warn.Dispatch");
            if (instance.traceLevels().dispatch > 0 || warningLevel > 0) {
                this.use(
                    next =>
                        new LoggerMiddleware(
                            next,
                            logger,
                            instance.traceLevels().dispatch,
                            instance.traceLevels().dispatchCat,
                            warningLevel,
                            instance.toStringMode(),
                        ),
                );
            }
        }
 
        if (this._noConfig) {
            this._reference = this._instance.referenceFactory().createFromString("dummy -t", "");
            this._messageSizeMax = this._instance.messageSizeMax();
            promise.resolve(this);
            return;
        }
 
        const properties = this._instance.initializationData().properties;
 
        Properties.validatePropertiesWithPrefix(this._name, properties, PropertyNames.ObjectAdapterProps);
 
        //
        // Make sure named adapter has some configuration.
        //
        if (router === null && properties.getPropertiesForPrefix(`${this._name}.`).size === 0) {
            throw new InitializationException(`Object adapter '${this._name}' requires configuration.`);
        }
 
        //
        // Setup a reference to be used to get the default proxy options
        // when creating new proxies. By default, create twoway proxies.
        //
        const proxyOptions = properties.getPropertyWithDefault(this._name + ".ProxyOptions", "-t");
        try {
            this._reference = this._instance.referenceFactory().createFromString("dummy " + proxyOptions, "");
        } catch (ex) {
            if (ex instanceof ParseException) {
                throw new InitializationException(
                    `invalid proxy options '${proxyOptions}' for object adapter '${name}'`,
                    { cause: ex },
                );
            } else {
                throw ex;
            }
        }
 
        // The maximum size of an Ice protocol message in bytes. This is limited to 0x7fffffff, which corresponds to
        // the maximum value of a 32-bit signed integer.
        const messageSizeMaxUpperLimit = 0x7fffffff;
        const defaultMessageSizeMax = this._instance.messageSizeMax() / 1024;
        const messageSizeMax = properties.getPropertyAsIntWithDefault(
            `${this._name}.MessageSizeMax`,
            defaultMessageSizeMax,
        );
        if (messageSizeMax > messageSizeMaxUpperLimit / 1024) {
            throw new InitializationException(
                `${this._name}.MessageSizeMax '${messageSizeMax}' is too large, it must be less than or equal to '${messageSizeMaxUpperLimit / 1024}' KiB`,
            );
        } else if (messageSizeMax < 1) {
            this._messageSizeMax = messageSizeMaxUpperLimit;
        } else {
            // The property is specified in kibibytes (KiB); _messageSizeMax is stored in bytes.
            this._messageSizeMax = messageSizeMax * 1024;
        }
 
        try {
            if (router === null && properties.getProperty(this._name + ".Router").length > 0) {
                router = new RouterPrx(communicator.propertyToProxy(this._name + ".Router"));
            }
            let p;
            if (router !== null) {
                this._routerInfo = this._instance.routerManager().find(router);
                console.assert(this._routerInfo !== null);
 
                //
                // Make sure this router is not already registered with another adapter.
                //
                if (this._routerInfo.getAdapter() !== null) {
                    throw new AlreadyRegisteredException(
                        "object adapter with router",
                        identityToString(router.ice_getIdentity(), this._instance.toStringMode()),
                    );
                }
 
                //
                // Associate this object adapter with the router. This way,
                // new outgoing connections to the router's client proxy will
                // use this object adapter for callbacks.
                //
                this._routerInfo.setAdapter(this);
 
                //
                // Also modify all existing outgoing connections to the
                // router's client proxy to use this object adapter for
                // callbacks.
                //
                p = this._instance.outgoingConnectionFactory().setRouterInfo(this._routerInfo);
            } else {
                p = Promise.resolve();
            }
 
            p.then(() => this.computePublishedEndpoints()).then(
                endpoints => {
                    this._publishedEndpoints = endpoints;
                    promise.resolve(this);
                },
                ex => {
                    this.destroy();
                    promise.reject(ex);
                },
            );
        } catch (ex) {
            this.destroy();
            throw ex;
        }
    }
 
    getName() {
        return this._noConfig ? "" : this._name;
    }
 
    getCommunicator() {
        return this._communicator;
    }
 
    destroy() {
        if (!this._isDestroyed) {
            this._isDestroyed = true;
 
            if (this._routerInfo !== null) {
                // Remove entry from the router manager.
                this._instance.routerManager().erase(this._routerInfo.getRouter());
 
                // Clear this object adapter with the router.
                this._routerInfo.setAdapter(null);
            }
            this._instance.outgoingConnectionFactory().removeAdapter(this);
 
            this._servantManager.destroy();
            this._objectAdapterFactory.removeObjectAdapter(this);
            this._publishedEndpoints = [];
        }
    }
 
    use(middleware) {
        if (this._dispatchPipeline !== null) {
            throw new Error("All middleware must be installed before the first dispatch.");
        }
        this._middlewareStack.push(middleware);
        return this;
    }
 
    add(object, ident) {
        return this.addFacet(object, ident, "");
    }
 
    addFacet(object, ident, facet) {
        this.checkForDestruction();
        ObjectAdapter.checkIdentity(ident);
        ObjectAdapter.checkServant(object);
 
        // Create a copy of the Identity argument, in case the caller reuses it.
        const id = ident.clone();
 
        this._servantManager.addServant(object, id, facet);
 
        return this.newProxy(id, facet);
    }
 
    addWithUUID(object) {
        return this.addFacetWithUUID(object, "");
    }
 
    addFacetWithUUID(object, facet) {
        return this.addFacet(object, new Identity(generateUUID(), ""), facet);
    }
 
    addDefaultServant(servant, category) {
        ObjectAdapter.checkServant(servant);
        this.checkForDestruction();
 
        this._servantManager.addDefaultServant(servant, category);
    }
 
    remove(ident) {
        return this.removeFacet(ident, "");
    }
 
    removeFacet(ident, facet) {
        this.checkForDestruction();
        ObjectAdapter.checkIdentity(ident);
 
        return this._servantManager.removeServant(ident, facet);
    }
 
    removeAllFacets(ident) {
        this.checkForDestruction();
        ObjectAdapter.checkIdentity(ident);
 
        return this._servantManager.removeAllFacets(ident);
    }
 
    removeDefaultServant(category) {
        this.checkForDestruction();
 
        return this._servantManager.removeDefaultServant(category);
    }
 
    find(ident) {
        return this.findFacet(ident, "");
    }
 
    findFacet(ident, facet) {
        this.checkForDestruction();
        ObjectAdapter.checkIdentity(ident);
        return this._servantManager.findServant(ident, facet);
    }
 
    findAllFacets(ident) {
        this.checkForDestruction();
        ObjectAdapter.checkIdentity(ident);
        return this._servantManager.findAllFacets(ident);
    }
 
    findByProxy(proxy) {
        this.checkForDestruction();
        const ref = proxy._getReference();
        return this.findFacet(ref.getIdentity(), ref.getFacet());
    }
 
    findDefaultServant(category) {
        this.checkForDestruction();
        return this._servantManager.findDefaultServant(category);
    }
 
    get dispatchPipeline() {
        if (this._dispatchPipeline === null) {
            let dispatchPipeline = this._servantManager; // the "final" dispatcher
            while (this._middlewareStack.length > 0) {
                const middleware = this._middlewareStack.pop();
                dispatchPipeline = middleware(dispatchPipeline);
            }
            this._dispatchPipeline = dispatchPipeline;
        }
        return this._dispatchPipeline;
    }
 
    addServantLocator(locator, prefix) {
        this.checkForDestruction();
        this._servantManager.addServantLocator(locator, prefix);
    }
 
    removeServantLocator(prefix) {
        this.checkForDestruction();
        return this._servantManager.removeServantLocator(prefix);
    }
 
    findServantLocator(prefix) {
        this.checkForDestruction();
        return this._servantManager.findServantLocator(prefix);
    }
 
    createProxy(ident) {
        this.checkForDestruction();
        ObjectAdapter.checkIdentity(ident);
        return this.newProxy(ident, "");
    }
 
    createDirectProxy(ident) {
        return this.createProxy(ident);
    }
 
    getEndpoints() {
        return [];
    }
 
    getPublishedEndpoints() {
        return ArrayUtil.clone(this._publishedEndpoints);
    }
 
    setPublishedEndpoints(newEndpoints) {
        this.checkForDestruction();
        if (this._routerInfo !== null) {
            throw new Error("can't set published endpoints on object adapter associated with a router");
        }
        this._publishedEndpoints = ArrayUtil.clone(newEndpoints);
    }
 
    getServantManager() {
        return this._servantManager;
    }
 
    messageSizeMax() {
        return this._messageSizeMax;
    }
 
    newProxy(ident, facet) {
        //
        // Now we also add the endpoints of the router's server proxy, if
        // any. This way, object references created by this object adapter
        // will also point to the router's server proxy endpoints.
        //
        //
        // Create a reference and return a proxy for this reference.
        //
        const reference = this._instance
            .referenceFactory()
            .create(ident, facet, this._reference, this._publishedEndpoints);
        return new ObjectPrx(reference);
    }
 
    checkForDestruction() {
        if (this._isDestroyed) {
            throw new ObjectAdapterDestroyedException(this.getName());
        }
    }
 
    static checkIdentity(ident) {
        if (ident.name === undefined || ident.name === null || ident.name.length === 0) {
            throw new TypeError("The name of an Ice object identity cannot be empty.");
        }
 
        if (ident.category === undefined || ident.category === null) {
            ident.category = "";
        }
    }
 
    static checkServant(servant) {
        if (servant === undefined || servant === null) {
            throw new TypeError("cannot add null servant to Object Adapter");
        }
    }
 
    async computePublishedEndpoints() {
        let endpoints = [];
        if (this._routerInfo !== null) {
            endpoints = await this._routerInfo.getServerEndpoints();
        } else {
            // Parse published endpoints. If set, these are used in proxies instead of the connection factory
            // endpoints.
            const s = this._instance.initializationData().properties.getProperty(this._name + ".PublishedEndpoints");
            const delim = " \t\n\r";
 
            let end = 0;
            let beg;
            while (end < s.length) {
                beg = StringUtil.findFirstNotOf(s, delim, end);
                if (beg === -1) {
                    if (s != "") {
                        throw new ParseException("invalid empty object adapter endpoint");
                    }
                    break;
                }
 
                end = beg;
                while (true) {
                    end = s.indexOf(":", end);
                    if (end == -1) {
                        end = s.length;
                        break;
                    } else {
                        let quoted = false;
                        let quote = beg;
                        while (true) {
                            quote = s.indexOf('"', quote);
                            if (quote == -1 || end < quote) {
                                break;
                            } else {
                                quote = s.indexOf('"', ++quote);
                                if (quote == -1) {
                                    break;
                                } else if (end < quote) {
                                    quoted = true;
                                    break;
                                }
                                ++quote;
                            }
                        }
                        if (!quoted) {
                            break;
                        }
                        ++end;
                    }
                }
 
                const endpointString = s.substring(beg, end);
                const endpoint = this._instance.endpointFactoryManager().create(endpointString, false);
                if (endpoint === null) {
                    throw new ParseException(`invalid object adapter endpoint '${s}'`);
                }
                endpoints.push(endpoint);
            }
        }
 
        if (this._instance.traceLevels().network >= 1 && endpoints.length > 0) {
            let msg = `published endpoints for object adapter '${this._name}':\n`;
            msg += endpoints.map(endpoint => endpoint.toString()).join(":");
            this._instance.initializationData().logger.trace(this._instance.traceLevels().networkCat, msg);
        }
        return endpoints;
    }
}