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