2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.jetbrains
.idea
.maven
.embedder
;
18 import com
.intellij
.execution
.process
.ProcessHandler
;
19 import com
.intellij
.openapi
.util
.text
.StringUtil
;
20 import gnu
.trove
.THashMap
;
21 import org
.apache
.maven
.plugin
.AbstractMojoExecutionException
;
23 import java
.text
.MessageFormat
;
26 public abstract class MavenConsole
{
27 public static final int LEVEL_AUTO
= -1;
28 public static final int LEVEL_DEBUG
= 0;
29 public static final int LEVEL_INFO
= 1;
30 public static final int LEVEL_WARN
= 2;
31 public static final int LEVEL_ERROR
= 3;
32 public static final int LEVEL_FATAL
= 4;
33 public static final int LEVEL_DISABLED
= 5;
34 public static final int LEVEL_ALWAYS
= 6;
36 public static final String LINE_SEPARATOR
= System
.getProperty("line.separator");
38 public enum OutputType
{
42 private final int myOutputLevel
;
43 private final boolean myPrintStrackTrace
;
44 private boolean isFinished
;
46 private static final Map
<String
, Integer
> PREFIX_TO_LEVEL
= new THashMap
<String
, Integer
>();
47 private static final Map
<Integer
, String
> LEVEL_TO_PREFIX
= new THashMap
<Integer
, String
>();
50 map("DEBUG", LEVEL_DEBUG
);
51 map("INFO", LEVEL_INFO
);
52 map("WARNING", LEVEL_WARN
);
53 map("ERROR", LEVEL_ERROR
);
54 map("FATAL_ERROR", LEVEL_FATAL
);
57 private static void map(String prefix
, int level
) {
58 PREFIX_TO_LEVEL
.put(prefix
, level
);
59 LEVEL_TO_PREFIX
.put(level
, prefix
);
62 public MavenConsole(MavenExecutionOptions
.LoggingLevel outputLevel
, boolean printStrackTrace
) {
63 myOutputLevel
= outputLevel
.getLevel();
64 myPrintStrackTrace
= printStrackTrace
;
67 public boolean isSuppressed(int level
) {
68 return level
< myOutputLevel
;
71 public boolean isSuppressed(String line
) {
72 return isSuppressed(getLevel(line
));
75 public abstract boolean canPause();
77 public abstract boolean isOutputPaused();
79 public abstract void setOutputPaused(boolean outputPaused
);
81 public boolean isFinished() {
85 public void finish() {
89 public abstract void attachToProcess(ProcessHandler processHandler
);
91 public void systemMessage(int level
, String string
, Throwable throwable
) {
92 printMessage(level
, string
, throwable
);
95 public void printMessage(int level
, String string
, Throwable throwable
) {
96 if (isSuppressed(level
)) return;
98 OutputType type
= OutputType
.NORMAL
;
99 if (throwable
!= null || level
== LEVEL_WARN
|| level
== LEVEL_ERROR
|| level
== LEVEL_FATAL
) {
100 type
= OutputType
.ERROR
;
103 doPrint(composeLine(level
, string
), type
);
105 if (level
== LEVEL_FATAL
) {
106 setOutputPaused(false);
109 if (throwable
!= null) {
110 String message
= null;
112 Throwable temp
= throwable
;
113 while (temp
!= null) {
114 if (temp
instanceof AbstractMojoExecutionException
) {
115 message
= appendExecutionFailureMessage(message
, temp
.getMessage());
116 message
= appendExecutionFailureMessage(message
, ((AbstractMojoExecutionException
)temp
).getLongMessage());
118 if (temp
.getCause() != null) {
119 message
= appendExecutionFailureMessage(message
, temp
.getCause().getMessage());
123 temp
= temp
.getCause();
126 if (message
== null) message
= throwable
.getMessage();
128 if (message
!= null) {
129 message
+= LINE_SEPARATOR
;
130 doPrint(LINE_SEPARATOR
+ composeLine(LEVEL_ERROR
, message
), type
);
133 if (myPrintStrackTrace
) {
134 doPrint(LINE_SEPARATOR
+ StringUtil
.getThrowableText(throwable
), OutputType
.ERROR
);
137 doPrint(LINE_SEPARATOR
+
138 "To view full stack traces, please go to the Settings->Maven and check the 'Print Exception Stack Traces' box." +
145 private String
appendExecutionFailureMessage(String message
, String newMessage
) {
146 if (message
== null) return newMessage
;
147 if (newMessage
== null) return message
;
148 return message
+ LINE_SEPARATOR
+ LINE_SEPARATOR
+ newMessage
;
151 protected abstract void doPrint(String text
, OutputType type
);
153 private static int getLevel(String line
) {
154 return getLevelByPrefix(extractPrefix(line
));
157 private static String
extractPrefix(String line
) {
158 if (line
.startsWith("[")) {
159 int closing
= line
.indexOf("] ", 1);
161 return line
.substring(1, closing
);
167 private static int getLevelByPrefix(String prefix
) {
168 Integer level
= PREFIX_TO_LEVEL
.get(prefix
);
169 return level
!= null ? level
: LEVEL_ALWAYS
;
172 private static String
composeLine(int level
, String message
) {
173 return MessageFormat
.format("[{0}] {1}", getPrefixByLevel(level
), message
);
176 private static String
getPrefixByLevel(int level
) {
177 return LEVEL_TO_PREFIX
.get(level
);