Skip to content

Package: LayeredResourceFileSystem

LayeredResourceFileSystem

nameinstructionbranchcomplexitylinemethod
createDecoratorFile(ResourceFile)
M: 2 C: 37
95%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 8
89%
M: 0 C: 1
100%
findFileByPath(String)
M: 19 C: 47
71%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 6 C: 13
68%
M: 0 C: 1
100%
getRoot()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.layered;
27:
28: import javax.annotation.CheckForNull;
29: import javax.annotation.Nonnull;
30: import java.util.IdentityHashMap;
31: import java.util.List;
32: import java.util.ListIterator;
33: import java.io.IOException;
34: import it.tidalwave.util.As;
35: import it.tidalwave.northernwind.core.model.ResourceFile;
36: import it.tidalwave.northernwind.core.model.ResourceFileSystemProvider;
37: import it.tidalwave.northernwind.core.model.spi.DecoratedResourceFileSystem;
38: import it.tidalwave.northernwind.frontend.filesystem.basic.FileSystemProvidersProvider;
39: import lombok.RequiredArgsConstructor;
40: import lombok.experimental.Delegate;
41: import lombok.extern.slf4j.Slf4j;
42:
43: /***************************************************************************************************************************************************************
44: *
45: * @author Fabrizio Giudici
46: *
47: **************************************************************************************************************************************************************/
48: @RequiredArgsConstructor @Slf4j
49: public class LayeredResourceFileSystem implements DecoratedResourceFileSystem
50: {
51: @Nonnull
52: private final List<ResourceFileSystemProvider> delegates;
53:
54: private final FileSystemProvidersProvider fileSystemProvidersProvider;
55:
56: private final IdentityHashMap<ResourceFile, ResourceFile> delegateLightWeightMap = new IdentityHashMap<>();
57:
58: @Delegate
59: private final As asSupport = As.forObject(this);
60:
61: /***********************************************************************************************************************************************************
62: * {@inheritDoc}
63: **********************************************************************************************************************************************************/
64: @Override @Nonnull
65: public ResourceFile getRoot()
66: {
67: return findFileByPath("");
68: }
69:
70: /***********************************************************************************************************************************************************
71: * {@inheritDoc}
72: **********************************************************************************************************************************************************/
73: @Override @CheckForNull
74: public ResourceFile findFileByPath (@Nonnull final String name)
75: {
76: log.trace("findResource({})", name);
77: ResourceFile result = null;
78:
79: // FIXME: move to init!
80:• if (fileSystemProvidersProvider != null)
81: {
82: delegates.clear();
83: final var fileSystemProviders = fileSystemProvidersProvider.getFileSystemProviders();
84: delegates.addAll(fileSystemProviders);
85: }
86:
87: for (final ListIterator<? extends ResourceFileSystemProvider> i = delegates.listIterator(delegates.size());
88:• i.hasPrevious(); )
89: {
90: try
91: {
92: final var fileSystem = i.previous().getFileSystem();
93: final var fileObject = fileSystem.findFileByPath(name);
94:
95:• if (fileObject != null)
96: {
97: log.trace(">>>> fileSystem: {}, fileObject: {}", fileSystem, fileObject.getPath());
98: result = createDecoratorFile(fileObject);
99: break;
100: }
101: }
102: catch (IOException e)
103: {
104: log.warn("", e);
105: }
106: }
107:
108: log.trace(">>>> returning {}", result);
109:
110: return result;
111: }
112:
113: /***********************************************************************************************************************************************************
114: * {@inheritDoc}
115: **********************************************************************************************************************************************************/
116: @Override @Nonnull
117: public synchronized ResourceFile createDecoratorFile (@Nonnull final ResourceFile delegateFile)
118: {
119:• if (delegateFile == null)
120: {
121: return null;
122: }
123:
124: var decorator = delegateLightWeightMap.get(delegateFile);
125:
126:• if (decorator == null)
127: {
128:• decorator = (delegateFile.isData() ? new DecoratorResourceFile(this, delegateFile)
129: : new DecoratorResourceFolder(this,
130: delegates,
131: delegateFile.getPath(),
132: delegateFile));
133: delegateLightWeightMap.put(delegateFile, decorator);
134: }
135:
136: return decorator;
137: }
138: }