Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Optimized all resource tracker to stop tracking services that are not re... #93

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
/*******************************************************************************
* Copyright (c) 2012,2015 EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* Copyright (c) 2012,2015 EclipseSource and others. All rights reserved. This program and the
* accompanying materials are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Holger Staudacher - initial API and implementation
* ProSyst Software GmbH. - compatibility with OSGi specification 4.2 APIs
* Ivan Iliev - added ServletConfigurationTracker
* http://www.eclipse.org/legal/epl-v10.html Contributors: Holger Staudacher - initial API and

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kolkoo You seem to have messed up the formatting of the license headers...

* implementation ProSyst Software GmbH. - compatibility with OSGi specification 4.2 APIs Ivan Iliev
* - added ServletConfigurationTracker
******************************************************************************/
package com.eclipsesource.jaxrs.publisher.internal;

Expand All @@ -30,7 +26,6 @@

import com.eclipsesource.jaxrs.publisher.ResourceFilter;


public class Activator implements BundleActivator {

private ServiceRegistration connectorRegistration;
Expand All @@ -43,12 +38,14 @@ public class Activator implements BundleActivator {

@Override
public void start( BundleContext context ) throws Exception {
System.setProperty( "javax.ws.rs.ext.RuntimeDelegate",
System.setProperty( "javax.ws.rs.ext.RuntimeDelegate",
"org.glassfish.jersey.server.internal.RuntimeDelegateImpl" );
startJerseyServer();
jaxRsConnector = new JAXRSConnector( context );
registerConfiguration( context );
connectorRegistration = context.registerService( JAXRSConnector.class.getName(), jaxRsConnector, null );
connectorRegistration = context.registerService( JAXRSConnector.class.getName(),
jaxRsConnector,
null );
openHttpServiceTracker( context );
openServletConfigurationTracker( context );
openApplicationConfigurationTracker( context );
Expand Down Expand Up @@ -79,12 +76,12 @@ private void openServletConfigurationTracker( BundleContext context ) {
servletConfigurationTracker = new ServletConfigurationTracker( context, jaxRsConnector );
servletConfigurationTracker.open();
}

private void openApplicationConfigurationTracker( BundleContext context ) {
applicationConfigurationTracker = new ApplicationConfigurationTracker( context, jaxRsConnector );
applicationConfigurationTracker.open();
}

private void openAllServiceTracker( BundleContext context ) throws InvalidSyntaxException {
ResourceFilter allResourceFilter = getResourceFilter( context );
allTracker = new ResourceTracker( context, allResourceFilter.getFilter(), jaxRsConnector );
Expand All @@ -101,6 +98,7 @@ private ResourceFilter getResourceFilter( BundleContext context ) {

@Override
public void stop( BundleContext context ) throws Exception {

httpTracker.close();
servletConfigurationTracker.close();
applicationConfigurationTracker.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ public class HttpTracker extends ServiceTracker {
}

@Override
public HttpService addingService( ServiceReference reference ) {
public Object addingService( ServiceReference reference ) {
return connector.addHttpService( reference );
}

public void removedService( ServiceReference reference, HttpService service ) {
connector.removeHttpService( service );
@Override
public void removedService( ServiceReference reference, Object service ) {
if(service instanceof HttpService) {
connector.removeHttpService( (HttpService)service );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/*******************************************************************************
* Copyright (c) 2012 EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* Copyright (c) 2012 EclipseSource and others. All rights reserved. This program and the
* accompanying materials are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Holger Staudacher - initial API and implementation
* ProSyst Software GmbH. - compatibility with OSGi specification 4.2 APIs
* http://www.eclipse.org/legal/epl-v10.html Contributors: Holger Staudacher - initial API and
* implementation ProSyst Software GmbH. - compatibility with OSGi specification 4.2 APIs
******************************************************************************/
package com.eclipsesource.jaxrs.publisher.internal;

Expand All @@ -24,13 +20,11 @@
import com.eclipsesource.jaxrs.publisher.ServletConfiguration;
import com.eclipsesource.jaxrs.publisher.internal.ServiceContainer.ServiceHolder;


public class JAXRSConnector {

private static final String HTTP_SERVICE_PORT_PROPERTY = "org.osgi.service.http.port";
private static final String RESOURCE_HTTP_PORT_PROPERTY = "http.port";
private static final String DEFAULT_HTTP_PORT = "80";

private final Object lock = new Object();
private final ServiceContainer httpServices;
private final ServiceContainer resources;
Expand All @@ -50,57 +44,75 @@ public class JAXRSConnector {
this.resourceCache = new ArrayList<ServiceHolder>();
this.applicationConfigurations = new ServiceContainer( bundleContext );
}

void updateConfiguration( Configuration configuration ) {
synchronized( lock ) {
this.configuration = configuration;
doUpdateHttpServices();
doUpdateConfiguration(configuration);
}
}

HttpService addHttpService( ServiceReference reference ) {
synchronized( lock ) {
return doAddHttpService( reference );
}
}

ServletConfiguration setServletConfiguration( ServiceReference reference ) {
if( servletConfiguration == null ) {
servletConfiguration = ( ServletConfiguration )bundleContext.getService( reference );
doUpdateHttpServices();
doUpdateServletConfiguration();
return servletConfiguration;
}
return null;
}
void unsetServletConfiguration(ServiceReference reference, ServletConfiguration service) {

void unsetServletConfiguration( ServiceReference reference, ServletConfiguration service ) {
if( servletConfiguration == service ) {
servletConfiguration = null;
bundleContext.ungetService( reference );
doUpdateHttpServices();
doUpdateServletConfiguration();
}
}
}

ApplicationConfiguration addApplicationConfiguration( ServiceReference reference ) {
synchronized( lock ) {
ApplicationConfiguration service = ( ApplicationConfiguration )applicationConfigurations.add( reference ).getService();
doUpdateHttpServices();
ApplicationConfiguration service = ( ApplicationConfiguration )applicationConfigurations.add( reference )
.getService();
doUpdateAppConfiguration();
return service;
}
}

void removeApplicationConfiguration( ServiceReference reference, ApplicationConfiguration service ) {
void removeApplicationConfiguration( ServiceReference reference, ApplicationConfiguration service )
{
synchronized( lock ) {
applicationConfigurations.remove( service );
doUpdateHttpServices();
doUpdateAppConfiguration();
}
}

private void doUpdateServletConfiguration() {
ServiceHolder[] services = httpServices.getServices();
for( ServiceHolder serviceHolder : services ) {
contextMap.get( serviceHolder.getService() )
.updateServletConfiguration( servletConfiguration );
}
}

private void doUpdateAppConfiguration() {
ServiceHolder[] services = httpServices.getServices();
for( ServiceHolder serviceHolder : services ) {
contextMap.get( serviceHolder.getService() )
.updateAppConfiguration( applicationConfigurations );
}
}

private void doUpdateHttpServices() {
private void doUpdateConfiguration(Configuration configuration) {
ServiceHolder[] services = httpServices.getServices();
for( ServiceHolder serviceHolder : services ) {
doRemoveHttpService( ( HttpService )serviceHolder.getService() );
doAddHttpService( serviceHolder.getReference() );
contextMap.get( serviceHolder.getService() )
.updateConfiguration( configuration );
}
}

Expand Down Expand Up @@ -221,6 +233,9 @@ JerseyContext createJerseyContext( HttpService service,
Configuration configuration,
ServletConfiguration servletConfiguration )
{
return new JerseyContext( service, configuration, servletConfiguration, applicationConfigurations );
return new JerseyContext( service,
configuration,
servletConfiguration,
applicationConfigurations );
}
}
Loading