BrightSide Workbench Full Report + Source Code
AllianceContext.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.alliance;
20 
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.configuration.JsonConfiguration;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.file.Document;
29 
34 public class AllianceContext {
35 
36  public static boolean isServer() {
37  return load().server;
38  }
39 
40  public static boolean isUnsecured() {
41  return load().unsecured;
42  }
43 
44  /* Fields */
45 
46  private boolean server;
47 
48  private boolean unsecured;
49 
50  /* Factory */
51 
52  private static final String ALLIANCE_CONTEXT = "/WEB-INF/elephant/alliance/context.json";
53 
54  private static AllianceContext load() {
55  Document file = Document.from(ElephantContext.getRealPath(ALLIANCE_CONTEXT));
56  if(file.exists()) {
57  try(Reader reader = file.reader()) {
58  return JsonConfiguration.read(reader, AllianceContext.class);
59  } catch (IOException ex) {
60  Logger.getLogger(AllianceContext.class.getName()).log(Level.SEVERE, null, ex);
61  return null;
62  }
63  } else {
64  return new AllianceContext();
65  }
66  }
67 
68  private static void save(AllianceContext context) {
69  Document file = Document.from(ElephantContext.getRealPath(ALLIANCE_CONTEXT));
70  file.folder().ensure();
71  try(Writer writer = file.writer()) {
72  JsonConfiguration.write(writer, context);
73  } catch (IOException ex) {
74  Logger.getLogger(AllianceContext.class.getName()).log(Level.SEVERE, null, ex);
75  }
76  }
77 
78 }