-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Improve performance / code readability
- Loading branch information
eschleb
committed
Oct 23, 2024
1 parent
05142a4
commit 2c428dc
Showing
6 changed files
with
258 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 80 additions & 52 deletions
132
...in/java/com/namics/oss/magnolia/dictionary/i18nsystem/DictionaryMessageBundlesLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,96 @@ | ||
package com.namics.oss.magnolia.dictionary.i18nsystem; | ||
|
||
import com.namics.oss.magnolia.dictionary.DictionaryConfiguration; | ||
import com.namics.oss.magnolia.dictionary.util.LocaleUtils; | ||
import com.namics.oss.magnolia.dictionary.util.NodeUtil; | ||
import info.magnolia.context.MgnlContext; | ||
import info.magnolia.context.SystemContext; | ||
import info.magnolia.jcr.util.PropertyUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import java.util.HashMap; | ||
import java.util.Collections; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import javax.inject.Provider; | ||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.jcr.observation.EventIterator; | ||
import javax.jcr.observation.EventListener; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import com.machinezoo.noexception.Exceptions; | ||
import com.namics.oss.magnolia.dictionary.DictionaryConfiguration; | ||
import com.namics.oss.magnolia.dictionary.util.LocaleUtils; | ||
import com.namics.oss.magnolia.dictionary.util.NodeUtil; | ||
|
||
@Singleton | ||
public class DictionaryMessageBundlesLoader implements EventListener { | ||
private static final Logger LOG = LoggerFactory.getLogger(DictionaryMessageBundlesLoader.class); | ||
private final Provider<SystemContext> systemContextProvider; | ||
private Map<Locale, Properties> messages = Collections.emptyMap(); | ||
|
||
/** | ||
* @author mrauch, Namics AG | ||
* @since 11.03.2016 | ||
*/ | ||
public class DictionaryMessageBundlesLoader { | ||
@Inject | ||
public DictionaryMessageBundlesLoader(final Provider<SystemContext> systemContextProvider) { | ||
this.systemContextProvider = systemContextProvider; | ||
} | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DictionaryMessageBundlesLoader.class); | ||
public void reload() { | ||
Exceptions.wrap().run(() -> { | ||
final Session session = systemContextProvider.get().getJCRSession(DictionaryConfiguration.REPOSITORY); | ||
loadMessages(session); | ||
session.logout(); | ||
}); | ||
} | ||
|
||
private final Map<Locale, Properties> messages = new HashMap<Locale, Properties>(); | ||
private void loadMessages(final Session session) throws RepositoryException { | ||
final Node root = session.getRootNode(); | ||
messages = LocaleUtils.getLocalesOfAllSiteDefinitions().stream().collect(Collectors.toMap( | ||
Function.identity(), | ||
locale -> getProperties(locale, root)) | ||
); | ||
} | ||
|
||
public DictionaryMessageBundlesLoader() { | ||
loadMessagesInSystemContext(); | ||
} | ||
private Properties getProperties(final Locale locale, final Node root) { | ||
LOG.debug("Loading dictionary properties with locale [{}]...", locale); | ||
final Properties properties = new Properties(); | ||
streamMessages(locale, root).forEach(entry -> | ||
properties.put(entry.getKey(), entry.getValue()) | ||
); | ||
return properties; | ||
} | ||
|
||
protected void loadMessagesInSystemContext() { | ||
try { | ||
MgnlContext.doInSystemContext(new MgnlContext.RepositoryOp() { | ||
@Override | ||
public void doExec() throws RepositoryException { | ||
loadMessages(); | ||
} | ||
}); | ||
} catch (RepositoryException e) { | ||
LOG.warn("An error occurred while trying to to load dictionary properties", e); | ||
} | ||
} | ||
private Stream<Map.Entry<String, String>> streamMessages(final Locale locale, final Node root) { | ||
return StreamSupport | ||
.stream( | ||
Spliterators.spliteratorUnknownSize(Exceptions.wrap().get(() -> NodeUtil.getNodes(root)).iterator(), Spliterator.ORDERED), | ||
false | ||
) | ||
.map(message -> | ||
Optional.ofNullable(PropertyUtil.getString(message, LocaleUtils.getLocaleString(locale))).map(value -> | ||
Map.entry(NodeUtil.getName(message), value) | ||
) | ||
) | ||
.flatMap(Optional::stream); | ||
} | ||
|
||
protected void loadMessages() { | ||
Node root = NodeUtil.getNodeByPathOrNull(DictionaryConfiguration.REPOSITORY, "/"); | ||
for (Locale locale : LocaleUtils.getLocalesOfAllSiteDefinitions()) { | ||
try { | ||
Properties properties = new Properties(); | ||
for (Node message : NodeUtil.asList(NodeUtil.getNodes(root))) { | ||
String key = message.getName(); | ||
String value = PropertyUtil.getString(message, LocaleUtils.getLocaleString(locale)); | ||
if (key != null && value != null) { | ||
properties.put(key, value); | ||
} | ||
} | ||
messages.put(locale, properties); | ||
} catch (RepositoryException e) { | ||
LOG.warn("An error occurred while trying to to load dictionary properties with locale[{}]", locale, e); | ||
} | ||
LOG.debug("Loading dictionary properties with locale [{}]...", locale); | ||
} | ||
} | ||
public Map<Locale, Properties> getMessages() { | ||
return messages; | ||
} | ||
|
||
public Map<Locale, Properties> getMessages() { | ||
return messages; | ||
} | ||
@Override | ||
public void onEvent(EventIterator events) { | ||
if (events.getSize() > 0) { | ||
reload(); | ||
} | ||
} | ||
} |
Oops, something went wrong.