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.ByteArrayInputStream;
025import java.io.IOException;
026import java.util.concurrent.atomic.AtomicInteger;
027
028import org.nuxeo.common.xmap.DOMSerializer;
029import org.nuxeo.common.xmap.annotation.XContent;
030import org.nuxeo.common.xmap.annotation.XNode;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.runtime.model.ComponentInstance;
033import org.nuxeo.runtime.model.ComponentName;
034import org.nuxeo.runtime.model.Extension;
035import org.nuxeo.runtime.model.RuntimeContext;
036import org.w3c.dom.Element;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041@XObject("extension")
042public class ExtensionImpl implements Extension {
043
044    private static final long serialVersionUID = 1L;
045
046    // used to generate the extension id if none was provided
047    private static AtomicInteger cnt = new AtomicInteger();
048
049    private static final ExtensionDescriptorReader reader = new ExtensionDescriptorReader();
050
051    @XNode("@target")
052    ComponentName target;
053
054    @XNode("@point")
055    String extensionPoint;
056
057    @XNode("@id")
058    private String id;
059
060    @XContent("documentation")
061    String documentation;
062
063    @XNode("")
064    Element element;
065
066    Object[] contributions;
067
068    // declaring component
069    ComponentInstance component;
070
071    @Override
072    public void dispose() {
073        element = null;
074        contributions = null;
075    }
076
077    @Override
078    public Element getElement() {
079        return element;
080    }
081
082    @Override
083    public void setElement(Element element) {
084        this.element = element;
085    }
086
087    @Override
088    public String getExtensionPoint() {
089        return extensionPoint;
090    }
091
092    @Override
093    public ComponentName getTargetComponent() {
094        return target;
095    }
096
097    @Override
098    public Object[] getContributions() {
099        return contributions;
100    }
101
102    @Override
103    public void setContributions(Object[] contributions) {
104        this.contributions = contributions;
105    }
106
107    @Override
108    public void setComponent(ComponentInstance component) {
109        this.component = component;
110    }
111
112    @Override
113    public ComponentInstance getComponent() {
114        return component;
115    }
116
117    @Override
118    public RuntimeContext getContext() {
119        return component.getContext();
120    }
121
122    @Override
123    public String getId() {
124        if (id == null) {
125            var count = cnt.getAndIncrement();
126            if (component != null) {
127                id = component.getName().getName() + '#' + extensionPoint + '.' + count;
128            } else {
129                id = "null#" + extensionPoint + '.' + count;
130            }
131        }
132        return id;
133    }
134
135    @Override
136    public String getDocumentation() {
137        return documentation;
138    }
139
140    @Override
141    public String toString() {
142        StringBuilder sb = new StringBuilder();
143        sb.append(ExtensionImpl.class.getSimpleName());
144        sb.append(" {");
145        sb.append("target: ");
146        sb.append(target);
147        sb.append(", point:");
148        sb.append(extensionPoint);
149        sb.append(", contributor:");
150        sb.append(component);
151        sb.append('}');
152        return sb.toString();
153    }
154
155    /**
156     * Gets the XML string for this extension.
157     */
158    @Override
159    public String toXML() {
160        try {
161            return DOMSerializer.toStringOmitXml(element);
162        } catch (IOException e) {
163            System.err.println("Failed to serialize extension " + e);
164            return null;
165        }
166    }
167
168    public static ExtensionImpl fromXML(RuntimeContext context, String xml) throws IOException {
169        return reader.read(context, new ByteArrayInputStream(xml.getBytes()));
170    }
171
172}