org.kie.internal.utils.ChainedProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kie-internal Show documentation
Show all versions of kie-internal Show documentation
The Drools and jBPM internal API which is NOT backwards compatible between releases.
/*
* Copyright 2010 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.internal.utils;
import java.io.Externalizable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Priority
*
* - System properties
* - META-INF/ of provided classLoader
*
*
* To improve performance in frequent session creation cases, chained properties can be cached by it's conf file name
* and requesting classloader. To take advantage of the case it must be enabled via system property:
* org.kie.property.cache.enabled
that needs to be set to true
* Cache entries are by default limited to 100 to reduce memory consumption but can be fine tuned by system property:
* org.kie.property.cache.size
that needs to be set to valid integer value
*/
public class ChainedProperties
implements
Externalizable, Cloneable {
private static final String[] PROPERTIES_ALLOWED_PREFIX = new String[] {
"java.", "jdk.", "sun.", "user.", "os.", "jboss.",
"drools.", "jbpm.", "org.uberfire.", "file.", "path.", "line."
};
protected static transient Logger logger = LoggerFactory.getLogger(ChainedProperties.class);
private List props = new ArrayList<>();
private List defaultProps = new ArrayList<>();
public ChainedProperties() {
}
public static ChainedProperties getChainedProperties( ClassLoader classLoader ) {
return getChainedProperties( "properties.conf", classLoader );
}
public static ChainedProperties getChainedProperties( String confFileName, ClassLoader classLoader ) {
return new ChainedProperties( confFileName, classLoader );
}
public ChainedProperties clone() {
ChainedProperties clone = new ChainedProperties();
clone.props.addAll( this.props );
clone.defaultProps.addAll( this.defaultProps );
return clone;
}
private ChainedProperties(String confFileName, ClassLoader classLoader) {
addProperties( System.getProperties() );
loadProperties( "META-INF/kie." + confFileName, classLoader, this.props );
loadProperties( "META-INF/kie.default." + confFileName, classLoader, this.defaultProps);
loadProperties( "META-INF/kie.default.mvel." + confFileName, classLoader, this.defaultProps);
// this happens only in OSGi: for some reason doing
// ClassLoader.getResources() doesn't work but doing
// Class.getResourse() does
if (this.defaultProps.isEmpty()) {
try {
Class> c = Class.forName( "org.drools.core.WorkingMemory", false, classLoader);
URL confURL = c.getResource("/META-INF/kie.default." + confFileName);
loadProperties(confURL, this.defaultProps);
} catch (ClassNotFoundException e) { }
}
}
public void filterDroolsPropertiesForSerialization() {
props = props.stream().map(this::filterDroolsProperties).collect(Collectors.toList());
defaultProps = defaultProps.stream().map(this::filterDroolsProperties).collect(Collectors.toList());
}
private Properties filterDroolsProperties(Properties originalProperties) {
Properties droolsProperties = new Properties();
for (Map.Entry