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