001    /*
002     * Copyright (c) 2024 QOS.ch Sarl (Switzerland)
003     * All rights reserved.
004     *
005     * Permission is hereby granted, free  of charge, to any person obtaining
006     * a  copy  of this  software  and  associated  documentation files  (the
007     * "Software"), to  deal in  the Software without  restriction, including
008     * without limitation  the rights to  use, copy, modify,  merge, publish,
009     * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010     * permit persons to whom the Software  is furnished to do so, subject to
011     * the following conditions:
012     *
013     * The  above  copyright  notice  and  this permission  notice  shall  be
014     * included in all copies or substantial portions of the Software.
015     *
016     * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017     * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018     * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019     * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020     * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021     * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022     * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023     *
024     *
025     *
026     */
027
028    package ch.qos.logback.tyler.base;
029
030    import ch.qos.logback.classic.Level;
031    import ch.qos.logback.classic.LoggerContext;
032    import ch.qos.logback.classic.spi.Configurator;
033    import ch.qos.logback.classic.tyler.TylerConfiguratorBase;
034    import ch.qos.logback.core.Context;
035    import ch.qos.logback.core.model.processor.ModelInterpretationContext;
036    import com.squareup.javapoet.FieldSpec;
037    import com.squareup.javapoet.MethodSpec;
038    import com.squareup.javapoet.ParameterSpec;
039    import com.squareup.javapoet.TypeSpec;
040
041    import javax.lang.model.element.Modifier;
042
043    import static ch.qos.logback.tyler.base.TylerConstants.CONFIGURE_METHOD_NAME;
044    import static ch.qos.logback.tyler.base.TylerConstants.CONTEXT_FIELD_NAME;
045    import static ch.qos.logback.tyler.base.TylerConstants.LEVEL_FIELD_NAME;
046    import static ch.qos.logback.tyler.base.TylerConstants.LOGGER_CONTEXT_PARAMETER_NAME;
047
048    public class TylerModelInterpretationContext extends ModelInterpretationContext {
049
050
051        final public TypeSpec.Builder tylerConfiguratorTSB;
052        final public MethodSpec.Builder configureMethodSpecBuilder;
053
054
055        final FieldSpec contextFieldSpec = FieldSpec.builder(LoggerContext.class, CONTEXT_FIELD_NAME, Modifier.PRIVATE).build();
056        final ParameterSpec contextParameterSpec = ParameterSpec.builder(LoggerContext.class, LOGGER_CONTEXT_PARAMETER_NAME).build();
057        final ParameterSpec levelParameterSpec = ParameterSpec.builder(Level.class, LEVEL_FIELD_NAME).build();
058
059        public TylerModelInterpretationContext(Context context) {
060            super(context);
061            this.configureMethodSpecBuilder = initializeConfigureMethodSpecBuilder();
062            this.tylerConfiguratorTSB = initializeTylerConfiguratorTSB();
063        }
064
065
066        TypeSpec.Builder initializeTylerConfiguratorTSB() {
067            //MethodSpec setupLoggerMS = makeSetupLoggerMethodSpec();
068
069            TypeSpec.Builder tsb  = TypeSpec.classBuilder(TylerConstants.TYLER_CONFIGURATOR)
070                    .addJavadoc("This class is intended to be copied and integrated into the user's project in order\nto "
071                            + "configure logback without using XML. ")
072                    .addSuperinterface(Configurator.class)
073                    .superclass(TylerConfiguratorBase.class);
074            return tsb;
075        }
076
077        private MethodSpec.Builder initializeConfigureMethodSpecBuilder() {
078            MethodSpec.Builder msb = MethodSpec.methodBuilder(CONFIGURE_METHOD_NAME)
079                    .addJavadoc("This method performs configuration per {@link $T} interface.\n\n", Configurator.class)
080                    .addJavadoc("<p></p>")
081                    .addModifiers(Modifier.PUBLIC)
082                    .addParameter(contextParameterSpec)
083                    .returns(Configurator.ExecutionStatus.class)
084                    .addStatement("$N($N)", TylerConfiguratorBase.SET_CONTEXT_METHOD_NAME, contextParameterSpec);
085
086            return msb;
087        }
088
089//        private MethodSpec makeSetupLoggerMethodSpec() {
090//            MethodSpec.Builder msb = MethodSpec.methodBuilder(SETUP_LOGGER_METHOD_NAME)
091//                    .addModifiers(Modifier.PRIVATE)
092//                    .addParameter(String.class, LOGGER_NAME_FIELD_NAME)
093//                    .addParameter(levelParameterSpec)
094//                    .addParameter(String.class, LEVEL_STRING_PARAMETER_NAME)
095//                    .addParameter(Boolean.class, ADDITIVITY_FIELD_NAME)
096//                    .returns(Logger.class)
097//                    .addStatement("$1T loggerContext = ($1T) $2N", LoggerContext.class, CONTEXT_FIELD_NAME)
098//                    .addStatement("$T $N = loggerContext.getLogger($N)", Logger.class, LOGGER_FIELD_NAME, LOGGER_NAME_FIELD_NAME)
099//                    .beginControlFlow("if (!$T.isNullOrEmptyOrAllSpaces($N))", OptionHelper.class, LEVEL_STRING_PARAMETER_NAME)
100//                    .addStatement("$N.setLevel($N)", LOGGER_FIELD_NAME, LEVEL_FIELD_NAME)
101//                    .endControlFlow()
102//                    .beginControlFlow("if ($N != null)", ADDITIVITY_FIELD_NAME)
103//                    .addStatement("$N.setAdditive($N)", LOGGER_FIELD_NAME, ADDITIVITY_FIELD_NAME)
104//                    .endControlFlow()
105//                    .addStatement("return $N", LOGGER_FIELD_NAME);
106//
107//            return msb.build();
108//        }
109
110        public FieldSpec getContextFieldSpec() {
111            return contextFieldSpec;
112        }
113    }