001 /* 002 * $RCSfile: VariablesToolkit.java,v $ 003 * 004 * Created on December 19, 2002, 3:29 PM 005 * 006 * This file is part of the STAR Scheduler. 007 * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory 008 * 009 * STAR Scheduler is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation; either version 2 of the License, or 012 * (at your option) any later version. 013 * 014 * STAR Scheduler is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with STAR Scheduler; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 */ 023 package gov.bnl.star.offline.scheduler.util; 024 025 import java.net.URL; 026 027 import java.util.Map; 028 import java.util.logging.Level; 029 import org.apache.log4j.Logger; 030 031 032 /** Utility class for environment variables, mainly variable substitution. 033 * @author Gabriele Carcassi 034 * @version $Revision: 1.8 $ $Date: 2006/11/21 00:41:29 $ 035 */ 036 public class VariablesToolkit { 037 static private Logger log = Logger.getLogger(VariablesToolkit.class.getName()); 038 039 /** Creates a new instance of VariablesToolkit */ 040 private VariablesToolkit() { 041 } 042 043 /* Given a URL, substitutes environment variable that might be present in 044 * the path and filename. 045 */ 046 047 /** Substitutes the envirnoment varilables present in the path of the URL. 048 * @param url the URL with environment variables 049 * @param variables a map from variable names to values 050 * @return the URL with expanded variables 051 */ 052 public static URL substituteVariables(URL url, Map variables) { 053 String temp = url.toString(); 054 temp = substituteVariables(temp, variables); 055 056 try { 057 return new URL(temp); 058 } catch (Exception e) { 059 // The substitution created an invalid URL 060 log.error("A URL variable substitution failed: " + url + " - " + temp, e); 061 throw new RuntimeException( "The environment variable substitution in the following URL led to an error: " + url); 062 } 063 } 064 065 /* Given a generic test String, substitutes all the variables that might 066 * be present in the variables list. 067 */ 068 069 /** Substitutes the envirnoment varilables present in a String. 070 * @param text the text with environment variables 071 * @param variables a map from variable names to values 072 * @return the text with expanded variables 073 */ 074 public static String substituteVariables(String text, Map variables) { 075 StringBuffer temp = new StringBuffer(text); 076 int offset = 0; 077 int current = 0; 078 079 // Continue when there are no more '$' characters 080 while ((current = temp.indexOf("$", offset)) != -1) { 081 int varLength = 1; 082 083 // Determine the length of the text that includes the environment 084 // variable 085 while (((current + varLength) < temp.length()) && Character.isLetterOrDigit(temp.charAt(current + varLength))) 086 varLength++; 087 088 String varName = temp.toString().substring(current + 1, 089 current + varLength); 090 log.debug("Substituting variable " + varName + " in " + text); 091 092 // Find a variable with the proper name. If it is not found, 093 // leave the text as it is and go on. 094 String value = (String) variables.get(varName); 095 096 if (value != null) { 097 temp.replace(current, current + varLength, value); 098 offset = current + value.length(); 099 } else { 100 offset++; 101 } 102 } 103 104 return temp.toString(); 105 } 106 }