Skip to contentPackage: DefaultSiteProvider
DefaultSiteProvider
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * NorthernWind - lightweight CMS
5: * http://tidalwave.it/projects/northernwind
6: *
7: * Copyright (C) 2011 - 2025 Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/northernwind-src
22: * git clone https://github.com/tidalwave-it/northernwind-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.northernwind.core.impl.model;
27:
28: import javax.annotation.Nonnull;
29: import javax.annotation.PostConstruct;
30: import javax.inject.Inject;
31: import javax.inject.Named;
32: import java.util.ArrayList;
33: import java.util.List;
34: import java.util.Locale;
35: import java.util.Optional;
36: import java.util.concurrent.atomic.AtomicBoolean;
37: import java.util.stream.Stream;
38: import java.io.File;
39: import java.io.IOException;
40: import javax.servlet.ServletContext;
41: import org.springframework.core.task.TaskExecutor;
42: import it.tidalwave.util.BundleUtilities;
43: import it.tidalwave.util.NotFoundException;
44: import it.tidalwave.northernwind.core.model.ModelFactory;
45: import it.tidalwave.northernwind.core.model.Site;
46: import it.tidalwave.northernwind.core.model.SiteProvider;
47: import lombok.Getter;
48: import lombok.Setter;
49: import lombok.ToString;
50: import lombok.extern.slf4j.Slf4j;
51: import static java.util.stream.Collectors.*;
52:
53: /***************************************************************************************************************************************************************
54: *
55: * The default implementation of {@link SiteProvider}.
56: *
57: * @author Fabrizio Giudici
58: *
59: **************************************************************************************************************************************************************/
60: @Slf4j @ToString
61: public class DefaultSiteProvider implements SiteProvider
62: {
63: private static final String ASTERISKS = "****************************************";
64:
65: private static final String DEFAULT_DOCUMENT_PATH = "/content/document";
66:
67: private static final String DEFAULT_MEDIA_PATH = "/content/media";
68:
69: private static final String DEFAULT_LIBRARY_PATH = "/content/library";
70:
71: private static final String DEFAULT_NODE_PATH = "/structure";
72:
73: public static final String DEFAULT_CONTEXT_PATH = "/";
74:
75: @Inject
76: private Optional<ServletContext> servletContext;
77:
78: @Inject
79: private ModelFactory modelFactory;
80:
81: @Inject @Named("taskExecutor")
82: private TaskExecutor executor;
83:
84: @Getter @Setter @Nonnull
85: private String documentPath = DEFAULT_DOCUMENT_PATH;
86:
87: @Getter @Setter @Nonnull
88: private String mediaPath = DEFAULT_MEDIA_PATH;
89:
90: @Getter @Setter @Nonnull
91: private String libraryPath = DEFAULT_LIBRARY_PATH;
92:
93: @Getter @Setter @Nonnull
94: private String nodePath = DEFAULT_NODE_PATH;
95:
96: @Getter @Setter
97: private boolean logConfigurationEnabled;
98:
99: @Getter @Setter @Nonnull
100: private String localesAsString = "";
101:
102: @Getter @Setter @Nonnull
103: private String ignoredFoldersAsString = "";
104:
105: private final List<String> ignoredFolders = new ArrayList<>();
106:
107: private final List<Locale> configuredLocales = new ArrayList<>();
108:
109: private Optional<DefaultSite> site = Optional.empty();
110:
111: private final AtomicBoolean siteAvailable = new AtomicBoolean();
112:
113: /***********************************************************************************************************************************************************
114: * {@inheritDoc}
115: **********************************************************************************************************************************************************/
116: @Override @Nonnull
117: public Site getSite()
118: {
119: return site.orElseThrow(() -> new IllegalStateException("Initialization error - @PostConstruct not called?"));
120: }
121:
122: /***********************************************************************************************************************************************************
123: * {@inheritDoc}
124: **********************************************************************************************************************************************************/
125: @Override
126: public boolean isSiteAvailable()
127: {
128: return siteAvailable.get();
129: }
130:
131: /***********************************************************************************************************************************************************
132: * {@inheritDoc}
133: **********************************************************************************************************************************************************/
134: @Override
135: public void reload()
136: {
137: log.info("reload()");
138: siteAvailable.set(false);
139:
140: site = Optional.of((DefaultSite)modelFactory.createSite().withContextPath(getContextPath())
141: .withDocumentPath(documentPath)
142: .withMediaPath(mediaPath)
143: .withLibraryPath(libraryPath)
144: .withNodePath(nodePath)
145: .withLogConfigurationEnabled(logConfigurationEnabled)
146: .withConfiguredLocales(configuredLocales)
147: .withIgnoredFolders(ignoredFolders)
148: .build());
149: executor.execute(() -> initialize(site.get()));
150: }
151:
152: /***********************************************************************************************************************************************************
153: * {@inheritDoc}
154: **********************************************************************************************************************************************************/
155: @Override @Nonnull
156: public String getVersionString()
157: {
158: return BundleUtilities.getMessage(DefaultSiteProvider.class, "NorthernWind.version");
159: }
160:
161: /***********************************************************************************************************************************************************
162: *
163: **********************************************************************************************************************************************************/
164: @PostConstruct
165: /* package */ void initialize()
166: {
167: log.info("initialize()");
168: ignoredFolders.addAll(List.of(ignoredFoldersAsString.trim().split(File.pathSeparator)));
169: configuredLocales.addAll(Stream.of(localesAsString.split(","))
170: .map(String::trim)
171: .map(Locale::new)
172: .collect(toList()));
173: reload();
174: }
175:
176: /***********************************************************************************************************************************************************
177: *
178: **********************************************************************************************************************************************************/
179: private void initialize (@Nonnull final DefaultSite site)
180: {
181: try
182: {
183: log.info(ASTERISKS);
184: log.info("SITE INITIALIZATION STARTED");
185: log.info(ASTERISKS);
186: final var time = System.currentTimeMillis();
187: site.initialize();
188: siteAvailable.set(true);
189: log.info(ASTERISKS);
190: log.info("SITE INITIALIZATION COMPLETED (in {} msec)", System.currentTimeMillis() - time);
191: log.info(ASTERISKS);
192: }
193: catch (IOException | NotFoundException | RuntimeException e)
194: {
195: log.error(ASTERISKS);
196: log.error("SITE INITIALIZATION FAILED!", e);
197: log.error(ASTERISKS);
198: }
199: }
200:
201: /***********************************************************************************************************************************************************
202: *
203: **********************************************************************************************************************************************************/
204: @Nonnull
205: /* package */ String getContextPath()
206: {
207: return servletContext.map(ServletContext::getContextPath).orElse(DEFAULT_CONTEXT_PATH);
208: // log.warn("Running in a non-web environment, set contextPath = {}", DEFAULT_CONTEXT_PATH);
209: }
210: }