BrightSide Workbench Full Report + Source Code
Publishing.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.publishing;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.impl.util.FileUtil;
30 import org.turro.file.FileWatch;
31 import org.turro.reflection.JSONSerializer;
32 
37 public class Publishing<E> implements Serializable {
38 
39  private String entityRoot;
40  private HashMap<String, PublishingContext> contexts;
41 
42  public String getEntityRoot() {
43  return entityRoot;
44  }
45 
46  public boolean hasContext(String context) {
47  return contexts.containsKey(context);
48  }
49 
50  public PublishingContext getContext(String context) {
51  return contexts.get(context);
52  }
53 
54  public boolean checkEntity(String context, E entity) {
55  PublishingContext pc = contexts.get(context);
56  return (pc != null) ? checkEntity(pc, entity) : null;
57  }
58 
59  protected boolean checkEntity(PublishingContext pc, E entity) {
60  return false;
61  }
62 
63  /* Factory */
64 
65  public static <T extends Publishing> Publishing getPublishing(Class<T> javaClass, String root) {
66  return doLoad(javaClass, root);
67  }
68 
69  public static <T extends Publishing> PublishingContext getPublishingContext(Class<T> javaClass, String root, String context) {
70  Publishing publishing = doLoad(javaClass, root);
71  if(publishing != null) {
72  return publishing.getContext(context);
73  } else {
74  return null;
75  }
76  }
77 
78  /* Publishing */
79 
80  private static final Map<String, Publishing> _pub = new HashMap<>();
81  private static final Map<String, Long> _lastLoads = new HashMap<>();
82 
83  protected static <T extends Publishing> Publishing doLoad(Class<T> javaClass, String publishingFile) {
84  if(_pub.get(publishingFile) == null || FileWatch.isNewer(convert(publishingFile), _lastLoads.get(publishingFile))
85  || !FileWatch.exists(convert(publishingFile))) {
86  T pub = null;
87  try {
88  JSONSerializer ser = new JSONSerializer(true);
89  pub = ser.fromJson(FileUtil.getContent(new File(convert(publishingFile))), javaClass);
90  } catch (IOException ex) {
91  Logger.getLogger(Publishing.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
92  }
93  if(pub != null) {
94  _pub.put(publishingFile, pub);
95  _lastLoads.put(publishingFile, FileWatch.getTime(convert(publishingFile)));
96  }
97  }
98  return _pub.get(publishingFile);
99  }
100 
101  private static String convert(String publishingFile) {
102  if(!publishingFile.contains("/")) {
103  publishingFile = "/WEB-INF/elephant/conf/" + publishingFile + "-publishing.json";
104  }
105  return ElephantContext.getRealPath(publishingFile);
106  }
107 
108 }
static String getContent(File file)
Definition: FileUtil.java:245
boolean checkEntity(PublishingContext pc, E entity)
Definition: Publishing.java:59
static< T extends Publishing > Publishing doLoad(Class< T > javaClass, String publishingFile)
Definition: Publishing.java:83
static< T extends Publishing > PublishingContext getPublishingContext(Class< T > javaClass, String root, String context)
Definition: Publishing.java:69
static< T extends Publishing > Publishing getPublishing(Class< T > javaClass, String root)
Definition: Publishing.java:65
PublishingContext getContext(String context)
Definition: Publishing.java:50
boolean checkEntity(String context, E entity)
Definition: Publishing.java:54
boolean hasContext(String context)
Definition: Publishing.java:46