Skip to content

Package: DefaultResource

DefaultResource

nameinstructionbranchcomplexitylinemethod
DefaultResource(Resource.Builder)
M: 89 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getProperties()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
loadProperties()
M: 132 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 23 C: 0
0%
M: 1 C: 0
0%
logProperties(String, ResourceProperties)
M: 65 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 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: * *************************************************************************************************************************************************************
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 java.util.HashMap;
32: import java.util.Locale;
33: import java.util.Map;
34: import java.io.IOException;
35: import org.springframework.beans.factory.annotation.Configurable;
36: import it.tidalwave.northernwind.core.model.RequestLocaleManager;
37: import it.tidalwave.northernwind.core.model.Resource;
38: import it.tidalwave.northernwind.core.model.ResourceProperties;
39: import it.tidalwave.northernwind.core.model.spi.ResourceSupport;
40: import lombok.Cleanup;
41: import lombok.Getter;
42: import lombok.ToString;
43: import lombok.extern.slf4j.Slf4j;
44: import static it.tidalwave.role.io.Unmarshallable._Unmarshallable_;
45:
46: /***************************************************************************************************************************************************************
47: *
48: * The default implementation for {@link Resource}.
49: *
50: * @author Fabrizio Giudici
51: *
52: **************************************************************************************************************************************************************/
53: @Configurable @Slf4j @ToString(callSuper = true, of = "placeHolder")
54: /* package */ class DefaultResource extends ResourceSupport
55: {
56: @Inject
57: private InheritanceHelper inheritanceHelper;
58:
59: @Inject
60: private RequestLocaleManager localeRequestManager;
61:
62: private final Map<Locale, ResourceProperties> propertyMapByLocale = new HashMap<>();
63:
64: @Getter
65: private boolean placeHolder;
66:
67: private final ResourceProperties.PropertyResolver propertyResolver;
68:
69: /***********************************************************************************************************************************************************
70: *
71: **********************************************************************************************************************************************************/
72: public DefaultResource (@Nonnull final Resource.Builder builder)
73: {
74:• super(builder);
75: propertyResolver = new TextResourcePropertyResolver(getFile());
76:• }
77:
78: /***********************************************************************************************************************************************************
79: * {@inheritDoc}
80: **********************************************************************************************************************************************************/
81: @Override @Nonnull
82: public ResourceProperties getProperties()
83: {
84: return propertyMapByLocale.get(localeRequestManager.getLocales().get(0));
85: }
86:
87: /***********************************************************************************************************************************************************
88: *
89: **********************************************************************************************************************************************************/
90: @PostConstruct
91: /* package */ void loadProperties()
92: throws IOException
93: {
94: final var file = getFile();
95: log.debug("loadProperties() for {}", file.getPath().asString());
96:
97: var tmpPlaceHolder = true;
98:
99:• for (final var locale : localeRequestManager.getLocales())
100: {
101: var properties = modelFactory.createProperties().withPropertyResolver(propertyResolver).build();
102:
103:• if (file.isData())
104: {
105: tmpPlaceHolder = false;
106: }
107: else
108: {
109:• for (final var propertyFile : inheritanceHelper.getInheritedPropertyFiles(file, locale, "Properties"))
110: {
111: log.trace(">>>> reading properties from {} ({})...", propertyFile.getPath().asString(), locale);
112:• @Cleanup final var is = propertyFile.getInputStream();
113: final ResourceProperties tempProperties =
114: modelFactory.createProperties().build().as(_Unmarshallable_).unmarshal(is);
115: // modelFactory.createProperties().withPropertyResolver(propertyResolver).build().as(_Unmarshallable_).unmarshal(is);
116: log.trace(">>>>>>>> read properties: {} ({})", tempProperties, locale);
117: properties = properties.merged(tempProperties);
118:• tmpPlaceHolder &= !propertyFile.getParent().equals(file);
119: }
120: }
121:
122: placeHolder = properties.getProperty(P_PLACE_HOLDER).orElse(tmpPlaceHolder);
123:
124:• if (log.isDebugEnabled())
125: {
126: log.debug(">>>> properties for {} ({}):", file.getPath().asString(), locale);
127: logProperties(">>>>>>>>", properties);
128: }
129:
130: propertyMapByLocale.put(locale, properties);
131: }
132: }
133:
134: /***********************************************************************************************************************************************************
135: * FIXME: move to ResourceProperties!
136: **********************************************************************************************************************************************************/
137: private static void logProperties (@Nonnull final String indent,
138: @Nonnull final ResourceProperties properties)
139: {
140: log.debug("{} property items:", indent);
141:
142:• for (final var key : properties.getKeys())
143: {
144: log.debug("{}>>>> {} = {}", indent, key, properties.getProperty(key));
145: }
146:
147: log.debug("{} property groups: {}", indent, properties.getGroupIds());
148:
149:• for (final var groupId : properties.getGroupIds())
150: {
151: log.debug("{}>>>> group: {}", indent, groupId);
152: logProperties(indent + ">>>>", properties.getGroup(groupId));
153: }
154: }
155: }