1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveremoting.caucho; |
16 | |
17 | import java.util.HashMap; |
18 | import java.util.Map; |
19 | |
20 | import org.apache.commons.logging.Log; |
21 | |
22 | /** |
23 | * @author Jean-Francois |
24 | */ |
25 | public class CauchoProxyConfiguratorImpl implements CauchoProxyConfigurator |
26 | { |
27 | public CauchoProxyConfiguratorImpl(Log logger) |
28 | { |
29 | _logger = logger; |
30 | } |
31 | |
32 | /* (non-Javadoc) |
33 | * @see net.sourceforge.hiveremoting.caucho.CauchoProxyConfigurator#setServiceProtocol(java.lang.String, net.sourceforge.hiveremoting.caucho.Protocol) |
34 | */ |
35 | public void setServiceProtocol(String service, Protocol protocol) |
36 | { |
37 | CauchoProxyContribution config = getServiceConfig(service, true); |
38 | config.setProtocol(protocol); |
39 | } |
40 | |
41 | /* (non-Javadoc) |
42 | * @see net.sourceforge.hiveremoting.caucho.CauchoProxyConfigurator#setServiceUrl(java.lang.String, java.lang.String) |
43 | */ |
44 | public void setServiceUrl(String service, String url) |
45 | { |
46 | CauchoProxyContribution config = getServiceConfig(service, true); |
47 | config.setUrl(url); |
48 | } |
49 | |
50 | /* (non-Javadoc) |
51 | * @see net.sourceforge.hiveremoting.caucho.CauchoProxyConfigurator#setServiceContextHandler(java.lang.String, net.sourceforge.hiveremoting.caucho.RemoteContextHandler) |
52 | */ |
53 | public void setServiceContextHandler(String service, RemoteContextHandler handler) |
54 | { |
55 | CauchoProxyContribution config = getServiceConfig(service, true); |
56 | config.setContextHandler(handler); |
57 | } |
58 | |
59 | /* (non-Javadoc) |
60 | * @see net.sourceforge.hiveremoting.caucho.CauchoProxyConfigurator#setServiceAuthentication(java.lang.String, java.lang.String, java.lang.String) |
61 | */ |
62 | public void setServiceAuthentication(String service, String user, String password) |
63 | { |
64 | CauchoProxyContribution config = getServiceConfig(service, true); |
65 | config.setUser(user); |
66 | config.setPassword(password); |
67 | } |
68 | |
69 | /* (non-Javadoc) |
70 | * @see net.sourceforge.hiveremoting.caucho.CauchoProxyConfigurator#getConfiguration(java.lang.String, net.sourceforge.hiveremoting.caucho.CauchoProxyContribution) |
71 | */ |
72 | public CauchoProxyContribution getConfiguration( String service, |
73 | CauchoProxyContribution source) |
74 | { |
75 | CauchoProxyContribution config = getServiceConfig(service, false); |
76 | if (config == null) |
77 | { |
78 | return source; |
79 | } |
80 | // Combine source into dest (null fields only) |
81 | CauchoProxyContribution dest = new CauchoProxyContribution(); |
82 | dest.setProtocol( |
83 | config.getProtocol() != null ? config.getProtocol() : source.getProtocol()); |
84 | dest.setUrl( |
85 | config.getUrl() != null ? config.getUrl() : source.getUrl()); |
86 | dest.setUser( |
87 | config.getUser() != null ? config.getUser() : source.getUser()); |
88 | dest.setPassword( |
89 | config.getPassword() != null ? config.getPassword() : source.getPassword()); |
90 | dest.setContextHandler( config.getContextHandler() != null |
91 | ? config.getContextHandler() |
92 | : source.getContextHandler()); |
93 | dest.setGzipInBufferSize(source.getGzipInBufferSize()); |
94 | dest.setGzipThreshold(source.getGzipThreshold()); |
95 | dest.setOverloadEnabled(source.getOverloadEnabled()); |
96 | dest.setProtocolVersion(source.getProtocolVersion()); |
97 | return dest; |
98 | } |
99 | |
100 | protected CauchoProxyContribution getServiceConfig(String service, boolean create) |
101 | { |
102 | CauchoProxyContribution config = _configs.get(service); |
103 | if (config == null) |
104 | { |
105 | if (create) |
106 | { |
107 | config = new CauchoProxyContribution(); |
108 | _configs.put(service, config); |
109 | } |
110 | else |
111 | { |
112 | _logger.debug("getServiceConfig, unknown service: " + service); |
113 | } |
114 | } |
115 | return config; |
116 | } |
117 | |
118 | private final Log _logger; |
119 | private final Map<String, CauchoProxyContribution> _configs = |
120 | new HashMap<String, CauchoProxyContribution>(); |
121 | } |