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