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

org.jboss.remoting3.ClientServiceHandle Maven / Gradle / Ivy

There is a newer version: 5.0.8.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2015, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.remoting3;

import static org.xnio.IoUtils.safeClose;

import java.io.IOException;
import java.util.function.Function;

import org.xnio.FinishedIoFuture;
import org.xnio.FutureResult;
import org.xnio.IoFuture;
import org.xnio.OptionMap;

/**
 * A handle for helping service protocol providers to create and maintain a single channel per connection.
 *
 * @author David M. Lloyd
 */
public final class ClientServiceHandle {
    private final Attachments.Key> key;
    private final String serviceName;
    private final Function> constructor;

    /**
     * Construct a new instance.  Only one instance should be constructed per service; instances may be safely
     * cached in {@code static} fields.
     *
     * @param serviceName the service name (may not be {@code null})
     * @param constructor the service future construction operation
     */
    @SuppressWarnings("unchecked")
    public ClientServiceHandle(final String serviceName, final Function> constructor) {
        key = (Attachments.Key>) (Object) new Attachments.Key<>(IoFuture.class);
        this.serviceName = serviceName;
        this.constructor = constructor;
    }

    /**
     * Get or establish the future client service for the given connection.
     *
     * @param connection the connection
     * @param optionMap the service options
     * @return the future service instance
     */
    public IoFuture getClientService(final Connection connection, OptionMap optionMap) {
        final Attachments attachments = connection.getAttachments();

        // check for prior
        IoFuture existing = attachments.getAttachment(key);
        if (existing != null) {
            return existing;
        }

        // create new future result, try to attach it
        final FutureResult futureResult = new FutureResult<>(connection.getEndpoint().getXnioWorker());
        final IoFuture future = futureResult.getIoFuture();
        existing = attachments.attachIfAbsent(key, future);
        if (existing != null) {
            return existing;
        }

        // open the channel and create notifiers to trigger user construction operation
        final IoFuture futureChannel = connection.openChannel(serviceName, optionMap);
        futureChannel.addNotifier(new IoFuture.HandlingNotifier>() {
            public void handleCancelled(final FutureResult futureResult) {
                futureResult.setCancelled();
                attachments.removeAttachment(key, futureResult.getIoFuture());
            }

            public void handleFailed(final IOException exception, final FutureResult futureResult) {
                futureResult.setException(exception);
                attachments.removeAttachment(key, futureResult.getIoFuture());
            }

            public void handleDone(final Channel channel, final FutureResult futureResult) {
                final IoFuture nextFuture = constructor.apply(channel);
                nextFuture.addNotifier(new IoFuture.HandlingNotifier>() {
                    public void handleCancelled(final FutureResult futureResult) {
                        safeClose(channel);
                        futureResult.setCancelled();
                        attachments.removeAttachment(key, futureResult.getIoFuture());
                    }

                    public void handleFailed(final IOException exception, final FutureResult futureResult) {
                        safeClose(channel);
                        futureResult.setException(exception);
                        attachments.removeAttachment(key, futureResult.getIoFuture());
                    }

                    public void handleDone(final T result, final FutureResult futureResult) {
                        // Publish result
                        futureResult.setResult(result);
                        // Optimize overall
                        attachments.replaceAttachment(key, futureResult.getIoFuture(), new FinishedIoFuture(result));
                        // Remove on close
                        channel.addCloseHandler((closed, exception) -> attachments.removeAttachment(key));
                    }
                }, futureResult);
                // make sure cancel requests now pass up to the service future
                futureResult.addCancelHandler(nextFuture);
            }
        }, futureResult);
        // make sure cancel requests pass up to the channel open request
        futureResult.addCancelHandler(futureChannel);
        return future;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy