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