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

org.bdware.doip.audit.PooledDoipClient Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.audit;

import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.exception.DoipConnectException;
import org.bdware.doip.codec.metadata.SearchParameter;
import org.bdware.doip.endpoint.client.ClientConfig;
import org.bdware.doip.endpoint.client.DoipClient;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.doip.endpoint.client.DoipMessageCallback;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PooledDoipClient implements DoipClient {
    static Map pooled = new ConcurrentHashMap<>();
    DoipClientImpl client;

    @Override
    public void hello(String id, DoipMessageCallback cb) {
        client.hello(id, cb);
    }

    @Override
    public void listOperations(String id, DoipMessageCallback cb) {
        client.listOperations(id, cb);
    }

    @Override
    public void retrieve(String id, String element, boolean includeElementData, DoipMessageCallback cb) {
        client.retrieve(id, element, includeElementData, cb);
    }

    @Override
    public void create(String targetDoipService, DigitalObject digitalObject, DoipMessageCallback cb) {
        client.create(targetDoipService, digitalObject, cb);
    }

    @Override
    public void update(DigitalObject digitalObject, DoipMessageCallback cb) {
        client.update(digitalObject, cb);
    }

    @Override
    public void delete(String id, DoipMessageCallback cb) {
        client.delete(id, cb);
    }

    @Override
    public void search(String id, SearchParameter sp, DoipMessageCallback cb) {
        client.search(id, sp, cb);
    }

    @Override
    public void sendRawMessage(DoipMessage msg, DoipMessageCallback cb) {
        client.sendRawMessage(msg, cb);
    }

    @Override
    public void close() {
        client = null;
        return;
    }

    @Override
    public void connect(ClientConfig url) {
        String key = url.url + "--" + url.protocolVersion;
        boolean needConnect = false;
        synchronized (pooled) {
            if (pooled.containsKey(key)) {
                client = pooled.get(key);
                if (!client.isConnected()) {
                    needConnect = true;
                }
            } else {
                client = new DoipClientImpl();
                pooled.put(key, client);
                needConnect = true;
            }
        }
        if (needConnect)
            client.connect(url);
        return;
    }

    @Override
    public void reconnect() throws DoipConnectException {
        client.reconnect();
    }

    @Override
    public String getRepoUrl() {
        return client.getRepoUrl();
    }

    @Override
    public String getRecipientID() {
        return client.getRecipientID();
    }

    @Override
    public void setRecipientID(String recipientID) {
        client.setRecipientID(recipientID);
    }

    @Override
    public boolean isConnected() {
        return client != null && client.isConnected();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy