001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.server.webservices;
018:
019: import java.net.InetAddress;
020: import java.net.URI;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
025: import org.apache.openejb.assembler.classic.ServiceInfo;
026: import org.apache.openejb.loader.SystemInstance;
027: import org.apache.openejb.server.httpd.HttpListener;
028: import org.apache.openejb.server.httpd.HttpListenerRegistry;
029: import org.apache.openejb.server.httpd.HttpRequest;
030: import org.apache.openejb.server.httpd.HttpResponse;
031: import org.apache.openejb.server.httpd.HttpServerFactory;
032: import org.apache.openejb.util.LogCategory;
033: import org.apache.openejb.util.Logger;
034:
035: public class OpenEJBHttpWsRegistry implements WsRegistry {
036: public static final Logger log = Logger.getInstance(
037: LogCategory.OPENEJB_WS, WsService.class);
038: private final HttpListenerRegistry registry;
039: private final List<URI> baseUris = new ArrayList<URI>();
040:
041: public OpenEJBHttpWsRegistry() {
042: try {
043: OpenEjbConfiguration configuration = SystemInstance.get()
044: .getComponent(OpenEjbConfiguration.class);
045: for (ServiceInfo service : configuration.facilities.services) {
046: if (service.className.equals(HttpServerFactory.class
047: .getName())) {
048: int port = Integer.parseInt(service.properties
049: .getProperty("port"));
050: String ip = service.properties.getProperty("bind");
051: if ("0.0.0.0".equals(ip)) {
052: InetAddress[] addresses = InetAddress
053: .getAllByName(ip);
054: for (InetAddress address : addresses) {
055: baseUris.add(new URI("http", null, address
056: .getHostAddress(), port, null,
057: null, null));
058: }
059: } else {
060: baseUris.add(new URI("http", null, ip, port,
061: null, null, null));
062: }
063: break;
064: }
065: }
066: } catch (Exception e) {
067: log
068: .error(
069: "Webservices Disabled: Unable to build base URIs for WebService registry",
070: e);
071: }
072: registry = SystemInstance.get().getComponent(
073: HttpListenerRegistry.class);
074: }
075:
076: public List<String> setWsContainer(String virtualHost,
077: String contextRoot, String servletName,
078: HttpListener wsContainer) throws Exception {
079: throw new UnsupportedOperationException(
080: "OpenEJB http server does not support POJO webservices");
081: }
082:
083: public void clearWsContainer(String virtualHost,
084: String contextRoot, String servletName) {
085: }
086:
087: public List<String> addWsContainer(String path,
088: HttpListener httpListener, String virtualHost, // ignored
089: String realmName, // ignored
090: String transportGuarantee, // ignored
091: String authMethod, // ignored
092: ClassLoader classLoader) throws Exception {
093:
094: if (path == null)
095: throw new NullPointerException("contextRoot is null");
096: if (httpListener == null)
097: throw new NullPointerException("httpListener is null");
098:
099: // assure context root with a leading slash
100: if (!path.startsWith("/"))
101: path = "/" + path;
102:
103: httpListener = new ClassLoaderHttpListener(httpListener,
104: classLoader);
105: registry.addHttpListener(httpListener, path);
106:
107: // register wsdl locations for service-ref resolution
108: List<String> addresses = new ArrayList<String>();
109: for (URI baseUri : baseUris) {
110: URI address = baseUri.resolve(path);
111: addresses.add(address.toString());
112: }
113: return addresses;
114: }
115:
116: public void removeWsContainer(String path) {
117: registry.removeHttpListener(path);
118: }
119:
120: private static class ClassLoaderHttpListener implements
121: HttpListener {
122: private final HttpListener delegate;
123: private final ClassLoader classLoader;
124:
125: private ClassLoaderHttpListener(HttpListener delegate,
126: ClassLoader classLoader) {
127: this .delegate = delegate;
128: this .classLoader = classLoader;
129: }
130:
131: public void onMessage(HttpRequest request, HttpResponse response)
132: throws Exception {
133: ClassLoader oldCl = Thread.currentThread()
134: .getContextClassLoader();
135: Thread.currentThread().setContextClassLoader(classLoader);
136: try {
137: delegate.onMessage(request, response);
138: } finally {
139: Thread.currentThread().setContextClassLoader(oldCl);
140: }
141: }
142: }
143: }
|