001package org.andromda.andromdapp; 002 003import java.io.InputStream; 004import java.net.URL; 005import java.util.ArrayList; 006import java.util.LinkedHashMap; 007import java.util.List; 008import java.util.Map; 009import java.util.Properties; 010import org.andromda.core.common.ResourceUtils; 011import org.apache.commons.io.IOUtils; 012 013/** 014 * Represents the configuration of an AndroMDAppType. 015 * 016 * @author Chad Brandon 017 * @see AndroMDAppType 018 */ 019public class Configuration 020{ 021 /** 022 * Stores any properties defined in this configuration. 023 */ 024 private final Properties properties = new Properties(); 025 026 /** 027 * Adds a property with the name and value to the current properties 028 * map. 029 * 030 * @param name the name of the property to add. 031 * @param value the value of the property. 032 */ 033 public void addProperty( 034 final String name, 035 final String value) 036 { 037 this.properties.put( 038 name, 039 value); 040 } 041 042 /** 043 * Stores any locations to property files. 044 */ 045 private final List<String> locations = new ArrayList<String>(); 046 047 /** 048 * Adds a location to this configuration. 049 * 050 * @param location the path of the location. 051 */ 052 public void addLocation(final String location) 053 { 054 this.locations.add(location); 055 } 056 057 /** 058 * The patterns to use for the locations 059 */ 060 private static final String[] LOCATION_PATTERNS = new String[] {"**/*.properties"}; 061 062 /** 063 * Retrieves all properties including all those found in the given locations. 064 * 065 * @return the map containing all properties 066 */ 067 public Map<String,String> getAllProperties() 068 { 069 final Map<String, String> allProperties = new LinkedHashMap<String, String>(); 070 for (final String location : this.locations) 071 { 072 final List<String> resources = 073 ResourceUtils.getDirectoryContents( 074 ResourceUtils.toURL(location), 075 true, 076 LOCATION_PATTERNS); 077 if (resources != null) 078 { 079 for (final String path : resources) 080 { 081 final URL resource = ResourceUtils.toURL(path); 082 final Properties properties = new Properties(); 083 InputStream stream = null; 084 try 085 { 086 stream = resource.openStream(); 087 properties.load(stream); 088 addProperties(allProperties, properties); 089 } 090 catch (final Exception ignore) 091 { 092 // - ignore 093 } 094 finally 095 { 096 IOUtils.closeQuietly(stream); 097 } 098 } 099 } 100 } 101 addProperties(allProperties, this.properties); 102 return allProperties; 103 } 104 105 /** 106 * @param target 107 * @param properties 108 */ 109 protected void addProperties(final Map<String, String> target, final Properties properties) 110 { 111 for (final String propertyName : properties.stringPropertyNames()) 112 { 113 target.put(propertyName, properties.getProperty(propertyName)); 114 } 115 } 116 117 /** 118 * Stores whether or not the application should be overwritten if it previously existed. 119 */ 120 private boolean overwrite; 121 122 /** 123 * Whether or not the application should be overwritten if it already exits. 124 * 125 * @return true/false 126 */ 127 public boolean isOverwrite() 128 { 129 return this.overwrite; 130 } 131 /** 132 * Sets whether or not the application should be overwritten if it previously existed. 133 * 134 * @param overwrite true/false 135 */ 136 public void setOverwrite(final boolean overwrite) 137 { 138 this.overwrite = overwrite; 139 } 140}