View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // UTGBConfig.java
20  // Since: 2009/08/21
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.config;
26  
27  import java.io.IOException;
28  import java.io.StringReader;
29  import java.net.URL;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Properties;
33  
34  import org.xerial.core.XerialException;
35  import org.xerial.lens.SilkLens;
36  
37  /**
38   * UTGB configuration. This object corresponds to config/{common, default}.silk file.
39   * 
40   * @author leo
41   * 
42   */
43  public class UTGBConfig {
44  
45  	public static class WebAction {
46  		public String alias;
47  		public String javaPackage;
48  
49  	}
50  
51  	public static class Database {
52  		public String id;
53  		public String dbms = "sqlite";
54  		public String driver;
55  		public String jdbcPrefix;
56  		public String address;
57  		public String user;
58  		public String pass;
59  
60  		@Override
61  		public String toString() {
62  			return String.format("id:%s, dbms:%s, driver:%s, address:%s", id, dbms, driver, address);
63  		}
64  	}
65  
66  	public String version = "1.0";
67  	public String group;
68  	public String projectName;
69  	public String javaPackage;
70  	public List<WebAction> webAction = new ArrayList<WebAction>();
71  	public List<Database> database = new ArrayList<Database>();
72  	public Properties _ = new Properties();
73  
74  	public void put(String key, String value) {
75  		_.put(key, value);
76  	}
77  
78  	public String getProperty(String key) {
79  		return _.getProperty(key);
80  	}
81  
82  	public boolean hasPropertyOf(String key) {
83  		return _.containsKey(key);
84  	}
85  
86  	public String getProperty(String key, String defaultValue) {
87  		return _.getProperty(key, defaultValue);
88  	}
89  
90  	public Database getDatabase(String id) {
91  		for (Database each : database) {
92  			if (id.equals(each.id))
93  				return each;
94  		}
95  		return null;
96  	}
97  
98  	/**
99  	 * Parse the input config file in Silk format, and return the UTGBConfig instance
100 	 * 
101 	 * @param configResource
102 	 * @return {@link UTGBConfig} object
103 	 * @throws IOException
104 	 *             when failed to read the resource
105 	 * @throws XerialException
106 	 *             when some syntax error is observed
107 	 */
108 	public static UTGBConfig parse(URL configResource) throws IOException, XerialException {
109 		return SilkLens.loadSilk(UTGBConfig.class, configResource);
110 	}
111 
112 	public static UTGBConfig parseSilk(String silk) throws XerialException, IOException {
113 		return SilkLens.loadSilk(UTGBConfig.class, new StringReader(silk));
114 	}
115 
116 	public String toSilk() {
117 		return SilkLens.toSilk(this);
118 	}
119 
120 }