Package: ZipFileSystemProvider
ZipFileSystemProvider
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ZipFileSystemProvider() |
|
|
|
|
|
||||||||||||||||||||
getFileSystem() |
|
|
|
|
|
||||||||||||||||||||
getModificationCheckInterval() |
|
|
|
|
|
||||||||||||||||||||
getZipFilePath() |
|
|
|
|
|
||||||||||||||||||||
setModificationCheckInterval(long) |
|
|
|
|
|
||||||||||||||||||||
setZipFilePath(String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
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.Nullable;
31: import javax.inject.Inject;
32: import javax.inject.Named;
33: import java.time.Instant;
34: import java.time.ZoneId;
35: import java.time.ZonedDateTime;
36: import java.util.Timer;
37: import java.util.TimerTask;
38: import java.io.File;
39: import java.io.FileNotFoundException;
40: import java.io.IOException;
41: import org.openide.filesystems.JarFileSystem;
42: import it.tidalwave.messagebus.MessageBus;
43: import it.tidalwave.northernwind.core.model.ResourceFileSystem;
44: import it.tidalwave.northernwind.core.model.ResourceFileSystemChangedEvent;
45: import it.tidalwave.northernwind.core.model.ResourceFileSystemProvider;
46: import it.tidalwave.northernwind.frontend.filesystem.impl.ResourceFileSystemNetBeansPlatform;
47: import lombok.Getter;
48: import lombok.Setter;
49: import lombok.ToString;
50: import lombok.extern.slf4j.Slf4j;
51:
52: /***********************************************************************************************************************
53: *
54: * A provider for a local {@link ResourceFileSystemProvider}.
55: *
56: * @author Fabrizio Giudici
57: *
58: **********************************************************************************************************************/
59: @Slf4j @ToString(of = {"zipFilePath", "latestModified", "changeWasDetected"})
60: public class ZipFileSystemProvider implements ResourceFileSystemProvider
61: {
62: @Getter @Setter @Nonnull
63: private String zipFilePath = "";
64:
65: @Getter @Setter
66: private long modificationCheckInterval = 5000;
67:
68: @Nullable
69: private ResourceFileSystem fileSystem;
70:
71: @Nullable
72: private JarFileSystem fileSystemDelegate;
73:
74: private ZonedDateTime latestModified;
75:
76: @Inject @Named("applicationMessageBus")
77: private MessageBus messageBus;
78:
79: private final Timer timer = new Timer("ZipFileSystemProvider.modificationTracker");
80:
81: private boolean changeWasDetected;
82:
83: /*******************************************************************************************************************
84: *
85: *
86: ******************************************************************************************************************/
87: private final TimerTask zipFileModificationTracker = new TimerTask()
88: {
89: @Override
90: public void run()
91: {
92: try
93: {
94: getFileSystem(); // force initialization
95: final var zipFile = fileSystemDelegate.getJarFile();
96: final var timestamp = Instant.ofEpochMilli(zipFile.lastModified()).atZone(ZoneId.of("GMT"));
97: // log.debug(">>>> checking zip file latest modification: was {}, is now {}",
98: // latestModified, timestamp);
99:
100: if (!changeWasDetected)
101: {
102: if (timestamp.isAfter(latestModified))
103: {
104: latestModified = timestamp;
105: changeWasDetected = true;
106: log.info("Detected change of {}: last modified time: {} - waiting for it to become stable",
107: zipFile,
108: latestModified);
109: }
110: }
111: else
112: {
113: if (timestamp.isAfter(latestModified))
114: {
115: latestModified = timestamp;
116: log.info(
117: "Detected unstable change of {}: last modified time: {} - waiting for it to become " +
118: "stable",
119: zipFile,
120: latestModified);
121: }
122: else
123: {
124: latestModified = timestamp;
125: changeWasDetected = false;
126: log.info("Detected stable change of {}: last modified time: {}", zipFile, latestModified);
127: messageBus.publish(new ResourceFileSystemChangedEvent(ZipFileSystemProvider.this,
128: latestModified));
129: }
130: }
131: }
132: catch (IOException e)
133: {
134: log.error("Cannot check changes on zip file system", e);
135: }
136: }
137: };
138:
139: /*******************************************************************************************************************
140: *
141: * {@inheritDoc}
142: *
143: ******************************************************************************************************************/
144: @Override @Nonnull
145: public synchronized ResourceFileSystem getFileSystem()
146: throws IOException
147: {
148:• if (fileSystem == null)
149: {
150: final var zipFile = new File(zipFilePath);
151: fileSystemDelegate = new JarFileSystem(zipFile);
152: final var rootFolder = fileSystemDelegate.getRoot();
153:
154:• if (rootFolder == null)
155: {
156: throw new FileNotFoundException(zipFilePath);
157: }
158:
159: log.info(">>>> fileSystem: {}", fileSystemDelegate);
160: latestModified = Instant.ofEpochMilli(zipFile.lastModified()).atZone(ZoneId.of("GMT"));
161: timer.scheduleAtFixedRate(zipFileModificationTracker, modificationCheckInterval, modificationCheckInterval);
162: fileSystem = new ResourceFileSystemNetBeansPlatform(fileSystemDelegate);
163: }
164:
165: return fileSystem;
166: }
167: }