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

org.fabric3.implementation.web.runtime.WebComponent Maven / Gradle / Ivy

The newest version!
/*
* Fabric3
* Copyright (c) 2009-2013 Metaform Systems
*
* Fabric3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version, with the
* following exception:
*
* Linking this software statically or dynamically with other
* modules is making a combined work based on this software.
* Thus, the terms and conditions of the GNU General Public
* License cover the whole combination.
*
* As a special exception, the copyright holders of this software
* give you permission to link this software with independent
* modules to produce an executable, regardless of the license
* terms of these independent modules, and to copy and distribute
* the resulting executable under terms of your choice, provided
* that you also meet, for each linked independent module, the
* terms and conditions of the license of that module. An
* independent module is a module which is not derived from or
* based on this software. If you modify this software, you may
* extend this exception to your version of the software, but
* you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version.
*
* Fabric3 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 General Public License for more details.
*
* You should have received a copy of the
* GNU General Public License along with Fabric3.
* If not, see .
*/
package org.fabric3.implementation.web.runtime;

import javax.xml.namespace.QName;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.fabric3.api.annotation.monitor.MonitorLevel;
import org.fabric3.container.web.spi.WebApplicationActivationException;
import org.fabric3.container.web.spi.WebApplicationActivator;
import org.fabric3.host.runtime.HostInfo;
import org.fabric3.implementation.pojo.spi.proxy.ProxyCreationException;
import org.fabric3.implementation.pojo.spi.proxy.WireProxyService;
import org.fabric3.spi.component.Component;
import org.fabric3.spi.component.ComponentException;
import org.fabric3.spi.model.type.java.InjectionSite;
import org.fabric3.spi.objectfactory.Injector;
import org.fabric3.spi.objectfactory.ObjectCreationException;
import org.fabric3.spi.objectfactory.ObjectFactory;
import org.fabric3.spi.objectfactory.SingletonObjectFactory;
import org.fabric3.spi.wire.Wire;
import org.oasisopen.sca.ServiceReference;
import static org.fabric3.container.web.spi.WebApplicationActivator.OASIS_CONTEXT_ATTRIBUTE;

/**
 * A component whose implementation is a web application.
 */
public class WebComponent implements Component {

    private final URI uri;
    private URI classLoaderId;
    private ClassLoader classLoader;
    private InjectorFactory injectorFactory;
    private final WebApplicationActivator activator;
    // injection site name to 
    private final Map> siteMappings;
    private final WireProxyService proxyService;
    private final QName groupId;
    private final Map> propertyFactories;
    private HostInfo info;
    private final Map> referenceFactories;
    private final URI archiveUri;
    private String contextUrl;
    private MonitorLevel level = MonitorLevel.INFO;

    public WebComponent(URI uri,
                        String contextUrl,
                        QName deployable,
                        URI archiveUri,
                        ClassLoader classLoader,
                        InjectorFactory injectorFactory,
                        WebApplicationActivator activator,
                        WireProxyService proxyService,
                        Map> propertyFactories,
                        Map> injectorMappings,
                        HostInfo info) {
        this.uri = uri;
        this.contextUrl = contextUrl;
        this.archiveUri = archiveUri;
        this.classLoader = classLoader;
        this.injectorFactory = injectorFactory;
        this.activator = activator;
        this.siteMappings = injectorMappings;
        this.proxyService = proxyService;
        this.groupId = deployable;
        this.propertyFactories = propertyFactories;
        this.info = info;
        referenceFactories = new ConcurrentHashMap>();
    }

    public URI getUri() {
        return uri;
    }

    public URI getClassLoaderId() {
        return classLoaderId;
    }

    public void setClassLoaderId(URI classLoaderId) {
        this.classLoaderId = classLoaderId;
    }

    public String getName() {
        return uri.toString();
    }

    public MonitorLevel getLevel() {
        return level;
    }

    public void setLevel(MonitorLevel level) {
        this.level = level;
    }

    public void start() throws ComponentException {
        try {
            Map>> injectors = new HashMap>>();
            injectorFactory.createInjectorMappings(injectors, siteMappings, referenceFactories, classLoader);
            injectorFactory.createInjectorMappings(injectors, siteMappings, propertyFactories, classLoader);
            OASISWebComponentContext oasisContext = new OASISWebComponentContext(this, info);
            Map> contextFactories = new HashMap>();

            SingletonObjectFactory oasisComponentContextFactory
                    = new SingletonObjectFactory(oasisContext);
            contextFactories.put(OASIS_CONTEXT_ATTRIBUTE, oasisComponentContextFactory);

            injectorFactory.createInjectorMappings(injectors, siteMappings, contextFactories, classLoader);
            // activate the web application
            activator.activate(contextUrl, archiveUri, classLoaderId, injectors, oasisContext);
        } catch (InjectionCreationException e) {
            throw new WebComponentStartException("Error starting web component: " + uri.toString(), e);
        } catch (WebApplicationActivationException e) {
            throw new WebComponentStartException("Error starting web component: " + uri.toString(), e);
        }

    }

    public void stop() throws ComponentException {
        try {
            activator.deactivate(archiveUri);
        } catch (WebApplicationActivationException e) {
            throw new WebComponentStopException("Error stopping web component: " + uri.toString(), e);
        }
    }

    public void startUpdate() {

    }

    public void endUpdate() {

    }

    public void attachWire(String name, Wire wire) throws ObjectCreationException {
        Map sites = siteMappings.get(name);
        if (sites == null || sites.isEmpty()) {
            throw new ObjectCreationException("Injection site not found for: " + name);
        }
        Class type;
        try {
            type = classLoader.loadClass(sites.values().iterator().next().getType());
        } catch (ClassNotFoundException e) {
            throw new ObjectCreationException("Reference type not found for: " + name, e);
        }
        ObjectFactory factory = createWireFactory(type, wire);
        attachWire(name, factory);
    }

    public void attachWire(String name, ObjectFactory factory) throws ObjectCreationException {
        referenceFactories.put(name, factory);
    }

    protected  ObjectFactory createWireFactory(Class interfaze, Wire wire) throws ObjectCreationException {
        try {
            return proxyService.createObjectFactory(interfaze, wire, null);
        } catch (ProxyCreationException e) {
            throw new ObjectCreationException(e);
        }
    }

    public QName getDeployable() {
        return groupId;
    }

    public  B getProperty(Class type, String propertyName) throws ObjectCreationException {
        ObjectFactory factory = propertyFactories.get(propertyName);
        if (factory != null) {
            return type.cast(factory.getInstance());
        } else {
            return null;
        }
    }

    @SuppressWarnings({"unchecked"})
    public > R cast(B target) {
        return (R) proxyService.cast(target);
    }

    public String toString() {
        return "[" + uri.toString() + "] in state [" + super.toString() + ']';
    }

}