You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2014/12/15 16:41:59 UTC
svn commit: r1645678 - in /felix/trunk/ipojo/runtime/core/src:
main/java/org/apache/felix/ipojo/InstanceManager.java
main/java/org/apache/felix/ipojo/parser/MethodMetadata.java
test/java/org/apache/felix/ipojo/InstanceManagerTest.java
Author: clement
Date: Mon Dec 15 15:41:59 2014
New Revision: 1645678
URL: http://svn.apache.org/r1645678
Log:
Fix FELIX-4728
Added:
felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/InstanceManagerTest.java
Modified:
felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java
Modified: felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java?rev=1645678&r1=1645677&r2=1645678&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java (original)
+++ felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/InstanceManager.java Mon Dec 15 15:41:59 2014
@@ -147,7 +147,7 @@ public class InstanceManager implements
* The Map storing the Method objects by ids.
* [id=>{@link Method}].
*/
- private Map m_methods = new HashMap();
+ private Map m_methods = Collections.synchronizedMap(new HashMap());
/**
* The instance's bundle context.
@@ -663,7 +663,7 @@ public class InstanceManager implements
/**
* Loads the manipulated class.
*/
- private void load() {
+ protected void load() {
try {
m_clazz = m_factory.loadClass(m_className);
} catch (ClassNotFoundException e) {
Modified: felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java?rev=1645678&r1=1645677&r2=1645678&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java (original)
+++ felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/parser/MethodMetadata.java Mon Dec 15 15:41:59 2014
@@ -75,7 +75,7 @@ public class MethodMetadata {
* Creates a Method Metadata.
* @param metadata the method manipulation element.
*/
- MethodMetadata(Element metadata) {
+ public MethodMetadata(Element metadata) {
m_name = metadata.getAttribute("name");
String arg = metadata.getAttribute("arguments");
String names = metadata.getAttribute("names");
Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/InstanceManagerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/InstanceManagerTest.java?rev=1645678&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/InstanceManagerTest.java (added)
+++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/InstanceManagerTest.java Mon Dec 15 15:41:59 2014
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.felix.ipojo;
+
+import org.apache.felix.ipojo.metadata.Attribute;
+import org.apache.felix.ipojo.metadata.Element;
+import org.apache.felix.ipojo.parser.MethodMetadata;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+import java.lang.reflect.Member;
+import java.util.Hashtable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class InstanceManagerTest {
+
+ private static final int CALLERS = 500;
+
+ @Test
+ public void testConcurrencyOfMethodId() throws InterruptedException, ConfigurationException, ClassNotFoundException {
+ ExecutorService executor = Executors.newFixedThreadPool(CALLERS);
+ final AtomicInteger counter = new AtomicInteger();
+
+ ComponentFactory factory = mock(ComponentFactory.class);
+ when(factory.loadClass(anyString())).thenReturn(MyComponent.class);
+ when(factory.getClassName()).thenReturn(MyComponent.class.getName());
+ Bundle bundle = mock(Bundle.class);
+ when(bundle.getHeaders()).thenReturn(new Hashtable<String, String>());
+ BundleContext context = mock(BundleContext.class);
+ when(context.getBundle()).thenReturn(bundle);
+ InstanceManager manager = new InstanceManager(factory, context, new HandlerManager[0]);
+
+ Element method1 = new Element("method", "");
+ method1.addAttribute(new Attribute("name", "foo"));
+ method1.addAttribute(new Attribute("arguments", "{java.lang.String}"));
+ method1.addAttribute(new Attribute("names", "{name}"));
+
+ Element method2 = new Element("method", "");
+ method2.addAttribute(new Attribute("name", "bar"));
+ method2.addAttribute(new Attribute("arguments", "{java.lang.String}"));
+ method2.addAttribute(new Attribute("names", "{name}"));
+
+ Element method3 = new Element("method", "");
+ method3.addAttribute(new Attribute("name", "baz"));
+ method3.addAttribute(new Attribute("arguments", "{java.lang.String}"));
+ method3.addAttribute(new Attribute("names", "{name}"));
+ final MethodMetadata metadata1 = new MethodMetadata(method1);
+ final MethodInterceptor interceptor = new MethodInterceptor() {
+ public void onEntry(Object pojo, Member method, Object[] args) {
+ if (method != null) {
+ counter.getAndIncrement();
+ } else {
+ System.out.println("No method object for " + args[0]);
+ }
+ }
+
+ public void onExit(Object pojo, Member method, Object returnedObj) {
+ if (method != null) {
+ counter.getAndDecrement();
+ } else {
+ System.out.println("No method object");
+ }
+ }
+
+ public void onError(Object pojo, Member method, Throwable throwable) {
+
+ }
+
+ public void onFinally(Object pojo, Member method) {
+
+ }
+ };
+ manager.register(metadata1, interceptor);
+ final MethodMetadata metadata2 = new MethodMetadata(method2);
+ manager.register(metadata2, interceptor);
+ final MethodMetadata metadata3 = new MethodMetadata(method3);
+ manager.register(metadata3, interceptor);
+
+ MyComponent component = new MyComponent();
+ manager.start();
+ manager.load();
+ CountDownLatch startSignal = new CountDownLatch(1);
+ CountDownLatch doneSignal = new CountDownLatch(CALLERS);
+ for (int i = 1; i < CALLERS + 1; ++i) {
+ // create and start threads
+ executor.execute(new Caller(manager, component,
+ metadata1.getMethodIdentifier(), startSignal, doneSignal, i));
+ executor.execute(new Caller(manager, component,
+ metadata2.getMethodIdentifier(), startSignal, doneSignal, i));
+ executor.execute(new Caller(manager, component,
+ metadata3.getMethodIdentifier(), startSignal, doneSignal, i));
+ }
+
+ startSignal.countDown(); // let all threads proceed
+ assertThat(doneSignal.await(1, TimeUnit.MINUTES)).isTrue();
+ assertThat(counter.get()).isEqualTo(0);
+ }
+
+ private class Caller implements Runnable {
+
+ private final CountDownLatch startSignal;
+ private final CountDownLatch doneSignal;
+ private final int id;
+ private final Object component;
+ private final String identifier;
+ private InstanceManager manager;
+
+ public Caller(InstanceManager manager, Object component, String identifier, CountDownLatch startSignal,
+ CountDownLatch doneSignal, int name) {
+ this.startSignal = startSignal;
+ this.doneSignal = doneSignal;
+ this.id = name;
+ this.manager = manager;
+ this.component = component;
+ this.identifier = identifier;
+ }
+
+ public void run() {
+ try {
+ startSignal.await();
+ manager.onEntry(component, identifier, new String[] {Integer.toString(id)});
+ manager.onExit(component, identifier, null);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ } finally {
+ doneSignal.countDown();
+ }
+ }
+ }
+
+
+ private class MyComponent {
+
+ public void foo(String name) {
+ System.out.println(name);
+ }
+
+ public void bar(String name) {
+ System.out.println(name);
+ }
+
+ public void baz(String name) {
+ System.out.println(name);
+ }
+
+ }
+
+}
\ No newline at end of file