531535 org.eclipse.gemini.blueprint.service.importer.support.internal.aop.StaticServiceReferenceProxy cannot be cast to org.eclipse.osgi.internal.serviceregistry.ServiceReferenceImpl

Signed-off-by: ootto <olaf@x100.de>
diff --git a/core/src/main/java/org/eclipse/gemini/blueprint/service/importer/support/internal/aop/ServiceStaticInterceptor.java b/core/src/main/java/org/eclipse/gemini/blueprint/service/importer/support/internal/aop/ServiceStaticInterceptor.java
index f568e1a..dbaafa1 100644
--- a/core/src/main/java/org/eclipse/gemini/blueprint/service/importer/support/internal/aop/ServiceStaticInterceptor.java
+++ b/core/src/main/java/org/eclipse/gemini/blueprint/service/importer/support/internal/aop/ServiceStaticInterceptor.java
@@ -1,21 +1,22 @@
-/******************************************************************************

- * Copyright (c) 2006, 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

- * and Apache License v2.0 which accompanies this distribution. 

- * The Eclipse Public License is available at 

- * http://www.eclipse.org/legal/epl-v10.html and the Apache License v2.0

- * is available at http://www.opensource.org/licenses/apache2.0.php.

- * You may elect to redistribute this code under either of these licenses. 

- * 

- * Contributors:

- *   VMware Inc.

- *****************************************************************************/

+/*

+ Copyright (c) 2006, 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

+ and Apache License v2.0 which accompanies this distribution.

+ The Eclipse Public License is available at

+ http://www.eclipse.org/legal/epl-v10.html and the Apache License v2.0

+ is available at http://www.opensource.org/licenses/apache2.0.php.

+ You may elect to redistribute this code under either of these licenses.

+

+ Contributors:

+ VMware Inc.

+ */

 

 package org.eclipse.gemini.blueprint.service.importer.support.internal.aop;

 

 import org.eclipse.gemini.blueprint.service.ServiceUnavailableException;

 import org.eclipse.gemini.blueprint.service.importer.ServiceProxyDestroyedException;

+import org.eclipse.gemini.blueprint.service.importer.ServiceReferenceProxy;

 import org.eclipse.gemini.blueprint.service.importer.support.internal.exception.BlueprintExceptionFactory;

 import org.eclipse.gemini.blueprint.util.OsgiFilterUtils;

 import org.osgi.framework.BundleContext;

@@ -45,7 +46,7 @@
 	private volatile Object target = null;

 

 	public ServiceStaticInterceptor(BundleContext context, ServiceReference reference) {

-		Assert.notNull(context);

+		Assert.notNull(context, "a not null service bundle context is required");

 		Assert.notNull(reference, "a not null service reference is required");

 		this.bundleContext = context;

 		this.reference = reference;

@@ -65,7 +66,7 @@
 			if (target == null) {

 				synchronized (lock) {

 					if (target == null && !destroyed) {

-						target = bundleContext.getService(reference);

+						target = bundleContext.getService(getReferenceForExporeToOSgiFramework());

 					}

 				}

 			}

@@ -76,6 +77,19 @@
 				: new ServiceUnavailableException(filter));

 	}

 

+	/**

+	 * The {@link ServiceReference} contract dictates that there must be no client implementation

+	 * of this interface. Consequently, an OSGi framework may always perform unchecked casts to it's

+	 * internal service reference implementation. Thus, the gemini-specific {@link ServiceReferenceProxy}

+	 * must never be exposed to the framework as it cannot be cast to the framework's internal implementation.

+	 */

+	private ServiceReference<?> getReferenceForExporeToOSgiFramework() {

+		if (reference instanceof ServiceReferenceProxy) {

+            return ((ServiceReferenceProxy) reference).getTargetServiceReference();

+        }

+        return reference;

+	}

+

 	public void setUseBlueprintExceptions(boolean useBlueprintExceptions) {

 		this.useBlueprintExceptions = useBlueprintExceptions;

 	}

@@ -90,7 +104,7 @@
 			destroyed = true;

 		}

 		try {

-			bundleContext.ungetService(reference);

+			bundleContext.ungetService(getReferenceForExporeToOSgiFramework());

 		} catch (IllegalStateException ex) {

 			// in case the context is not valid anymore

 		}

diff --git a/core/src/test/java/org/eclipse/gemini/blueprint/blueprint/ReferenceDelegateFactory.java b/core/src/test/java/org/eclipse/gemini/blueprint/blueprint/ReferenceDelegateFactory.java
index 5057ebb..3fff392 100644
--- a/core/src/test/java/org/eclipse/gemini/blueprint/blueprint/ReferenceDelegateFactory.java
+++ b/core/src/test/java/org/eclipse/gemini/blueprint/blueprint/ReferenceDelegateFactory.java
@@ -14,12 +14,11 @@
 

 package org.eclipse.gemini.blueprint.blueprint;

 

+import org.eclipse.gemini.blueprint.mock.MockServiceReference;

 import org.eclipse.gemini.blueprint.service.importer.ImportedOsgiServiceProxy;

-import org.eclipse.gemini.blueprint.service.importer.ServiceReferenceProxy;

 import org.eclipse.gemini.blueprint.service.importer.support.internal.aop.StaticServiceReferenceProxy;

 import org.osgi.framework.ServiceReference;

 import org.springframework.beans.factory.FactoryBean;

-import org.eclipse.gemini.blueprint.mock.MockServiceReference;

 

 /**

  * @author Costin Leau

@@ -29,19 +28,12 @@
 	private final ServiceReference ref;

 

 

-	public ReferenceDelegateFactory() throws Exception {

+	public ReferenceDelegateFactory() {

 		ref = new MockServiceReference();

 	}

 

-	public Object getObject() throws Exception {

-		ImportedOsgiServiceProxy mockProxy = new ImportedOsgiServiceProxy() {

-

-			public ServiceReferenceProxy getServiceReference() {

-				return new StaticServiceReferenceProxy(ref);

-			}

-		};

-

-		return mockProxy;

+	public Object getObject() {

+		return (ImportedOsgiServiceProxy) () -> new StaticServiceReferenceProxy(ref);

 	}

 

 	public Class getObjectType() {

diff --git a/core/src/test/java/org/eclipse/gemini/blueprint/service/importer/support/internal/aop/PublicStaticSwappingServiceReferenceProxy.java b/core/src/test/java/org/eclipse/gemini/blueprint/service/importer/support/internal/aop/PublicStaticSwappingServiceReferenceProxy.java
deleted file mode 100644
index a5e1b73..0000000
--- a/core/src/test/java/org/eclipse/gemini/blueprint/service/importer/support/internal/aop/PublicStaticSwappingServiceReferenceProxy.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/******************************************************************************

- * Copyright (c) 2006, 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

- * and Apache License v2.0 which accompanies this distribution. 

- * The Eclipse Public License is available at 

- * http://www.eclipse.org/legal/epl-v10.html and the Apache License v2.0

- * is available at http://www.opensource.org/licenses/apache2.0.php.

- * You may elect to redistribute this code under either of these licenses. 

- * 

- * Contributors:

- *   VMware Inc.

- *****************************************************************************/

-

-package org.eclipse.gemini.blueprint.service.importer.support.internal.aop;

-

-import org.eclipse.gemini.blueprint.service.importer.support.internal.aop.StaticServiceReferenceProxy;

-import org.osgi.framework.ServiceReference;

-

-/**

- * @author Costin Leau

- */

-public class PublicStaticSwappingServiceReferenceProxy extends StaticServiceReferenceProxy {

-

-	/**

-	 * Constructs a new <code>PublicStaticSwappingServiceReferenceProxy</code>

-	 * instance.

-	 * 

-	 * @param target

-	 */

-	public PublicStaticSwappingServiceReferenceProxy(ServiceReference target) {

-		super(target);

-	}

-

-}