1 5 package org.exoplatform.container.configuration; 6 7 import java.io.InputStream ; 8 import java.net.URL ; 9 import java.util.*; 10 import javax.servlet.ServletContext ; 11 12 20 public class ConfigurationManagerImpl implements ConfigurationManager { 21 final static public String WAR_CONF_LOCATION = "/WEB-INF"; 22 23 protected Configuration configurations_; 24 25 private ServletContext scontext_; 26 27 public ConfigurationManagerImpl(ServletContext context) { 28 scontext_ = context; 29 } 30 31 public void addConfiguration(String url) throws Exception { 32 addConfiguration(getURL(url)); 33 } 34 35 public void addConfiguration(Collection urls) throws Exception { 36 Iterator i = urls.iterator(); 37 while (i.hasNext()) { 38 URL url = (URL ) i.next(); 39 addConfiguration(url); 40 } 41 } 42 43 public void addConfiguration(URL url) throws Exception { 44 Configuration conf = XMLParser.parse(url.openStream()); 45 if (configurations_ == null) 46 configurations_ = conf; 47 else 48 configurations_.mergeConfiguration(conf); 49 List urls = conf.getImportConfiguration(); 50 for (int i = 0; i < urls.size(); i++) { 51 String uri = (String ) urls.get(i); 52 try { 53 InputStream is = getURL(uri).openStream(); 54 conf = XMLParser.parse(is); 55 configurations_.mergeConfiguration(conf); 56 } catch (Exception ex) { 57 System.err.println("Error: " + ex.getMessage()); 58 System.err.println("Error: cannot process the configuration" + url); 59 } 60 } 61 } 62 63 public void processRemoveConfiguration() { 64 if(configurations_ == null) return; 65 List list = configurations_.getRemoveConfiguration(); 66 for (int i = 0; i < list.size(); i++) { 67 String type = (String ) list.get(i); 68 configurations_.removeServiceConfiguration(type); 69 } 70 } 71 72 public ValuesParam getGlobalInitParam(String name) throws Exception { 73 return configurations_.getInitParam(name); 74 } 75 76 public ServiceConfiguration getServiceConfiguration(String service) 77 throws Exception { 78 return configurations_.getServiceConfiguration(service); 79 } 80 81 public ServiceConfiguration getServiceConfiguration(Class clazz) 82 throws Exception { 83 return getServiceConfiguration(clazz.getName()); 84 } 85 86 public Collection getServiceConfigurations() { 87 if(configurations_ == null) return null; 88 return configurations_.getServiceConfigurations(); 89 } 90 91 public Collection getGroovyServiceConfigurations() { 92 return configurations_.getGroovyServiceConfigurations(); 93 } 94 95 public URL getResource(String url, String defaultURL) throws Exception { 96 return null; 97 } 98 99 public URL getResource(String uri) throws Exception { 100 return getURL(uri); 101 } 102 103 public InputStream getInputStream(String url, String defaultURL) 104 throws Exception { 105 return null; 106 } 107 108 public InputStream getInputStream(String uri) throws Exception { 109 URL url = getURL(uri); 110 return url.openStream(); 111 } 112 113 protected URL getURL(String url) throws Exception { 114 if (url.startsWith("jar:")) { 115 String path = removePrefix("jar:/", url); 116 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 117 return cl.getResource(path); 118 } else if (url.startsWith("war:")) { 119 if (scontext_ == null) { 120 throw new Exception ("unsupport war uri in this configuration service"); 121 } 122 String path = removePrefix("war:", url); 123 return scontext_.getResource(WAR_CONF_LOCATION + path); 124 } else if (url.startsWith("file:")) { 125 url = resolveSystemProperties(url); 126 return new URL (url); 127 } 128 return null; 129 } 130 131 private String resolveSystemProperties(String input) { 132 final int NORMAL = 0; 133 final int SEEN_DOLLAR = 1; 134 final int IN_BRACKET = 2; 135 if (input == null) 136 return input; 137 char[] chars = input.toCharArray(); 138 StringBuffer buffer = new StringBuffer (); 139 boolean properties = false; 140 int state = NORMAL; 141 int start = 0; 142 for (int i = 0; i < chars.length; ++i) { 143 char c = chars[i]; 144 if (c == '$' && state != IN_BRACKET) 145 state = SEEN_DOLLAR; 146 else if (c == '{' && state == SEEN_DOLLAR) { 147 buffer.append(input.substring(start, i - 1)); 148 state = IN_BRACKET; 149 start = i - 1; 150 } 151 else if (state == SEEN_DOLLAR) 152 state = NORMAL; 153 else if (c == '}' && state == IN_BRACKET) { 154 if (start + 2 == i) { 155 buffer.append("${}"); 156 } 157 else { 158 String value = null; 159 String key = input.substring(start + 2, i); 160 value = System.getProperty(key); 161 if (value != null) { 162 properties = true; 163 buffer.append(value); 164 } 165 } 166 start = i + 1; 167 state = NORMAL; 168 } 169 } 170 if (properties == false) 171 return input; 172 if (start != chars.length) 173 buffer.append(input.substring(start, chars.length)); 174 return buffer.toString(); 175 176 } 177 178 public boolean isDefault(String value) { 179 return value == null || value.length() == 0 || "default".equals(value) ; 180 } 181 182 protected String removePrefix(String prefix, String url) { 183 return url.substring(prefix.length(), url.length()); 184 } 185 } | Popular Tags |