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
35   177   9   7
6   95   0.26   5
5     1.8  
1    
 
  ServicesConfigurationFactory       Line # 49 35 0% 9 1 97.8% 0.9782609
 
  (10)
 
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.Collection;
23    import java.util.Collections;
24    import java.util.EnumMap;
25    import java.util.HashMap;
26    import java.util.HashSet;
27    import java.util.List;
28    import java.util.Map;
29   
30    import net.sf.arestc.core.services.BaseService;
31    import net.sf.arestc.core.services.HTTPMethods;
32    import net.sf.arestc.core.services.RESTService;
33    import net.sf.arestc.core.services.ReturnedTypes;
34    import net.sf.arestc.core.services.ServiceAuthenticationData;
35    import net.sf.arestc.core.services.ServiceConnectionData;
36    import net.sf.arestc.core.services.ServiceConnectorData;
37    import net.sf.arestc.core.services.ServiceParameterTypes;
38   
39    import org.apache.commons.configuration.ConfigurationException;
40    import org.apache.commons.configuration.XMLConfiguration;
41    import org.apache.commons.httpclient.Credentials;
42    import org.apache.commons.httpclient.UsernamePasswordCredentials;
43   
44    /**
45    * A factory for creating ServicesConfiguration objects.
46    *
47    * @author georgosn
48    */
 
49    public class ServicesConfigurationFactory {
50   
51    /** The Constant SERVICES_FILE. */
52    private static final String SERVICES_FILE = "services.xml";
53   
54    /** The Constant _instance. */
55    public static final ServicesConfigurationFactory _instance = new ServicesConfigurationFactory();
56   
57    /**
58    * Instantiates a new services configuration factory.
59    */
 
60  1 toggle private ServicesConfigurationFactory() {
61   
62    }
63   
 
64  266 toggle private RESTService configService(final Credentials credentials,
65    final XMLConfiguration config, final String server, final int port,
66    final int i) {
67  266 final String prefix = "services.service(" + i + ")";
68  266 final String name = config.getString(prefix + ".[@name]");
69   
70    // Connection Data
71  266 final String contextPath = config.getString(prefix + "[@context-path]");
72  266 final HTTPMethods method = HTTPMethods.valueOf(config.getString(prefix
73    + "[@method]"));
74  266 final ServiceConnectionData connectionData = new ServiceConnectionData(
75    server, port, contextPath, method);
76   
77    // Authentication data
78  266 final Boolean authReq = config.getBoolean(prefix
79    + ".authentication[@required]");
80  266 final String user = config.getString(prefix + ".authentication");
81  266 final ServiceAuthenticationData authData = new ServiceAuthenticationData(
82  266 authReq, user, "admin".equalsIgnoreCase(user) ? credentials
83    : null);
84   
85    // connectorData
86  266 final ReturnedTypes retType = ReturnedTypes.valueOf(config.getString(
87    prefix + "[@returns]").toUpperCase());
88  266 final ServiceConnectorData connectorData = new ServiceConnectorData(
89    config.getString(prefix + "[@content-type]"), retType);
90   
91    // ServiceParameters
92  266 final List<String> paramsNames = config.getList(prefix
93    + ".params.param[@name]");
94   
95  266 final EnumMap<ServiceParameterTypes, Collection<String>> params1 = new EnumMap<ServiceParameterTypes, Collection<String>>(
96    ServiceParameterTypes.class);
97  266 for (final ServiceParameterTypes type : ServiceParameterTypes.values()) {
98  798 params1.put(type, new HashSet<String>());
99    }
100  1026 for (int j = 0; j < paramsNames.size(); j++) {
101  760 final String paramPrefix = prefix + ".params.param(" + j + ")";
102  760 params1.get(
103    ServiceParameterTypes.valueOf(config.getString(
104    paramPrefix + "[@value-type]").toUpperCase())).add(
105    paramsNames.get(j));
106    }
107   
108  266 return new BaseService(name, params1, connectionData, authData,
109    connectorData);
110    }
111   
112    /**
113    * Configure.
114    *
115    * @return the map
116    * @throws ConfigurationException
117    * the configuration exception
118    */
 
119  37 toggle public Map<String, RESTService> configure() throws ConfigurationException {
120   
121  37 final XMLConfiguration config = new XMLConfiguration(SERVICES_FILE);
122   
123  37 return control(config);
124    }
125   
126    /**
127    * Configuration method that allows the submission of the config file from
128    * an arbitrary position.
129    *
130    * @param configFileName
131    * the config file name
132    * @return the map
133    * @throws ConfigurationException
134    * the configuration exception
135    */
 
136  1 toggle public Map<String, RESTService> configure(final String configFileName)
137    throws ConfigurationException {
138  1 XMLConfiguration config;
139  1 try {
140  1 config = new XMLConfiguration(configFileName);
141   
142  1 return control(config);
143    } catch (final ConfigurationException e) {
144  0 return configure();
145    } finally {
146  1 config = null;
147    }
148    }
149   
150    /**
151    * controls the setup of the client.
152    *
153    * @param config
154    * the config
155    * @return the map
156    * @throws ConfigurationException
157    * the configuration exception
158    */
 
159  38 toggle protected Map<String, RESTService> control(final XMLConfiguration config)
160    throws ConfigurationException {
161  38 final String server = config.getString("server[@url]");
162  38 final int port = config.getInt("server[@port]");
163  38 final Credentials credentials = new UsernamePasswordCredentials(
164    config.getString("admin[@username]"),
165    config.getString("admin[@password]"));
166  38 final Map<String, RESTService> services = new HashMap<String, RESTService>();
167  38 final List<String> names = config.getList("services.service[@name]");
168  304 for (int i = 0; i < names.size(); i++) {
169   
170  266 final RESTService service = configService(credentials, config,
171    server, port, i);
172  266 services.put(service.getName(), service);
173   
174    }
175  38 return Collections.unmodifiableMap(services);
176    }
177    }