Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * 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.drools.util;
 
 import  org.slf4j.Logger;
 import  org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
Priority System properties, home directory, working directory, META-INF/ of optionally provided classLoader META-INF/ of Thread.currentThread().getContextClassLoader() and META-INF/ of ClassLoader.getSystemClassLoader()
 
 public class ChainedProperties
     implements
     Externalizable {
 
     protected static transient Logger logger = LoggerFactory.getLogger(ChainedProperties.class);
 
     private List<Propertiesprops;
     private List<PropertiesdefaultProps;
 
     public ChainedProperties() {
     }
     
     public ChainedProperties(String confFileNameClassLoader classLoader) {
         thisconfFileName,
               classLoader,
               true );
     }
     
     public ChainedProperties(String confFileName,
                              ClassLoader classLoader,
                              boolean populateDefaults) {
 
         this. = new ArrayList<Properties>();
         this. = new ArrayList<Properties>();
 
         // Properties added in precedence order
 
         // System defined properties always get precedence
         addProperties( System.getProperties() );
 
         // System property defined properties file
         loadProperties( System.getProperty"drools." + confFileName ),
                         this. );
 
         // User home properties file
         loadProperties( System.getProperty"user.home" ) + "/drools." + confFileName,
                         this. );
 
         // Working directory properties file
         loadProperties"drools." + confFileName,
                         this. );
         
 //        if ( classLoader == null ) {
 //            classLoader = Thread.currentThread().getContextClassLoader();
 //            if ( classLoader == null ) {
 //                classLoader = cls.getClassLoader();
 //            }
 //        }
 
         // check META-INF directories for all known ClassLoaders
         ClassLoader confClassLoader = classLoader;
         loadPropertiesgetResources"META-INF/drools." + confFileName,
                                       confClassLoader ),
                         this. );
         loadPropertiesgetResources"/META-INF/drools." + confFileName,
                                       confClassLoader ),
                         this. );
 
         confClassLoader = ClassLoader.getSystemClassLoader();
        if ( confClassLoader != null && confClassLoader != classLoader ) {
            loadPropertiesgetResources"META-INF/drools." + confFileName,
                                          confClassLoader ),
                            this. );
            loadPropertiesgetResources"/META-INF/drools." + confFileName,
                                          confClassLoader ),
                            this. );
        }
        if ( !populateDefaults ) {
            return;
        }
        // load defaults
        confClassLoader = classLoader;
        loadPropertiesgetResources"META-INF/drools.default." + confFileName,
                                      confClassLoader ),
                        this. );
        loadPropertiesgetResources"/META-INF/drools.default." + confFileName,
                                      confClassLoader ),
                        this. );
        confClassLoader = ClassLoader.getSystemClassLoader();
        if ( confClassLoader != null && confClassLoader != classLoader ) {
            loadPropertiesgetResources"META-INF/drools.default." + confFileName,
                                          confClassLoader ),
                            this. );
            loadPropertiesgetResources"/META-INF/drools.default." + confFileName,
                                          confClassLoader ),
                            this. );
        }
    }
    @SuppressWarnings("unchecked")
    public void readExternal(ObjectInput inthrows IOException,
                                            ClassNotFoundException {
         = (List<Properties>) in.readObject();
         = (List<Properties>) in.readObject();
    }
    public void writeExternal(ObjectOutput outthrows IOException {
        out.writeObject );
        out.writeObject );
    }
    private Enumeration<URLgetResources(String name,
                                          ClassLoader classLoader) {
        Enumeration<URLenumeration = null;
        try {
            enumeration = classLoader.getResourcesname );
        } catch ( IOException e ) {
            .error("error"e);
        }
        return enumeration;
    }

    
Specifically added properties take priority, so they go to the front of the list.

Parameters:
properties
    public void addProperties(Properties properties) {
        this..add( 0, properties );
    }
    public String getProperty(String key,
                              String defaultValue) {
        String value = null;
        for ( Properties props : this. ) {
            value = props.getPropertykey );
            if ( value != null ) {
                break;
            }
        }
        if ( value == null ) {
            for ( Properties props : this. ) {
                value = props.getPropertykey );
                if ( value != null ) {
                    break;
                }
            }
        }
        return (value != null) ? value : defaultValue;
    }
    public void mapStartsWith(Map<StringStringmap,
                              String startsWith,
                              boolean includeSubProperties) {
        for ( Properties props : this. ) {
            mapStartsWithmap,
                           props,
                           startsWith,
                           includeSubProperties );
        }
        for ( Properties props : this. ) {
            mapStartsWithmap,
                           props,
                           startsWith,
                           includeSubProperties );
        }
    }
    private void mapStartsWith(Map<StringStringmap,
                               Properties properties,
                               String startsWith,
                               boolean includeSubProperties) {
        Enumeration< ? > enumeration = properties.propertyNames();
        while ( enumeration.hasMoreElements() ) {
            String key = (Stringenumeration.nextElement();
            if ( key.startsWithstartsWith ) ) {
                if ( !includeSubProperties && key.substringstartsWith.length() + 1 ).indexOf'.' ) > 0 ) {
                    // +1 to the length, as we do allow the direct property, just not ones below it
                    // This key has sub properties beyond the given startsWith, so skip
                    continue;
                }
                if ( !map.containsKeykey ) ) {
                    map.putkey,
                             properties.getPropertykey ) );
                }
            }
        }
    }
    private void loadProperties(Enumeration<URLenumeration,
                                List<Propertieschain) {
        if ( enumeration == null ) {
            return;
        }
        while ( enumeration.hasMoreElements() ) {
            URL url = (URLenumeration.nextElement();
            loadPropertiesurl,
                            chain );
        }
    }
    private void loadProperties(String fileName,
                                List<Propertieschain) {
        if ( fileName != null ) {
            File file = new FilefileName );
            if ( file != null && file.exists() ) {
                try {
                    loadPropertiesfile.toURL(),
                                    chain );
                } catch ( MalformedURLException e ) {
                    throw new IllegalArgumentException"file.toURL() failed for " + fileName + " properties value '" + file + "'" );
                }
            } else {
                //throw new IllegalArgumentException( fileName + " is specified but cannot be found '" + file + "'" );
            }
        }
    }
    private void loadProperties(URL confURL,
                                List<Propertieschain) {
        if ( confURL == null ) {
            return;
        }
        Properties properties = new Properties();
        try {
            java.io.InputStream is = confURL.openStream();
            properties.loadis );
            is.close();
            chain.addproperties );
        } catch ( IOException e ) {
            //throw new IllegalArgumentException( "Invalid URL to properties file '" + confURL.toExternalForm() + "'" );
        }
    }
New to GrepCode? Check out our FAQ X