001/*
002 * (C) Copyright 2006-2011 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.model.impl;
023
024import java.io.Serializable;
025
026import org.nuxeo.common.xmap.XMap;
027import org.nuxeo.common.xmap.annotation.XContent;
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XObject;
031import org.nuxeo.common.xmap.annotation.XParent;
032import org.nuxeo.runtime.model.Extension;
033import org.nuxeo.runtime.model.ExtensionPoint;
034import org.nuxeo.runtime.model.RegistrationInfo;
035import org.w3c.dom.Element;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040@XObject
041public class ExtensionPointImpl implements ExtensionPoint, Serializable {
042
043    private static final long serialVersionUID = 3959978759388449332L;
044
045    @XNode("@name")
046    public String name;
047
048    @XNode("@target")
049    public String superComponent;
050
051    @XContent("documentation")
052    public String documentation;
053
054    @XNodeList(value = "object@class", type = Class[].class, componentType = Class.class)
055    public transient Class<?>[] contributions;
056
057    public transient XMap xmap;
058
059    @XParent
060    public transient RegistrationInfo ri;
061
062    @Override
063    public Class<?>[] getContributions() {
064        return contributions;
065    }
066
067    @Override
068    public String getName() {
069        return name;
070    }
071
072    @Override
073    public String getDocumentation() {
074        return documentation;
075    }
076
077    @Override
078    public String getSuperComponent() {
079        return superComponent;
080    }
081
082    public Extension createExtension(Element element) {
083        return null;
084    }
085
086    public Object[] loadContributions(RegistrationInfo owner, Extension extension) {
087        Object[] contribs = extension.getContributions();
088        if (contribs != null) {
089            // contributions already computed - this should e an overloaded (extended) extension point
090            return contribs;
091        }
092        // should compute now the contributions
093        if (contributions != null) {
094            if (xmap == null) {
095                xmap = new XMap();
096                for (Class<?> contrib : contributions) {
097                    if (contrib != null) {
098                        xmap.register(contrib);
099                    } else {
100                        throw new RuntimeException("Unknown implementation class when contributing to "
101                                + owner.getComponent().getName());
102                    }
103                }
104            }
105            contribs = xmap.loadAll(new XMapContext(extension.getContext()), extension.getElement());
106            extension.setContributions(contribs);
107        }
108        return contribs;
109    }
110
111}