    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements. See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache license, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License. You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the license for the specific language governing permissions and
    015     * limitations under the license.
    016     */
    017    package org.apache.logging.log4j.core.appender.routing;
    018
    019    import org.apache.logging.log4j.Logger;
    020    import org.apache.logging.log4j.core.config.plugins.Plugin;
    021    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
    022    import org.apache.logging.log4j.core.config.plugins.PluginElement;
    023    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    024    import org.apache.logging.log4j.status.StatusLogger;
    025
    026    /**
    027     * Used to contain the individual Route elements.
    028     */
    029    @Plugin(name = "Routes", category = "Core", printObject = true)
    030    public final class Routes {
    031
    032        private static final Logger LOGGER = StatusLogger.getLogger();
    033
    034        private final String pattern;
    035        private final Route[] routes;
    036
    037        private Routes(final String pattern, final Route... routes) {
    038            this.pattern = pattern;
    039            this.routes = routes;
    040        }
    041
    042        /**
    043         * Returns the pattern.
    044         * @return the pattern.
    045         */
    046        public String getPattern() {
    047            return pattern;
    048        }
    049
    050        /**
    051         * Returns the array of Route elements.
    052         * @return an array of Route elements.
    053         */
    054        public Route[] getRoutes() {
    055            return routes;
    056        }
    057
    058        @Override
    059        public String toString() {
    060            final StringBuilder sb = new StringBuilder("{");
    061            boolean first = true;
    062            for (final Route route : routes) {
    063                if (!first) {
    064                    sb.append(',');
    065                }
    066                first = false;
    067                sb.append(route.toString());
    068            }
    069            sb.append('}');
    070            return sb.toString();
    071
    072        }
    073
    074        /**
    075         * Create the Routes.
    076         * @param pattern The pattern.
    077         * @param routes An array of Route elements.
    078         * @return The Routes container.
    079         */
    080        @PluginFactory
    081        public static Routes createRoutes(
    082                @PluginAttribute("pattern") final String pattern,
    083                @PluginElement("Routes") final Route... routes) {
    084            if (pattern == null) {
    085                LOGGER.error("A pattern is required");
    086                return null;
    087            }
    088            if (routes == null || routes.length == 0) {
    089                LOGGER.error("No routes configured");
    090                return null;
    091            }
    092            return new Routes(pattern, routes);
    093        }
    094    }