001package org.nuxeo.ecm.webengine.gwt;
002
003import java.io.IOException;
004import java.net.URISyntaxException;
005import java.net.URL;
006
007import org.nuxeo.ecm.core.api.NuxeoException;
008import org.nuxeo.runtime.model.ComponentInstance;
009import org.nuxeo.runtime.model.DefaultComponent;
010
011public class GwtComponent extends DefaultComponent {
012
013        protected final GwtResolver resolver = new GwtResolver();
014
015        @Override
016        public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
017                if (contribution instanceof GwtWarStrategy) {
018                        GwtWarStrategy descriptor = (GwtWarStrategy) contribution;
019                        resolver.install(descriptor.name, descriptor.strategy);
020                } else if (contribution instanceof GwtWarDirectory) {
021                        GwtWarDirectory descriptor = (GwtWarDirectory) contribution;
022                        try {
023                                resolver.install(descriptor.name, descriptor.dir.toURI());
024                        } catch (IOException cause) {
025                                throw new NuxeoException("Cannot install " + descriptor, cause);
026                        }
027                } else if (contribution instanceof GwtWarBundle) {
028                        GwtWarBundle descriptor = (GwtWarBundle) contribution;
029                        URL location = contributor.getContext().getBundle().getEntry(descriptor.pathname);
030                        if (location == null) {
031                                throw new NuxeoException("Cannot locate GWT " + descriptor + " in "
032                                                + contributor.getContext().getBundle());
033                        }
034                        try {
035                                resolver.install(descriptor.name, location.toURI());
036                        } catch (IOException | URISyntaxException cause) {
037                                throw new NuxeoException("Cannot install " + descriptor, cause);
038                        }
039                }
040        }
041
042        @Override
043        public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
044                if (contribution instanceof GwtWarStrategy) {
045                        GwtWarStrategy descriptor = (GwtWarStrategy) contribution;
046                        resolver.uninstall(descriptor.name);
047                }
048        }
049
050        @Override
051        public <T> T getAdapter(Class<T> adapter) {
052                if (adapter.isAssignableFrom(GwtResolver.class)) {
053                        return adapter.cast(resolver);
054                }
055                return super.getAdapter(adapter);
056        }
057
058}