1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.smartcrawler.common;
28 import java.io.File;
29 import java.util.Hashtable;
30 import java.util.TreeMap;
31 import org.apache.commons.configuration.Configuration;
32 import org.apache.commons.configuration.XMLConfiguration;
33
34 /***
35 *
36 *
37 * @author <a href="mailto:pozzad@alice.it">Davide Pozza</a>
38 * @version <tt>$Revision: 1.5 $</tt>
39 */
40 public class SiteConfiguration {
41
42 public static final int RETRIEVER_CLASS = 1;
43 public static final int PERSISTER_CLASS = 2;
44 public static final int PREC_FILTERS_LIST = 3;
45 public static final int POST_FILTERS_LIST = 4;
46 public static final int ENGINES_THREADS_NUMBER = 5;
47 public static final int LOGGERS = 6;
48 public static final int PERSISTER_ROOTDIR = 7;
49
50 public static final int INITIAL_LINK = 10;
51
52 private Configuration conf;
53
54 private static Link initialLink;
55
56
57
58
59 public SiteConfiguration() {
60 loadConfig("smartcrawler-config.xml");
61 }
62
63 /***
64 *
65 * @param paramId
66 * @return
67 */
68 public Object get(int paramId) {
69 String item = "";
70 Object res = null;
71 switch(paramId) {
72 case RETRIEVER_CLASS: {
73
74
75
76
77
78
79
80
81 break;
82 }
83 case PERSISTER_CLASS: {
84
85
86
87
88
89
90
91
92
93 break;
94 }
95 case PREC_FILTERS_LIST: {
96 int n = this.conf.getList("retriever.filters.filter.class").size();
97 TreeMap map = new TreeMap();
98 for (int i = 0; i < n; i++) {
99 String key = this.conf.getString(
100 "retriever.filters.filter(" + i + ").priority");
101 String value = this.conf.getString(
102 "retriever.filters.filter(" + i + ").class");
103 String prevVal = (String)map.put(key, value);
104 String newKey = key;
105 while (prevVal != null) {
106 newKey = "" + (Integer.parseInt(newKey) + 1);
107 prevVal = (String)map.put(newKey, prevVal);
108 }
109 }
110 res = map.values();
111 break;
112 }
113 case POST_FILTERS_LIST: {
114 int n = this.conf.getList("persister.filters.filter.class").size();
115 TreeMap map = new TreeMap();
116 for (int i = 0; i < n; i++) {
117 String key = this.conf.getString(
118 "persister.filters.filter(" + i + ").priority");
119 String value = this.conf.getString(
120 "persister.filters.filter(" + i + ").class");
121 String prevVal = (String)map.put(key, value);
122 String newKey = key;
123 while (prevVal != null) {
124 newKey = "" + (Integer.parseInt(newKey) + 1);
125 prevVal = (String)map.put(newKey, prevVal);
126 }
127 }
128 res = map.values();
129 break;
130 }
131 case ENGINES_THREADS_NUMBER: {
132 res = this.conf.getInteger(
133 "engine.threadsNumber",
134 new Integer("5"));
135 break;
136 }
137 case LOGGERS: {
138 int n = this.conf.getList("loggers.logger").size();
139 Hashtable loggers = new Hashtable();
140 for (int i = 0; i < n; i++) {
141 String active = this.conf.getString(
142 "loggers.logger(" + i + ")[@active]");
143 String str = this.conf.getString(
144 "loggers.logger(" + i + ")[@type]");
145 loggers.put(str, active);
146 }
147 res = loggers;
148 break;
149 }
150 case PERSISTER_ROOTDIR: {
151 item = this.conf.getString("persister.rootdir");
152 if (item == null) {
153 item = ".";
154 }
155 res = new File(item);
156 break;
157 }
158 default: {
159 }
160 }
161 return res;
162 }
163
164 /***
165 *
166 * @param configFile
167 */
168 public SiteConfiguration(String configFile) {
169 loadConfig(configFile);
170 }
171
172 /***
173 *
174 * @param configFile
175 */
176 protected void loadConfig(String configFile) {
177 try {
178 this.conf = new XMLConfiguration(configFile);
179 } catch (Exception e) {
180 e.printStackTrace();
181 }
182 }
183
184 /***
185 *
186 * @return
187 */
188 public Link getInitialLink() {
189 return initialLink;
190 }
191
192 /***
193 *
194 * @param initialLink
195 */
196 public void setInitialLink(Link initialLink) {
197 this.initialLink = initialLink;
198 }
199
200 }