001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.runtime.deployment.preprocessor;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.common.xmap.annotation.XContent;
031import org.nuxeo.common.xmap.annotation.XNode;
032import org.nuxeo.common.xmap.annotation.XNodeList;
033import org.nuxeo.common.xmap.annotation.XNodeMap;
034import org.nuxeo.common.xmap.annotation.XObject;
035import org.nuxeo.runtime.deployment.preprocessor.install.CommandProcessor;
036import org.nuxeo.runtime.deployment.preprocessor.install.DOMCommandsParser;
037import org.nuxeo.runtime.deployment.preprocessor.template.TemplateContribution;
038import org.w3c.dom.DocumentFragment;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043@XObject("fragment")
044public class FragmentDescriptor {
045
046    private static final Log log = LogFactory.getLog(FragmentDescriptor.class);
047
048    /**
049     * Marker used for better control on requirements. see "all" marker in FragmentRegistry
050     */
051    public static final FragmentDescriptor ALL = new FragmentDescriptor("all", true);
052
053    // the name is the name of the XML fragment file for XML fragments
054    // or the name of the JAR container for archive or directory fragments
055    @XNode("@name")
056    public String name;
057
058    @XNode("@version")
059    public int version = 0;
060
061    public String fileName;
062
063    public String filePath;
064
065    /**
066     * The start level is used to control bundle start order. The following levels are defined:
067     * <ul>
068     * <li>0 - system level - used by the OSGi framework itself
069     * <li>1 - runtime level - used by nuxeo-runtime bundles
070     * <li>2 - core level - used for core bundles
071     * <li>3 - platform level - used for platform service bundles
072     * <li>4 - presentation level - used for UI service bundles (e.g. seam components etc)
073     * <li>5 - UI level -used for UI bundles (e.g. war / web, widgets contribs)
074     * <li>6 - user level
075     * </ul>
076     * The start level is overwritten by the one specified at MANIFEST level using the Nuxeo-StartLevel header. If the
077     * start header is missing it will be initialized from the OSGi Bundle-Category (if any) as follows:
078     * <ul>
079     * <li><code>nuxeo-framework</code>
080     * <li><code>nuxeo-runtime</code>
081     * <li><code>nuxeo-service</code>
082     * <li><code>nuxeo-core</code>
083     * <li><code>nuxeo-platform</code>
084     * <li><code>nuxeo-presentation</code>
085     * <li><code>nuxeo-ui</code>
086     * <li><code>nuxeo-plugin</code>
087     * </ul>
088     * If the start level could not be computed then the default value of 6 (user level) is used The recommended method
089     * of specifying the start level is to use the <code>Bundle-Category</code> since start level numbering may change
090     * (this header has the advantage of using symbolic names)
091     */
092    @XNode("@startLevel")
093    @Deprecated
094    public int startLevel;
095
096    @XNodeList(value = "extension", type = TemplateContribution[].class, componentType = TemplateContribution.class)
097    public TemplateContribution[] contributions;
098
099    @XNodeList(value = "require", type = ArrayList.class, componentType = String.class)
100    public List<String> requires;
101
102    @XNodeList(value = "requiredBy", type = String[].class, componentType = String.class)
103    public String[] requiredBy;
104
105    @XNodeMap(value = "template", key = "@name", type = HashMap.class, componentType = TemplateDescriptor.class)
106    public Map<String, TemplateDescriptor> templates;
107
108    public CommandProcessor install;
109
110    public CommandProcessor uninstall;
111
112    protected boolean isMarker;
113
114    /**
115     *
116     */
117    public FragmentDescriptor() {
118        // TODO Auto-generated constructor stub
119    }
120
121    public FragmentDescriptor(String name, boolean isMarker) {
122        this.name = name;
123        this.isMarker = isMarker;
124    }
125
126    public boolean isMarker() {
127        return isMarker;
128    }
129
130    @XContent("install")
131    public void setInstallCommands(DocumentFragment df) {
132        try {
133            install = DOMCommandsParser.parse(df);
134        } catch (IOException e) {
135            log.error("Failed to set install commands");
136        }
137    }
138
139    @XContent("uninstall")
140    public void setUninstallCommands(DocumentFragment df) {
141        try {
142            uninstall = DOMCommandsParser.parse(df);
143        } catch (IOException e) {
144            log.error("Failed to set uninstall commands");
145        }
146    }
147
148    @Override
149    public String toString() {
150        return name + " [" + fileName + ']';
151    }
152
153}