001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.cmis; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Set; 021 022/** 023 * Splits common configuration map into set of repository specific parameters. 024 * 025 * @author kulikov 026 */ 027public class RepositoryConfig { 028 private static final String REPO_PARAMETER_PREFIX = "jcr."; 029 030 public static Map<String, Map<String, String>> load( Map<String, String> params ) { 031 // create map for results 032 Map<String, Map<String, String>> set = new HashMap<String, Map<String, String>>(); 033 034 // extract parameter's keys 035 Set<String> keys = params.keySet(); 036 for (String key : keys) { 037 // search for repositories parameters 038 if (key.startsWith(REPO_PARAMETER_PREFIX)) { 039 // found repository parameter, cut prefix 040 String fqn = key.substring(REPO_PARAMETER_PREFIX.length()); 041 042 // cut repository ID from the begining of parameter 043 String repositoryId = fqn.substring(0, fqn.indexOf(".")); 044 045 // get or create map for this repository Id 046 Map<String, String> map = set.get(repositoryId); 047 if (map == null) { 048 map = new HashMap<String, String>(); 049 set.put(repositoryId, map); 050 } 051 052 // extract clean parameter name and value 053 String name = fqn.substring(fqn.indexOf(".") + 1, fqn.length()); 054 String value = params.get(key); 055 056 // store parameter 057 map.put(name, replaceSystemProperties(value)); 058 } 059 } 060 061 return set; 062 } 063 064 private static String replaceSystemProperties( String s ) { 065 if (s == null) { 066 return null; 067 } 068 069 StringBuilder result = new StringBuilder(); 070 StringBuilder property = null; 071 boolean inProperty = false; 072 073 for (int i = 0; i < s.length(); i++) { 074 char c = s.charAt(i); 075 076 if (inProperty) { 077 assert property != null; 078 if (c == '}') { 079 String value = System.getProperty(property.toString()); 080 if (value != null) { 081 result.append(value); 082 } 083 inProperty = false; 084 } else { 085 property.append(c); 086 } 087 } else { 088 if (c == '{') { 089 property = new StringBuilder(); 090 inProperty = true; 091 } else { 092 result.append(c); 093 } 094 } 095 } 096 097 return result.toString(); 098 } 099}