Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.virgo.kernel.deployer.dm/src')
-rw-r--r--org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ApplicationBundleUtils.java40
-rw-r--r--org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ModuleContextWrapper.java101
-rw-r--r--org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessor.java71
-rw-r--r--org.eclipse.virgo.kernel.deployer.dm/src/main/resources/META-INF/spring/deployer-dm-context.xml16
-rw-r--r--org.eclipse.virgo.kernel.deployer.dm/src/test/java/.gitignore0
-rw-r--r--org.eclipse.virgo.kernel.deployer.dm/src/test/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessorTests.java142
-rw-r--r--org.eclipse.virgo.kernel.deployer.dm/src/test/resources/.gitignore0
7 files changed, 370 insertions, 0 deletions
diff --git a/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ApplicationBundleUtils.java b/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ApplicationBundleUtils.java
new file mode 100644
index 00000000..99d0201e
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ApplicationBundleUtils.java
@@ -0,0 +1,40 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * 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:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.deployer.app.spring;
+
+import org.osgi.framework.Bundle;
+
+/**
+ * Utility methods for working with server application module bundles. <p/>
+ *
+ * <strong>Concurrent Semantics</strong><br />
+ *
+ * Thread-safe.
+ *
+ */
+public final class ApplicationBundleUtils {
+
+ private static final String MODULE_TYPE_MANIFEST_HEADER = "Module-Type";
+
+
+ /**
+ * Gets the server module type for the supplied {@link Bundle}.
+ *
+ * @param bundle the <code>Bundle</code>.
+ * @return the server module type, or <code>null</code> if the supplied <code>Bundle</code> is not a server
+ * application module.
+ */
+ public static String getModuleType(Bundle bundle) {
+ String value = (String) bundle.getHeaders().get(MODULE_TYPE_MANIFEST_HEADER);
+ return (value == null || value.length() == 0 ? null : value);
+ }
+}
diff --git a/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ModuleContextWrapper.java b/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ModuleContextWrapper.java
new file mode 100644
index 00000000..1f8c6c91
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/ModuleContextWrapper.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * 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:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.deployer.app.spring;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ConfigurableApplicationContext;
+
+import org.eclipse.virgo.kernel.module.Component;
+import org.eclipse.virgo.kernel.module.ModuleContext;
+import org.eclipse.virgo.kernel.module.NoSuchComponentException;
+
+/**
+ * {@link ModuleContextWrapper} wraps a {@link ConfigurableApplicationContext} to provide an equivalent {@link ModuleContext}.
+ * <p />
+ *
+ * <strong>Concurrent Semantics</strong><br />
+ *
+ * Thread safe.
+ *
+ */
+final class ModuleContextWrapper implements ModuleContext {
+
+ private final ConfigurableApplicationContext appCtx;
+
+ ModuleContextWrapper(ConfigurableApplicationContext appCtx) {
+ this.appCtx = appCtx;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getApplicationContext() {
+ return this.appCtx;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Component getComponent(final String componentName) throws NoSuchComponentException {
+ try {
+ final Object component = this.appCtx.getBean(componentName);
+ return new Component() {
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getName() {
+ return componentName;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getType() {
+ return component.getClass().getName();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isPrototype() {
+ return ModuleContextWrapper.this.appCtx.getBeanFactory().isPrototype(componentName);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isSingleton() {
+ return ModuleContextWrapper.this.appCtx.getBeanFactory().isSingleton(componentName);
+ }
+
+ };
+ } catch (BeansException e) {
+ throw new NoSuchComponentException(e.getMessage());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String[] getComponentNames() {
+ return this.appCtx.getBeanDefinitionNames();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getDisplayName() {
+ return this.appCtx.getDisplayName();
+ }
+
+}
diff --git a/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessor.java b/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessor.java
new file mode 100644
index 00000000..0821a1e7
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer.dm/src/main/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessor.java
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * 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:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.deployer.app.spring;
+
+import org.eclipse.virgo.kernel.serviceability.Assert;
+import org.eclipse.virgo.kernel.serviceability.NonNull;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.springframework.context.ApplicationContext;
+import org.springframework.osgi.context.ConfigurableOsgiBundleApplicationContext;
+
+import org.eclipse.virgo.kernel.module.ModuleContext;
+import org.eclipse.virgo.kernel.module.ModuleContextAccessor;
+
+
+/**
+ * {@link UserRegionModuleContextAccessor} accesses {@link ModuleContext ModuleContexts} in the user region.
+ * <p />
+ *
+ * <strong>Concurrent Semantics</strong><br />
+ *
+ * Thread safe.
+ *
+ */
+final class UserRegionModuleContextAccessor implements ModuleContextAccessor {
+
+ /**
+ * {@inheritDoc}
+ */
+ public ModuleContext getModuleContext(@NonNull Bundle bundle) {
+ BundleContext bundleContext = bundle.getBundleContext();
+ // The bundle must have a bundle context in order to have a module context.
+ if (bundleContext != null) {
+ String symbolicName = bundle.getSymbolicName();
+ try {
+ ServiceReference[] refs = bundleContext.getServiceReferences(ApplicationContext.class.getName(), "(Bundle-SymbolicName="
+ + symbolicName + ")");
+ if (refs != null) {
+ for (ServiceReference ref : refs) {
+ ApplicationContext appCtx = (ApplicationContext) bundleContext.getService(ref);
+ try {
+ if (appCtx instanceof ConfigurableOsgiBundleApplicationContext) {
+ ConfigurableOsgiBundleApplicationContext cAppCtx = (ConfigurableOsgiBundleApplicationContext) appCtx;
+ if (bundleContext == cAppCtx.getBundleContext()) {
+ return new ModuleContextWrapper(cAppCtx);
+ }
+ }
+ } finally {
+ bundleContext.ungetService(ref);
+ }
+ }
+ }
+ } catch (InvalidSyntaxException e) {
+ Assert.isFalse(true, "Unexpected exception %s", e.getMessage());
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/org.eclipse.virgo.kernel.deployer.dm/src/main/resources/META-INF/spring/deployer-dm-context.xml b/org.eclipse.virgo.kernel.deployer.dm/src/main/resources/META-INF/spring/deployer-dm-context.xml
new file mode 100644
index 00000000..b6f267c8
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer.dm/src/main/resources/META-INF/spring/deployer-dm-context.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans:beans xmlns="http://www.springframework.org/schema/osgi"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
+ xmlns:osgi="http://www.springframework.org/schema/osgi"
+ xsi:schemaLocation="http://www.springframework.org/schema/osgi
+ http://www.springframework.org/schema/osgi/spring-osgi.xsd
+ http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+
+ <beans:bean id="moduleContextAccessor"
+ class="org.eclipse.virgo.kernel.deployer.app.spring.UserRegionModuleContextAccessor" />
+
+ <osgi:service ref="moduleContextAccessor"
+ interface="org.eclipse.virgo.kernel.module.ModuleContextAccessor" />
+
+</beans:beans>
diff --git a/org.eclipse.virgo.kernel.deployer.dm/src/test/java/.gitignore b/org.eclipse.virgo.kernel.deployer.dm/src/test/java/.gitignore
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer.dm/src/test/java/.gitignore
diff --git a/org.eclipse.virgo.kernel.deployer.dm/src/test/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessorTests.java b/org.eclipse.virgo.kernel.deployer.dm/src/test/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessorTests.java
new file mode 100644
index 00000000..40a4c3fa
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer.dm/src/test/java/org/eclipse/virgo/kernel/deployer/app/spring/UserRegionModuleContextAccessorTests.java
@@ -0,0 +1,142 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2010 VMware Inc.
+ * 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:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.deployer.app.spring;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.Dictionary;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+import org.springframework.context.ApplicationContext;
+import org.springframework.osgi.context.ConfigurableOsgiBundleApplicationContext;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+import org.eclipse.virgo.kernel.deployer.app.spring.UserRegionModuleContextAccessor;
+import org.eclipse.virgo.kernel.module.ModuleContext;
+import org.eclipse.virgo.kernel.module.ModuleContextAccessor;
+import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
+import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
+
+/**
+ */
+public class UserRegionModuleContextAccessorTests {
+
+ private static final String BUNDLE_SYMBOLIC_NAME = "bundle";
+
+ private ModuleContextAccessor moduleContextAccessor;
+
+ private StubBundle stubBundle;
+
+ private StubBundleContext stubBundleContext;
+
+ private ApplicationContext mockBasicApplicationContext;
+
+ private interface TestAppCtx extends ConfigurableOsgiBundleApplicationContext, ModuleContext {}
+
+ private TestAppCtx mockTestAppCtx;
+
+ private static final Filter NEGATIVE_FILTER = new Filter() {
+
+ public boolean match(ServiceReference reference) {
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ public boolean match(Dictionary dictionary) {
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ public boolean matchCase(Dictionary dictionary) {
+ return false;
+ }
+
+ };
+
+private static final Filter POSITIVE_FILTER = new Filter() {
+
+ public boolean match(ServiceReference reference) {
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ public boolean match(Dictionary dictionary) {
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ public boolean matchCase(Dictionary dictionary) {
+ return true;
+ }
+
+ };
+
+ @Before
+ public void setUp() throws Exception {
+ this.moduleContextAccessor = new UserRegionModuleContextAccessor();
+
+ this.stubBundle = new StubBundle(BUNDLE_SYMBOLIC_NAME, Version.emptyVersion);
+
+ this.stubBundleContext = new StubBundleContext();
+
+ this.stubBundle.setBundleContext(this.stubBundleContext);
+
+ this.mockBasicApplicationContext= createMock(ApplicationContext.class);
+
+ this.mockTestAppCtx = createMock(TestAppCtx.class);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test
+ public void testUnpublishedApplicationContext() {
+ this.stubBundleContext.addFilter("(Bundle-SymbolicName=" + BUNDLE_SYMBOLIC_NAME + ")", NEGATIVE_FILTER);
+ assertNull(this.moduleContextAccessor.getModuleContext(this.stubBundle));
+ }
+
+ @Test
+ public void testUnpublishedApplicationContextWithOtherServicesPresent() {
+ this.stubBundleContext.addFilter("(Bundle-SymbolicName=" + BUNDLE_SYMBOLIC_NAME + ")", NEGATIVE_FILTER);
+ this.stubBundleContext.registerService(String.class.getName(), "", null);
+ assertNull(this.moduleContextAccessor.getModuleContext(this.stubBundle));
+ }
+
+ @Test
+ public void testPublishedApplicationContextOfWrongType() {
+ this.stubBundleContext.addFilter("(Bundle-SymbolicName=" + BUNDLE_SYMBOLIC_NAME + ")", POSITIVE_FILTER);
+ this.stubBundleContext.registerService(ApplicationContext.class.getName(), this.mockBasicApplicationContext, null);
+ assertNull(this.moduleContextAccessor.getModuleContext(this.stubBundle));
+ }
+
+ @Test
+ public void testPublishedApplicationContextOfCorrectType() {
+ this.stubBundleContext.addFilter("(Bundle-SymbolicName=" + BUNDLE_SYMBOLIC_NAME + ")", POSITIVE_FILTER);
+ this.stubBundleContext.registerService(ApplicationContext.class.getName(), this.mockTestAppCtx, null);
+ expect(this.mockTestAppCtx.getBundleContext()).andReturn(this.stubBundleContext);
+ replay(this.mockTestAppCtx);
+ assertNotNull(this.moduleContextAccessor.getModuleContext(this.stubBundle));
+ verify(this.mockTestAppCtx);
+ }
+
+}
diff --git a/org.eclipse.virgo.kernel.deployer.dm/src/test/resources/.gitignore b/org.eclipse.virgo.kernel.deployer.dm/src/test/resources/.gitignore
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/org.eclipse.virgo.kernel.deployer.dm/src/test/resources/.gitignore

Back to the top