|  | 
|  | 1 | +/* | 
|  | 2 | +Copyright (c) 2008 Sonatype, Inc. All rights reserved. | 
|  | 3 | +
 | 
|  | 4 | +This program is licensed to you under the Apache License Version 2.0, | 
|  | 5 | +and you may not use this file except in compliance with the Apache License Version 2.0. | 
|  | 6 | +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | 
|  | 7 | +
 | 
|  | 8 | +Unless required by applicable law or agreed to in writing, | 
|  | 9 | +software distributed under the Apache License Version 2.0 is distributed on an | 
|  | 10 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 11 | +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | 
|  | 12 | +*/ | 
|  | 13 | +package org.codehaus.plexus.build.messages; | 
|  | 14 | + | 
|  | 15 | +import javax.inject.Inject; | 
|  | 16 | +import javax.inject.Named; | 
|  | 17 | +import javax.inject.Singleton; | 
|  | 18 | + | 
|  | 19 | +import java.nio.file.Path; | 
|  | 20 | + | 
|  | 21 | +import org.codehaus.plexus.build.BuildContext; | 
|  | 22 | +import org.slf4j.Logger; | 
|  | 23 | +import org.slf4j.LoggerFactory; | 
|  | 24 | + | 
|  | 25 | +/** | 
|  | 26 | + * Default implementation of the Messages interface. | 
|  | 27 | + * <p> | 
|  | 28 | + * This implementation delegates to the BuildContext for compatibility with existing | 
|  | 29 | + * message handling infrastructure. It logs messages and calls the legacy BuildContext | 
|  | 30 | + * message API. | 
|  | 31 | + * </p> | 
|  | 32 | + */ | 
|  | 33 | +@Named("default") | 
|  | 34 | +@Singleton | 
|  | 35 | +public class DefaultMessages implements Messages { | 
|  | 36 | + | 
|  | 37 | +    private static final Logger logger = LoggerFactory.getLogger(DefaultMessages.class); | 
|  | 38 | + | 
|  | 39 | +    private final BuildContext buildContext; | 
|  | 40 | + | 
|  | 41 | +    /** | 
|  | 42 | +     * Creates a new DefaultMessages instance. | 
|  | 43 | +     * | 
|  | 44 | +     * @param buildContext the BuildContext to which messages will be delegated | 
|  | 45 | +     */ | 
|  | 46 | +    @Inject | 
|  | 47 | +    public DefaultMessages(BuildContext buildContext) { | 
|  | 48 | +        this.buildContext = buildContext; | 
|  | 49 | +    } | 
|  | 50 | + | 
|  | 51 | +    @Override | 
|  | 52 | +    public void clearAll() { | 
|  | 53 | +        // This is a no-op in the default implementation | 
|  | 54 | +        // Custom implementations may provide actual clearing functionality | 
|  | 55 | +    } | 
|  | 56 | + | 
|  | 57 | +    @Override | 
|  | 58 | +    public void clear(Path path) { | 
|  | 59 | +        if (path != null) { | 
|  | 60 | +            buildContext.removeMessages(path.toFile()); | 
|  | 61 | +        } | 
|  | 62 | +    } | 
|  | 63 | + | 
|  | 64 | +    @Override | 
|  | 65 | +    public MessageBuilder buildError(Path path) { | 
|  | 66 | +        return build(MessageType.ERROR, path); | 
|  | 67 | +    } | 
|  | 68 | + | 
|  | 69 | +    @Override | 
|  | 70 | +    public MessageBuilder buildWarning(Path path) { | 
|  | 71 | +        return build(MessageType.WARNING, path); | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    @Override | 
|  | 75 | +    public MessageBuilder buildInfo(Path path) { | 
|  | 76 | +        return build(MessageType.INFO, path); | 
|  | 77 | +    } | 
|  | 78 | + | 
|  | 79 | +    @Override | 
|  | 80 | +    public MessageBuilder build(MessageType type, Path path) { | 
|  | 81 | +        return new MessageBuilder(type, path, this::handleMessage); | 
|  | 82 | +    } | 
|  | 83 | + | 
|  | 84 | +    /** | 
|  | 85 | +     * Handles a message by logging it and delegating to the BuildContext. | 
|  | 86 | +     * | 
|  | 87 | +     * @param message the message to handle | 
|  | 88 | +     */ | 
|  | 89 | +    private void handleMessage(Message message) { | 
|  | 90 | +        // Log the message | 
|  | 91 | +        String logMessage = formatLogMessage(message); | 
|  | 92 | + | 
|  | 93 | +        switch (message.getType()) { | 
|  | 94 | +            case ERROR: | 
|  | 95 | +                logger.error(logMessage, message.getCause()); | 
|  | 96 | +                break; | 
|  | 97 | +            case WARNING: | 
|  | 98 | +                logger.warn(logMessage, message.getCause()); | 
|  | 99 | +                break; | 
|  | 100 | +            case INFO: | 
|  | 101 | +                logger.info(logMessage, message.getCause()); | 
|  | 102 | +                break; | 
|  | 103 | +        } | 
|  | 104 | + | 
|  | 105 | +        // Delegate to BuildContext for compatibility | 
|  | 106 | +        if (message.getPath() != null) { | 
|  | 107 | +            int severity = mapTypeToSeverity(message.getType()); | 
|  | 108 | +            buildContext.addMessage( | 
|  | 109 | +                    message.getPath().toFile(), | 
|  | 110 | +                    message.getLine(), | 
|  | 111 | +                    message.getColumn(), | 
|  | 112 | +                    message.getMessage(), | 
|  | 113 | +                    severity, | 
|  | 114 | +                    message.getCause()); | 
|  | 115 | +        } | 
|  | 116 | +    } | 
|  | 117 | + | 
|  | 118 | +    /** | 
|  | 119 | +     * Formats a message for logging. | 
|  | 120 | +     * | 
|  | 121 | +     * @param message the message to format | 
|  | 122 | +     * @return the formatted log message | 
|  | 123 | +     */ | 
|  | 124 | +    private String formatLogMessage(Message message) { | 
|  | 125 | +        StringBuilder sb = new StringBuilder(); | 
|  | 126 | + | 
|  | 127 | +        if (message.getPath() != null) { | 
|  | 128 | +            sb.append(message.getPath().toAbsolutePath()); | 
|  | 129 | +        } | 
|  | 130 | + | 
|  | 131 | +        if (message.getLine() > 0 || message.getColumn() > 0) { | 
|  | 132 | +            sb.append(" ["); | 
|  | 133 | +            if (message.getLine() > 0) { | 
|  | 134 | +                sb.append(message.getLine()); | 
|  | 135 | +            } | 
|  | 136 | +            if (message.getColumn() > 0) { | 
|  | 137 | +                sb.append(':').append(message.getColumn()); | 
|  | 138 | +            } | 
|  | 139 | +            sb.append("]"); | 
|  | 140 | +        } | 
|  | 141 | + | 
|  | 142 | +        if (message.getMessage() != null) { | 
|  | 143 | +            if (sb.length() > 0) { | 
|  | 144 | +                sb.append(": "); | 
|  | 145 | +            } | 
|  | 146 | +            sb.append(message.getMessage()); | 
|  | 147 | +        } | 
|  | 148 | + | 
|  | 149 | +        return sb.toString(); | 
|  | 150 | +    } | 
|  | 151 | + | 
|  | 152 | +    /** | 
|  | 153 | +     * Maps a MessageType to a BuildContext severity level. | 
|  | 154 | +     * | 
|  | 155 | +     * @param type the message type | 
|  | 156 | +     * @return the corresponding BuildContext severity | 
|  | 157 | +     */ | 
|  | 158 | +    private int mapTypeToSeverity(MessageType type) { | 
|  | 159 | +        switch (type) { | 
|  | 160 | +            case ERROR: | 
|  | 161 | +                return BuildContext.SEVERITY_ERROR; | 
|  | 162 | +            case WARNING: | 
|  | 163 | +                return BuildContext.SEVERITY_WARNING; | 
|  | 164 | +            case INFO: | 
|  | 165 | +            default: | 
|  | 166 | +                // There's no INFO severity in BuildContext, use WARNING as fallback | 
|  | 167 | +                return BuildContext.SEVERITY_WARNING; | 
|  | 168 | +        } | 
|  | 169 | +    } | 
|  | 170 | +} | 
0 commit comments