BrightSide Workbench Full Report + Source Code
DocumentDefinitions.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.documentation;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.Collection;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.impl.util.FileUtil;
31 import org.turro.file.FileWatch;
32 import org.turro.lock.Initializer;
33 import org.turro.reflection.JSONSerializer;
34 
39 public class DocumentDefinitions extends TreeSet<DocumentDefinition> {
40 
41  public DocumentDefinition get(String name) {
42  return stream().filter(dd -> dd.getName().equals(name)).findFirst().orElse(null);
43  }
44 
45  public SortedSet<String> names() {
46  return new TreeSet(stream().map(dd -> dd.getName()).toList());
47  }
48 
49  public DocumentDefinitions filtered(String root) {
50  return new DocumentDefinitions(stream().filter(dd -> dd.matches(root)).toList());
51  }
52 
53  public void save() {
54  save(this);
55  }
56 
57  /* Factory */
58 
59  private static final Initializer<DocumentDefinitions> INIT = new Initializer<>();
60  private static final FileWatch watch = new FileWatch(definitionsFile());
61 
62  public static DocumentDefinitions instance() {
63  return INIT.instance(() -> !watch.isNewer(), () -> load());
64  }
65 
66  public static void reset() {
67  INIT.reset();
68  }
69 
70  private static final String DOCUMENTATION_FILE = "/WEB-INF/elephant/conf/documentation.json";
71 
72  protected static DocumentDefinitions load() {
73  DocumentDefinitions definitions = null;
74  if(watch.exists()) try {
75  watch.reset();
76  JSONSerializer ser = new JSONSerializer(true);
77  definitions = ser.fromJson(FileUtil.getContent(watch.getFile()), DocumentDefinitions.class);
78  } catch (IOException ex) {
79  Logger.getLogger(DocumentDefinitions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
80  }
81  if(definitions == null) {
82  definitions = new DocumentDefinitions();
83  save(definitions);
84  }
85  return definitions;
86  }
87 
88  private static void save(DocumentDefinitions definitions) {
89  try (Writer writer = FileUtil.getFileWriter(watch.getFileSafe())) {
90  JSONSerializer ser = new JSONSerializer(definitions, true);
91  writer.write(ser.toJson());
92  } catch (IOException ex) {
93  Logger.getLogger(DocumentDefinitions.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
94  }
95  }
96 
97  private static File definitionsFile() {
98  return new File(ElephantContext.getRealPath(DOCUMENTATION_FILE));
99  }
100 
102  }
103 
104  private DocumentDefinitions(Collection<DocumentDefinition> definitions) {
105  super(definitions);
106  }
107 
108 }
DocumentDefinitions filtered(String root)
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
static String getContent(File file)
Definition: FileUtil.java:245