Clover Coverage Report - ARESTC 0.1.7-SNAPSHOT
Coverage timestamp: Fri Aug 27 2010 19:12:04 CEST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
34   190   14   4.86
8   86   0.41   7
7     2  
1    
 
  ClientConfigurationFactory       Line # 38 34 0% 14 4 91.8% 0.9183673
 
  (2)
 
1    /*
2    *
3    * (C)opyright 2010, Nikolaos Georgosopoulos
4    *
5    * This file is part of ARESTC.
6    *
7    * ARESTC is free software: you can redistribute it and/or modify it under the
8    * terms of the Lesser General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10    * version.
11    *
12    * ARESTC is distributed in the hope that it will be useful, but WITHOUT ANY
13    * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14    * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15    *
16    * You should have received a copy of the Lesser General Public License along
17    * with ARESTC. If not, see <http://www.gnu.org/licenses/>.
18    */
19   
20    package net.sf.arestc.configuration;
21   
22    import java.util.List;
23   
24    import org.apache.commons.configuration.ConfigurationException;
25    import org.apache.commons.configuration.XMLConfiguration;
26    import org.apache.commons.httpclient.Credentials;
27    import org.apache.commons.httpclient.HttpClient;
28    import org.apache.commons.httpclient.UsernamePasswordCredentials;
29    import org.apache.commons.httpclient.auth.AuthScope;
30   
31    /**
32    * A factory for creating Configuration objects. this factory caches the last
33    * configured client so that the same instance will not be configured twice. In
34    * some cases of threading this can produce problems.
35    *
36    * @author georgosn
37    */
 
38    public class ClientConfigurationFactory {
39   
40    /** The Constant HTTPCONFIGURATION_FILENAME. */
41    private static final String HTTPCONFIGURATION_FILENAME = "httpconfiguration.xml";
42    /** The Constant _instance. */
43    private static final ClientConfigurationFactory _instance = new ClientConfigurationFactory();
44   
45    /**
46    * Gets the single instance of ClientConfigurationFactory.
47    *
48    * @return single instance of ClientConfigurationFactory
49    */
 
50  4 toggle public static ClientConfigurationFactory getInstance() {
51  4 return _instance;
52    }
53   
54    private HttpClient localClient;
55   
56    /**
57    * Configure.
58    *
59    * @param client
60    * the client
61    * @throws ConfigurationException
62    * the configuration exception
63    */
 
64  3 toggle public void configure(final HttpClient client)
65    throws ConfigurationException {
66  3 if (null == localClient || !localClient.equals(client)) {
67  3 final XMLConfiguration config = new XMLConfiguration(
68    HTTPCONFIGURATION_FILENAME);
69  3 control(config, client);
70  3 localClient = client;
71    }
72    }
73   
74    /**
75    * Configuration method that allows the submission of the config file from
76    * an arbitrary position.
77    *
78    * @param client
79    * the client
80    * @param configFileName
81    * the config file name
82    * @throws ConfigurationException
83    * the configuration exception
84    */
 
85  2 toggle public void configure(final HttpClient client, final String configFileName)
86    throws ConfigurationException {
87  2 if (null == localClient || !localClient.equals(client)) {
88  1 XMLConfiguration config;
89  1 try {
90  1 config = new XMLConfiguration(configFileName);
91  0 control(config, client);
92    } catch (final ConfigurationException e) {
93  1 configure(client);
94    } finally {
95  1 config = null;
96  1 localClient = client;
97    }
98    }
99    }
100   
101    /**
102    * Configure hosts.
103    *
104    * @param config
105    * the config
106    * @param client
107    * the client
108    */
 
109  3 toggle protected void configureHosts(final XMLConfiguration config,
110    final HttpClient client) {
111  3 final List<String> hosts = config.getList("hosts.host.server");
112  6 for (int i = 0; i < hosts.size(); i++) {
113  3 final String hostInstance = "hosts.host(" + i + ").";
114  3 final String username = config.getString(hostInstance + "username",
115    "admin");
116  3 final String password = config.getString(hostInstance + "password",
117    "admin1");
118  3 final String host = config.getString(hostInstance + "server",
119    "localhost");
120  3 final int port = config.getInt(hostInstance + "port", 80);
121   
122  3 final Credentials defaultcreds = new UsernamePasswordCredentials(
123    username, password);
124  3 client.getState().setCredentials(
125    new AuthScope(host, port, AuthScope.ANY_REALM),
126    defaultcreds);
127    }
128    }
129   
130    /**
131    * Configure preemptive.
132    *
133    * @param config
134    * the config
135    * @param client
136    * the client
137    */
 
138  3 toggle protected void configurePreemptive(final XMLConfiguration config,
139    final HttpClient client) {
140  3 if (config.getBoolean("preemtive", false)) {
141  0 client.getParams().setAuthenticationPreemptive(true);
142    }
143    }
144   
145    /**
146    * Configure proxies.
147    *
148    * @param config
149    * the config
150    * @param client
151    * the client
152    */
 
153  3 toggle protected void configureProxies(final XMLConfiguration config,
154    final HttpClient client) {
155   
156  3 final String proxyInstance = "proxy";
157  3 final String username = config.getString(proxyInstance + "username",
158    "admin");
159  3 final String password = config.getString(proxyInstance + "password",
160    "admin1");
161  3 final String host = config.getString(proxyInstance + "server",
162    "localhost");
163  3 final int port = config.getInt(proxyInstance + "port", 80);
164   
165  3 final Credentials defaultcreds = new UsernamePasswordCredentials(
166    username, password);
167  3 client.getState().setProxyCredentials(
168    new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
169   
170    }
171   
172    /**
173    * controls the setup of the client.
174    *
175    * @param config
176    * the config
177    * @param client
178    * the client
179    * @throws ConfigurationException
180    * the configuration exception
181    */
 
182  3 toggle protected void control(final XMLConfiguration config,
183    final HttpClient client) throws ConfigurationException {
184   
185  3 configurePreemptive(config, client);
186  3 configureHosts(config, client);
187  3 configureProxies(config, client);
188    }
189   
190    }