Clover Coverage Report - ARESTC 0.1.7-SNAPSHOT
Coverage timestamp: Fri Aug 27 2010 19:12:04 CEST
../../../../img/srcFileCovDistChart6.png 89% of files have more coverage
37   209   16   4.11
14   92   0.43   9
9     1.78  
1    
 
  CommunicationFactory       Line # 39 37 0% 16 24 60% 0.6
 
  (5)
 
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.io.IOException;
23    import java.io.ObjectInputStream;
24    import java.io.ObjectOutputStream;
25    import java.io.Serializable;
26   
27    import org.apache.commons.configuration.ConfigurationException;
28    import org.apache.commons.httpclient.HttpClient;
29    import org.apache.commons.httpclient.HttpConnectionManager;
30    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
31    import org.apache.commons.httpclient.SimpleHttpConnectionManager;
32   
33    // TODO: Auto-generated Javadoc
34    /**
35    * A FACTORY for creating Communication objects.
36    *
37    * @author georgosn
38    */
 
39    public final class CommunicationFactory implements Serializable {
40   
41    /** The Constant serialVersionUID. */
42    private static final long serialVersionUID = 1L;
43   
44    /** The Constant FACTORY. */
45    private static final CommunicationFactory FACTORY = new CommunicationFactory();
46   
47    /**
48    * Gets the single instance of CommunicationFactory.
49    *
50    * @return single instance of CommunicationFactory
51    */
 
52  20 toggle public static CommunicationFactory getInstance() {
53  20 return FACTORY;
54    }
55   
56    /** The configured. */
57    private transient boolean configured = false;
58   
59    /** The client. */
60    private transient HttpClient client;
61   
62    /** The manager. */
63    private transient HttpConnectionManager manager;
64   
65    /** The multithreaded. */
66    private boolean multithreaded;
67   
68    /**
69    * Instantiates a new communication FACTORY.
70    */
 
71  1 toggle private CommunicationFactory() {
72  1 configured = false;
73  1 client = null;
74  1 multithreaded = false;
75    }
76   
77    /**
78    * Configure client.
79    *
80    * @param localThreadSceme
81    * the multi threaded
82    * @return the http client
83    * @throws ConfigurationException
84    * the configuration exception
85    */
 
86  13 toggle public HttpClient configureClient(final boolean localThreadSceme)
87    throws ConfigurationException {
88  13 if (null == client) {
89  2 multithreaded = localThreadSceme;
90  2 if (multithreaded) {
91  2 manager = new MultiThreadedHttpConnectionManager();
92    } else {
93  0 manager = new SimpleHttpConnectionManager();
94    }
95  2 client = new HttpClient(manager);
96  2 synchronized (this) {
97    // Lets configure our client making sure none can use the class
98    // while we are at it.
99  2 ClientConfigurationFactory.getInstance().configure(client);
100    }
101    // last statement always
102  2 configured = true;
103    } else {
104    // in case a new client is requested with a different threading
105    // scheme than the one that is active, throw a configuration
106    // exception
107  11 if (localThreadSceme != multithreaded) {
108  0 throw new ConfigurationException();
109    }
110    }
111  13 return client;
112    }
113   
 
114  0 toggle public HttpClient configureClient(final boolean localThreadSceme,
115    final String configFileName) throws ConfigurationException {
116  0 if (null == client) {
117  0 multithreaded = localThreadSceme;
118  0 if (multithreaded) {
119  0 manager = new MultiThreadedHttpConnectionManager();
120    } else {
121  0 manager = new SimpleHttpConnectionManager();
122    }
123  0 client = new HttpClient(manager);
124  0 synchronized (this) {
125    // Lets configure our client making sure none can use the class
126    // while we are at it.
127  0 ClientConfigurationFactory.getInstance().configure(client,
128    configFileName);
129    }
130    // last statement always
131  0 configured = true;
132    } else {
133    // in case a new client is requested with a different threading
134    // scheme than the one that is active, throw a configuration
135    // exception
136  0 if (localThreadSceme != multithreaded) {
137  0 throw new ConfigurationException();
138    }
139    }
140  0 return client;
141    }
142   
143    /**
144    * Checks if is configured.
145    *
146    * @return true, if is configured
147    */
 
148  1 toggle public boolean isConfigured() {
149  1 return configured;
150    }
151   
152    /**
153    * Checks if is multi threaded.
154    *
155    * @return true, if is multi threaded
156    */
 
157  3 toggle public boolean isMultiThreaded() {
158  3 return multithreaded;
159    }
160   
161    /**
162    * Read object.
163    *
164    * @param aInputStream
165    * the a input stream
166    * @throws ClassNotFoundException
167    * the class not found exception
168    * @throws IOException
169    * Signals that an I/O exception has occurred.
170    * @throws ConfigurationException
171    * the configuration exception
172    */
 
173  1 toggle private void readObject(final ObjectInputStream aInputStream)
174    throws ClassNotFoundException, IOException, ConfigurationException {
175    // always perform the default de-serialization first
176  1 aInputStream.defaultReadObject();
177    // instantiate the client. the serialization of the httpclient is NOT
178    // saving state of the client itself
179  1 configureClient(multithreaded);
180    }
181   
182    /**
183    * Shutdown.
184    */
 
185  4 toggle public void shutdown() {
186  4 synchronized (this) {
187  4 if (multithreaded) {
188  4 MultiThreadedHttpConnectionManager.shutdownAll();
189    }
190  4 client = null;
191    }
192    }
193   
194    /**
195    * Write object.
196    *
197    * @param aOutputStream
198    * the a output stream
199    * @throws IOException
200    * Signals that an I/O exception has occurred.
201    */
 
202  1 toggle private void writeObject(final ObjectOutputStream aOutputStream)
203    throws IOException {
204    // perform the default serialization for all non-transient, non-static
205    // fields
206  1 aOutputStream.defaultWriteObject();
207    }
208   
209    }