All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.milton.ldap.SearchManager Maven / Gradle / Ivy

Go to download

Milton Community Edition: Supports DAV level 1 and is available on Apache2 license

There is a newer version: 4.0.3.2215
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.milton.ldap;

import com.sun.jndi.ldap.BerDecoder;
import io.milton.common.LogUtils;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author brad
 */
public class SearchManager {

    private static final Logger log = LoggerFactory.getLogger(SearchManager.class);
    /**
     * Search threads map
     */
    private final HashMap> mapOfSearchesByConnection = new HashMap<>();
    private final HashMap mapOfUuids = new HashMap<>();
    private final LdapTransactionManager txManager;

    public SearchManager(LdapTransactionManager txManager) {
        this.txManager = txManager;
    }        

    private Map getThreadMap(LdapConnection connection) {
        synchronized (mapOfSearchesByConnection) {
            Map map = mapOfSearchesByConnection.computeIfAbsent(connection, k -> new HashMap<>());
            return map;
        }
    }

    public void cancelAllSearches(LdapConnection aThis) {
        Map searchThreadMap = getThreadMap(aThis);
        synchronized (searchThreadMap) {
            for (SearchRunnable searchRunnable : searchThreadMap.values()) {
                searchRunnable.abandon();
            }
        }
    }

    public void beginAsyncSearch(LdapConnection aThis, int currentMessageId, final SearchRunnable searchRunnable) {
        searchRunnable.setUuid(UUID.randomUUID());
        Map searchThreadMap = getThreadMap(aThis);
        synchronized (searchThreadMap) {
            searchThreadMap.put(currentMessageId, searchRunnable);
            mapOfUuids.put(searchRunnable.getUuid(), aThis);
        }
        Runnable runnableRunner = () -> {
            try {
                txManager.tx(searchRunnable);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        };
        Thread searchThread = new Thread(runnableRunner);
        searchThread.setName(aThis.getName() + "-Search-" + currentMessageId);
        searchThread.start();
    }

    public void search(LdapConnection aThis, SearchRunnable searchRunnable) {
        searchRunnable.run();
    }

    public void abandonSearch(LdapConnection aThis, int currentMessageId, BerDecoder reqBer) {
        int abandonMessageId = 0;
        try {
            abandonMessageId = (Integer) Ldap.PARSE_INT_WITH_TAG_METHOD.invoke(reqBer, Ldap.LDAP_REQ_ABANDON);
            Map searchThreadMap = getThreadMap(aThis);
            synchronized (searchThreadMap) {
                SearchRunnable searchRunnable = searchThreadMap.get(abandonMessageId);
                if (searchRunnable != null) {
                    searchRunnable.abandon();
                    searchThreadMap.remove(currentMessageId);
                }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            log.error("", e);
        }
        LogUtils.debug(log, "LOG_LDAP_REQ_ABANDON_SEARCH", currentMessageId, abandonMessageId);
    }

    public void searchComplete(UUID id, Integer currentMessageId) {
        LdapConnection con = mapOfUuids.get(id);
        if (con != null) {
            Map searchThreadMap = getThreadMap(con);
            synchronized (searchThreadMap) {
                searchThreadMap.remove(currentMessageId);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy