View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.model.processor;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.model.Model;
19  import ch.qos.logback.core.model.SerializeModelModel;
20  
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.ObjectOutputStream;
24  import java.time.Instant;
25  import java.time.format.DateTimeFormatter;
26  
27  import static ch.qos.logback.core.CoreConstants.FILE_TIMESTAMP_PATTERN;
28  import static ch.qos.logback.core.CoreConstants.MODEL_CONFIG_FILE_EXTENSION;
29  
30  public class SerializeModelModelHandler extends ModelHandlerBase {
31  
32      public SerializeModelModelHandler(Context context) {
33          super(context);
34      }
35  
36      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) {
37          return new SerializeModelModelHandler(context);
38      }
39  
40      @Override
41      public void handle(ModelInterpretationContext modelInterpretationContext, Model model)
42              throws ModelHandlerException {
43  
44  
45          Object configuratorHint = modelInterpretationContext.getConfiguratorHint();
46  
47          if(configuratorHint != null && configuratorHint.getClass().getName().equals("ch.qos.logback.classic.joran.SerializedModelConfigurator")) {
48              addInfo("Skipping model serialization as calling configurator is already model based.");
49              return;
50          }
51  
52          if (!(model instanceof SerializeModelModel)) {
53              addWarn("Model parameter is not of type SerializeModelModel. Skipping serialization of model structure");
54              return;
55          }
56  
57          SerializeModelModel serializeModelModel = (SerializeModelModel) model;
58  
59          Model topModel = modelInterpretationContext.getTopModel();
60  
61          if (topModel == null) {
62              addWarn("Could not find top most model. Skipping serialization of model structure.");
63              return;
64          }
65  
66          String fileStr = serializeModelModel.getFile();
67          if (fileStr == null) {
68              DateTimeFormatter dft = DateTimeFormatter.ofPattern(FILE_TIMESTAMP_PATTERN);
69              Instant now = Instant.now();
70              String timestamp = dft.format(now);
71              fileStr = "logback-" + timestamp + MODEL_CONFIG_FILE_EXTENSION;
72              addInfo("For model serialization, using default file destination [" + fileStr + "]");
73          } else {
74              fileStr = modelInterpretationContext.subst(fileStr);
75          }
76  
77          writeModel(fileStr, topModel);
78      }
79  
80      private void writeModel(String fileStr, Model firstModel) {
81  
82          addInfo("Serializing model to file ["+fileStr+"]");
83  
84          try (FileOutputStream fos = new FileOutputStream(fileStr)) {
85              ObjectOutputStream oos = new ObjectOutputStream(fos);
86              oos.writeObject(firstModel);
87              oos.flush();
88              oos.close();
89          } catch (IOException e) {
90              addError("IO failure while serializing Model ["+fileStr+"]");
91          }
92      }
93  }