001/*
002 * (C) Copyright 2012-2015 Nuxeo SA (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-2.1.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 *     mguillaume
016 */
017
018package org.nuxeo.launcher.info;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import javax.xml.bind.annotation.XmlAccessType;
024import javax.xml.bind.annotation.XmlAccessorType;
025import javax.xml.bind.annotation.XmlAttribute;
026import javax.xml.bind.annotation.XmlElement;
027import javax.xml.bind.annotation.XmlElementWrapper;
028import javax.xml.bind.annotation.XmlRootElement;
029
030import org.apache.commons.lang.builder.ReflectionToStringBuilder;
031import org.apache.commons.lang.builder.ToStringStyle;
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.apache.commons.logging.impl.SimpleLog;
035
036@XmlAccessorType(XmlAccessType.NONE)
037@XmlRootElement(name = "command")
038/**
039 * @since 5.6
040 */
041public class CommandInfo {
042
043    static final Log log = LogFactory.getLog(CommandInfo.class);
044
045    public static final String CMD_UNKNOWN = "unknown";
046
047    public static final String CMD_LIST = "list";
048
049    public static final String CMD_ADD = "add";
050
051    public static final String CMD_INSTALL = "install";
052
053    public static final String CMD_UNINSTALL = "uninstall";
054
055    public static final String CMD_REMOVE = "remove";
056
057    public static final String CMD_RESET = "reset";
058
059    /**
060     * @since 5.7
061     */
062    public static final String CMD_DOWNLOAD = "download";
063
064    public static final String CMD_INIT = "init";
065
066    /**
067     * @since 5.7
068     */
069    public static final String CMD_SHOW = "show";
070
071    public CommandInfo() {
072    }
073
074    public CommandInfo(String cmdType) {
075        name = cmdType;
076    }
077
078    @XmlAttribute()
079    public String name;
080
081    @XmlAttribute()
082    public String param;
083
084    @XmlAttribute()
085    public Integer exitCode = 0;
086
087    @XmlAttribute()
088    public String id;
089
090    @XmlAttribute()
091    public boolean pending = false;
092
093    @XmlElementWrapper(name = "messages")
094    @XmlElement(name = "message")
095    public List<MessageInfo> messages = new ArrayList<>();
096
097    @XmlElementWrapper(name = "packages")
098    @XmlElement(name = "package")
099    public List<PackageInfo> packages = new ArrayList<>();
100
101    /**
102     * @return new {@link MessageInfo} added to messages
103     * @since 5.7
104     */
105    public MessageInfo newMessage() {
106        MessageInfo messageInfo = new MessageInfo();
107        messages.add(messageInfo);
108        return messageInfo;
109    }
110
111    /**
112     * @return new {@link MessageInfo} added to messages
113     * @since 5.7
114     */
115    public MessageInfo newMessage(int level, String message) {
116        MessageInfo messageInfo = new MessageInfo(level, message);
117        messages.add(messageInfo);
118        return messageInfo;
119    }
120
121    /**
122     * @return new {@link MessageInfo} added to messages
123     * @since 5.7
124     */
125    public MessageInfo newMessage(Exception e) {
126        return newMessage(SimpleLog.LOG_LEVEL_ERROR, e);
127    }
128
129    /**
130     * @return new {@link MessageInfo} added to messages
131     * @since 5.7
132     */
133    public MessageInfo newMessage(int level, Exception e) {
134        log.debug(e, e);
135        return newMessage(level, e.getMessage());
136    }
137
138    /**
139     * Log content of the command info
140     *
141     * @since 5.7
142     */
143    public void log(boolean debug) {
144        StringBuilder sb = new StringBuilder();
145        if (pending) {
146            sb.append("* Pending action: " + name);
147        } else {
148            sb.append("* " + name);
149        }
150        if (id != null) {
151            sb.append(" [" + id + "]");
152        }
153        if (param != null) {
154            sb.append(" (" + param + ")");
155        }
156        for (PackageInfo packageInfo : packages) {
157            sb.append("\n\t"
158                    + new ReflectionToStringBuilder(packageInfo, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(
159                            new String[] { "description" })
160                                                                                                  .toString());
161        }
162        if (exitCode != 0 || debug) {
163            if (exitCode != 0) {
164                log.error(sb.toString());
165            } else {
166                log.info(sb.toString());
167            }
168            for (MessageInfo messageInfo : messages) {
169                messageInfo.log();
170            }
171        }
172    }
173
174}