Search in sources :

Example 41 with Supplier

use of java.util.function.Supplier in project fess by codelibs.

the class LdapManager method insert.

public void insert(final User user) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (!fessConfig.isLdapAdminEnabled(user.getName())) {
        return;
    }
    final Supplier<Hashtable<String, String>> adminEnv = () -> createAdminEnv();
    final String userDN = fessConfig.getLdapAdminUserSecurityPrincipal(user.getName());
    // attributes
    search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), null, adminEnv, result -> {
        if (!result.isEmpty()) {
            modifyUserAttributes(user, adminEnv, userDN, result, fessConfig);
        } else {
            final BasicAttributes entry = new BasicAttributes();
            addUserAttributes(entry, user, fessConfig);
            final Attribute oc = fessConfig.getLdapAdminUserObjectClassAttribute();
            entry.put(oc);
            insert(userDN, entry, adminEnv);
        }
    });
    // groups and roles
    search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), new String[] { fessConfig.getLdapMemberofAttribute() }, adminEnv, result -> {
        if (!result.isEmpty()) {
            final List<String> oldGroupList = new ArrayList<>();
            final List<String> oldRoleList = new ArrayList<>();
            final String lowerGroupDn = fessConfig.getLdapAdminGroupBaseDn().toLowerCase(Locale.ROOT);
            final String lowerRoleDn = fessConfig.getLdapAdminRoleBaseDn().toLowerCase(Locale.ROOT);
            processSearchRoles(result, (entryDn, name) -> {
                final String lowerEntryDn = entryDn.toLowerCase(Locale.ROOT);
                if (lowerEntryDn.indexOf(lowerGroupDn) != -1) {
                    oldGroupList.add(name);
                } else if (lowerEntryDn.indexOf(lowerRoleDn) != -1) {
                    oldRoleList.add(name);
                }
            });
            final List<String> newGroupList = stream(user.getGroupNames()).get(stream -> stream.collect(Collectors.toList()));
            stream(user.getGroupNames()).of(stream -> stream.forEach(name -> {
                if (oldGroupList.contains(name)) {
                    oldGroupList.remove(name);
                    newGroupList.remove(name);
                }
            }));
            oldGroupList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
                    if (!subResult.isEmpty()) {
                        final List<ModificationItem> modifyList = new ArrayList<>();
                        modifyDeleteEntry(modifyList, "member", userDN);
                        modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
                    }
                });
            });
            newGroupList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
                    if (!!subResult.isEmpty()) {
                        final Group group = new Group();
                        group.setName(name);
                        insert(group);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
                });
            });
            final List<String> newRoleList = stream(user.getRoleNames()).get(stream -> stream.collect(Collectors.toList()));
            stream(user.getRoleNames()).of(stream -> stream.forEach(name -> {
                if (oldRoleList.contains(name)) {
                    oldRoleList.remove(name);
                    newRoleList.remove(name);
                }
            }));
            oldRoleList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
                    if (!subResult.isEmpty()) {
                        final List<ModificationItem> modifyList = new ArrayList<>();
                        modifyDeleteEntry(modifyList, "member", userDN);
                        modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
                    }
                });
            });
            newRoleList.stream().forEach(name -> {
                search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
                    if (!!subResult.isEmpty()) {
                        final Role role = new Role();
                        role.setName(name);
                        insert(role);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
                });
            });
        } else {
            stream(user.getGroupNames()).of(stream -> stream.forEach(name -> {
                search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
                    if (!!subResult.isEmpty()) {
                        final Group group = new Group();
                        group.setName(name);
                        insert(group);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
                });
            }));
            stream(user.getRoleNames()).of(stream -> stream.forEach(name -> {
                search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
                    if (!!subResult.isEmpty()) {
                        final Role role = new Role();
                        role.setName(name);
                        insert(role);
                    }
                    final List<ModificationItem> modifyList = new ArrayList<>();
                    modifyAddEntry(modifyList, "member", userDN);
                    modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
                });
            }));
        }
    });
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) Constants(org.codelibs.fess.Constants) LoggerFactory(org.slf4j.LoggerFactory) NamingException(javax.naming.NamingException) User(org.codelibs.fess.es.user.exentity.User) Supplier(java.util.function.Supplier) SearchControls(javax.naming.directory.SearchControls) ArrayList(java.util.ArrayList) InitialDirContext(javax.naming.directory.InitialDirContext) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Role(org.codelibs.fess.es.user.exentity.Role) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Locale(java.util.Locale) BiConsumer(java.util.function.BiConsumer) FessUser(org.codelibs.fess.entity.FessUser) Context(javax.naming.Context) Hashtable(java.util.Hashtable) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Logger(org.slf4j.Logger) OptionalUtil(org.codelibs.fess.util.OptionalUtil) OptionalEntity(org.dbflute.optional.OptionalEntity) LdapOperationException(org.codelibs.fess.exception.LdapOperationException) DirContext(javax.naming.directory.DirContext) StringUtil(org.codelibs.core.lang.StringUtil) BasicAttributes(javax.naming.directory.BasicAttributes) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Base64(java.util.Base64) List(java.util.List) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DfTypeUtil(org.dbflute.util.DfTypeUtil) Attributes(javax.naming.directory.Attributes) SystemHelper(org.codelibs.fess.helper.SystemHelper) Collections(java.util.Collections) SearchResult(javax.naming.directory.SearchResult) Group(org.codelibs.fess.es.user.exentity.Group) BasicAttributes(javax.naming.directory.BasicAttributes) Group(org.codelibs.fess.es.user.exentity.Group) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Role(org.codelibs.fess.es.user.exentity.Role) ModificationItem(javax.naming.directory.ModificationItem) ArrayList(java.util.ArrayList) List(java.util.List)

Example 42 with Supplier

use of java.util.function.Supplier in project fess by codelibs.

the class LdapManager method delete.

public void delete(final User user) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (!fessConfig.isLdapAdminEnabled(user.getName())) {
        return;
    }
    final Supplier<Hashtable<String, String>> adminEnv = () -> createAdminEnv();
    final String userDN = fessConfig.getLdapAdminUserSecurityPrincipal(user.getName());
    stream(user.getGroupNames()).of(stream -> stream.forEach(name -> {
        search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
            if (!!subResult.isEmpty()) {
                final Group group = new Group();
                group.setName(name);
                insert(group);
            }
            final List<ModificationItem> modifyList = new ArrayList<>();
            modifyDeleteEntry(modifyList, "member", userDN);
            modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
        });
    }));
    stream(user.getRoleNames()).of(stream -> stream.forEach(name -> {
        search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
            if (!!subResult.isEmpty()) {
                final Role role = new Role();
                role.setName(name);
                insert(role);
            }
            final List<ModificationItem> modifyList = new ArrayList<>();
            modifyDeleteEntry(modifyList, "member", userDN);
            modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
        });
    }));
    search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), null, adminEnv, result -> {
        if (!result.isEmpty()) {
            delete(userDN, adminEnv);
        } else {
            logger.info("{} does not exist in LDAP server.", user.getName());
        }
    });
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) Constants(org.codelibs.fess.Constants) LoggerFactory(org.slf4j.LoggerFactory) NamingException(javax.naming.NamingException) User(org.codelibs.fess.es.user.exentity.User) Supplier(java.util.function.Supplier) SearchControls(javax.naming.directory.SearchControls) ArrayList(java.util.ArrayList) InitialDirContext(javax.naming.directory.InitialDirContext) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Role(org.codelibs.fess.es.user.exentity.Role) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Locale(java.util.Locale) BiConsumer(java.util.function.BiConsumer) FessUser(org.codelibs.fess.entity.FessUser) Context(javax.naming.Context) Hashtable(java.util.Hashtable) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Logger(org.slf4j.Logger) OptionalUtil(org.codelibs.fess.util.OptionalUtil) OptionalEntity(org.dbflute.optional.OptionalEntity) LdapOperationException(org.codelibs.fess.exception.LdapOperationException) DirContext(javax.naming.directory.DirContext) StringUtil(org.codelibs.core.lang.StringUtil) BasicAttributes(javax.naming.directory.BasicAttributes) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Base64(java.util.Base64) List(java.util.List) ComponentUtil(org.codelibs.fess.util.ComponentUtil) DfTypeUtil(org.dbflute.util.DfTypeUtil) Attributes(javax.naming.directory.Attributes) SystemHelper(org.codelibs.fess.helper.SystemHelper) Collections(java.util.Collections) SearchResult(javax.naming.directory.SearchResult) Group(org.codelibs.fess.es.user.exentity.Group) Role(org.codelibs.fess.es.user.exentity.Role) Group(org.codelibs.fess.es.user.exentity.Group) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) List(java.util.List) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 43 with Supplier

use of java.util.function.Supplier in project Gargoyle by callakrsos.

the class CrudBaseColumnMapper method commboBox.

private CommboBoxTableColumn<T, Object> commboBox(Class<?> classType, String columnName, IOptions naming) {
    CommboInfo<?> comboBox = naming.comboBox(columnName);
    ObservableList codeList = (ObservableList) comboBox.getCodeList();
    String code = comboBox.getCode();
    String codeNm = comboBox.getCodeNm();
    Supplier<ChoiceBoxTableCell<T, Object>> supplier = () -> {
        ChoiceBoxTableCell<T, Object> choiceBoxTableCell = new ChoiceBoxTableCell<T, Object>(codeList) {

            @Override
            public void startEdit() {
                Object vo = tableViewProperty().get().getItems().get(super.getIndex());
                if (vo instanceof AbstractDVO) {
                    AbstractDVO _abstractvo = (AbstractDVO) vo;
                    if (Objects.equals(CommonConst._STATUS_CREATE, _abstractvo.get_status())) {
                        boolean editable = naming.editable(columnName);
                        if (!editable)
                            return;
                        super.startEdit();
                    } else if (Objects.equals(CommonConst._STATUS_UPDATE, _abstractvo.get_status())) {
                        boolean editable = naming.editable(columnName);
                        if (!editable)
                            return;
                        NonEditable annotationClass = getAnnotationClass(_abstractvo.getClass(), NonEditable.class, columnName);
                        if (annotationClass != null) {
                            LOGGER.debug("non start Edit");
                        } else {
                            super.startEdit();
                            LOGGER.debug("start Edit");
                        }
                    }
                }
            }
        };
        EventDispatcher originalDispatcher = choiceBoxTableCell.getEventDispatcher();
        choiceBoxTableCell.setEventDispatcher((event, tail) -> {
            return choiceBoxTableCellCellEventDispatcher(choiceBoxTableCell, originalDispatcher, event, tail);
        });
        // 아직까지는 codeList가 쓰일일이 없어서 주석처리함.. 과연 필요한 케이스가 생길지...?
        choiceBoxTableCell.setConverter(new CommboBoxStringConverter<Object>(/* codeList, */
        code, codeNm));
        return choiceBoxTableCell;
    };
    CommboBoxTableColumn<T, Object> combobox = new CommboBoxTableColumn<T, Object>(supplier, columnName, codeList, code, codeNm);
    return combobox;
}
Also used : Logger(org.slf4j.Logger) PropertyValueFactory(javafx.scene.control.cell.PropertyValueFactory) TableRow(javafx.scene.control.TableRow) LoggerFactory(org.slf4j.LoggerFactory) Property(javafx.beans.property.Property) Event(javafx.event.Event) CheckBox(javafx.scene.control.CheckBox) TextFieldTableCell(javafx.scene.control.cell.TextFieldTableCell) StringConverter(javafx.util.StringConverter) Field(java.lang.reflect.Field) Supplier(java.util.function.Supplier) EventDispatcher(javafx.event.EventDispatcher) TableColumn(javafx.scene.control.TableColumn) Objects(java.util.Objects) EventDispatchChain(javafx.event.EventDispatchChain) BooleanProperty(javafx.beans.property.BooleanProperty) ActionEvent(javafx.event.ActionEvent) CheckBoxTableCell(javafx.scene.control.cell.CheckBoxTableCell) Annotation(java.lang.annotation.Annotation) ChoiceBoxTableCell(javafx.scene.control.cell.ChoiceBoxTableCell) ObservableList(javafx.collections.ObservableList) TableView(javafx.scene.control.TableView) ChoiceBoxTableCell(javafx.scene.control.cell.ChoiceBoxTableCell) EventDispatcher(javafx.event.EventDispatcher) ObservableList(javafx.collections.ObservableList)

Example 44 with Supplier

use of java.util.function.Supplier in project ignite by apache.

the class PartitionDataStorage method computeDataIfAbsent.

/**
 * Retrieves partition {@code data} correspondent to specified partition index if it exists in local storage or
 * loads it using the specified {@code supplier}. Unlike {@link ConcurrentMap#computeIfAbsent(Object, Function)},
 * this method guarantees that function will be called only once.
 *
 * @param <D> Type of data.
 * @param part Partition index.
 * @param supplier Partition {@code data} supplier.
 * @return Partition {@code data}.
 */
@SuppressWarnings("unchecked")
<D> D computeDataIfAbsent(int part, Supplier<D> supplier) {
    Object data = storage.get(part);
    if (data == null) {
        Lock lock = locks.computeIfAbsent(part, p -> new ReentrantLock());
        lock.lock();
        try {
            data = storage.computeIfAbsent(part, p -> supplier.get());
        } finally {
            lock.unlock();
        }
    }
    return (D) data;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 45 with Supplier

use of java.util.function.Supplier in project pravega by pravega.

the class OrderedItemProcessorTests method testInstantCompletion.

/**
 * Tests a scenario where all item processors finish immediately (they return a completed future).
 */
@Test
public void testInstantCompletion() {
    final int itemCount = 10000;
    Supplier<Integer> nextIndex = new AtomicInteger()::incrementAndGet;
    Function<Integer, CompletableFuture<Integer>> itemProcessor = i -> CompletableFuture.completedFuture(nextIndex.get());
    @Cleanup val p = new TestProcessor(CAPACITY, itemProcessor, executorService());
    val resultFutures = new ArrayList<CompletableFuture<Integer>>(itemCount);
    for (int i = 0; i < itemCount; i++) {
        resultFutures.add(p.process(i));
    }
    // Verify they have been executed in order.
    val results = Futures.allOfWithResults(resultFutures).join();
    for (int i = 0; i < results.size(); i++) {
        Assert.assertEquals("Unexpected result at index " + i, i + 1, (int) results.get(i));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectClosedException(io.pravega.common.ObjectClosedException) Setter(lombok.Setter) Getter(lombok.Getter) AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Timeout(org.junit.rules.Timeout) Executor(java.util.concurrent.Executor) Semaphore(java.util.concurrent.Semaphore) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Assert(org.junit.Assert) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) lombok.val(lombok.val) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

Supplier (java.util.function.Supplier)104 Test (org.junit.Test)43 List (java.util.List)41 ArrayList (java.util.ArrayList)28 Map (java.util.Map)24 Collectors (java.util.stream.Collectors)23 HashMap (java.util.HashMap)21 Function (java.util.function.Function)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 IOException (java.io.IOException)17 Arrays (java.util.Arrays)16 TimeUnit (java.util.concurrent.TimeUnit)14 Consumer (java.util.function.Consumer)14 Assert (org.junit.Assert)14 Collections (java.util.Collections)13 CompletableFuture (java.util.concurrent.CompletableFuture)13 Rule (org.junit.Rule)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Cleanup (lombok.Cleanup)12 Timeout (org.junit.rules.Timeout)11