You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ha...@apache.org on 2015/11/25 02:55:23 UTC
[13/21] incubator-brooklyn git commit: [BROOKLYN-183] Provide access
to management context in OSGi
[BROOKLYN-183] Provide access to management context in OSGi
In order to keep both the monolithic launcher and have the OSGi launcher
working we need a temporary solution that works both ways. This OSGi
antipattern should be removed as soon as client code gets better.
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/94de4642
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/94de4642
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/94de4642
Branch: refs/heads/master
Commit: 94de46427eb84b97449456c02863fcd9ff164b7c
Parents: cbb58ba
Author: Ciprian Ciubotariu <ch...@gmx.net>
Authored: Thu Nov 19 20:44:31 2015 +0200
Committer: Ciprian Ciubotariu <ch...@gmx.net>
Committed: Thu Nov 19 20:44:31 2015 +0200
----------------------------------------------------------------------
.../apache/brooklyn/util/core/osgi/Compat.java | 69 ++++++++++++++++++++
.../BrooklynJavascriptGuiLauncherTest.java | 5 +-
.../BrooklynPropertiesSecurityFilter.java | 4 +-
.../rest/filter/HaMasterCheckFilter.java | 4 +-
.../resources/AbstractBrooklynRestResource.java | 4 +-
.../apache/brooklyn/rest/util/OsgiCompat.java | 46 +++++++++++++
.../util/json/BrooklynJacksonJsonProvider.java | 6 +-
.../brooklyn/rest/BrooklynRestApiLauncher.java | 4 +-
.../BrooklynRestApiLauncherTestFixture.java | 4 +-
9 files changed, 129 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/core/src/main/java/org/apache/brooklyn/util/core/osgi/Compat.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/osgi/Compat.java b/core/src/main/java/org/apache/brooklyn/util/core/osgi/Compat.java
new file mode 100644
index 0000000..1f5d88b
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/util/core/osgi/Compat.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.brooklyn.util.core.osgi;
+
+import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * Compatibility methods between karaf launcher and monolithic launcher.
+ *
+ * @todo Remove after transition to karaf launcher.
+ */
+public class Compat {
+
+ /* synchronized by class initialization */
+ private static class SingletonHolder {
+
+ private static final Compat instance = new Compat();
+ }
+
+ public static Compat getInstance() {
+ return SingletonHolder.instance;
+ }
+
+ private final ServiceTracker managementContextTracker;
+
+ private Compat() {
+ Bundle bundle = FrameworkUtil.getBundle(Compat.class);
+ if (bundle != null) {
+ BundleContext bundleContext = bundle.getBundleContext();
+ managementContextTracker = new ServiceTracker(bundleContext, ManagementContext.class, null);
+ managementContextTracker.open();
+ } else {
+ managementContextTracker = null;
+ }
+ }
+
+ /**
+ * Provides the management context service.
+ *
+ * Either from the encompassing OSGi framework or from the servlet context, depending on which launcher was used.
+ *
+ * @todo This does not allow ungetting the service after usage, so the bundle will remain blocked until all dependent bundles are
+ * stopped.
+ * @fixme Drop this for good after switching to karaf launcher.
+ */
+ public ManagementContext getManagementContext() {
+ if (managementContextTracker != null) {
+ return (ManagementContext) managementContextTracker.getService();
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/jsgui/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
----------------------------------------------------------------------
diff --git a/usage/jsgui/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java b/usage/jsgui/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
index 16c5996..86eb4fa 100644
--- a/usage/jsgui/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
+++ b/usage/jsgui/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
@@ -18,7 +18,6 @@
*/
package org.apache.brooklyn.rest.jsgui;
-import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.test.HttpTestUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
@@ -26,8 +25,8 @@ import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.apache.brooklyn.api.mgmt.ManagementContext;
import org.apache.brooklyn.core.entity.Entities;
-import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
import org.apache.brooklyn.rest.BrooklynRestApiLauncherTestFixture;
+import org.apache.brooklyn.rest.util.OsgiCompat;
import org.eclipse.jetty.server.NetworkConnector;
/** Convenience and demo for launching programmatically. */
@@ -76,7 +75,7 @@ public class BrooklynJavascriptGuiLauncherTest {
}
private ManagementContext getManagementContextFromJettyServerAttributes(Server server) {
- return (ManagementContext) ((ContextHandler)server.getHandler()).getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ return OsgiCompat.getManagementContext((ContextHandler) server.getHandler());
}
}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/BrooklynPropertiesSecurityFilter.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/BrooklynPropertiesSecurityFilter.java b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/BrooklynPropertiesSecurityFilter.java
index 5e43f20..07afa7a 100644
--- a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/BrooklynPropertiesSecurityFilter.java
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/BrooklynPropertiesSecurityFilter.java
@@ -36,11 +36,11 @@ import org.slf4j.LoggerFactory;
import org.apache.brooklyn.api.mgmt.ManagementContext;
import org.apache.brooklyn.core.mgmt.entitlement.Entitlements;
import org.apache.brooklyn.core.mgmt.entitlement.WebEntitlementContext;
-import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
import org.apache.brooklyn.rest.security.provider.DelegatingSecurityProvider;
import org.apache.brooklyn.util.text.Strings;
import com.sun.jersey.core.util.Base64;
+import org.apache.brooklyn.rest.util.OsgiCompat;
/**
* Provides basic HTTP authentication.
@@ -155,7 +155,7 @@ public class BrooklynPropertiesSecurityFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
- ManagementContext mgmt = (ManagementContext) config.getServletContext().getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ ManagementContext mgmt = OsgiCompat.getManagementContext(config.getServletContext());
provider = new DelegatingSecurityProvider(mgmt);
}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/HaMasterCheckFilter.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/HaMasterCheckFilter.java b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/HaMasterCheckFilter.java
index 2020c13..71c6c27 100644
--- a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/HaMasterCheckFilter.java
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/filter/HaMasterCheckFilter.java
@@ -36,12 +36,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.brooklyn.api.mgmt.ManagementContext;
import org.apache.brooklyn.api.mgmt.ha.ManagementNodeState;
-import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
import org.apache.brooklyn.rest.domain.ApiError;
import org.apache.brooklyn.rest.util.WebResourceUtils;
import org.apache.brooklyn.util.text.Strings;
import com.google.common.collect.Sets;
+import org.apache.brooklyn.rest.util.OsgiCompat;
/**
* Checks that for requests that want HA master state, the server is up and in that state.
@@ -62,7 +62,7 @@ public class HaMasterCheckFilter implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
servletContext = config.getServletContext();
- mgmt = (ManagementContext) servletContext.getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ mgmt = OsgiCompat.getManagementContext(servletContext);
}
static String lookForProblemIfServerNotRunning(ManagementContext mgmt) {
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/AbstractBrooklynRestResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/AbstractBrooklynRestResource.java b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/AbstractBrooklynRestResource.java
index 1e058f8..a8cc909 100644
--- a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/AbstractBrooklynRestResource.java
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/resources/AbstractBrooklynRestResource.java
@@ -26,8 +26,8 @@ import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.mgmt.ManagementContext;
import org.apache.brooklyn.core.config.render.RendererHints;
import org.apache.brooklyn.core.mgmt.ManagementContextInjectable;
-import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
import org.apache.brooklyn.rest.util.BrooklynRestResourceUtils;
+import org.apache.brooklyn.rest.util.OsgiCompat;
import org.apache.brooklyn.rest.util.WebResourceUtils;
import org.apache.brooklyn.rest.util.json.BrooklynJacksonJsonProvider;
import org.apache.brooklyn.util.core.task.Tasks;
@@ -53,7 +53,7 @@ public abstract class AbstractBrooklynRestResource implements ManagementContextI
protected synchronized Maybe<ManagementContext> mgmtMaybe() {
if (managementContext!=null) return Maybe.of(managementContext);
- managementContext = (ManagementContext) servletContext.getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ managementContext = OsgiCompat.getManagementContext(servletContext);
if (managementContext!=null) return Maybe.of(managementContext);
return Maybe.absent("ManagementContext not available for Brooklyn Jersey Resource "+this);
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/OsgiCompat.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/OsgiCompat.java b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/OsgiCompat.java
new file mode 100644
index 0000000..1570efc
--- /dev/null
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/OsgiCompat.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+package org.apache.brooklyn.rest.util;
+
+import javax.servlet.ServletContext;
+import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
+import org.apache.brooklyn.util.core.osgi.Compat;
+import org.eclipse.jetty.server.handler.ContextHandler;
+
+/**
+ * Compatibility methods between karaf launcher and monolithic launcher.
+ *
+ * @todo Remove after transition to karaf launcher.
+ */
+public class OsgiCompat {
+
+ public static ManagementContext getManagementContext(ServletContext servletContext) {
+ ManagementContext managementContext = Compat.getInstance().getManagementContext();
+ if (managementContext == null && servletContext != null) {
+ managementContext = (ManagementContext) servletContext.getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ }
+ return managementContext;
+ }
+
+ public static ManagementContext getManagementContext(ContextHandler jettyServerHandler) {
+ ManagementContext managementContext = Compat.getInstance().getManagementContext();
+ if (managementContext == null && jettyServerHandler != null) {
+ managementContext = (ManagementContext) jettyServerHandler.getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ }
+ return managementContext;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonJsonProvider.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonJsonProvider.java b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonJsonProvider.java
index f6f0e2d..a58531c 100644
--- a/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonJsonProvider.java
+++ b/usage/rest-server/src/main/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonJsonProvider.java
@@ -28,6 +28,7 @@ import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.internal.BrooklynProperties;
import org.apache.brooklyn.core.mgmt.ManagementContextInjectable;
import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
+import org.apache.brooklyn.rest.util.OsgiCompat;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.codehaus.jackson.map.ObjectMapper;
@@ -158,10 +159,7 @@ public class BrooklynJacksonJsonProvider extends JacksonJsonProvider implements
}
public static ManagementContext getManagementContext(ServletContext servletContext) {
- if (servletContext == null)
- return null;
-
- return (ManagementContext) servletContext.getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ return OsgiCompat.getManagementContext(servletContext);
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java b/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
index d35247c..d003b76 100644
--- a/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
+++ b/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
@@ -70,8 +70,8 @@ import com.google.common.io.Files;
import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.spi.container.servlet.ServletContainer;
-import org.eclipse.jetty.server.NetworkConnector;
import org.apache.brooklyn.rest.filter.SwaggerFilter;
+import org.apache.brooklyn.rest.util.OsgiCompat;
import org.eclipse.jetty.server.NetworkConnector;
/** Convenience and demo for launching programmatically. Also used for automated tests.
@@ -359,7 +359,7 @@ public class BrooklynRestApiLauncher {
FilterHolder filterHolder = new FilterHolder(new ServletContainer(config));
context.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class));
- ManagementContext mgmt = (ManagementContext) context.getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ ManagementContext mgmt = OsgiCompat.getManagementContext(context);
config.getSingletons().add(new ManagementContextProvider(mgmt));
config.getSingletons().add(new ShutdownHandlerProvider(shutdownListener));
}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/94de4642/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java b/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
index 6cb2970..b2c5031 100644
--- a/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
+++ b/usage/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
@@ -28,8 +28,8 @@ import org.apache.brooklyn.core.entity.Entities;
import org.apache.brooklyn.core.internal.BrooklynProperties;
import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
import org.apache.brooklyn.core.server.BrooklynServerConfig;
-import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
import org.apache.brooklyn.rest.security.provider.AnyoneSecurityProvider;
+import org.apache.brooklyn.rest.util.OsgiCompat;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.eclipse.jetty.server.NetworkConnector;
@@ -104,7 +104,7 @@ public abstract class BrooklynRestApiLauncherTestFixture {
}
public static ManagementContext getManagementContextFromJettyServerAttributes(Server server) {
- return (ManagementContext) ((ContextHandler) server.getHandler()).getAttribute(BrooklynServiceAttributes.BROOKLYN_MANAGEMENT_CONTEXT);
+ return OsgiCompat.getManagementContext((ContextHandler) server.getHandler());
}
}