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

com.fireflysource.net.websocket.common.extension.ExtensionFactory Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.websocket.common.extension;


import com.fireflysource.common.collection.CollectionUtils;
import com.fireflysource.net.websocket.common.extension.compress.DeflateFrameExtension;
import com.fireflysource.net.websocket.common.extension.compress.PerMessageDeflateExtension;
import com.fireflysource.net.websocket.common.extension.compress.XWebkitDeflateFrameExtension;
import com.fireflysource.net.websocket.common.extension.fragment.FragmentExtension;
import com.fireflysource.net.websocket.common.extension.identity.IdentityExtension;
import com.fireflysource.net.websocket.common.model.Extension;
import com.fireflysource.net.websocket.common.model.ExtensionConfig;

import java.util.*;

public abstract class ExtensionFactory implements Iterable> {
    private ServiceLoader extensionLoader = ServiceLoader.load(Extension.class);
    private Map> availableExtensions;

    public ExtensionFactory() {
        availableExtensions = new HashMap<>();
        for (Extension ext : extensionLoader) {
            if (ext != null) {
                availableExtensions.put(ext.getName(), ext.getClass());
            }
        }
        if (CollectionUtils.isEmpty(availableExtensions)) {
            availableExtensions.put(new DeflateFrameExtension().getName(), DeflateFrameExtension.class);
            availableExtensions.put(new PerMessageDeflateExtension().getName(), PerMessageDeflateExtension.class);
            availableExtensions.put(new XWebkitDeflateFrameExtension().getName(), XWebkitDeflateFrameExtension.class);
            availableExtensions.put(new IdentityExtension().getName(), IdentityExtension.class);
            availableExtensions.put(new FragmentExtension().getName(), FragmentExtension.class);
        }
    }

    public Map> getAvailableExtensions() {
        return availableExtensions;
    }

    public Class getExtension(String name) {
        return availableExtensions.get(name);
    }

    public Set getExtensionNames() {
        return availableExtensions.keySet();
    }

    public boolean isAvailable(String name) {
        return availableExtensions.containsKey(name);
    }

    @Override
    public Iterator> iterator() {
        return availableExtensions.values().iterator();
    }

    public abstract Extension newInstance(ExtensionConfig config);

    public void register(String name, Class extension) {
        availableExtensions.put(name, extension);
    }

    public void unregister(String name) {
        availableExtensions.remove(name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy