| 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.lang.reflect.Proxy; |
| 18 | |
| 19 | import org.apache.commons.httpclient.HttpConnectionManager; |
| 20 | import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
| 21 | import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory; |
| 22 | import org.apache.commons.httpclient.protocol.Protocol; |
| 23 | import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; |
| 24 | import org.apache.commons.logging.Log; |
| 25 | import org.apache.hivemind.ServiceImplementationFactory; |
| 26 | import org.apache.hivemind.ServiceImplementationFactoryParameters; |
| 27 | |
| 28 | import com.caucho.hessian.io.SerializerFactory; |
| 29 | |
| 30 | /** |
| 31 | * This service creates Proxys to remote services that are published through |
| 32 | * Caucho Hessian or Burlap protocols. |
| 33 | * <p> |
| 34 | * <b>ServiceModel must be singleton</b> |
| 35 | * <p> |
| 36 | * Note: how to dynamically define user/password for authentication? |
| 37 | * <p> |
| 38 | * The idea is to put them into System properties and use HiveMind |
| 39 | * <code>SystemPropertiesSymbolSource</code> to do the substitution at Registry |
| 40 | * construction time (please note this is not possible to have real dynamic user/ |
| 41 | * password definition after HiveMind Registry has been built). |
| 42 | * <p> |
| 43 | * In hivemind.xml, you would have: |
| 44 | * <pre> |
| 45 | * <contribution configuration-id="hivemind.SymbolSources"> |
| 46 | * <source name="SystemProperties" |
| 47 | * class="org.apache.hivemind.impl.SystemPropertiesSymbolSource"/> |
| 48 | * </contribution> |
| 49 | * |
| 50 | * <implementation service-id="hiveboard.shared.WhiteBoardUserService"> |
| 51 | * <invoke-factory service-id="hiveremoting.caucho.CauchoProxyFactory" |
| 52 | * model="singleton"> |
| 53 | * <proxy url="http://server:8080/webapp/path" |
| 54 | * protocol="Hessian" user="${user}" password="${password}" /> |
| 55 | * </invoke-factory> |
| 56 | * </implementation> |
| 57 | * </pre> |
| 58 | * Then in your source code, you would have something like: |
| 59 | * <pre> |
| 60 | * Properties props = System.getProperties(); |
| 61 | * props.setProperty("user", "myusername"); |
| 62 | * props.setProperty("password", "mypassword"); |
| 63 | * Registry registry = RegistryBuilder.constructDefaultRegistry(); |
| 64 | * </pre> |
| 65 | * |
| 66 | * @author Jean-Francois Poilpret |
| 67 | */ |
| 68 | public class CauchoProxyFactory implements ServiceImplementationFactory |
| 69 | { |
| 70 | public CauchoProxyFactory( CauchoProxyConfigurator configurator, |
| 71 | SerializerFactory factory, |
| 72 | int maxConnections, |
| 73 | int maxConnectionsPerHost, |
| 74 | boolean httpsStrictCertificateCheck) |
| 75 | { |
| 76 | _configurator = configurator; |
| 77 | _factory = factory; |
| 78 | _cnxManager = new MultiThreadedHttpConnectionManager(); |
| 79 | _cnxManager.getParams().setMaxTotalConnections(maxConnections); |
| 80 | _cnxManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionsPerHost); |
| 81 | if (!httpsStrictCertificateCheck) |
| 82 | { |
| 83 | ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory(); |
| 84 | Protocol protocol = new Protocol("https", socketFactory, DEFAULT_HTTPS_PORT); |
| 85 | Protocol.registerProtocol("https", protocol); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public Object createCoreServiceImplementation( |
| 90 | ServiceImplementationFactoryParameters factoryParams) |
| 91 | { |
| 92 | Log logger = factoryParams.getLog(); |
| 93 | // Read parameter |
| 94 | CauchoProxyContribution contrib = |
| 95 | (CauchoProxyContribution) factoryParams.getFirstParameter(); |
| 96 | // Just in time update of setup with programmatically provided values |
| 97 | contrib = _configurator.getConfiguration(factoryParams.getServiceId(), contrib); |
| 98 | |
| 99 | AbstractCauchoProxy proxy = null; |
| 100 | if (contrib.getProtocol() == net.sourceforge.hiveremoting.caucho.Protocol.Hessian) |
| 101 | { |
| 102 | proxy = new HessianProxy(contrib, _factory, _cnxManager, logger); |
| 103 | } |
| 104 | else if (contrib.getProtocol() == net.sourceforge.hiveremoting.caucho.Protocol.Burlap) |
| 105 | { |
| 106 | proxy = new BurlapProxy(contrib, _factory, _cnxManager, logger); |
| 107 | } |
| 108 | return Proxy.newProxyInstance( |
| 109 | factoryParams.getInvokingModule().getClassResolver().getClassLoader(), |
| 110 | new Class[] {factoryParams.getServiceInterface()}, |
| 111 | proxy); |
| 112 | } |
| 113 | |
| 114 | static private final int DEFAULT_HTTPS_PORT = 443; |
| 115 | |
| 116 | private final CauchoProxyConfigurator _configurator; |
| 117 | private final HttpConnectionManager _cnxManager; |
| 118 | private final SerializerFactory _factory; |
| 119 | } |