001 package toolbus;
002
003 import java.io.File;
004 import java.io.FileInputStream;
005 import java.io.FileNotFoundException;
006 import java.io.IOException;
007 import java.util.Map;
008 import java.util.HashMap;
009 import java.util.Iterator;
010 import java.util.Properties;
011 import java.util.regex.Matcher;
012 import java.util.regex.Pattern;
013
014 public class PropertyManager{
015 private final Properties properties;
016 private String propertyFileName = null;
017 private final Map<String, String> defines;
018 private final Map<String, String> toolClasspaths;
019 private String includes = "";
020 private String tbscript = null;
021 private int port;
022 private boolean withConsole = false;
023
024 public PropertyManager(String[] args){
025 port = -1;
026
027 defines = new HashMap<String, String>();
028 toolClasspaths = new HashMap<String, String>();
029
030 properties = new Properties(System.getProperties());
031
032 handleComandLineArguments(args);
033
034 FileInputStream fis = null;
035 try{
036 fis = new FileInputStream(getPropertyFile());
037 properties.load(fis);
038 }catch(FileNotFoundException fnfio){
039 // Ignore this.
040 }catch(IOException ioex){
041 System.err.println("Cannot open configuration file; using built-in settings.");
042 }finally{
043 if(fis != null){
044 try{
045 fis.close();
046 }catch(IOException ioex){
047 System.err.println("Unable to close properties file stream."+ioex.getMessage());
048 }
049 }
050 }
051
052 Iterator<String> definesIterator = defines.keySet().iterator();
053 while(definesIterator.hasNext()){
054 String key = definesIterator.next();
055
056 set(key, defines.get(key));
057 }
058 Iterator<String> toolClasspathsIterator = toolClasspaths.keySet().iterator();
059 while(definesIterator.hasNext()){
060 String key = toolClasspathsIterator.next();
061
062 set(key, defines.get(key));
063 }
064 if(includes != ""){
065 set("include.path", includes);
066 }
067 if(tbscript != null){
068 set("script.path", tbscript);
069 }
070 }
071
072 private void handleComandLineArguments(String args[]){
073 Pattern pdefine = Pattern.compile("-D(.*)=(.*)");
074 Pattern pinclude = Pattern.compile("-I(.*)");
075 Pattern pscript = Pattern.compile("-S(.*)");
076 Pattern pport = Pattern.compile("-P(.*)");
077 Pattern toolClassPath = Pattern.compile("-TC(.*)=(.*)");
078
079 for(int i = 0; i < args.length; i++){
080 String arg = args[i];
081 if(arg.equals("-properties") && i + 1 < args.length){
082 propertyFileName = args[++i];
083 }else if(arg.equals("--with-console")){
084 withConsole = true;
085 }else if(arg.equals("-output") && i + 1 < args.length){
086 set("gentifs.output", args[++i]);
087 }else{
088 Matcher mdefine = pdefine.matcher(arg);
089 if(mdefine.matches()){
090 String name = mdefine.group(1);
091 String val = "\"" + mdefine.group(2) + "\"";
092 //System.err.println("define: name = " + name + ", " + " val = " + val);
093 defines.put(name, val);
094 }else{
095 Matcher idefine = pinclude.matcher(arg);
096 if(idefine.matches()){
097 String name = idefine.group(1);
098 //System.err.println("include: name = " + name);
099 includes = (includes == "") ? name : includes + ", " + name;
100 }else{
101 Matcher sdefine = pscript.matcher(arg);
102 if (sdefine.matches()) {
103 String name = sdefine.group(1);
104 //System.err.println("tbscript: name = " + name);
105 if (tbscript != null) {
106 System.err.println("tbscript: already defined (" + tbscript + ")");
107 }
108 tbscript = name;
109 }else{
110 Matcher portDefine = pport.matcher(arg);
111 if(portDefine.matches()){
112 String portNumber = portDefine.group(1);
113 port = Integer.parseInt(portNumber);
114 }else{
115 Matcher toolClassPathMatcher = toolClassPath.matcher(arg);
116 if(toolClassPathMatcher.matches()){
117 String toolClasspathName = toolClassPathMatcher.group(1);
118 String classpath = toolClassPathMatcher.group(2);
119
120 toolClasspaths.put(toolClasspathName, classpath);
121 }
122 }
123 }
124 }
125 }
126 }
127 }
128 }
129
130 /**
131 * Get the name of the property file to be used
132 */
133 private String getPropertyFile() throws FileNotFoundException{
134 if(propertyFileName != null){
135 return propertyFileName;
136 }
137 String user = System.getProperty("user.name");
138
139 String hostname = ToolBus.getHostName();
140
141 String[] prefix = new String[]{user + "@" + hostname + "-", user + "-", ""};
142 for(int i = 0; i < prefix.length; i++){
143 String fname = prefix[i] + "toolbus.props";
144 File f = new File(fname);
145 if(f.exists()){
146 return fname;
147 }
148 }
149
150 throw new FileNotFoundException("Property file");
151 }
152
153 public String get(String p){
154 return properties.getProperty(p);
155 }
156
157 public String get(String p, String def){
158 return properties.getProperty(p, def);
159 }
160
161 public void set(String key, String val){
162 properties.setProperty(key, val);
163 }
164
165 public int getUserSpecifiedPort(){
166 return port;
167 }
168
169 public boolean withConsole(){
170 return withConsole;
171 }
172 }