001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.runtime.model.impl;
016
017import java.io.ByteArrayInputStream;
018import java.io.IOException;
019
020import org.nuxeo.common.xmap.DOMSerializer;
021import org.nuxeo.common.xmap.annotation.XContent;
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XObject;
024import org.nuxeo.runtime.model.ComponentInstance;
025import org.nuxeo.runtime.model.ComponentName;
026import org.nuxeo.runtime.model.Extension;
027import org.nuxeo.runtime.model.RuntimeContext;
028import org.w3c.dom.Element;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033@XObject("extension")
034public class ExtensionImpl implements Extension {
035
036    // used to generate the extension id if none was provided
037    private static int cnt = 0;
038
039    private static final long serialVersionUID = 8504100747683248986L;
040
041    private static final ExtensionDescriptorReader reader = new ExtensionDescriptorReader();
042
043    @XNode("@target")
044    ComponentName target;
045
046    @XNode("@point")
047    String extensionPoint;
048
049    @XNode("@id")
050    private String id;
051
052    @XContent("documentation")
053    String documentation;
054
055    @XNode("")
056    transient Element element;
057
058    transient Object[] contributions;
059
060    // declaring component
061    transient ComponentInstance component;
062
063    @Override
064    public void dispose() {
065        element = null;
066        contributions = null;
067    }
068
069    @Override
070    public Element getElement() {
071        return element;
072    }
073
074    @Override
075    public void setElement(Element element) {
076        this.element = element;
077    }
078
079    @Override
080    public String getExtensionPoint() {
081        return extensionPoint;
082    }
083
084    @Override
085    public ComponentName getTargetComponent() {
086        return target;
087    }
088
089    @Override
090    public Object[] getContributions() {
091        return contributions;
092    }
093
094    @Override
095    public void setContributions(Object[] contributions) {
096        this.contributions = contributions;
097    }
098
099    @Override
100    public void setComponent(ComponentInstance component) {
101        this.component = component;
102    }
103
104    @Override
105    public ComponentInstance getComponent() {
106        return component;
107    }
108
109    @Override
110    public RuntimeContext getContext() {
111        return component.getContext();
112    }
113
114    @Override
115    public String getId() {
116        if (id == null) {
117            if (component != null) {
118                id = component.getName().getName() + '#' + extensionPoint + '.' + (cnt++);
119            } else {
120                id = "null#" + extensionPoint + '.' + (cnt++);
121            }
122        }
123        return id;
124    }
125
126    @Override
127    public String getDocumentation() {
128        return documentation;
129    }
130
131    @Override
132    public String toString() {
133        StringBuilder buf = new StringBuilder();
134        buf.append(ExtensionImpl.class.getSimpleName());
135        buf.append(" {");
136        buf.append("target: ");
137        buf.append(target);
138        buf.append(", point:");
139        buf.append(extensionPoint);
140        buf.append(", contributor:");
141        buf.append(component);
142        buf.append('}');
143        return buf.toString();
144    }
145
146    /**
147     * Gets the XML string for this extension.
148     */
149    @Override
150    public String toXML() {
151        try {
152            return DOMSerializer.toStringOmitXml(element);
153        } catch (IOException e) {
154            System.err.println("Failed to serialize extension " + e);
155            return null;
156        }
157    }
158
159    public static ExtensionImpl fromXML(RuntimeContext context, String xml) throws IOException {
160        return reader.read(context, new ByteArrayInputStream(xml.getBytes()));
161    }
162
163}