01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.cms.linking.impl;
19:
20: import java.util.HashMap;
21: import java.util.Map;
22:
23: import org.apache.avalon.framework.configuration.Configurable;
24: import org.apache.avalon.framework.configuration.Configuration;
25: import org.apache.avalon.framework.configuration.ConfigurationException;
26: import org.apache.avalon.framework.logger.AbstractLogEnabled;
27: import org.apache.avalon.framework.service.ServiceException;
28: import org.apache.avalon.framework.service.ServiceManager;
29: import org.apache.avalon.framework.service.Serviceable;
30: import org.apache.avalon.framework.thread.ThreadSafe;
31: import org.apache.lenya.cms.cocoon.components.context.ContextUtility;
32: import org.apache.lenya.cms.linking.GlobalProxies;
33: import org.apache.lenya.cms.publication.Proxy;
34:
35: /**
36: * GlobalProxy service implementation.
37: * The class is implemented as a singleton.
38: */
39: public class GlobalProxiesImpl extends AbstractLogEnabled implements
40: GlobalProxies, Serviceable, ThreadSafe, Configurable {
41:
42: private Map ssl2proxy = new HashMap();
43: private ServiceManager manager;
44:
45: public Proxy getProxy(boolean ssl) {
46: Object key = Boolean.valueOf(ssl);
47: Proxy proxy = (Proxy) this .ssl2proxy.get(key);
48: if (proxy == null) {
49: proxy = initializeProxy(key);
50: }
51: return proxy;
52: }
53:
54: protected synchronized Proxy initializeProxy(Object key) {
55: Proxy proxy;
56: proxy = new Proxy();
57: ContextUtility context = null;
58: try {
59: context = (ContextUtility) manager
60: .lookup(ContextUtility.ROLE);
61: proxy.setUrl(context.getRequest().getContextPath());
62: } catch (ServiceException e) {
63: throw new RuntimeException(e);
64: } finally {
65: if (context != null) {
66: this .manager.release(context);
67: }
68: }
69: this .ssl2proxy.put(key, proxy);
70: return proxy;
71: }
72:
73: public void service(ServiceManager manager) throws ServiceException {
74: this .manager = manager;
75: }
76:
77: public void configure(Configuration config)
78: throws ConfigurationException {
79: Configuration[] proxyConfigs = config.getChildren("proxy");
80: for (int p = 0; p < proxyConfigs.length; p++) {
81: boolean ssl = proxyConfigs[p].getAttributeAsBoolean("ssl");
82: String url = proxyConfigs[p].getAttribute("url");
83: Proxy proxy = new Proxy();
84: proxy.setUrl(url);
85: setProxy(ssl, proxy);
86: }
87: }
88:
89: /**
90: * @param ssl If the proxy is responsible for SSL requests.
91: * @param proxy A proxy.
92: */
93: public void setProxy(boolean ssl, Proxy proxy) {
94: this.ssl2proxy.put(Boolean.valueOf(ssl), proxy);
95: }
96:
97: }
|