MCPcopy Create free account
hub / github.com/BaseXdb/basex / WebModules

Class WebModules

basex-api/src/main/java/org/basex/http/web/WebModules.java:29–382  ·  view source on GitHub ↗

This class caches RESTXQ modules found in the HTTP root directory. @author BaseX Team, BSD License @author Christian Gruen

Source from the content-addressed store, hash-verified

27 * @author Christian Gruen
28 */
29public final class WebModules {
30 /** Singleton instance. */
31 private static volatile WebModules instance;
32
33 /** RESTXQ path. */
34 private final IOFile path;
35
36 /** Module cache. */
37 private HashMap<String, WebModule> modules = new HashMap<>();
38 /** Indicates if modules have been cached. */
39 private boolean parsed;
40 /** Last access time. */
41 private long access;
42
43 /**
44 * Private constructor.
45 * @param ctx database context
46 */
47 private WebModules(final Context ctx) {
48 final StaticOptions sopts = ctx.soptions;
49 final String webpath = sopts.get(StaticOptions.WEBPATH);
50 final String rxqpath = sopts.get(StaticOptions.RESTXQPATH);
51 path = new IOFile(webpath).resolve(rxqpath);
52
53 // RESTXQ parsing
54 final int sec = sopts.get(StaticOptions.PARSERESTXQ);
55 // < 0: process until cache is invalidated
56 if(sec >= 0) {
57 // speed up permission checks: keep cache for a minimum of time even if caching is disabled
58 final int ms = sec == 0 ? 10 : sec * 1000;
59 new Timer(true).scheduleAtFixedRate(new TimerTask() {
60 @Override
61 public void run() {
62 synchronized(WebModules.this) {
63 if(System.currentTimeMillis() - access >= ms) init(true);
64 }
65 }
66 }, 0, 100);
67 }
68 }
69
70 /**
71 * Returns the singleton instance.
72 * @param ctx database context
73 * @return instance
74 */
75 public static WebModules get(final Context ctx) {
76 if(instance == null) instance = new WebModules(ctx);
77 return instance;
78 }
79
80 /**
81 * Initializes the module cache.
82 * @param update only update new modules
83 */
84 public synchronized void init(final boolean update) {
85 if(!update) modules = new HashMap<>();
86 parsed = false;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected