Returns the persisted instance (this has extra properties + id set).
@param sf search favourite
@param onFinished callback when finished
357978
public static void saveSearchFavourite(SearchFavourite sf, final DataCallback<SearchFavourite> onFinished) {
		SaveSearchFavouriteRequest ssfr = new SaveSearchFavouriteRequest();
		ssfr.setSearchFavourite(sf);
		GwtCommand command = new GwtCommand(SaveSearchFavouriteRequest.COMMAND);
		command.setCommandRequest(ssfr);
		GwtCommandDispatcher.getInstance().execute(command, new AbstractCommandCallback<SaveSearchFavouriteResponse>() {
			public void execute(SaveSearchFavouriteResponse response) {
				if (onFinished != null) {
					onFinished.execute(response.getSearchFavourite());
				}
			}
		});
	}
769722
private Future<?> submit(FLVFinalizer flvFinalizer) {
        if (executor != null && !executor.isTerminated()) {
            return executor.submit(flvFinalizer);
        }
        return null;
    }
1118806
def wf_savedsearch_entity?(id)
      return true if %w[DASHBOARD ALERT MAINTENANCE_WINDOW
                        NOTIFICANT EVENT SOURCE EXTERNAL_LINK AGENT
                        CLOUD_INTEGRATION].include?(id)
      raise Wavefront::Exception::InvalidSavedSearchEntity
    end
602943
private Collection<SagaInstanceInfo> continueExistingSaga(final SagaType sagaType, final Object instanceKey) {
        Collection<SagaInstanceInfo> sagas = new ArrayList<>();

        Collection<? extends SagaState> sagaStates = stateStorage.load(sagaType.getSagaClass().getName(), instanceKey);
        for (SagaState sagaState : sagaStates) {
            try {
                Saga saga = instanceFactory.continueExisting(sagaType.getSagaClass(), sagaState);
                sagas.add(SagaInstanceInfo.define(saga, false));
            } catch (ExecutionException e) {
                Throwables.propagate(e);
            }
        }

        return sagas;
    }
1301736
function instantDeferred(resolve, returnValue, context) {
	var deferred = $.Deferred();
	if (resolve) {
		deferred.resolveWith(context, [returnValue]);
	} else {
		deferred.rejectWith(context, [returnValue]);
	}

	return deferred;
}
1116162
@Transactional
    public E findUniqueOrNone(E entity, SearchParameters sp) {
        // this code is an optimization to prevent using a count
        sp.setFirst(0);
        sp.setMaxResults(2);
        List<E> results = find(entity, sp);

        if (results == null || results.isEmpty()) {
            return null;
        } else if (results.size() > 1) {
            throw new NonUniqueResultException("Developper: You expected 1 result but we found more ! sample: " + entity);
        } else {
            return results.iterator().next();
        }
    }
523432
private static PersistenceContext newPersistenceContext(final String fJndiName, final String fUnitName, final int fCtxType,
                                                            final List<Property> fCtxProperties) {
        return new PersistenceContext() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return PersistenceContext.class;
            }

            @Override
            public String name() {
                return fJndiName;
            }

            @Override
            public PersistenceProperty[] properties() {
                //TODO not ideal doing this conversion processing here
                PersistenceProperty[] props = new PersistenceProperty[fCtxProperties.size()];
                int i = 0;
                for (Property property : fCtxProperties) {
                    final String name = property.getName();
                    final String value = property.getValue();
                    PersistenceProperty prop = new PersistenceProperty() {

                        @Override
                        public Class<? extends Annotation> annotationType() {
                            return PersistenceProperty.class;
                        }

                        @Override
                        public String name() {
                            return name;
                        }

                        @Override
                        public String value() {
                            return value;
                        }

                    };
                    props[i++] = prop;
                }
                return props;
            }

            @Override
            public PersistenceContextType type() {
                if (fCtxType == PersistenceContextRef.TYPE_TRANSACTION) {
                    return PersistenceContextType.TRANSACTION;
                } else {
                    return PersistenceContextType.EXTENDED;
                }
            }

            @Override
            public String unitName() {
                return fUnitName;
            }

            @Override
            public String toString() {
                return "JPA.PersistenceContext(name=" + fJndiName +
                       ", unitName=" + fUnitName + ")";
            }
        };
    }
722214
private SagaInstanceInfo continueExistingSaga(final SagaState sagaState) {
        SagaInstanceInfo instanceInfo;

        try {
            Saga saga = instanceFactory.continueExisting(sagaState.getType(), sagaState);
            instanceInfo = SagaInstanceInfo.define(saga, false);
        } catch (ExecutionException e) {
            Throwables.propagate(e);
            instanceInfo = null;
        }

        return instanceInfo;
    }
387952
@Override
  public V computeIfAbsent(final K key, final Callable<V> callable) {
    Entry<K,V> e;
    for (;;) {
      e = lookupOrNewEntry(key);
      if (e.hasFreshData(clock)) {
        return returnValue(e);
      }
      synchronized (e) {
        e.waitForProcessing();
        if (e.hasFreshData(clock)) {
          return returnValue(e);
        }
        if (e.isGone()) {
          metrics.goneSpin();
          continue;
        }
        e.startProcessing(Entry.ProcessingState.COMPUTE);
        break;
      }
    }
    metrics.peekMiss();
    boolean _finished = false;
    long t = 0, t0 = 0;
    V _value;
    try {
      if (!isUpdateTimeNeeded()) {
        _value = callable.call();
      } else {
        t0 = clock.millis();
        _value = callable.call();
        if (!metrics.isDisabled()) {
          t = clock.millis();
        }
      }
      synchronized (e) {
        insertOrUpdateAndCalculateExpiry(e, _value, t0, t, t0, INSERT_STAT_PUT);
        e.processingDone();
      }
      _finished = true;

    } catch (RuntimeException ex) {
      throw ex;
    } catch (Exception ex) {
      throw new CacheLoaderException(ex);
    } finally {
      e.ensureAbort(_finished);
    }
    return returnValue(e);
  }
1064156
function onFinished (msg, listener) {
  if (isFinished(msg) !== false) {
    defer(listener, null, msg)
    return msg
  }

  // attach the listener to the message
  attachListener(msg, listener)

  return msg
}
1109071
function callOnFinish (original) {
    return function (callback) {
      if (concurrent === 0) {
        original.call(this, callback);
      } else {
        pendingFinish = original.bind(this, callback);
      }
    }
  }
330143
public ContainerAS getCurrentSessionalUOW(boolean checkMarkedReset) throws CSIException, CSITransactionRolledbackException {
        ContainerAS result = null;
        Object currASKey = uowCtrl.getCurrentSessionalUOW(checkMarkedReset);

        //---------------------------------------------------------
        // No activity session available in after_completion callbacks.
        // Okay to return null in this case.
        //---------------------------------------------------------

        if (currASKey == null) {
            return null;
        }

        result = containerASMap.get(currASKey);
        if (result == null) {
            result = new ContainerAS(this, currASKey);
            uowCtrl.enlistWithSession(result);
            containerASMap.put(currASKey, result);
        }

        return result;
    }
545237
@SuppressWarnings("unchecked")
    public <T> T persist(T object) {
        pm.currentTransaction().begin();
        pm.makePersistent(object);
        pm.currentTransaction().commit();
        pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS);
        pm.refresh(object);
        return object;
    }
327990
private void instanceAlreadyLoaded(
		final Tuple resultset,
		final int i,
			//TODO create an interface for this usage
		final OgmEntityPersister persister,
		final org.hibernate.engine.spi.EntityKey key,
		final Object object,
		final LockMode lockMode,
		final SharedSessionContractImplementor session)
	throws HibernateException {
		if ( !persister.isInstance( object ) ) {
			throw new WrongClassException(
					"loaded object was of wrong class " + object.getClass(),
					key.getIdentifier(),
					persister.getEntityName()
				);
		}

		if ( LockMode.NONE != lockMode && upgradeLocks() ) { // no point doing this if NONE was requested

			final boolean isVersionCheckNeeded = persister.isVersioned() &&
					session.getPersistenceContext().getEntry( object )
							.getLockMode().lessThan( lockMode );
			// we don't need to worry about existing version being uninitialized
			// because this block isn't called by a re-entrant load (re-entrant
			// loads _always_ have lock mode NONE)
			if ( isVersionCheckNeeded ) {
				//we only check the version when _upgrading_ lock modes
				Object oldVersion = session.getPersistenceContext().getEntry( object ).getVersion();
				persister.checkVersionAndRaiseSOSE( key.getIdentifier(), oldVersion, session, resultset );
				//we need to upgrade the lock mode to the mode requested
				session.getPersistenceContext().getEntry( object )
						.setLockMode( lockMode );
			}
		}
	}
824497
function Persistence (appName, log, Utils, Constants, __private) {
    /**************************************************************
                        The Persistable Object
    **************************************************************/
    // A persistable object needs a key and value, but will also have a type and a timestamp which
    // is automatically computed. The object also provides a method to extract the original value
    // in its original type.

    const Type = {
        STRING: typeof '',
        NUMBER: typeof Number.MAX_VALUE,
        BOOLEAN: typeof true,
        ARRAY: 'array', // JavaScript does not provide this type by default, but we need it.
        OBJECT: typeof {},
        UNDEFINED: typeof undefined
    };

    // A utility method to extract the original value in its original type.
    const _fromPersistable = function (item) {
        let x;
        switch (item.type) {
            case Type.STRING:
            case Type.NUMBER:
            case Type.BOOLEAN:
            case Type.UNDEFINED:
                return item.value;
            case Type.ARRAY:
                x = [];
                item.value.forEach(function (e) {
                    let key = e.key.substring(e.key.lastIndexOf('[') + 1, e.key.lastIndexOf(']'));
                    x[parseInt(key, 10)] = _fromPersistable(e);
                });
                return x;
            case Type.OBJECT:
                x = {};
                item.value.forEach(function (e) {
                    let key = e.key.substring(e.key.lastIndexOf('[') + 1, e.key.lastIndexOf(']'));
                    x[key] = _fromPersistable(e);
                });
                return x;
            default:
                _logUnknownType(item.type);
        }
    };

    // The persistable object.
    function Persistable (key, value) {
        const __self = this;
        __self.key = key;

        // Set value and type of object.
        if (value === undefined || value === null) {
            __self.value = undefined;
            __self.type = typeof undefined;
        } else if (value instanceof Array) {
            __self.value = [];
            value.forEach(function (e, i) {
                __self.value.push(new Persistable(key + '[' + i + ']', e));
            });
            __self.type = Type.ARRAY;
        } else {
            switch (typeof value) {
                case Type.STRING:
                case Type.NUMBER:
                case Type.BOOLEAN:
                    __self.value = value;
                    __self.type = typeof value;
                    break;
                case Type.OBJECT:
                    __self.value = [];
                    Object.keys(value).forEach(function (e) {
                        __self.value.push(new Persistable(key + '[' + e + ']', value[e]));
                    });
                    __self.type = typeof value;
                    break;
                default:
                    _logUnknownType(typeof value);
            }
        }

        __self.timestamp = Date.now();

        __self.toOriginal = function () {
            return _fromPersistable(__self);
        };
    }

    /**************************************************************
                        Common Utility Methods
    **************************************************************/
    const _logUnknownType = function (type) {
        log.warn('Unknown type:', type);
    };

    const _handleRequestError = function (e) {
        /* istanbul ignore if */
        // It is impossible to test this scenario as there would be issues in the test runner
        // if URLs were invalid. This is easily testable using an integration test-case, since
        // PM2/node will eventually report the error after several seconds.
        if (e) {
            log.warn('Connection error when making request to persistence service:', e);
        }
    };

    // A utility method to covert keys to/from local from/to remote.
    const _convertKey = function (key) {
        let result = key.split('');
        if (key.indexOf('/') > -1) {
            while (result.indexOf('/') > -1) {
                result.splice(result.indexOf('/') + 1, 0, '[');
                result[result.indexOf('/')] = ']';
            }
            result.push(result.splice(result.indexOf(']'), 1));
        } else if (key.indexOf('[') > -1) {
            while (result.indexOf('[') > -1) {
                result[result.indexOf('[')] = '/';
                result.splice(result.indexOf(']'), 1);
            }
        }
        return result.join('');
    };

    /**************************************************************
                           Remote Operations
    **************************************************************/
    const updateRemoteItem = function (item) {
        /* istanbul ignore if */
        // The persistence service should always be set before calling this method.
        // This check is an additional precaution.
        if (!__private.provider) {
            log.error('Unable to perform remote operation. The persistence service has not been set');
            return;
        }

        if (item.value instanceof Array) {
            item.value.forEach(function (e) {
                updateRemoteItem(e);
            });
        } else if (item !== undefined && item.type !== undefined) {
            const url = __private.provider + '/' + appName + '/' + _convertKey(item.key);
            if (item.value === undefined) {
                request.delete(url, _handleRequestError);
            } else {
                request.post(url, {
                    headers: { 'Content-Type': Constants.HTTP_CONTENT_TYPE_JSON },
                    json: { value: item.value, type: item.type, timestamp: item.timestamp }
                }, _handleRequestError);
            }
        }
    };

    const deleteRemoteItem = function (item) {
        /* istanbul ignore if */
        // The persistence service should always be set before calling this method.
        // This check is an additional precaution.
        if (!__private.provider) {
            log.error('Unable to perform remote operation. The persistence service has not been set');
            return;
        }

        if (item.value instanceof Array) {
            item.value.forEach(function (e) {
                deleteRemoteItem(e);
            });
        } else {
            // A delete operation is equivalent to an update operation with an undefined key.
            updateRemoteItem(new Persistable(item.key, undefined));
        }
    };

    /**************************************************************
                        Compare and Set Method
    **************************************************************/

    // A utility method to sort values of a persistable object or array, which is used when
    // comparing current values with future values.
    const _sortValues = function (item) {
        item.value.sort(function (a, b) {
            if (a.key < b.key) {
                return -1;
            }
            /* istanbul ignore next */
            // Below line exists only for the sake of completeness. Generally the array will
            // always be sorted, and therefore it is impossible to get to this state in a test.
            return a.key > b.key ? 1 : 0;
        });
        return item;
    };

    const compareAndSet = function (future, current) {
        if (current === undefined) {
            updateRemoteItem(future);
            return future;
        } else if (current.type !== future.type || current.key !== future.key) {
            // If the key or type are different, the object is not comparable and therefore
            // must be deleted and recreated.
            deleteRemoteItem(current);
            updateRemoteItem(future);
            return future;
        }

        switch (current.type) {
            case Type.STRING:
            case Type.NUMBER:
            case Type.BOOLEAN:
            case Type.UNDEFINED:
                if (!Utils.JSON.equals(current.value, future.value)) {
                    updateRemoteItem(future);
                    return future;
                }
                return current;
            case Type.ARRAY:
            case Type.OBJECT:
                // The arrays are sorted to avoid objects being replaced during parsing.
                current.value = _sortValues(current).value;
                future.value = _sortValues(future).value;
                // We use two counters since the lists would never match in size.
                let i = 0;
                let j = 0;
                while (i < current.value.length) {
                    if (!future.value[j]) {
                        // No more items in the future list, so delete subsequent values in the
                        // current list.
                        current.value.splice(i, current.value.length - i).forEach(function (e) {
                            deleteRemoteItem(e);
                        });
                    } else if (current.value[i].key !== future.value[j].key) {
                        // The item has been deleted, and a new item has taken its place.
                        deleteRemoteItem(current.value.splice(i, 1)[0]);
                    } else {
                        // Compare and set the value of the existing item if it has changed.
                        current.value[i] = compareAndSet(future.value[j], current.value[i]);
                        i++;
                        j++;
                    }
                }
                // All items remaining in the future list do not yet exist in the current list,
                // so we add them.
                if (current.value.length < future.value.length) {
                    for (let i = current.value.length; i < future.value.length; i++) {
                        current.value.push(compareAndSet(future.value[i], current.value[i]));
                    }
                }
                return current;
            default:
                _logUnknownType(current.type);
        }
    };

    /**************************************************************
                CRUD Operations on Persistable Objects
    **************************************************************/
    const createParentIfNotExisting = function (key) {
        let parent = readPersistable(__private.local, key, true);
        if (!parent) {
            const parentKey = key.substring(0, key.lastIndexOf('['));
            createParentIfNotExisting(parentKey);
            // If a parent does not exist locally, but exists remotely, then, it should be
            // created locally. If a parent exists, it could be either an object or an array
            // and there is no easy way of figuring that out based on just the key. So, we
            // make an assumption that objects would not generally have keys that start with
            // numbers.
            const itemKey = key.substring(key.lastIndexOf('[') + 1, key.length - 1);
            const isArray = !isNaN(parseInt(itemKey, 10));
            createOrUpdatePersistable(parentKey, new Persistable(parentKey, isArray ? [] : {}));
        }
    };

    const createOrUpdatePersistable = function (key, value) {
        let parent = readPersistable(__private.local, key, true);
        if (!parent) {
            log.error('Invalid key:', key);
            return;
        }

        if (parent === __private.local) {
            __private.local[key] = __private.provider ? compareAndSet(value, __private.local[key]) : value;
            return;
        }

        let exists = false;
        parent.value.forEach(function (e, i) {
            if (e.key === key) {
                parent.value[i] = __private.provider ? compareAndSet(value, e) : value;
                exists = true;
            }
        });
        if (!exists) {
            parent.value.push(__private.provider ? compareAndSet(value) : value);
        }
    };

    const readPersistable = function (item, key, returnParent) {
        let firstPart;
        let nextPart;
        if (key.indexOf('[') === -1) {
            firstPart = key;
            nextPart = null;
        } else {
            firstPart = key.substring(0, key.indexOf('['));
            nextPart = key.substring(key.indexOf('[') + 1, key.indexOf(']')) + key.substring(key.indexOf(']') + 1);
        }
        let result;
        if (item === __private.local) {
            result = item[firstPart];
        } else if (item.type === 'array' || item.type === 'object') {
            item.value.forEach(function (e) {
                if (e.key.endsWith('[' + firstPart + ']')) {
                    result = e;
                }
            });
        }
        if (!result && nextPart) {
            return result;
        } if (!result || !nextPart) {
            return returnParent ? item : result;
        }
        return readPersistable(result, nextPart, returnParent);
    };

    const deletePersistable = function (key, deleteRemoteCopy) {
        let result;
        if (readPersistable(__private.local, key)) {
            let parent = readPersistable(__private.local, key, true);
            if (parent === __private.local) {
                result = __private.local[key];
                delete __private.local[key];
            } else {
                parent.value.forEach(function (e, i) {
                    if (e.key === key) {
                        result = parent.value.splice(i, 1)[0];
                    }
                });
            }
            if (deleteRemoteCopy) {
                deleteRemoteItem(result);
            }
        }
        return result;
    };

    /**************************************************************
                 Synchronisation of Local with Remote
    **************************************************************/
    // A utility method to get local items for the sync operation.
    const _getLocalItems = function (item) {
        let result = {};
        if (item === __private.local) {
            Object.keys(__private.local).forEach(function (e) {
                result = Object.assign(result, _getLocalItems(__private.local[e]));
            });
            return result;
        }
        switch (item.type) {
            case Type.STRING:
            case Type.NUMBER:
            case Type.BOOLEAN:
            case Type.UNDEFINED:
                result[item.key] = item.timestamp;
                break;
            case Type.ARRAY:
            case Type.OBJECT:
                item.value.forEach(function (e) {
                    result = Object.assign(result, _getLocalItems(e));
                });
                break;
            default:
                _logUnknownType(item.type);
        }
        return result;
    };

    const sync = function () {
        if (!__private.provider) {
            log.warn('Ignoring sync request. The persistence service has not been set');
            return;
        }

        request(__private.provider + '/' + appName, { json: true }, function (err, _res, remoteList) {
            if (err) {
                log.error('Unable to get of keys from persistence service:',
                    __private.provider, ', got:', err);
                return;
            }

            const localList = _getLocalItems(__private.local);

            // Delete all local items that do not exist remotely.
            Object.keys(localList).forEach(function (key) {
                const convertedKey = _convertKey(key);
                if (!remoteList[convertedKey]) {
                    deletePersistable(key);
                }
            });

            // Add any remote item if it does not exist locally or if it has a newer timestamp.
            Object.keys(remoteList).forEach(function (key) {
                const convertedKey = _convertKey(key);
                if (!localList[convertedKey] || remoteList[key] > localList[convertedKey]) {
                    const url = __private.provider + '/' + appName + '/' + key;
                    request(url, { json: true }, function (err, _res, result) {
                        if (err) {
                            log.error('Unable to read key:', convertedKey,
                                'from persistence service:', __private.provider, ', got:', err);
                            return;
                        }
                        createParentIfNotExisting(convertedKey);
                        createOrUpdatePersistable(convertedKey,
                            new Persistable(convertedKey, result.value));
                    });
                }
            });
        });
    };

    /**************************************************************
                     Functionality that is exposed
    **************************************************************/
    this.get = function (key) {
        const persistable = readPersistable(__private.local, key);
        return !persistable ? persistable : persistable.toOriginal();
    };

    this.set = function (key, value) {
        createOrUpdatePersistable(key, new Persistable(key, value));
    };

    this.del = function (key) {
        return deletePersistable(key, __private.provider);
    };

    this.sync = sync;
}
826381
protected RegisteredService getRegisteredServiceFromRequest(final RequestContext requestContext, final String entityId) {
        val currentService = WebUtils.getService(requestContext);
        val service = this.serviceFactory.createService(entityId);
        var registeredService = this.servicesManager.findServiceBy(service);
        if (registeredService == null) {
            LOGGER.debug("Entity id [{}] not found in the registry. Fallback onto [{}]", entityId, currentService);
            registeredService = this.servicesManager.findServiceBy(currentService);
        }
        LOGGER.debug("Located service definition [{}]", registeredService);
        return registeredService;
    }
1173203
private void onSetCaching(CfDef cfDef, Properties cfProperties, StringBuilder builder)
    {
        String caching = cfProperties.getProperty(CassandraConstants.CACHING);
        if (caching != null)
        {
            if (builder != null)
            {
                appendPropertyToBuilder(builder, caching, CassandraConstants.CACHING);
            }
            else
            {
                cfDef.setCaching(caching);
            }
        }
    }