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