Skip to content

Package: LocalCopyFileSystemProvider$1

LocalCopyFileSystemProvider$1

nameinstructionbranchcomplexitylinemethod
notify(ResourceFileSystemChangedEvent)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.filesystem.basic;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.PostConstruct;
31: import javax.inject.Inject;
32: import javax.inject.Named;
33: import java.time.ZonedDateTime;
34: import java.io.File;
35: import java.io.IOException;
36: import it.tidalwave.messagebus.MessageBus;
37: import it.tidalwave.messagebus.MessageBus.Listener;
38: import it.tidalwave.northernwind.core.model.ResourceFile;
39: import it.tidalwave.northernwind.core.model.ResourceFileSystem;
40: import it.tidalwave.northernwind.core.model.ResourceFileSystemChangedEvent;
41: import it.tidalwave.northernwind.core.model.ResourceFileSystemProvider;
42: import lombok.Getter;
43: import lombok.Setter;
44: import lombok.ToString;
45: import lombok.extern.slf4j.Slf4j;
46:
47: /***********************************************************************************************************************
48: *
49: * A provider for the {@link ResourceFileSystemProvider} that clones a source provider into a local
50: * {@code ResourceFileSystemProvider} for performance purposes...
51: *
52: * @author Fabrizio Giudici
53: *
54: **********************************************************************************************************************/
55: @Slf4j @ToString(of = "rootPath")
56: public class LocalCopyFileSystemProvider implements ResourceFileSystemProvider
57: {
58: @Getter @Setter @Nonnull
59: private ResourceFileSystemProvider sourceProvider;
60:
61: @Getter @Setter @Nonnull
62: private String rootPath = "";
63:
64: private LocalFileSystemProvider targetProvider = new LocalFileSystemProvider();
65:
66: @Inject @Named("applicationMessageBus")
67: private MessageBus messageBus;
68:
69: /*******************************************************************************************************************
70: *
71: *
72: ******************************************************************************************************************/
73: private final Listener<ResourceFileSystemChangedEvent> sourceProviderChangeListener =
74: new Listener<>()
75: {
76: @Override
77: public void notify (@Nonnull final ResourceFileSystemChangedEvent event)
78: {
79:• if (event.getFileSystemProvider() == sourceProvider)
80: {
81: try
82: {
83: log.info("Detected file change, regenerating local file system...");
84: generateLocalFileSystem();
85: messageBus.publish(new ResourceFileSystemChangedEvent(LocalCopyFileSystemProvider.this,
86: ZonedDateTime.now()));
87: }
88: catch (IOException e)
89: {
90: log.error("While resetting site: ", e);
91: }
92: }
93: }
94: };
95:
96: /*******************************************************************************************************************
97: *
98: * {@inheritDoc}
99: *
100: ******************************************************************************************************************/
101: @Override @Nonnull
102: public synchronized ResourceFileSystem getFileSystem()
103: throws IOException
104: {
105: return targetProvider.getFileSystem();
106: }
107:
108: /*******************************************************************************************************************
109: *
110: *
111: ******************************************************************************************************************/
112: @PostConstruct
113: /* package */ void initialize()
114: throws IOException
115: {
116: log.info("initialize()");
117: generateLocalFileSystem();
118: messageBus.subscribe(ResourceFileSystemChangedEvent.class, sourceProviderChangeListener);
119: }
120:
121: /*******************************************************************************************************************
122: *
123: *
124: ******************************************************************************************************************/
125: private void generateLocalFileSystem()
126: throws IOException
127: {
128: log.info("generateLocalFileSystem()");
129:
130: if (!new File(rootPath).mkdirs()) // TODO: use NwFileSystem API
131: {
132: throw new IOException("Cannot create dirs for " + rootPath);
133: }
134:
135: // FIXME: shouldn't be needed, but otherwise after a second call to this method won't find files
136: targetProvider = new LocalFileSystemProvider();
137: targetProvider.setRootPath(rootPath);
138: final var targetRoot = targetProvider.getFileSystem().getRoot();
139: final var path = targetRoot.toFile().getAbsolutePath();
140: log.info(">>>> scratching {} ...", path);
141: emptyFolder(targetRoot);
142: log.info(">>>> copying files to {} ...", path);
143: copyFolder(sourceProvider.getFileSystem().getRoot(), targetRoot);
144: // targetProvider.getFileSystem().refresh(true);
145: }
146:
147: /*******************************************************************************************************************
148: *
149: *
150: ******************************************************************************************************************/
151: private static void emptyFolder (@Nonnull final ResourceFile folder)
152: throws IOException
153: {
154: log.trace("emptyFolder({}", folder);
155:
156: for (final var child : folder.findChildren().results())
157: {
158: child.delete();
159: }
160: }
161:
162: /*******************************************************************************************************************
163: *
164: *
165: ******************************************************************************************************************/
166: private static void copyFolder (@Nonnull final ResourceFile sourceFolder, @Nonnull final ResourceFile targetFolder)
167: throws IOException
168: {
169: log.trace("copyFolder({}, {}", sourceFolder, targetFolder);
170:
171: for (final var sourceChild : sourceFolder.findChildren().results())
172: {
173: if (!sourceChild.isFolder())
174: {
175: log.trace(">>>> copying {} into {} ...", sourceChild, targetFolder);
176: sourceChild.copyTo(targetFolder);
177: }
178: }
179:
180: for (final var sourceChild : sourceFolder.findChildren().results())
181: {
182: if (sourceChild.isFolder())
183: {
184: copyFolder(sourceChild, targetFolder.createFolder(sourceChild.getName()));
185: }
186: }
187: }
188: }