Skip to content
Permalink
Browse files
[DOSGI-242] Extract common module, use DS
  • Loading branch information
cschneider committed Jul 1, 2016
1 parent 79bb9a4 commit d2a3c75f3a6496ec52961ee7238fcf519bd9a8a5
Show file tree
Hide file tree
Showing 40 changed files with 398 additions and 634 deletions.
@@ -0,0 +1,6 @@
Import-Package: javax.servlet;version='[2,4)', javax.servlet.http;version='[2,4)', *
Export-Package: \
org.apache.cxf.dosgi.common.httpservice,\
org.apache.cxf.dosgi.common.util,\
org.apache.cxf.dosgi.common.intent
Private-Package: org.apache.cxf.dosgi.common.osgi
@@ -0,0 +1,78 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<artifactId>cxf-dosgi-ri-common</artifactId>
<packaging>bundle</packaging>
<name>CXF dOSGi Common</name>

<parent>
<groupId>org.apache.cxf.dosgi</groupId>
<artifactId>cxf-dosgi-ri-parent</artifactId>
<version>1.9-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<properties>
<topDirectoryLocation>..</topDirectoryLocation>
</properties>

<dependencies>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-aegis</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>${cxf.version}</version>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_${servlet.version}_spec</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

</project>
@@ -16,9 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.dosgi.dsw.httpservice;
package org.apache.cxf.dosgi.common.httpservice;

import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
@@ -34,36 +35,51 @@
import org.osgi.framework.ServiceException;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
configurationPid = "cxf-dsw",
service = HttpServiceManager.class
)
public class HttpServiceManager {

/**
* Prefix to create an absolute URL from a relative URL.
* See HttpServiceManager.getAbsoluteAddress
*
* Defaults to: http://<host name>:8181
*/
public static final String KEY_HTTP_BASE = "httpBase";
public static final String KEY_CXF_SERVLET_ALIAS = "cxfServletAlias";
public static final String DEFAULT_CXF_SERVLET_ALIAS = "/cxf";
private static final Logger LOG = LoggerFactory.getLogger(HttpServiceManager.class);
private static final long SERVICE_LOOKUP_TIMEOUT = 10000;
private ServiceTracker<HttpService, HttpService> tracker;
private BundleContext bundleContext;

private Map<Long, String> exportedAliases = Collections.synchronizedMap(new HashMap<Long, String>());
private String httpBase;
private String cxfServletAlias;

public HttpServiceManager(BundleContext bundleContext, String httpBase, String cxfServletAlias) {
this(bundleContext, httpBase, cxfServletAlias,
new ServiceTracker<HttpService, HttpService>(bundleContext, HttpService.class, null));
this.tracker.open();
private HttpService httpService;
private BundleContext context;

@SuppressWarnings("unchecked")
@Activate
public void activate(ComponentContext compContext) {
Dictionary<String, String> config = compContext.getProperties();
initFromConfig(config);
this.context = compContext.getBundleContext();
}

// Only for tests
public HttpServiceManager(BundleContext bundleContext,
String httpBase, String cxfServletAlias,
ServiceTracker<HttpService, HttpService> tracker) {
this.bundleContext = bundleContext;
this.tracker = tracker;
this.httpBase = getWithDefault(httpBase, "http://" + LocalHostUtil.getLocalIp() + ":8181");
this.cxfServletAlias = getWithDefault(cxfServletAlias, "/cxf");
void initFromConfig(Dictionary<String, String> config) {
if (config == null) {
config = new Hashtable<String, String>();
}
this.httpBase = getWithDefault(config.get(KEY_HTTP_BASE), "http://" + LocalHostUtil.getLocalIp() + ":8181");
this.cxfServletAlias = getWithDefault(config.get(KEY_CXF_SERVLET_ALIAS), "/cxf");
}

private String getWithDefault(String value, String defaultValue) {
@@ -75,9 +91,10 @@ public Bus registerServlet(Bus bus, String contextRoot, BundleContext callingCon
CXFNonSpringServlet cxf = new CXFNonSpringServlet();
cxf.setBus(bus);
try {
HttpService httpService = getHttpService();
HttpContext httpContext1 = httpService.createDefaultHttpContext();
HttpContext httpContext = new SecurityDelegatingHttpContext(callingContext, httpContext1);
httpService.registerServlet(contextRoot, cxf, new Hashtable<String, String>(),
getHttpContext(callingContext, httpService));
httpContext);

registerUnexportHook(sid, contextRoot);

@@ -88,24 +105,6 @@ public Bus registerServlet(Bus bus, String contextRoot, BundleContext callingCon
return bus;
}

protected HttpService getHttpService() {
HttpService service = null;
try {
service = tracker.waitForService(SERVICE_LOOKUP_TIMEOUT);
} catch (InterruptedException ex) {
LOG.warn("waitForService interrupeted", ex);
}
if (service == null) {
throw new RuntimeException("No HTTPService found");
}
return service;
}

private HttpContext getHttpContext(BundleContext bc, HttpService httpService) {
HttpContext httpContext = httpService.createDefaultHttpContext();
return new SecurityDelegatingHttpContext(bc, httpContext);
}

/**
* This listens for service removal events and "un-exports" the service
* from the HttpService.
@@ -122,9 +121,9 @@ private void registerUnexportHook(Long sid, String alias) {
}

try {
Filter f = bundleContext.createFilter("(" + org.osgi.framework.Constants.SERVICE_ID + "=" + sid + ")");
Filter f = context.createFilter("(" + org.osgi.framework.Constants.SERVICE_ID + "=" + sid + ")");
if (f != null) {
bundleContext.addServiceListener(new UnregisterListener(), f.toString());
context.addServiceListener(new UnregisterListener(), f.toString());
} else {
LOG.warn("Service listener could not be started. The service will not be automatically unexported.");
}
@@ -145,10 +144,6 @@ public String getAbsoluteAddress(String contextRoot, String relativeEndpointAddr
return this.httpBase + effContextRoot + relativeEndpointAddress;
}

public void close() {
tracker.close();
}

private final class UnregisterListener implements ServiceListener {

public void serviceChanged(ServiceEvent event) {
@@ -166,11 +161,19 @@ public void serviceChanged(ServiceEvent event) {
}
LOG.debug("Unexporting HTTP servlet for alias '{}'", alias);
try {
HttpService http = getHttpService();
http.unregister(alias);
httpService.unregister(alias);
} catch (Exception e) {
LOG.warn("An exception occurred while unregistering service for HTTP servlet alias '{}'", alias, e);
}
}
}

public void setContext(BundleContext context) {
this.context = context;
}

@Reference
public void setHttpService(HttpService httpService) {
this.httpService = httpService;
}
}
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.dosgi.dsw.httpservice;
package org.apache.cxf.dosgi.common.httpservice;

import java.net.InetAddress;
import java.net.NetworkInterface;
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.dosgi.dsw.httpservice;
package org.apache.cxf.dosgi.common.httpservice;

import java.io.IOException;
import java.net.URL;
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.dosgi.dsw.qos;
package org.apache.cxf.dosgi.common.intent;

import java.util.HashMap;
import java.util.Map;
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.dosgi.dsw.qos;
package org.apache.cxf.dosgi.common.intent;

import java.util.List;
import java.util.Map;
@@ -25,6 +25,10 @@
import org.apache.cxf.feature.Feature;

public interface IntentManager {
@Deprecated
String EXPORTED_INTENTS_OLD = "osgi.remote.requires.intents";

String INTENT_NAME_PROP = "org.apache.cxf.dosgi.IntentName";

String[] applyIntents(List<Feature> features, AbstractEndpointFactory factory, Map<String, Object> props);
void assertAllIntentsSupported(Map<String, Object> serviceProperties);

0 comments on commit d2a3c75

Please sign in to comment.