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