diff options
author | Roberto E. Escobar | 2014-07-02 01:13:24 +0000 |
---|---|---|
committer | Roberto E. Escobar | 2014-08-28 23:59:01 +0000 |
commit | 2779a1c3f7d5ceddab0e2fa9afe16646a861f34c (patch) | |
tree | 2ae2c940e1e796aebe9d3c39f761c67a02eb928d /plugins/org.eclipse.osee.jaxrs.server | |
parent | a63e2c74baba840f036a43ee078e2a3c186ca5ef (diff) | |
download | org.eclipse.osee-2779a1c3f7d5ceddab0e2fa9afe16646a861f34c.tar.gz org.eclipse.osee-2779a1c3f7d5ceddab0e2fa9afe16646a861f34c.tar.xz org.eclipse.osee-2779a1c3f7d5ceddab0e2fa9afe16646a861f34c.zip |
feature[ats_ATS72784]: Enable dynamic server side JAX-RS provider contributions
Change-Id: I23871b7570451c114f64189fedd4e860bdc15fc3
Diffstat (limited to 'plugins/org.eclipse.osee.jaxrs.server')
15 files changed, 413 insertions, 102 deletions
diff --git a/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF index b656c031ed9..8ed35180099 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.jaxrs.server/META-INF/MANIFEST.MF @@ -37,5 +37,6 @@ Import-Package: com.google.common.io, org.eclipse.osee.logger, org.eclipse.osee.template.engine, org.osgi.framework, - org.osgi.service.http + org.osgi.service.http, + org.osgi.util.tracker;version="1.5.1" Bundle-Vendor: Eclipse Open System Engineering Environment diff --git a/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.application.manager.xml b/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.application.manager.xml index b037334b642..9c8d81ffabc 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.application.manager.xml +++ b/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.application.manager.xml @@ -1,7 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" configuration-policy="optional" deactivate="stop" enabled="true" immediate="true" modified="update"> <implementation class="org.eclipse.osee.jaxrs.server.internal.JaxRsApplicationManager" /> - - <reference interface="javax.ws.rs.core.Application" name="Application" cardinality="1..n" policy="dynamic" bind="addApplication" unbind="removeApplication" /> <reference bind="setJaxRsApplicationRegistry" cardinality="1..1" interface="org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry" name="JaxRsApplicationRegistry" policy="static" unbind="unsetJaxRsApplicationRegistry"/> + <reference interface="javax.ws.rs.core.Application" name="Application" cardinality="1..n" policy="dynamic" bind="addService" unbind="removeService" /> </scr:component> diff --git a/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.providers.manager.xml b/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.providers.manager.xml new file mode 100644 index 00000000000..6c972d64577 --- /dev/null +++ b/plugins/org.eclipse.osee.jaxrs.server/OSGI-INF/jaxrs.providers.manager.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" configuration-policy="optional" deactivate="stop" enabled="true" immediate="true" modified="update"> + <implementation class="org.eclipse.osee.jaxrs.server.internal.JaxRsProvidersManager" /> + <reference bind="setJaxRsApplicationRegistry" cardinality="1..1" interface="org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry" name="JaxRsApplicationRegistry" policy="static" unbind="unsetJaxRsApplicationRegistry"/> + <reference bind="addService" cardinality="1..n" interface="java.lang.Object" name="Provider" policy="dynamic" target="(&(objectClass=javax.ws.rs.*))" unbind="removeService"/> +</scr:component> diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsAdminApplication.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsAdminApplication.java index bf031667601..fc86f58318e 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsAdminApplication.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsAdminApplication.java @@ -15,7 +15,7 @@ import java.util.Set; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry; -import org.eclipse.osee.jaxrs.server.internal.resources.ApplicationResource; +import org.eclipse.osee.jaxrs.server.internal.resources.JaxRsContributionsResource; /** * @author Roberto E. Escobar @@ -32,7 +32,7 @@ public class JaxRsAdminApplication extends Application { } public void start() { - singletons.add(new ApplicationResource(registry)); + singletons.add(new JaxRsContributionsResource(registry)); } public void stop() { diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsApplicationManager.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsApplicationManager.java index e10ef7e8f73..32e71cab53c 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsApplicationManager.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsApplicationManager.java @@ -10,11 +10,7 @@ *******************************************************************************/ package org.eclipse.osee.jaxrs.server.internal; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; import javax.ws.rs.core.Application; import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry; import org.osgi.framework.Bundle; @@ -23,46 +19,13 @@ import org.osgi.framework.ServiceReference; /** * @author Roberto E. Escobar */ -public final class JaxRsApplicationManager { +public final class JaxRsApplicationManager extends JaxRsDynamicServiceManager<Application> { - private final List<ServiceReference<Application>> pendingReg = new ArrayList<ServiceReference<Application>>(); - private final AtomicReference<JaxRsApplicationRegistry> registryRef = - new AtomicReference<JaxRsApplicationRegistry>(); - private volatile JaxRsConfiguration config; - - public void setJaxRsApplicationRegistry(JaxRsApplicationRegistry registry) { - registryRef.set(registry); - } - - public void unsetJaxRsApplicationRegistry(JaxRsApplicationRegistry registry) { - registryRef.set(null); - } - - private JaxRsApplicationRegistry getRegistry() { - return registryRef.get(); - } - - public void start(Map<String, Object> props) throws Exception { - update(props); - final JaxRsApplicationRegistry registry = getRegistry(); - synchronized (pendingReg) { - Iterator<ServiceReference<Application>> iterator = pendingReg.iterator(); - while (iterator.hasNext()) { - ServiceReference<Application> reference = iterator.next(); - register(registry, reference); - iterator.remove(); - } - } - } - - public void stop() { - JaxRsApplicationRegistry registry = getRegistry(); - if (registry != null) { - registry.deregisterAll(); - } - } + private JaxRsConfiguration config; + @Override public void update(Map<String, Object> props) { + super.update(props); config = JaxRsConfiguration.fromProperties(props).build(); JaxRsApplicationRegistry registry = getRegistry(); if (registry != null) { @@ -70,30 +33,8 @@ public final class JaxRsApplicationManager { } } - public void addApplication(ServiceReference<Application> reference) throws Exception { - JaxRsApplicationRegistry registry = getRegistry(); - if (registry != null) { - register(registry, reference); - } else { - synchronized (pendingReg) { - pendingReg.add(reference); - } - } - } - - public void removeApplication(ServiceReference<Application> reference) { - JaxRsApplicationRegistry registry = getRegistry(); - if (registry != null) { - String componentName = JaxRsUtils.getComponentName(reference); - registry.deregister(componentName); - } else { - synchronized (pendingReg) { - pendingReg.remove(reference); - } - } - } - - private void register(JaxRsApplicationRegistry registry, ServiceReference<Application> reference) throws Exception { + @Override + public void register(JaxRsApplicationRegistry registry, ServiceReference<Application> reference) { String componentName = JaxRsUtils.getComponentName(reference); Bundle bundle = reference.getBundle(); Application application = bundle.getBundleContext().getService(reference); @@ -101,4 +42,10 @@ public final class JaxRsApplicationManager { registry.register(componentName, contextName, bundle, application); } + @Override + public void deregister(JaxRsApplicationRegistry registry, ServiceReference<Application> reference) { + String componentName = JaxRsUtils.getComponentName(reference); + registry.deregister(componentName); + } + } diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsDynamicServiceManager.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsDynamicServiceManager.java new file mode 100644 index 00000000000..b8b059eda78 --- /dev/null +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsDynamicServiceManager.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2014 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.jaxrs.server.internal; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; +import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry; +import org.osgi.framework.ServiceReference; + +/** + * @author Roberto E. Escobar + */ +public abstract class JaxRsDynamicServiceManager<T> { + + private final List<ServiceReference<T>> pendingReg = new ArrayList<ServiceReference<T>>(); + private final AtomicReference<JaxRsApplicationRegistry> registryRef = + new AtomicReference<JaxRsApplicationRegistry>(); + + public void setJaxRsApplicationRegistry(JaxRsApplicationRegistry registry) { + registryRef.set(registry); + } + + public void unsetJaxRsApplicationRegistry(JaxRsApplicationRegistry registry) { + registryRef.set(null); + } + + protected JaxRsApplicationRegistry getRegistry() { + return registryRef.get(); + } + + public void start(Map<String, Object> props) { + update(props); + JaxRsApplicationRegistry registry = getRegistry(); + synchronized (pendingReg) { + Iterator<ServiceReference<T>> iterator = pendingReg.iterator(); + while (iterator.hasNext()) { + ServiceReference<T> reference = iterator.next(); + register(registry, reference); + iterator.remove(); + } + } + } + + public void stop(Map<String, Object> props) { + JaxRsApplicationRegistry registry = getRegistry(); + if (registry != null) { + synchronized (pendingReg) { + Iterator<ServiceReference<T>> iterator = pendingReg.iterator(); + while (iterator.hasNext()) { + ServiceReference<T> reference = iterator.next(); + deregister(registry, reference); + iterator.remove(); + } + } + } + } + + public void update(Map<String, Object> props) { + // + } + + public void addService(ServiceReference<T> reference) { + JaxRsApplicationRegistry registry = getRegistry(); + if (registry != null) { + register(registry, reference); + } else { + synchronized (pendingReg) { + pendingReg.add(reference); + } + } + } + + public void removeService(ServiceReference<T> reference) { + JaxRsApplicationRegistry registry = getRegistry(); + if (registry != null) { + deregister(registry, reference); + } else { + synchronized (pendingReg) { + pendingReg.remove(reference); + } + } + } + + public abstract void register(JaxRsApplicationRegistry registry, ServiceReference<T> reference); + + public abstract void deregister(JaxRsApplicationRegistry registry, ServiceReference<T> reference); + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsProvidersManager.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsProvidersManager.java new file mode 100644 index 00000000000..66ad9587d17 --- /dev/null +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsProvidersManager.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2014 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.jaxrs.server.internal; + +import javax.ws.rs.container.DynamicFeature; +import javax.ws.rs.core.Feature; +import javax.ws.rs.ext.Provider; +import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegistry; +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceReference; + +/** + * @author Roberto E. Escobar + */ +public final class JaxRsProvidersManager extends JaxRsDynamicServiceManager<Object> { + + @Override + public void register(JaxRsApplicationRegistry registry, ServiceReference<Object> reference) { + String componentName = JaxRsUtils.getComponentName(reference); + Bundle bundle = reference.getBundle(); + Object provider = bundle.getBundleContext().getService(reference); + if (isJaxRsProvider(provider)) { + registry.registerProvider(componentName, bundle, provider); + } + } + + @Override + public void deregister(JaxRsApplicationRegistry registry, ServiceReference<Object> reference) { + String componentName = JaxRsUtils.getComponentName(reference); + registry.deregisterProvider(componentName); + } + + private boolean isJaxRsProvider(Object service) { + return service != null && (hasRegisterableAnnotation(service) || // + service instanceof Feature || service instanceof DynamicFeature); + } + + private boolean hasRegisterableAnnotation(Object service) { + boolean result = hasJaxRsAnnotation(service.getClass()); + if (!result) { + Class<?>[] interfaces = service.getClass().getInterfaces(); + for (Class<?> type : interfaces) { + result = result || hasJaxRsAnnotation(type); + } + } + return result; + } + + private boolean hasJaxRsAnnotation(Class<?> type) { + return type.isAnnotationPresent(Provider.class); + } + +} diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsVisitor.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsVisitor.java index eee0d55de43..d22179e24a7 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsVisitor.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/JaxRsVisitor.java @@ -26,6 +26,10 @@ public class JaxRsVisitor { // } + public void onProvider(String componentName, Bundle bundle, Object provider) { + // + } + public void onStartApplicationContainer(String applicationContext, int size) { // } diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsApplicationContainer.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsApplicationContainer.java index 2b2f5a7a1be..3a712328818 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsApplicationContainer.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsApplicationContainer.java @@ -108,6 +108,11 @@ public abstract class AbstractJaxRsApplicationContainer implements JaxRsVisitabl }; } + @Override + public String toString() { + return " applicationContext=" + applicationContext; + } + private final class JaxRsApplicationEntry { private final Bundle bundle; private final Application application; diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsContainer.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsContainer.java index 4a497a305d1..67135cf3f3e 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsContainer.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/AbstractJaxRsContainer.java @@ -12,6 +12,7 @@ package org.eclipse.osee.jaxrs.server.internal.applications; import java.util.Dictionary; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -29,13 +30,15 @@ import org.osgi.service.http.HttpService; /** * @author Roberto E. Escobar */ -public abstract class AbstractJaxRsContainer<H extends HttpServlet, C extends AbstractJaxRsApplicationContainer> implements JaxRsContainer, JaxRsVisitable { +public abstract class AbstractJaxRsContainer<H extends HttpServlet, C extends AbstractJaxRsApplicationContainer, F extends JaxRsProvider> implements JaxRsContainer, JaxRsVisitable, JaxRsProviders { private final Log logger; private final HttpService httpService; private final Dictionary<String, Object> props; private final ConcurrentHashMap<String, C> applications = new ConcurrentHashMap<String, C>(); + private final ConcurrentHashMap<String, F> providers = new ConcurrentHashMap<String, F>(); + private final Map<String, String> componentToContext = new ConcurrentHashMap<String, String>(); private final AtomicReference<String> servletContextName = new AtomicReference<String>(); private final AtomicBoolean isRegistered = new AtomicBoolean(false); @@ -73,7 +76,17 @@ public abstract class AbstractJaxRsContainer<H extends HttpServlet, C extends Ab @Override public boolean isEmpty() { - return applications.isEmpty(); + return applications.isEmpty() && providers.isEmpty(); + } + + @Override + public boolean hasProviders() { + return !providers.isEmpty(); + } + + @Override + public Iterable<? extends JaxRsProvider> getProviders() { + return providers.values(); } @Override @@ -82,6 +95,10 @@ public abstract class AbstractJaxRsContainer<H extends HttpServlet, C extends Ab for (C container : applications.values()) { container.accept(visitor); } + for (Entry<String, F> item : providers.entrySet()) { + F value = item.getValue(); + visitor.onProvider(item.getKey(), value.getBundle(), value.getProvider()); + } } protected abstract C createApplicationContainer(String applicationContext); @@ -92,6 +109,8 @@ public abstract class AbstractJaxRsContainer<H extends HttpServlet, C extends Ab protected abstract void stopContainer(C container); + protected abstract F createJaxRsProvider(Bundle bundle, Object provider); + private C getContextContainerInitIfNull(String applicationContext) { C container = applications.get(applicationContext); if (container == null) { @@ -139,6 +158,45 @@ public abstract class AbstractJaxRsContainer<H extends HttpServlet, C extends Ab } } + private void registerProviders() { + startServlet(); + for (C container : applications.values()) { + stopContainer(container); + try { + startContainer(container); + } catch (Exception ex) { + logger.error(ex, "Error starting container [%s] on [%s]", this, container); + } + } + } + + @Override + public synchronized void addProvider(String componentName, Bundle bundle, Object provider) { + logger.trace("Add Provider - [%s] - provider[%s]", this, componentName); + F entry = createJaxRsProvider(bundle, provider); + providers.put(componentName, entry); + if (!applications.isEmpty()) { + registerProviders(); + } + } + + @Override + public synchronized void removeProvider(String componentName) { + logger.trace("Remove Provider - [%s] - provider[%s]", this, componentName); + JaxRsProvider feature = providers.remove(componentName); + if (feature != null) { + for (Entry<String, C> entry : applications.entrySet()) { + C container = entry.getValue(); + stopContainer(container); + if (container.isEmpty()) { + applications.remove(entry.getKey()); + } else { + startContainer(container); + } + } + } + } + @Override public synchronized void start() { startServlet(); diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsApplicationRegistry.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsApplicationRegistry.java index f30c1b62fe7..6749efbf9e2 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsApplicationRegistry.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsApplicationRegistry.java @@ -51,6 +51,11 @@ public class JaxRsApplicationRegistry implements JaxRsVisitable { void stop(); void accept(JaxRsVisitor visitor); + + void addProvider(String componentName, Bundle bundle, Object provider); + + void removeProvider(String componentName); + } private final ConcurrentHashMap<String, JaxRsContainerProvider> servlets = @@ -75,6 +80,14 @@ public class JaxRsApplicationRegistry implements JaxRsVisitable { public void stop() { logger.trace("Stopping [%s]...", getClass().getSimpleName()); + Iterator<JaxRsContainerProvider> iterator = servlets.values().iterator(); + while (iterator.hasNext()) { + JaxRsContainer container = iterator.next().get(); + if (container != null) { + container.stop(); + } + iterator.remove(); + } } public String getBaseContext() { @@ -96,10 +109,17 @@ public class JaxRsApplicationRegistry implements JaxRsVisitable { container.accept(new JaxRsVisitor() { @Override public void onApplication(String applicationContext, String componentName, Bundle bundle, Application application) { - final String contextName = getBaseContext(); - JaxRsContainer newContainer = getContainerInitIfNull(contextName); + final String baseContext = getBaseContext(); + JaxRsContainer newContainer = getContainerInitIfNull(baseContext); newContainer.addApplication(componentName, applicationContext, bundle, application); } + + @Override + public void onProvider(String componentName, Bundle bundle, Object provider) { + final String baseContext = getBaseContext(); + JaxRsContainer newContainer = getContainerInitIfNull(baseContext); + newContainer.addProvider(componentName, bundle, provider); + } }); } } @@ -113,24 +133,30 @@ public class JaxRsApplicationRegistry implements JaxRsVisitable { } public void deregister(String componentName) { - final String contextName = getBaseContext(); - JaxRsContainer container = getContainerOrNull(contextName); + final String baseContext = getBaseContext(); + JaxRsContainer container = getContainerOrNull(baseContext); if (container != null) { container.removeApplication(componentName); if (container.isEmpty()) { - servlets.remove(contextName); + servlets.remove(baseContext); } } } - public void deregisterAll() { - Iterator<JaxRsContainerProvider> iterator = servlets.values().iterator(); - while (iterator.hasNext()) { - JaxRsContainer container = iterator.next().get(); - if (container != null) { - container.stop(); + public void registerProvider(String componentName, Bundle bundle, Object provider) { + final String baseContext = getBaseContext(); + JaxRsContainer container = getContainerInitIfNull(baseContext); + container.addProvider(componentName, bundle, provider); + } + + public void deregisterProvider(String componentName) { + final String baseContext = getBaseContext(); + JaxRsContainer container = getContainerOrNull(baseContext); + if (container != null) { + container.removeProvider(componentName); + if (container.isEmpty()) { + servlets.remove(baseContext); } - iterator.remove(); } } @@ -164,10 +190,11 @@ public class JaxRsApplicationRegistry implements JaxRsVisitable { JaxRsContainerProvider newContainer = factory.newJaxRsContainerProvider(contextName); reference = servlets.putIfAbsent(contextName, newContainer); if (reference == null) { + servlets.put(contextName, newContainer); reference = newContainer; } } return reference.get(); } -}
\ No newline at end of file +} diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsProvider.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsProvider.java new file mode 100644 index 00000000000..7f8ab12acfd --- /dev/null +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsProvider.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2014 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.jaxrs.server.internal.applications; + +import org.osgi.framework.Bundle; + +/** + * @author Roberto E. Escobar + */ +public interface JaxRsProvider { + + Bundle getBundle(); + + Object getProvider(); + +} diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsProviders.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsProviders.java new file mode 100644 index 00000000000..d0d6da1a321 --- /dev/null +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/applications/JaxRsProviders.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2014 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.jaxrs.server.internal.applications; + +/** + * @author Roberto E. Escobar + */ +public interface JaxRsProviders { + + boolean hasProviders(); + + Iterable<? extends JaxRsProvider> getProviders(); + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/ext/CxfJaxRsFactory.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/ext/CxfJaxRsFactory.java index 900bf254833..0f1261f7ea7 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/ext/CxfJaxRsFactory.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/ext/CxfJaxRsFactory.java @@ -43,10 +43,13 @@ import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsApplicationRegis import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsContainerProviderImpl; import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsFactory; import org.eclipse.osee.jaxrs.server.internal.exceptions.ErrorResponseMessageBodyWriter; +import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsProvider; +import org.eclipse.osee.jaxrs.server.internal.applications.JaxRsProviders; import org.eclipse.osee.jaxrs.server.internal.exceptions.GenericExceptionMapper; import org.eclipse.osee.jaxrs.server.internal.filters.SecurityContextFilter; import org.eclipse.osee.jaxrs.server.internal.filters.SecurityContextProviderImpl; import org.eclipse.osee.logger.Log; +import org.osgi.framework.Bundle; import org.osgi.service.http.HttpService; /** @@ -166,15 +169,15 @@ public final class CxfJaxRsFactory implements JaxRsFactory { return container; } - private CxfJaxRsApplicationContainer newApplicationContainer(String applicationContext) { - return new CxfJaxRsApplicationContainer(applicationContext); + private CxfJaxRsApplicationContainer newApplicationContainer(String applicationContext, JaxRsProviders provider) { + return new CxfJaxRsApplicationContainer(applicationContext, provider); } private CXFNonSpringServlet newBaseJaxsRsServlet(JaxRsVisitable visitable) { return new CXFNonSpringServlet(); } - public Server newCxfServer(CXFNonSpringServlet servlet, String applicationPath, Application application) { + public Server newCxfServer(CXFNonSpringServlet servlet, String applicationPath, Application application, JaxRsProviders providers) { String contextName = servlet.getServletName(); Bus bus = servlet.getBus(); if (bus == null) { @@ -192,6 +195,12 @@ public final class CxfJaxRsFactory implements JaxRsFactory { bean.setAddress(subAddress); } + if (providers.hasProviders()) { + for (JaxRsProvider container : providers.getProviders()) { + bean.setProvider(container.getProvider()); + } + } + bean.setProviders(getProviders()); bean.setFeatures(getFeatures()); bean.setProperties(getProperties()); @@ -207,7 +216,7 @@ public final class CxfJaxRsFactory implements JaxRsFactory { return server; } - private final class CxfJaxRsContainer extends AbstractJaxRsContainer<CXFNonSpringServlet, CxfJaxRsApplicationContainer> { + private final class CxfJaxRsContainer extends AbstractJaxRsContainer<CXFNonSpringServlet, CxfJaxRsApplicationContainer, JaxRsProvider> { public CxfJaxRsContainer(Log logger, HttpService httpService, Dictionary<String, Object> props) { super(logger, httpService, props); @@ -215,7 +224,7 @@ public final class CxfJaxRsFactory implements JaxRsFactory { @Override protected CxfJaxRsApplicationContainer createApplicationContainer(String applicationContext) { - return newApplicationContainer(applicationContext); + return newApplicationContainer(applicationContext, this); } @Override @@ -234,20 +243,27 @@ public final class CxfJaxRsFactory implements JaxRsFactory { container.stopContainer(); } + @Override + protected JaxRsProvider createJaxRsProvider(Bundle bundle, Object provider) { + return new JaxRsFeatureImpl(bundle, provider); + } + }; private final class CxfJaxRsApplicationContainer extends AbstractJaxRsApplicationContainer { private final AtomicBoolean isRegistered = new AtomicBoolean(false); + private final JaxRsProviders providers; private volatile Server server; - public CxfJaxRsApplicationContainer(String applicationContext) { + public CxfJaxRsApplicationContainer(String applicationContext, JaxRsProviders providers) { super(applicationContext); + this.providers = providers; } public void startContainer(CXFNonSpringServlet servlet) { if (!isRegistered.getAndSet(true)) { - Server newServer = newCxfServer(servlet, getApplicationContext(), getApplication()); + Server newServer = newCxfServer(servlet, getApplicationContext(), getApplication(), providers); newServer.start(); server = newServer; } @@ -263,4 +279,27 @@ public final class CxfJaxRsFactory implements JaxRsFactory { } } + private static final class JaxRsFeatureImpl implements JaxRsProvider { + + private final Bundle bundle; + private final Object provider; + + public JaxRsFeatureImpl(Bundle bundle, Object provider) { + super(); + this.bundle = bundle; + this.provider = provider; + } + + @Override + public Bundle getBundle() { + return bundle; + } + + @Override + public Object getProvider() { + return provider; + } + + } + } diff --git a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/resources/ApplicationResource.java b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/resources/JaxRsContributionsResource.java index 51211216649..f7ffaac112b 100644 --- a/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/resources/ApplicationResource.java +++ b/plugins/org.eclipse.osee.jaxrs.server/src/org/eclipse/osee/jaxrs/server/internal/resources/JaxRsContributionsResource.java @@ -26,6 +26,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.eclipse.osee.jaxrs.ApplicationInfo; +import org.eclipse.osee.jaxrs.JaxRsContributionInfo; import org.eclipse.osee.jaxrs.server.internal.JaxRsVisitable; import org.eclipse.osee.jaxrs.server.internal.JaxRsVisitor; import org.osgi.framework.Bundle; @@ -34,12 +35,12 @@ import org.osgi.framework.Constants; /** * @author Roberto E. Escobar */ -@Path("application-details") -public class ApplicationResource { +@Path("contributions") +public class JaxRsContributionsResource { private final JaxRsVisitable visitable; - public ApplicationResource(JaxRsVisitable visitable) { + public JaxRsContributionsResource(JaxRsVisitable visitable) { super(); this.visitable = visitable; } @@ -47,8 +48,8 @@ public class ApplicationResource { @PermitAll @GET @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - public List<ApplicationInfo> getApplicationsInfo(final @Context UriInfo uriInfo) { - final List<ApplicationInfo> toReturn = new ArrayList<ApplicationInfo>(); + public List<JaxRsContributionInfo> getContributionDetails(final @Context UriInfo uriInfo) { + final List<JaxRsContributionInfo> toReturn = new ArrayList<JaxRsContributionInfo>(); visitable.accept(new JaxRsVisitor() { @Override @@ -61,7 +62,7 @@ public class ApplicationResource { ApplicationInfo info = new ApplicationInfo(); info.setBundleName(bundleName); info.setVersion(bundleVersion); - info.setApplicationName(componentName); + info.setName(componentName); String absolutePath = getServletPath(); @@ -71,18 +72,36 @@ public class ApplicationResource { toReturn.add(info); } + @Override + public void onProvider(String componentName, Bundle bundle, Object provider) { + Dictionary<String, String> headers = bundle.getHeaders(); + + String bundleName = headers.get(Constants.BUNDLE_SYMBOLICNAME); + String bundleVersion = headers.get(Constants.BUNDLE_VERSION); + + JaxRsContributionInfo info = new JaxRsContributionInfo(); + info.setBundleName(bundleName); + info.setVersion(bundleVersion); + info.setName(componentName); + toReturn.add(info); + } + private String getServletPath() { String absolutePath = uriInfo.getAbsolutePath().toASCIIString(); - absolutePath = absolutePath.replaceAll("/jaxrs-admin/application-details", ""); + absolutePath = absolutePath.replaceAll("/jaxrs-admin/contributions", ""); return absolutePath; } }); - Collections.sort(toReturn, new Comparator<ApplicationInfo>() { + Collections.sort(toReturn, new Comparator<JaxRsContributionInfo>() { @Override - public int compare(ApplicationInfo o1, ApplicationInfo o2) { - return o1.getApplicationName().compareTo(o2.getApplicationName()); + public int compare(JaxRsContributionInfo o1, JaxRsContributionInfo o2) { + int toReturn = o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()); + if (toReturn == 0) { + toReturn = o1.getName().compareTo(o2.getName()); + } + return toReturn; } }); |