001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Stephane Lacoin
018 */
019package org.nuxeo.ecm.webengine.gwt;
020
021import java.io.IOException;
022import java.net.URISyntaxException;
023import java.net.URL;
024
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.runtime.model.ComponentInstance;
027import org.nuxeo.runtime.model.DefaultComponent;
028
029public class GwtComponent extends DefaultComponent {
030
031        protected final GwtResolver resolver = new GwtResolver();
032
033        @Override
034        public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
035                if (contribution instanceof GwtWarStrategy) {
036                        GwtWarStrategy descriptor = (GwtWarStrategy) contribution;
037                        resolver.install(descriptor.name, descriptor.strategy);
038                } else if (contribution instanceof GwtWarDirectory) {
039                        GwtWarDirectory descriptor = (GwtWarDirectory) contribution;
040                        try {
041                                resolver.install(descriptor.name, descriptor.dir.toURI());
042                        } catch (IOException cause) {
043                                throw new NuxeoException("Cannot install " + descriptor, cause);
044                        }
045                } else if (contribution instanceof GwtWarBundle) {
046                        GwtWarBundle descriptor = (GwtWarBundle) contribution;
047                        URL location = contributor.getContext().getBundle().getEntry(descriptor.pathname);
048                        if (location == null) {
049                                throw new NuxeoException("Cannot locate GWT " + descriptor + " in "
050                                                + contributor.getContext().getBundle());
051                        }
052                        try {
053                                resolver.install(descriptor.name, location.toURI());
054                        } catch (IOException | URISyntaxException cause) {
055                                throw new NuxeoException("Cannot install " + descriptor, cause);
056                        }
057                }
058        }
059
060        @Override
061        public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
062                if (contribution instanceof GwtWarStrategy) {
063                        GwtWarStrategy descriptor = (GwtWarStrategy) contribution;
064                        resolver.uninstall(descriptor.name);
065                }
066        }
067
068        @Override
069        public <T> T getAdapter(Class<T> adapter) {
070                if (adapter.isAssignableFrom(GwtResolver.class)) {
071                        return adapter.cast(resolver);
072                }
073                return super.getAdapter(adapter);
074        }
075
076}