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

org.apache.sshd.server.auth.AsyncAuthException Maven / Gradle / Ivy

There is a newer version: 2.14.0
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 org.apache.sshd.server.auth;

import java.lang.reflect.Array;
import java.util.function.Consumer;

import org.apache.sshd.common.RuntimeSshException;

/**
 * @author Apache MINA SSHD Project
 */
public class AsyncAuthException extends RuntimeSshException {

    private static final long serialVersionUID = 6741236101797649869L;

    protected Object listener;
    protected Boolean authed;

    public AsyncAuthException() {
        super();
    }

    public void setAuthed(boolean authed) {
        Object listener;
        synchronized (this) {
            if (this.authed != null) {
                return;
            }
            this.authed = authed;
            listener = this.listener;
        }

        if (listener != null) {
            if (listener instanceof Consumer) {
                Consumer lst = asListener(listener);
                lst.accept(authed);
            } else {
                int l = Array.getLength(listener);
                for (int i = 0; i < l; i++) {
                    Object lstInstance = Array.get(listener, i);
                    Consumer lst = asListener(lstInstance);
                    if (lst != null) {
                        lst.accept(authed);
                    }
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    protected Consumer asListener(Object listener) {
        return (Consumer) listener;
    }

    public void addListener(Consumer listener) {
        Boolean result;
        synchronized (this) {
            if (this.listener == null) {
                this.listener = listener;
            } else if (this.listener instanceof Consumer) {
                this.listener = new Object[] { this.listener, listener };
            } else {
                Object[] ol = (Object[]) this.listener;
                int l = ol.length;
                Object[] nl = new Object[l + 1];
                System.arraycopy(ol, 0, nl, 0, l);
                nl[l] = listener;
                this.listener = nl;
            }
            result = this.authed;
        }
        if (result != null) {
            listener.accept(result);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy