You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2012/04/06 11:32:44 UTC
svn commit: r1310261 - in /servicemix/smx4/specs/trunk/locator/src:
main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
test/java/org/apache/servicemix/specs/locator/OsgiLocatorTest.java
Author: gnodet
Date: Fri Apr 6 09:32:44 2012
New Revision: 1310261
URL: http://svn.apache.org/viewvc?rev=1310261&view=rev
Log:
Change the default specs timeout to 0
Modified:
servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
servicemix/smx4/specs/trunk/locator/src/test/java/org/apache/servicemix/specs/locator/OsgiLocatorTest.java
Modified: servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java?rev=1310261&r1=1310260&r2=1310261&view=diff
==============================================================================
--- servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java (original)
+++ servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java Fri Apr 6 09:32:44 2012
@@ -26,15 +26,8 @@ import java.util.concurrent.locks.Reentr
public class OsgiLocator {
- private static long timeout = 5000l;
- static {
- try {
- String prop = System.getProperty("org.apache.servicemix.specs.timeout");
- if (prop != null) {
- timeout = Long.parseLong(prop);
- }
- } catch (Throwable t) { }
- }
+ public static final long DEFAULT_TIMEOUT = 0l;
+ public static final String TIMEOUT = "org.apache.servicemix.specs.timeout";
private static Map<String, List<Callable<Class>>> factories;
@@ -82,17 +75,29 @@ public class OsgiLocator {
return locate(factoryId, factoryId.getName());
}
+ private static long getTimeout() {
+ long timeout = DEFAULT_TIMEOUT;
+ try {
+ String prop = System.getProperty(TIMEOUT);
+ if (prop != null) {
+ timeout = Long.parseLong(prop);
+ }
+ } catch (Throwable t) { }
+ return timeout;
+ }
+
public static <T> Class<? extends T> locate(Class<T> factoryClass, String factoryId) {
- long t0 = -1;
+ long timeout = getTimeout();
+ if (timeout <= 0) {
+ return doLocate(factoryClass, factoryId);
+ }
+ long t0 = System.currentTimeMillis();
long t1 = t0;
while (t1 - t0 < timeout) {
Class<? extends T> impl = doLocate(factoryClass, factoryId);
if (impl != null) {
return impl;
}
- if (t0 == -1) {
- t0 = t1 = System.currentTimeMillis();
- }
synchronized (lock) {
try {
lock.wait(timeout - (t1 - t0));
Modified: servicemix/smx4/specs/trunk/locator/src/test/java/org/apache/servicemix/specs/locator/OsgiLocatorTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/locator/src/test/java/org/apache/servicemix/specs/locator/OsgiLocatorTest.java?rev=1310261&r1=1310260&r2=1310261&view=diff
==============================================================================
--- servicemix/smx4/specs/trunk/locator/src/test/java/org/apache/servicemix/specs/locator/OsgiLocatorTest.java (original)
+++ servicemix/smx4/specs/trunk/locator/src/test/java/org/apache/servicemix/specs/locator/OsgiLocatorTest.java Fri Apr 6 09:32:44 2012
@@ -27,25 +27,57 @@ public class OsgiLocatorTest extends Ass
OsgiLocator.register("Factory", new MockCallable());
OsgiLocator.register("Factory", new MockCallable2());
}
-
+
@Test
public void testLocatorWithSystemProperty() {
+ System.setProperty(OsgiLocator.TIMEOUT, "0");
System.setProperty("Factory", "org.apache.servicemix.specs.locator.MockCallable");
Class clazz = OsgiLocator.locate(Object.class, "Factory");
- assertNotNull("Except to find the class", clazz);
- assertEquals("Get a wrong class.", MockCallable.class.getName(), clazz.getName());
-
+ assertNotNull("Expected to find a class", clazz);
+ assertEquals("Got the wrong class", MockCallable.class.getName(), clazz.getName());
+
System.setProperty("Factory", "org.apache.servicemix.specs.locator");
clazz = OsgiLocator.locate(Object.class, "Factory");
- assertNull("Except to find the class", clazz);
- System.clearProperty("Factory");
+ assertNull("Did not expect to find a class", clazz);
}
-
+
@Test
public void testLocatorWithoutSystemProperty() {
+ System.setProperty(OsgiLocator.TIMEOUT, "0");
+ System.clearProperty("Factory");
+ Class clazz = OsgiLocator.locate(Object.class, "Factory");
+ assertNotNull("Expected to find a class", clazz);
+ assertEquals("Got the wrong class", MockCallable2.class.getName(), clazz.getName());
+ }
+
+ @Test
+ public void testLocatorWithSystemPropertyAndTimeout() {
+ long timeout = 1000;
+ System.setProperty(OsgiLocator.TIMEOUT, Long.toString(timeout));
+ System.setProperty("Factory", "org.apache.servicemix.specs.locator.MockCallable");
+ Class clazz = OsgiLocator.locate(Object.class, "Factory");
+ assertNotNull("Expected to find a class", clazz);
+ assertEquals("Got the wrong class.", MockCallable.class.getName(), clazz.getName());
+
+ System.setProperty("Factory", "org.apache.servicemix.specs.locator");
+ long t0 = System.currentTimeMillis();
+ clazz = OsgiLocator.locate(Object.class, "Factory");
+ long t1 = System.currentTimeMillis();
+ assertNull("Did not expect to find a class", clazz);
+ assertTrue("Timeout issue", (t1 - t0) > timeout / 2);
+ }
+
+ @Test
+ public void testLocatorWithoutSystemPropertyAndTimeout() {
+ long timeout = 1000;
+ System.setProperty(OsgiLocator.TIMEOUT, Long.toString(timeout));
+ System.clearProperty("Factory");
+ long t0 = System.currentTimeMillis();
Class clazz = OsgiLocator.locate(Object.class, "Factory");
- assertNotNull("Except to find the class", clazz);
- assertEquals("Get a wrong class.", MockCallable2.class.getName(), clazz.getName());
+ long t1 = System.currentTimeMillis();
+ assertNotNull("Expected to find a class", clazz);
+ assertEquals("Got the wrong class", MockCallable2.class.getName(), clazz.getName());
+ assertTrue("Timeout issue", (t1 - t0) < timeout / 2);
}
}