Skip to content

Commit

Permalink
KNL-1586 Remove icu4j lib and use java 8 time (sakaiproject#5357)
Browse files Browse the repository at this point in the history
- cleaned up logging in StoredConfigService
  • Loading branch information
ern authored Feb 28, 2018
1 parent 7874376 commit 1ce4085
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 58 deletions.
7 changes: 0 additions & 7 deletions kernel/kernel-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,6 @@
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>4.8.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

<!-- antisamy -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@

package org.sakaiproject.config.impl;

import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.Calendar;

import java.util.*;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -75,6 +80,7 @@ public class StoredConfigService implements ConfigurationListener, Configuration
public static final String SAKAI_CONFIG_USE_RAW = "sakai.config.use.raw";
// config that should never be persisted
public static final String SAKAI_CONFIG_NEVER_PERSIST = "sakai.config.never.persist";
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");

private ScheduledExecutorService scheduler;

Expand All @@ -86,11 +92,9 @@ public class StoredConfigService implements ConfigurationListener, Configuration
private String node;

public void init() {
log.info("init()");

node = serverConfigurationService.getServerId();
if (StringUtils.isBlank(node)) {
log.error("init(); node cannot be blank, StoredConfigService is disabled");
log.error("node cannot be blank, StoredConfigService is disabled");
return;
}

Expand All @@ -99,15 +103,15 @@ public void init() {
List<String> tmpdoNotPersist;
if (doNotPersistConfig == null) {
// SCS can return a null here if config is not found
tmpdoNotPersist = new ArrayList<String>();
tmpdoNotPersist = new ArrayList<>();
} else {
tmpdoNotPersist = Arrays.asList(doNotPersistConfig);
}
// always add [email protected]
tmpdoNotPersist.add("[email protected]");
// TODO add more stuff here like serverId, DB password
// Setup list of items we should never persist
neverPersistItems = Collections.unmodifiableSet(new HashSet<String>(tmpdoNotPersist));
neverPersistItems = Collections.unmodifiableSet(new HashSet<>(tmpdoNotPersist));

// delete items that should never persisted
for (String item : neverPersistItems) {
Expand All @@ -127,7 +131,7 @@ public void init() {
// schedule task for every pollDelaySeconds
scheduler.scheduleWithFixedDelay(
new Runnable() {
Date pollDate;
ZonedDateTime pollDate;
@Override
public void run() {
pollDate = storedConfigPoller(pollDelaySeconds, pollDate);
Expand All @@ -136,7 +140,7 @@ public void run() {
pollDelaySeconds < 120 ? 120 : pollDelaySeconds, // minimally wait 120 seconds for sakai to start
pollDelaySeconds, TimeUnit.SECONDS
);
log.info("init() " + SAKAI_CONFIG_POLL_ENABLE + " is enabled and polling every " + pollDelaySeconds + " seconds");
log.info("{} is enabled and polling every {} seconds", SAKAI_CONFIG_POLL_ENABLE, pollDelaySeconds);
}
}

Expand All @@ -147,14 +151,13 @@ public void destroy() {
}
}

private Date storedConfigPoller(int delay, Date then) {
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
private ZonedDateTime storedConfigPoller(int delay, ZonedDateTime then) {
ZonedDateTime now = ZonedDateTime.now();

if (then == null) {
// on start set then
calendar.add(Calendar.SECOND, -(delay));
then = calendar.getTime();
now.minusSeconds(delay);
then = now;
}

List<HibernateConfigItem> polled = findPollOn(then, now);
Expand All @@ -169,13 +172,15 @@ private Date storedConfigPoller(int delay, Date then) {
registered++;
}
} else {
log.warn("storedConfigPoller() item " + item.getName() + " is not registered skipping");
log.warn("Item {} is not registered skipping", item.getName());
}
}
if (log.isDebugEnabled()) {
log.debug("storedConfigPoller() Polling found " + polled.size() + " config item(s) (from " +
new SimpleDateFormat("HH:mm:ss").format(then) + " to " +
new SimpleDateFormat("HH:mm:ss").format(now) + "), " + registered + " item(s) registered");
log.debug("storedConfigPoller() Polling found {} config item(s) (from {} to {}), {} item(s) registered",
polled.size(),
dtf.format(then),
dtf.format(now),
registered);
}

return now;
Expand Down Expand Up @@ -225,7 +230,7 @@ private void learnConfig(List<ConfigItem> items) {

saveOrUpdate(hItem);
}
log.info("initItems() processed " + total + " config items, updated " + updated + " created " + created);
log.info("processed {} config items, updated {} created {}", total, updated, created);
}

/**
Expand All @@ -243,13 +248,13 @@ public void saveOrUpdate(HibernateConfigItem hItem) {
String type = hItem.getType();

if (name == null || name.isEmpty()) {
log.warn("saveOrUpdate() item name is missing");
log.warn("item name is missing");
return;
} else if (!(ServerConfigurationService.TYPE_STRING.equals(type)
|| ServerConfigurationService.TYPE_INT.equals(type)
|| ServerConfigurationService.TYPE_BOOLEAN.equals(type)
|| ServerConfigurationService.TYPE_ARRAY.equals(type))) {
log.warn("saveOrUpdate() item type is incorrect");
log.warn("item type is incorrect");
return;
}

Expand All @@ -269,9 +274,7 @@ public List<ConfigItem> getConfigItems() {
ConfigItem item = createConfigItem(hItem);
if (item != null) {
configItems.add(item);
if (log.isDebugEnabled()) {
log.debug("getConfigItems() " + item.toString());
}
log.debug("{}", item);
}
}

Expand Down Expand Up @@ -317,9 +320,7 @@ public ConfigItem createConfigItem(HibernateConfigItem hItem) throws IllegalClas
hItem.isDynamic()
);

if (log.isDebugEnabled()) {
log.debug("createConfigItem() " + item.toString());
}
log.debug("{}", item);

return item;
}
Expand All @@ -336,9 +337,7 @@ public HibernateConfigItem createHibernateConfigItem(ConfigItem item) throws Ill
return null;
}

if (log.isDebugEnabled()) {
log.debug("createHibernateConfigItem() New ConfigItem = " + item.toString());
}
log.debug("New ConfigItem = {}", item);

String serialValue;
String serialDefaultValue;
Expand All @@ -349,7 +348,7 @@ public HibernateConfigItem createHibernateConfigItem(ConfigItem item) throws Ill
serialDefaultValue = serializeValue(item.getDefaultValue(), item.getType(), item.isSecured());
serialRawValue = serializeValue(getRawProperty(item.getName()), ServerConfigurationService.TYPE_STRING, item.isSecured());
} catch (IllegalClassException ice) {
log.error("createHibernateConfigItem() IllegalClassException " + ice.getMessage() + " skip ConfigItem " + item.toString(), ice);
log.error("Skip ConfigItem {}, {}", item, ice.getMessage());
return null;
}

Expand All @@ -366,9 +365,7 @@ public HibernateConfigItem createHibernateConfigItem(ConfigItem item) throws Ill
item.isSecured(),
item.isDynamic());

if (log.isDebugEnabled()) {
log.debug("createHibernateConfigItem() Created HibernateConfigItem = " + hItem.toString());
}
log.debug("Created HibernateConfigItem = {}", hItem);

return hItem;
}
Expand All @@ -391,9 +388,7 @@ public HibernateConfigItem updateHibernateConfigItem(HibernateConfigItem hItem,
// check if updating is needed, update it
if (!hItem.similar(item)) {
// if they are not similar update it
if (log.isDebugEnabled()) {
log.debug("updateHibernateConfigItem() Before " + hItem.toString());
}
log.debug("Before = {}", hItem);

Object value = deSerializeValue(hItem.getValue(), hItem.getType(), hItem.isSecured());
Object defaultValue = deSerializeValue(hItem.getDefaultValue(), hItem.getType(), hItem.isSecured());
Expand All @@ -419,7 +414,7 @@ public HibernateConfigItem updateHibernateConfigItem(HibernateConfigItem hItem,
hItem.setDefaultValue(serializeValue(item.getDefaultValue(), item.getType(), item.isSecured()));
}
} catch (IllegalClassException ice) {
log.error("updateHibernateConfigItem() IllegalClassException " + ice.getMessage() + " skip ConfigItem " + item.toString(), ice);
log.error("Skip ConfigItem = {}, {}", item, ice.getMessage());
return null;
}

Expand All @@ -431,11 +426,9 @@ public HibernateConfigItem updateHibernateConfigItem(HibernateConfigItem hItem,
hItem.setSource(item.getSource());
hItem.setDescription(item.getDescription());
hItem.setDynamic(item.isDynamic());
hItem.setModified(Calendar.getInstance().getTime());
hItem.setModified(new Date());

if (log.isDebugEnabled()) {
log.debug("updateHibernateConfigItem() After " + hItem.toString());
}
log.debug("After = {}", hItem);

updatedItem = hItem;
}
Expand Down Expand Up @@ -466,7 +459,7 @@ public void deleteHibernateConfigItem(String name) {

HibernateConfigItem hItem = findByName(name);
if (hItem != null) {
log.info("deleteHibernateConfigItem() delete HibernateConfigItem = " + hItem);
log.info("Delete HibernateConfigItem = {}", hItem);
dao.delete(hItem);
}
}
Expand Down Expand Up @@ -598,8 +591,8 @@ public List<HibernateConfigItem> findDefaulted() {
* @param before select items before this timestamp
* @return a List of HibernateConfigItem(s)
*/
public List<HibernateConfigItem> findPollOn(Date after, Date before) {
return dao.findPollOnByNode(node, after, before);
public List<HibernateConfigItem> findPollOn(ZonedDateTime after, ZonedDateTime before) {
return dao.findPollOnByNode(node, Date.from(after.toInstant()), Date.from(before.toInstant()));
}

private String getRawProperty(String name) {
Expand Down Expand Up @@ -627,7 +620,7 @@ private Object deSerializeValue(String value, String type, boolean secured) thro
if (Base64.isBase64(value)) {
string = textEncryptor.decrypt(value);
} else {
log.warn("deSerializeValue() Invalid value found attempting to decrypt a secured property, check your secured properties");
log.warn("Invalid value found attempting to decrypt a secured property, check your secured properties");
string = value;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

package org.sakaiproject.content.impl;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
Expand Down Expand Up @@ -79,6 +76,8 @@
import org.apache.tika.detect.Detector;
import org.apache.tika.mime.MimeTypes;

import org.apache.tika.parser.txt.CharsetDetector;
import org.apache.tika.parser.txt.CharsetMatch;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down

0 comments on commit 1ce4085

Please sign in to comment.