Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#21619 Changes on Config class #22209

Merged
merged 4 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand All @@ -15,6 +16,7 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tuckey.web.filters.urlrewrite.Conf;

public class ConfigTest {

Expand Down Expand Up @@ -261,6 +263,30 @@ public void test_get_string_array_from_env() {
assertEquals(notArray, "VALUE1,VALUE2");
}

/**
* Method to test {@link Config#getBooleanProperty(String, boolean)}
* Here we test that the "same" property seen as an environmental var overrides the other regular property already set in the map
*/
@Test
public void Test_Env_Prop_Overrides_Regular_Prop(){

final String DOT_MY_BOOLEAN_PROPERTY = "DOT_MY_BOOLEAN_PROPERTY";
final String MY_BOOLEAN_PROPERTY = "my.boolean.property";

//Now lets suppose the two properties exits in the Config
Config.setProperty(DOT_MY_BOOLEAN_PROPERTY, false);
Config.setProperty(MY_BOOLEAN_PROPERTY, true);
//But one must take precedence over the other and that's the one that starts with dot.

//if I request the dot prop one should easily expect the value it was initialized with
assertFalse(Config.getBooleanProperty(DOT_MY_BOOLEAN_PROPERTY,true));
//if I request the regular non-dot prop we should still get the value assigned to the dot prop because it overrides it
assertFalse(Config.getBooleanProperty(MY_BOOLEAN_PROPERTY,true));

//The second I get rid of the DOT property now I should get the original regular prop
Config.setProperty(DOT_MY_BOOLEAN_PROPERTY, null);
assertTrue(Config.getBooleanProperty(MY_BOOLEAN_PROPERTY,false));
}

/*
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.dotmarketing.util.VelocityUtil;
import com.google.common.collect.ImmutableSet;
import com.liferay.util.StringPool;
import io.vavr.Lazy;
import io.vavr.control.Try;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -57,7 +58,8 @@

public class ContentTypeFactoryImpl implements ContentTypeFactory {

final ContentTypeSql contentTypeSql;
private static final String LOAD_CONTENTTYPE_DETAILS_FROM_CACHE = "LOAD_CONTENTTYPE_DETAILS_FROM_CACHE";
final ContentTypeSql contentTypeSql;
final ContentTypeCache2 cache;


Expand Down Expand Up @@ -626,7 +628,8 @@ private boolean dbDelete(final ContentType type) throws DotDataException {
return true;
}

final boolean LOAD_FROM_CACHE=Config.getBooleanProperty("LOAD_CONTENTTYPE_DETAILS_FROM_CACHE", true);
final Lazy<Boolean> LOAD_FROM_CACHE=Lazy.of(()->Config.getBooleanProperty(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

LOAD_CONTENTTYPE_DETAILS_FROM_CACHE, true));

private List<ContentType> dbSearch(final String search, final int baseType, String orderBy, int limit, final int offset,final String hostId)
throws DotDataException {
Expand All @@ -647,7 +650,7 @@ private List<ContentType> dbSearch(final String search, final int baseType, Stri
final String hostParam = UtilMethods.isSet(hostId) ? StringPool.PERCENT + hostId + StringPool.PERCENT : StringPool.PERCENT;
DotConnect dc = new DotConnect();

if(LOAD_FROM_CACHE) {
if(LOAD_FROM_CACHE.get()) {
dc.setSQL( String.format( this.contentTypeSql.SELECT_INODE_ONLY_QUERY_CONDITION, SQLUtil.sanitizeCondition( searchCondition.condition ), orderBy ) );
}else {
dc.setSQL( String.format( this.contentTypeSql.SELECT_QUERY_CONDITION, SQLUtil.sanitizeCondition( searchCondition.condition ), orderBy ) );
Expand All @@ -663,7 +666,7 @@ private List<ContentType> dbSearch(final String search, final int baseType, Stri

Logger.debug(this, ()-> "QUERY " + dc.getSQL());

if(LOAD_FROM_CACHE) {
if(LOAD_FROM_CACHE.get()) {
return dc.loadObjectResults()
.stream()
.map(m-> Try.of(()->find((String) m.get("inode")))
Expand Down
7 changes: 4 additions & 3 deletions dotCMS/src/main/java/com/dotmarketing/util/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dotmarketing.util;

import com.google.common.collect.ImmutableList;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -9,6 +10,7 @@
import java.nio.file.Files;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.configuration.Configuration;
Expand Down Expand Up @@ -522,9 +524,8 @@ public static boolean getBooleanProperty (String name) {
* @return
*/
public static boolean getBooleanProperty (String name, boolean defaultVal) {
_refreshProperties ();
Boolean value = Try.of(()->props.getBoolean(envKey(name))).getOrNull();
if(value!=null) {
final Boolean value = props.containsKey(envKey(name)) ? Try.of(()->props.getBoolean(envKey(name))).getOrNull() : null;
if(null != value) {
return value;
}
return props.getBoolean(name, defaultVal);
Expand Down