View Javadoc

1   /**
2    * Copyright 2007 Björn Voß
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.oxclient.communication.factory;
17  
18  import net.sf.oval.constraint.NotEmpty;
19  import net.sf.oval.constraint.NotNull;
20  import net.sf.oval.constraint.Range;
21  import net.sf.oval.guard.Guarded;
22  import net.sf.oxclient.communication.ICommunicationClient;
23  import net.sf.oxclient.communication.HttpCommunicationClient;
24  import net.sf.oxclient.util.SpringBeanFactory;
25  
26  import org.apache.commons.httpclient.HttpClient;
27  import org.springframework.beans.factory.annotation.Required;
28  
29  /**
30   * @author Björn Voß
31   *
32   */
33  @Guarded
34  public class SpringCommunicationClientFactory extends SpringBeanFactory implements
35  		ICommunicationClientFactory {
36  
37  	private int serverPort = 0;
38  	private String serverName = null;
39  
40  	public SpringCommunicationClientFactory() {
41  		super("httpClient");
42  	}
43  
44  	/* (non-Javadoc)
45  	 * @see net.sf.oxclient.communication.factory.ICommunicationClientFactory#getNewInstance()
46  	 */
47  	public ICommunicationClient getNewInstance() {
48  		final HttpClient httpClient = (HttpClient) getBean();
49  		httpClient.getHostConfiguration().setHost(this.serverName, this.serverPort);
50  		return new HttpCommunicationClient(httpClient);
51  	}
52  
53  	/* (non-Javadoc)
54  	 * @see net.sf.oxclient.communication.factory.ICommunicationClientFactory#setServerPort(int)
55  	 */
56  	@Required
57  	public void setServerPort(@Range(min=1, max=32768) final int port) {
58  		this.serverPort = port;
59  	}
60  
61  	/* (non-Javadoc)
62  	 * @see net.sf.oxclient.communication.factory.ICommunicationClientFactory#setServerUrl(java.lang.String)
63  	 */
64  	@Required
65  	public void setServer(@NotNull @NotEmpty final String url) {
66  		this.serverName = url;
67  	}
68  
69  	/* (non-Javadoc)
70  	 * @see net.sf.oxclient.communication.factory.ICommunicationClientFactory#getServer()
71  	 */
72  	public String getServer() {
73  		return this.serverName;
74  	}
75  
76  	/* (non-Javadoc)
77  	 * @see net.sf.oxclient.communication.factory.ICommunicationClientFactory#getServerPort()
78  	 */
79  	public int getServerPort() {
80  		return this.serverPort;
81  	}
82  
83  
84  
85  }