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