You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2013/06/17 12:44:12 UTC

svn commit: r1493710 [1/2] - in /tomee/tomee/trunk: container/ container/openejb-concurrency-utilities-ee/ container/openejb-concurrency-utilities-ee/src/ container/openejb-concurrency-utilities-ee/src/main/ container/openejb-concurrency-utilities-ee/s...

Author: rmannibucau
Date: Mon Jun 17 10:44:11 2013
New Revision: 1493710

URL: http://svn.apache.org/r1493710
Log:
TOMEE-979 basic concurrency utilities module (jsr 236)

Added:
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/pom.xml
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedExecutorServiceImplFactory.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedScheduledExecutorServiceImplFactory.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedThreadFactoryImplFactory.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUFuture.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUScheduleFuture.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ContextServiceImpl.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedExecutorServiceImpl.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedScheduledExecutorServiceImpl.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedThreadFactoryImpl.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/reject/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/reject/CURejectHandler.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUCallable.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CURunnable.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/ManagedTaskListenerTask.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerCallable.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerRunnable.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerTask.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ContextServiceTest.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/CustomInjectionTest.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/InjectionTest.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ManagedExecutorServiceTest.java
    tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ManagedScheduledExecutorServiceTest.java
Modified:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
    tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.embedded/service-jar.xml
    tomee/tomee/trunk/container/openejb-core/src/main/resources/META-INF/org.apache.openejb/service-jar.xml
    tomee/tomee/trunk/container/pom.xml
    tomee/tomee/trunk/tomee/tomee-embedded/src/main/resources/META-INF/org.apache.tomee/service-jar.xml
    tomee/tomee/trunk/tomee/tomee-webapp/src/main/resources/META-INF/org.apache.tomee/service-jar.xml

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/pom.xml
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/pom.xml?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/pom.xml (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/pom.xml Mon Jun 17 10:44:11 2013
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>container</artifactId>
+    <groupId>org.apache.openejb</groupId>
+    <version>4.6.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>openejb-concurrency-utilities-ee</artifactId>
+  <name>OpenEJB :: Container :: Concurrency Utilities EE</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+
+    <!--
+    <dependency>
+      <groupId>org.apache.geronimo.specs</groupId>
+      <artifactId>geronimo-concurrent_1.0_spec</artifactId>
+      <version>TODO</version>
+    </dependency>
+    -->
+
+    <dependency> <!-- to replace with geronimo -->
+      <groupId>javax.enterprise.concurrent</groupId>
+      <artifactId>javax.enterprise.concurrent-api</artifactId>
+      <version>1.0</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+</project>

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedExecutorServiceImplFactory.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedExecutorServiceImplFactory.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedExecutorServiceImplFactory.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedExecutorServiceImplFactory.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,87 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.factory;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedExecutorServiceImpl;
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedThreadFactoryImpl;
+import org.apache.openejb.concurrencyutilities.ee.reject.CURejectHandler;
+import org.apache.openejb.util.Duration;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+
+import javax.enterprise.concurrent.ManagedThreadFactory;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+
+public class ManagedExecutorServiceImplFactory {
+    private int core = 5;
+    private int max = 25;
+    private Duration keepAlive = new Duration("5 second");
+    private int queue = 15;
+    private String threadFactory;
+    private Duration waitAtShutdown = new Duration("30 seconds");
+
+    public ManagedExecutorServiceImpl create() {
+        return new ManagedExecutorServiceImpl(createExecutorService(), waitAtShutdown);
+    }
+
+    private ExecutorService createExecutorService() {
+        final BlockingQueue<Runnable> blockingQueue;
+        if (queue <= 0) {
+            blockingQueue = new LinkedBlockingQueue<Runnable>();
+        } else {
+            blockingQueue = new ArrayBlockingQueue<Runnable>(queue);
+        }
+
+        ManagedThreadFactory managedThreadFactory;
+        try {
+            managedThreadFactory = ManagedThreadFactory.class.cast(Thread.currentThread().getContextClassLoader().loadClass(threadFactory).newInstance());
+        } catch (final Exception e) {
+            Logger.getInstance(LogCategory.OPENEJB, ManagedExecutorServiceImplFactory.class).warning("Can't create configured thread factory: " + threadFactory, e);
+            managedThreadFactory = new ManagedThreadFactoryImpl();
+        }
+
+        return new ThreadPoolExecutor(core, max, keepAlive.getTime(), keepAlive.getUnit(), blockingQueue, managedThreadFactory, CURejectHandler.INSTANCE);
+    }
+
+    public void setCore(final int core) {
+        this.core = core;
+    }
+
+    public void setMax(final int max) {
+        this.max = max;
+    }
+
+    public void setKeepAlive(final Duration keepAlive) {
+        this.keepAlive = keepAlive;
+    }
+
+    public void setQueue(final int queue) {
+        this.queue = queue;
+    }
+
+    public void setThreadFactory(final String threadFactory) {
+        this.threadFactory = threadFactory;
+    }
+
+    public void setWaitAtShutdown(final Duration waitAtShutdown) {
+        this.waitAtShutdown = waitAtShutdown;
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedScheduledExecutorServiceImplFactory.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedScheduledExecutorServiceImplFactory.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedScheduledExecutorServiceImplFactory.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedScheduledExecutorServiceImplFactory.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,62 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.factory;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedScheduledExecutorServiceImpl;
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedThreadFactoryImpl;
+import org.apache.openejb.concurrencyutilities.ee.reject.CURejectHandler;
+import org.apache.openejb.util.Duration;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+
+import javax.enterprise.concurrent.ManagedThreadFactory;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+
+public class ManagedScheduledExecutorServiceImplFactory {
+    private int core = 5;
+    private String threadFactory = ManagedThreadFactoryImpl.class.getName();
+    private Duration waitAtShutdown = new Duration("30 seconds");
+
+    public ManagedScheduledExecutorServiceImpl create() {
+        return new ManagedScheduledExecutorServiceImpl(createScheduledExecutorService(), waitAtShutdown);
+    }
+
+    private ScheduledExecutorService createScheduledExecutorService() {
+        ManagedThreadFactory managedThreadFactory;
+        try {
+            managedThreadFactory = ManagedThreadFactory.class.cast(Thread.currentThread().getContextClassLoader().loadClass(threadFactory).newInstance());
+        } catch (final Exception e) {
+            Logger.getInstance(LogCategory.OPENEJB, ManagedScheduledExecutorServiceImplFactory.class).warning("Can't create configured thread factory: " + threadFactory, e);
+            managedThreadFactory = new ManagedThreadFactoryImpl();
+        }
+
+        return new ScheduledThreadPoolExecutor(core, managedThreadFactory, CURejectHandler.INSTANCE);
+    }
+
+    public void setCore(final int core) {
+        this.core = core;
+    }
+
+    public void setThreadFactory(final String threadFactory) {
+        this.threadFactory = threadFactory;
+    }
+
+    public void setWaitAtShutdown(final Duration waitAtShutdown) {
+        this.waitAtShutdown = waitAtShutdown;
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedThreadFactoryImplFactory.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedThreadFactoryImplFactory.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedThreadFactoryImplFactory.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/factory/ManagedThreadFactoryImplFactory.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,17 @@
+package org.apache.openejb.concurrencyutilities.ee.factory;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedThreadFactoryImpl;
+
+import javax.enterprise.concurrent.ManagedThreadFactory;
+
+public class ManagedThreadFactoryImplFactory {
+    private String prefix = "openejb-managed-thread-";
+
+    public ManagedThreadFactory create() {
+        return new ManagedThreadFactoryImpl(prefix);
+    }
+
+    public void setPrefix(final String prefix) {
+        this.prefix = prefix;
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUFuture.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUFuture.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUFuture.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUFuture.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,62 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.future;
+
+import org.apache.openejb.concurrencyutilities.ee.task.ManagedTaskListenerTask;
+
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+public class CUFuture<V> implements Future<V> {
+    protected final Future<V> delegate;
+    protected final ManagedTaskListenerTask listener;
+
+    public CUFuture(final Future<V> delegate, final ManagedTaskListenerTask listener) {
+        this.delegate = delegate;
+        this.listener = listener;
+    }
+
+    @Override
+    public boolean cancel(final boolean mayInterruptIfRunning) {
+        final boolean result = delegate.cancel(mayInterruptIfRunning);
+        listener.taskAborted(new CancellationException());
+        return result;
+    }
+
+    @Override
+    public boolean isCancelled() {
+        return delegate.isCancelled();
+    }
+
+    @Override
+    public boolean isDone() {
+        return delegate.isDone();
+    }
+
+    @Override
+    public V get() throws InterruptedException, ExecutionException {
+        return delegate.get();
+    }
+
+    @Override
+    public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
+        return delegate.get(timeout, unit);
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUScheduleFuture.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUScheduleFuture.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUScheduleFuture.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/future/CUScheduleFuture.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,39 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.future;
+
+import org.apache.openejb.concurrencyutilities.ee.task.ManagedTaskListenerTask;
+
+import java.util.concurrent.Delayed;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+public class CUScheduleFuture<V> extends CUFuture<V> implements ScheduledFuture<V> {
+    public CUScheduleFuture(final ScheduledFuture<V> delegate, final ManagedTaskListenerTask listener) {
+        super(delegate, listener);
+    }
+
+    @Override
+    public long getDelay(final TimeUnit unit) {
+        return ScheduledFuture.class.cast(delegate).getDelay(unit);
+    }
+
+    @Override
+    public int compareTo(final Delayed o) {
+        return ScheduledFuture.class.cast(delegate).compareTo(o);
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ContextServiceImpl.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ContextServiceImpl.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ContextServiceImpl.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ContextServiceImpl.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,82 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.impl;
+
+import org.apache.openejb.concurrencyutilities.ee.task.CUTask;
+
+import javax.enterprise.concurrent.ContextService;
+import java.io.Serializable;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Callable;
+
+public class ContextServiceImpl implements ContextService {
+    private static final HashMap<String,String> EMPTY_PROPS = new HashMap<String, String>();
+
+    @Override
+    public <T> T createContextualProxy(final T instance, final Class<T> intf) {
+        return intf.cast(createContextualProxy(instance, new Class<?>[] { intf }));
+    }
+
+    @Override
+    public Object createContextualProxy(final Object instance, final Class<?>... interfaces) {
+        return createContextualProxy(instance, EMPTY_PROPS, interfaces);
+    }
+
+    @Override
+    public <T> T createContextualProxy(final T instance, final Map<String, String> executionProperties, final Class<T> intf) {
+        return intf.cast(createContextualProxy(instance, executionProperties, new Class<?>[] { intf }));
+    }
+
+    @Override
+    public Object createContextualProxy(final Object instance, final Map<String, String> executionProperties, final Class<?>... interfaces) {
+        return Proxy.newProxyInstance(instance.getClass().getClassLoader(), interfaces, new CUHandler(instance, executionProperties));
+    }
+
+    @Override
+    public Map<String, String> getExecutionProperties(final Object contextualProxy) {
+        return CUHandler.class.cast(Proxy.getInvocationHandler(contextualProxy)).properties;
+    }
+
+    private static class CUHandler extends CUTask<Object> implements InvocationHandler, Serializable {
+        private final Object instance;
+        private final Map<String, String> properties;
+
+        private CUHandler(final Object instance, final Map<String, String> props) {
+            super(instance);
+            this.instance = instance;
+            this.properties = props;
+        }
+
+        @Override
+        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+            if (method.getDeclaringClass() == Object.class) {
+                return method.invoke(this, args);
+            }
+
+            return invoke(new Callable<Object>() {
+                @Override
+                public Object call() throws Exception {
+                    return method.invoke(instance, args);
+                }
+            });
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedExecutorServiceImpl.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedExecutorServiceImpl.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedExecutorServiceImpl.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedExecutorServiceImpl.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,105 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.impl;
+
+import org.apache.openejb.concurrencyutilities.ee.future.CUFuture;
+import org.apache.openejb.concurrencyutilities.ee.task.CUCallable;
+import org.apache.openejb.concurrencyutilities.ee.task.CURunnable;
+import org.apache.openejb.util.Duration;
+
+import javax.enterprise.concurrent.ManagedExecutorService;
+import java.util.List;
+import java.util.concurrent.AbstractExecutorService;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+public class ManagedExecutorServiceImpl extends AbstractExecutorService implements ManagedExecutorService {
+    private final ExecutorService delegate;
+    private final Duration waitAtShutdown;
+
+    public ManagedExecutorServiceImpl(final ExecutorService delegate, Duration waitAtShutdown) {
+        this.delegate = delegate;
+        this.waitAtShutdown = waitAtShutdown;
+    }
+
+    @Override
+    public void shutdown() {
+        delegate.shutdown();
+        try { // wait a bit to let task a chance to be done
+            delegate.awaitTermination(waitAtShutdown.getTime(), waitAtShutdown.getUnit());
+        } catch (final InterruptedException e) {
+            // no-op
+        }
+    }
+
+    @Override
+    public List<Runnable> shutdownNow() {
+        return delegate.shutdownNow();
+    }
+
+    @Override
+    public boolean isShutdown() {
+        return delegate.isShutdown();
+    }
+
+    @Override
+    public boolean isTerminated() {
+        return delegate.isTerminated();
+    }
+
+    @Override
+    public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException {
+        return delegate.awaitTermination(timeout, unit);
+    }
+
+    @Override
+    public <T> Future<T> submit(final Callable<T> task) {
+        final CUCallable<T> wrapper = new CUCallable<T>(task);
+        final Future<T> future = delegate.submit(wrapper);
+        wrapper.taskSubmitted(future, this, task);
+        return new CUFuture<T>(future, wrapper);
+    }
+
+    @Override
+    public <T> Future<T> submit(final Runnable task, final T result) {
+        final CURunnable wrapper = new CURunnable(task);
+        final Future<T> future = delegate.submit(wrapper, result);
+        wrapper.taskSubmitted(future, this, task);
+        return new CUFuture<T>(future, wrapper);
+    }
+
+    @Override
+    public Future<?> submit(final Runnable task) {
+        final CURunnable wrapper = new CURunnable(task);
+        final Future<?> future = delegate.submit(wrapper);
+        wrapper.taskSubmitted(future, this, task);
+        return new CUFuture<Void>(CUFuture.class.cast(future), wrapper);
+    }
+
+    @Override
+    public void execute(final Runnable command) {
+        final CURunnable wrapper = new CURunnable(command);
+        delegate.execute(wrapper);
+        wrapper.taskSubmitted(null, this, command);
+    }
+
+    public ExecutorService getDelegate() {
+        return delegate;
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedScheduledExecutorServiceImpl.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedScheduledExecutorServiceImpl.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedScheduledExecutorServiceImpl.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedScheduledExecutorServiceImpl.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,119 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.impl;
+
+import org.apache.openejb.concurrencyutilities.ee.future.CUScheduleFuture;
+import org.apache.openejb.concurrencyutilities.ee.task.CUCallable;
+import org.apache.openejb.concurrencyutilities.ee.task.CURunnable;
+import org.apache.openejb.concurrencyutilities.ee.task.TriggerCallable;
+import org.apache.openejb.concurrencyutilities.ee.task.TriggerRunnable;
+import org.apache.openejb.util.Duration;
+
+import javax.enterprise.concurrent.ManagedScheduledExecutorService;
+import javax.enterprise.concurrent.ManagedTask;
+import javax.enterprise.concurrent.Trigger;
+import java.util.Date;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+public class ManagedScheduledExecutorServiceImpl extends ManagedExecutorServiceImpl implements ManagedScheduledExecutorService {
+    private final ScheduledExecutorService delegate;
+
+    public ManagedScheduledExecutorServiceImpl(final ScheduledExecutorService delegate, final Duration waitAtShutdown) {
+        super(delegate, waitAtShutdown);
+        this.delegate = delegate;
+    }
+
+
+    @Override
+    public ScheduledFuture<?> schedule(final Runnable runnable, final Trigger trigger) {
+        final Date taskScheduledTime = new Date();
+        final TriggerRunnable wrapper = new TriggerRunnable(this, runnable, new CURunnable(runnable), trigger, taskScheduledTime, getTaskId(runnable));
+        final ScheduledFuture<?> future = delegate.schedule(new Runnable() {
+            @Override
+            public void run() {
+                wrapper.run();
+                final ScheduledFuture<?> future = schedule(this, trigger.getNextRunTime(wrapper.getLastExecution(), taskScheduledTime).getTime() - nowMs(), TimeUnit.MILLISECONDS);
+                wrapper.taskSubmitted(future, ManagedScheduledExecutorServiceImpl.this, runnable);
+            }
+        }, trigger.getNextRunTime(wrapper.getLastExecution(), taskScheduledTime).getTime() - nowMs(), TimeUnit.MILLISECONDS);
+        wrapper.taskSubmitted(future, this, runnable);
+        return new CUScheduleFuture<Object>(ScheduledFuture.class.cast(future), wrapper);
+    }
+
+    @Override
+    public <V> ScheduledFuture<V> schedule(final Callable<V> vCallable, final Trigger trigger) {
+        final Date taskScheduledTime = new Date();
+        final TriggerCallable<V> wrapper = new TriggerCallable<V>(this, vCallable, new CUCallable<V>(vCallable), trigger, taskScheduledTime, getTaskId(vCallable));
+        final ScheduledFuture<V> future = delegate.schedule(new Callable<V>() {
+            @Override
+            public V call() throws Exception {
+                final V result = wrapper.call();
+                final ScheduledFuture<V> future = schedule(this, trigger.getNextRunTime(wrapper.getLastExecution(), taskScheduledTime).getTime() - nowMs(), TimeUnit.MILLISECONDS);
+                wrapper.taskSubmitted(future, ManagedScheduledExecutorServiceImpl.this, vCallable);
+                return result;
+            }
+        }, trigger.getNextRunTime(wrapper.getLastExecution(), taskScheduledTime).getTime() - nowMs(), TimeUnit.MILLISECONDS);
+        wrapper.taskSubmitted(future, this, vCallable);
+        return new CUScheduleFuture<V>(future, wrapper);
+    }
+
+    @Override
+    public ScheduledFuture<?> schedule(final Runnable command, final long delay, final TimeUnit unit) {
+        final CURunnable wrapper = new CURunnable(command);
+        final ScheduledFuture<?> future = delegate.schedule(wrapper, delay, unit);
+        wrapper.taskSubmitted(future, this, command);
+        return new CUScheduleFuture<Object>(ScheduledFuture.class.cast(future), wrapper);
+    }
+
+    @Override
+    public <V> ScheduledFuture<V> schedule(final Callable<V> callable, final long delay, final TimeUnit unit) {
+        final CUCallable<V> wrapper = new CUCallable<V>(callable);
+        final ScheduledFuture<V> future = delegate.schedule(wrapper, delay, unit);
+        wrapper.taskSubmitted(future, this, callable);
+        return new CUScheduleFuture<V>(future, wrapper);
+    }
+
+    @Override
+    public ScheduledFuture<?> scheduleAtFixedRate(final Runnable command, final long initialDelay, final long period, final TimeUnit unit) {
+        final CURunnable wrapper = new CURunnable(command);
+        final ScheduledFuture<?> future = delegate.scheduleAtFixedRate(wrapper, initialDelay, period, unit);
+        wrapper.taskSubmitted(future, this, command);
+        return new CUScheduleFuture<Object>(ScheduledFuture.class.cast(future), wrapper);
+    }
+
+    @Override
+    public ScheduledFuture<?> scheduleWithFixedDelay(final Runnable command, final long initialDelay, final long delay, final TimeUnit unit) {
+        final CURunnable wrapper = new CURunnable(command);
+        final ScheduledFuture<?> future = delegate.scheduleWithFixedDelay(wrapper, initialDelay, delay, unit);
+        wrapper.taskSubmitted(future, this, command);
+        return new CUScheduleFuture<Object>(ScheduledFuture.class.cast(future), wrapper);
+    }
+
+    public static long nowMs() {
+        return System.currentTimeMillis(); // need to be comparable to java.util.Date
+    }
+
+    private static String getTaskId(final Object runnable) {
+        if (ManagedTask.class.isInstance(runnable)) {
+            return ManagedTask.class.cast(runnable).getExecutionProperties().get(ManagedTask.IDENTITY_NAME);
+        }
+        return null;
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedThreadFactoryImpl.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedThreadFactoryImpl.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedThreadFactoryImpl.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/impl/ManagedThreadFactoryImpl.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,54 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.impl;
+
+import javax.enterprise.concurrent.ManageableThread;
+import javax.enterprise.concurrent.ManagedThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class ManagedThreadFactoryImpl implements ManagedThreadFactory {
+    private static final AtomicInteger ID = new AtomicInteger();
+
+    private final String prefix;
+
+    public ManagedThreadFactoryImpl() {
+        this.prefix = "managed-thread-";
+    }
+
+    public ManagedThreadFactoryImpl(final String prefix) {
+        this.prefix = prefix;
+    }
+
+    @Override
+    public Thread newThread(Runnable r) {
+        final Thread thread = new ManagedThread(r);
+        thread.setDaemon(true);
+        thread.setName(prefix + ID.incrementAndGet());
+        return thread;
+    }
+
+    public static class ManagedThread extends Thread implements ManageableThread {
+        public ManagedThread(final Runnable r) {
+            super(r);
+        }
+
+        @Override
+        public boolean isShutdown() {
+            return !isAlive();
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/reject/CURejectHandler.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/reject/CURejectHandler.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/reject/CURejectHandler.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/reject/CURejectHandler.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,35 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.reject;
+
+import org.apache.openejb.concurrencyutilities.ee.task.ManagedTaskListenerTask;
+
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ThreadPoolExecutor;
+
+public class CURejectHandler extends ThreadPoolExecutor.AbortPolicy {
+    public static final RejectedExecutionHandler INSTANCE = new CURejectHandler();
+
+    @Override
+    public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
+        if (ManagedTaskListenerTask.class.isInstance(r)) {
+            ManagedTaskListenerTask.class.cast(r).taskAborted(new RejectedExecutionException());
+        }
+        super.rejectedExecution(r, executor);
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUCallable.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUCallable.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUCallable.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUCallable.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,38 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.task;
+
+import java.util.concurrent.Callable;
+
+public class CUCallable<T> extends CUTask<T> implements Callable<T> {
+    private final Callable<? extends T> delegate;
+
+    public CUCallable(final Callable<? extends T> task) {
+        super(task);
+        delegate = task;
+    }
+
+    @Override
+    public T call() throws Exception {
+        return invoke(new Callable<T>() {
+            @Override
+            public T call() throws Exception {
+                return delegate.call();
+            }
+        });
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CURunnable.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CURunnable.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CURunnable.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CURunnable.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,45 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.task;
+
+import java.util.concurrent.Callable;
+
+public class CURunnable extends CUTask<Void> implements Runnable {
+    private final Runnable delegate;
+
+    public CURunnable(final Runnable task) {
+        super(task);
+        delegate = task;
+    }
+
+    @Override
+    public void run() {
+        try {
+            invoke(new Callable<Void>() {
+                @Override
+                public Void call() throws Exception {
+                    delegate.run();
+                    return null;
+                }
+            });
+        } catch (final RuntimeException re) {
+            throw re;
+        } catch (final Exception e) {
+            // can't happen normally since not in the contract of Runnable
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/CUTask.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,105 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.task;
+
+import org.apache.openejb.OpenEJBRuntimeException;
+import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.SecurityService;
+
+import java.util.concurrent.Callable;
+
+public abstract class CUTask<T> extends ManagedTaskListenerTask {
+    private static final SecurityService<?> SECURITY_SERVICE = SystemInstance.get().getComponent(SecurityService.class);
+
+    private final Context initialContext;
+
+    public CUTask(final Object task) {
+        super(task);
+
+        initialContext = new Context(SECURITY_SERVICE.currentState(), ThreadContext.getThreadContext(), Thread.currentThread().getContextClassLoader());
+    }
+
+    protected T invoke(final Callable<T> call) throws Exception{
+        initialContext.enter();
+
+        Throwable throwable = null;
+        try {
+            taskStarting(future, executor, delegate); // do it in try to avoid issues if an exception is thrown
+            return call.call();
+        } catch (final Throwable t) {
+            throwable = t;
+            taskAborted(throwable);
+            return rethrow(t);
+        } finally {
+            taskDone(future, executor, delegate, throwable);
+
+            initialContext.exit();
+        }
+    }
+
+    private T rethrow(final Throwable t) throws Exception {
+        if (Exception.class.isInstance(t)) {
+            throw Exception.class.cast(t);
+        } else if (Error.class.isInstance(t)) {
+            throw Error.class.cast(t);
+        }
+        throw new OpenEJBRuntimeException(t.getMessage(), t);
+    }
+
+    private static class Context {
+        private final Object securityServiceState;
+        private final ThreadContext threadContext;
+        private final ClassLoader loader;
+
+        private Context currentContext = null;
+
+        private Context(final Object initialSecurityServiceState, final ThreadContext initialThreadContext, final ClassLoader initialLoader) {
+            this.securityServiceState = initialSecurityServiceState;
+            this.threadContext = initialThreadContext;
+            this.loader = initialLoader;
+        }
+
+        public void enter() {
+            final Thread thread = Thread.currentThread();
+
+            final ClassLoader oldCl = thread.getContextClassLoader();
+            thread.setContextClassLoader(loader);
+
+            final ThreadContext oldCtx;
+            if (threadContext != null) {
+                oldCtx = ThreadContext.enter(new ThreadContext(threadContext));
+            } else {
+                oldCtx = null;
+            }
+
+            final Object threadState = SECURITY_SERVICE.currentState();
+            SECURITY_SERVICE.setState(securityServiceState);
+
+            currentContext = new Context(threadState, oldCtx, oldCl);
+        }
+
+        public void exit() {
+            SECURITY_SERVICE.setState(currentContext.securityServiceState);
+            if (currentContext.threadContext != null) {
+                ThreadContext.exit(currentContext.threadContext);
+            }
+            Thread.currentThread().setContextClassLoader(currentContext.loader);
+            currentContext = null;
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/ManagedTaskListenerTask.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/ManagedTaskListenerTask.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/ManagedTaskListenerTask.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/ManagedTaskListenerTask.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,91 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.task;
+
+import javax.enterprise.concurrent.ManagedExecutorService;
+import javax.enterprise.concurrent.ManagedTask;
+import javax.enterprise.concurrent.ManagedTaskListener;
+import java.util.concurrent.Future;
+
+public abstract class ManagedTaskListenerTask implements ManagedTaskListener {
+    private final ManagedTaskListener listener;
+
+    protected Future<?> future;
+    protected final Object delegate;
+    protected ManagedExecutorService executor;
+
+    protected ManagedTaskListenerTask(final Object task) {
+        if (ManagedTask.class.isInstance(task)) {
+            listener = ManagedTask.class.cast(task).getManagedTaskListener();
+        } else {
+            listener = NoopManagedTaskListener.INSTANCE;
+        }
+        this.delegate = task;
+    }
+
+    @Override
+    public void taskSubmitted(final Future<?> future, final ManagedExecutorService executor, final Object task) {
+        this.future = future;
+        this.executor = executor;
+
+        listener.taskSubmitted(future, executor, task);
+    }
+
+    @Override
+    public void taskAborted(final Future<?> future, final ManagedExecutorService executor, final Object task, final Throwable exception) {
+        // use saved values since called with null excepted for the exception
+        listener.taskAborted(this.future, this.executor, this.delegate, exception);
+    }
+
+    @Override
+    public void taskDone(final Future<?> future, final ManagedExecutorService executor, final Object task, final Throwable exception) {
+        listener.taskDone(future, executor, task, exception);
+    }
+
+    @Override
+    public void taskStarting(final Future<?> future, final ManagedExecutorService executor, final Object task) {
+        listener.taskStarting(future, executor, task);
+    }
+
+    public void taskAborted(final Throwable skippedException) {
+        taskAborted(future, executor, delegate, skippedException);
+    }
+
+    private static class NoopManagedTaskListener implements ManagedTaskListener {
+        private static final NoopManagedTaskListener INSTANCE = new NoopManagedTaskListener();
+
+        @Override
+        public void taskSubmitted(final Future<?> future, final ManagedExecutorService executor, final Object task) {
+            // no-op
+        }
+
+        @Override
+        public void taskAborted(final Future<?> future, final ManagedExecutorService executor, final Object task, final Throwable exception) {
+            // no-op
+        }
+
+        @Override
+        public void taskDone(final Future<?> future, final ManagedExecutorService executor, final Object task, final Throwable exception) {
+            // no-op
+        }
+
+        @Override
+        public void taskStarting(final Future<?> future, final ManagedExecutorService executor, final Object task) {
+            // no-op
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerCallable.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerCallable.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerCallable.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerCallable.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,44 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.task;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedScheduledExecutorServiceImpl;
+
+import javax.enterprise.concurrent.Trigger;
+import java.util.Date;
+import java.util.concurrent.Callable;
+
+public class TriggerCallable<V> extends TriggerTask<V> implements Callable<V> {
+    private final Callable<V> delegate;
+
+    public TriggerCallable(final ManagedScheduledExecutorServiceImpl es,
+                           final Callable<V> original, final Callable<V> wrap,
+                           final Trigger trigger, final Date taskScheduledTime, final String id) {
+        super(original, es, trigger, taskScheduledTime, id);
+        this.delegate = wrap;
+    }
+
+    @Override
+    public V call() throws Exception {
+        return invoke();
+    }
+
+    @Override
+    protected V doInvoke() throws Exception {
+        return delegate.call();
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerRunnable.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerRunnable.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerRunnable.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerRunnable.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,51 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.task;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedScheduledExecutorServiceImpl;
+
+import javax.enterprise.concurrent.Trigger;
+import java.util.Date;
+
+public class TriggerRunnable extends TriggerTask<Void> implements Runnable {
+    private final Runnable delegate;
+
+    public TriggerRunnable(final ManagedScheduledExecutorServiceImpl es,
+                           final Runnable original, final Runnable runnable,
+                           final Trigger trigger,
+                           final Date taskScheduledTime, final String id) {
+        super(original, es, trigger, taskScheduledTime, id);
+        this.delegate = runnable;
+    }
+
+    @Override
+    protected Void doInvoke() {
+        delegate.run();
+        return null;
+    }
+
+    @Override
+    public void run() {
+        try {
+            invoke();
+        } catch (final RuntimeException re) {
+            throw re;
+        } catch (final Exception e) {
+            // can't happen normally since not in the contract of Runnable
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerTask.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerTask.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerTask.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/main/java/org/apache/openejb/concurrencyutilities/ee/task/TriggerTask.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,164 @@
+/**
+ * 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.openejb.concurrencyutilities.ee.task;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedScheduledExecutorServiceImpl;
+
+import javax.enterprise.concurrent.LastExecution;
+import javax.enterprise.concurrent.SkippedException;
+import javax.enterprise.concurrent.Trigger;
+import java.util.Date;
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public abstract class TriggerTask<T> extends CUTask<T> {
+    protected final ManagedScheduledExecutorServiceImpl executorService;
+    protected final Trigger trigger;
+    protected final Date scheduledTime;
+    protected final String id;
+    protected LastExecution lastExecution;
+    protected volatile boolean skipped = false;
+
+    protected volatile boolean done = false;
+
+    private final AtomicBoolean running = new AtomicBoolean(true);
+    private volatile T result = null;
+
+    protected TriggerTask(final Object original, final ManagedScheduledExecutorServiceImpl es, final Trigger trigger,
+                          final Date taskScheduledTime, final String id) {
+        super(original);
+        this.executorService = es;
+        this.trigger = trigger;
+        this.scheduledTime = taskScheduledTime;
+        this.id = id;
+    }
+
+    public T invoke() throws Exception {
+        return invoke(new Callable<T>() {
+            @Override
+            public T call() throws Exception {
+                final long wait = nextDelay(trigger.getNextRunTime(lastExecution, scheduledTime));
+                if (wait > 0) {
+                    Thread.sleep(wait);
+                } // else if wait < 0 then ??
+
+                final Date now = new Date();
+                try {
+                    final boolean skip = trigger.skipRun(lastExecution, now);
+                    if (!skip) {
+                        result = doInvoke();
+                        taskDone(future, executor, delegate, null);
+                        lastExecution = new LastExecutionImpl(id, result, scheduledTime, now, new Date());
+                    } else {
+                        result = null;
+                        skipped = true;
+                        running.set(false);
+                    }
+                } catch (final RuntimeException re) {
+                    final SkippedException skippedException = new SkippedException(re);
+                    taskAborted(skippedException);
+                    throw skippedException;
+                }
+                return result;
+            }
+        });
+    }
+
+    protected long nextDelay(final Date next) {
+        return next.getTime() - ManagedScheduledExecutorServiceImpl.nowMs();
+    }
+
+    protected abstract T doInvoke() throws Exception ;
+
+    public String getId() {
+        return id;
+    }
+
+    public void stop() {
+        running.set(false);
+    }
+
+    public boolean isDone() {
+        return done;
+    }
+
+    public T getResult() {
+        return result;
+    }
+
+    public boolean isSkipped() {
+        return skipped;
+    }
+
+    public LastExecution getLastExecution() {
+        return lastExecution;
+    }
+
+    private static class LastExecutionImpl implements LastExecution {
+        private final String identityName;
+        private final Object result;
+        private final Date scheduledStart;
+        private final Date runStart;
+        private final Date runEnd;
+
+        public LastExecutionImpl(final String identityName, final Object result, final Date scheduledStart,
+                                 final Date runStart, final Date runEnd) {
+            this.identityName = identityName;
+            this.result = result;
+            this.scheduledStart = scheduledStart;
+            this.runStart = runStart;
+            this.runEnd = runEnd;
+        }
+
+        @Override
+        public String getIdentityName() {
+            return identityName;
+        }
+
+        @Override
+        public Object getResult() {
+            return result;
+        }
+
+        @Override
+        public Date getScheduledStart() {
+            return scheduledStart;
+        }
+
+        @Override
+        public Date getRunStart() {
+            return runStart;
+        }
+
+        @Override
+        public Date getRunEnd() {
+            return runEnd;
+        }
+
+        @Override
+        public String toString() {
+            return "LastExecutionImpl{" +
+                    (identityName != null ? "identityName='" + identityName + "\', " : "") +
+                    "result=" + result +
+                    ", scheduledStart=" + scheduledStart +
+                    ", runStart=" + runStart +
+                    ", runEnd=" + runEnd +
+                    '}';
+        }
+    }
+
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ContextServiceTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ContextServiceTest.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ContextServiceTest.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ContextServiceTest.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,68 @@
+/**
+ * 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.openejb.concurrencyutilities.test;
+
+import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.SingletonBean;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Module;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+import javax.enterprise.concurrent.ContextService;
+import java.util.concurrent.Callable;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(ApplicationComposer.class)
+public class ContextServiceTest {
+    @Module
+    public EnterpriseBean bean() {
+        return new SingletonBean(ContextServiceFacade.class).localBean();
+    }
+
+    @EJB
+    private ContextServiceFacade facade;
+
+    @Test
+    public void checkContext() throws Exception {
+        final Callable<Boolean> callable = new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                return ThreadContext.getThreadContext().getBeanContext().getBeanClass() == ContextServiceFacade.class;
+            }
+        };
+        assertFalse(callable.call());
+        assertTrue(facade.proxy(callable).call());
+    }
+
+    @Singleton
+    public static class ContextServiceFacade {
+        @Resource
+        private ContextService cs;
+
+        public Callable<Boolean> proxy(final Callable<Boolean> callable) {
+            return cs.createContextualProxy(callable, Callable.class);
+        }
+
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/CustomInjectionTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/CustomInjectionTest.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/CustomInjectionTest.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/CustomInjectionTest.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,128 @@
+/**
+ * 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.openejb.concurrencyutilities.test;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedExecutorServiceImpl;
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedScheduledExecutorServiceImpl;
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedThreadFactoryImpl;
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.SingletonBean;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testing.Module;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.apache.openejb.util.reflection.Reflections;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+import javax.enterprise.concurrent.ManagedExecutorService;
+import javax.enterprise.concurrent.ManagedScheduledExecutorService;
+import javax.enterprise.concurrent.ManagedThreadFactory;
+import java.util.Properties;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+@RunWith(ApplicationComposer.class)
+public class CustomInjectionTest {
+    @Configuration
+    public Properties configuration() {
+        return new PropertiesBuilder()
+
+                .property("concurrent/es", "new://Resource?type=ManagedExecutorService")
+                .property("concurrent/es.core", "2")
+                .property("concurrent/es.max", "10")
+                .property("concurrent/es.keepAlive", "4 minutes")
+                .property("concurrent/es.queue", "3")
+
+                .property("concurrent/ses", "new://Resource?type=ManagedScheduledExecutorService")
+                .property("concurrent/ses.core", "12")
+
+                .property("concurrent/tf", "new://Resource?type=ManagedThreadFactory")
+                .property("concurrent/tf.prefix", "custom-")
+
+                .build();
+    }
+
+    @Module
+    public EnterpriseBean bean() {
+        return new SingletonBean(CustomCUBean.class).localBean();
+    }
+
+    @Resource(name = "concurrent/es")
+    private ManagedExecutorService es;
+
+    @Resource(name = "concurrent/ses")
+    private ManagedScheduledExecutorService ses;
+
+    @Resource(name = "concurrent/tf")
+    private ManagedThreadFactory tf;
+
+    @EJB
+    private CustomCUBean bean;
+
+    @Test
+    public void checkInjections() {
+        doCheck(es, ses, tf);
+
+        bean.check();
+    }
+
+    private static void doCheck(final ManagedExecutorService es, final ManagedScheduledExecutorService ses, final ManagedThreadFactory tf) {
+        assertNotNull(es);
+        assertNotNull(ses);
+        assertNotNull(tf);
+
+        assertThat(es, instanceOf(ManagedExecutorServiceImpl.class));
+        assertEquals(2, pool(es).getCorePoolSize());
+        assertEquals(10, pool(es).getMaximumPoolSize());
+        assertEquals(4, pool(es).getKeepAliveTime(TimeUnit.MINUTES));
+
+        assertThat(ses, instanceOf(ManagedScheduledExecutorServiceImpl.class));
+        assertEquals(12, pool(ses).getCorePoolSize());
+
+        assertThat(tf, instanceOf(ManagedThreadFactoryImpl.class));
+        assertEquals("custom-", Reflections.get(tf, "prefix"));
+    }
+
+    private static ThreadPoolExecutor pool(ManagedExecutorService es) {
+        return ThreadPoolExecutor.class.cast(ManagedExecutorServiceImpl.class.cast(es).getDelegate());
+    }
+
+    @Singleton
+    public static class CustomCUBean {
+        @Resource(name = "concurrent/es")
+        private ManagedExecutorService es;
+
+        @Resource(name = "concurrent/ses")
+        private ManagedScheduledExecutorService ses;
+
+        @Resource(name = "concurrent/tf")
+        private ManagedThreadFactory tf;
+
+        public void check() {
+            doCheck(es, ses, tf);
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/InjectionTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/InjectionTest.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/InjectionTest.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/InjectionTest.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,129 @@
+/**
+ * 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.openejb.concurrencyutilities.test;
+
+import org.apache.openejb.concurrencyutilities.ee.impl.ContextServiceImpl;
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedExecutorServiceImpl;
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedScheduledExecutorServiceImpl;
+import org.apache.openejb.concurrencyutilities.ee.impl.ManagedThreadFactoryImpl;
+import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.jee.EnterpriseBean;
+import org.apache.openejb.jee.SingletonBean;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Module;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+import javax.enterprise.concurrent.ContextService;
+import javax.enterprise.concurrent.ManagedExecutorService;
+import javax.enterprise.concurrent.ManagedScheduledExecutorService;
+import javax.enterprise.concurrent.ManagedThreadFactory;
+import javax.naming.InitialContext;
+import javax.transaction.UserTransaction;
+import java.util.concurrent.Callable;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(ApplicationComposer.class)
+public class InjectionTest {
+    @Module
+    public EnterpriseBean bean() {
+        return new SingletonBean(CUBean.class).localBean();
+    }
+
+    @Resource
+    private ManagedExecutorService es;
+
+    @Resource
+    private ManagedScheduledExecutorService ses;
+
+    @Resource
+    private ContextService ces;
+
+    @Resource
+    private ManagedThreadFactory tf;
+
+    @EJB
+    private CUBean bean;
+
+    @Before
+    public void cleanUpContext() {
+        ThreadContext.exit(null);
+    }
+
+    @Test
+    public void checkInjections() {
+        doCheck(es, ses, ces, tf);
+
+        bean.checkNotNull();
+    }
+
+    @Test
+    public void checkUserTransactionIsAccessible() throws Exception {
+        assertTrue(es.submit(new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                try {
+                    final Object ut = new InitialContext().lookup("java:comp/UserTransaction");
+                    assertThat(ut, instanceOf(UserTransaction.class));
+                    return true;
+                } catch (final Exception e) {
+                    return false;
+                }
+            }
+        }).get());
+    }
+
+    private static void doCheck(final ManagedExecutorService es, final ManagedScheduledExecutorService ses,
+                                final ContextService ces, final ManagedThreadFactory tf) {
+        assertNotNull(es);
+        assertNotNull(ses);
+        assertNotNull(ces);
+        assertNotNull(tf);
+
+        assertThat(es, instanceOf(ManagedExecutorServiceImpl.class));
+        assertThat(ses, instanceOf(ManagedScheduledExecutorServiceImpl.class));
+        assertThat(ces, instanceOf(ContextServiceImpl.class));
+        assertThat(tf, instanceOf(ManagedThreadFactoryImpl.class));
+    }
+
+    @Singleton
+    public static class CUBean {
+        @Resource
+        private ManagedExecutorService es;
+
+        @Resource
+        private ManagedScheduledExecutorService ses;
+
+        @Resource
+        private ContextService ces;
+
+        @Resource
+        private ManagedThreadFactory tf;
+
+        public void checkNotNull() {
+            doCheck(es, ses, ces, tf);
+        }
+    }
+}

Added: tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ManagedExecutorServiceTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ManagedExecutorServiceTest.java?rev=1493710&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ManagedExecutorServiceTest.java (added)
+++ tomee/tomee/trunk/container/openejb-concurrency-utilities-ee/src/test/java/org/apache/openejb/concurrencyutilities/test/ManagedExecutorServiceTest.java Mon Jun 17 10:44:11 2013
@@ -0,0 +1,94 @@
+/**
+ * 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.openejb.concurrencyutilities.test;
+
+import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Module;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+import javax.enterprise.concurrent.ManagedExecutorService;
+import javax.enterprise.inject.Typed;
+import javax.inject.Inject;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+
+import static org.junit.Assert.assertTrue;
+
+@RunWith(ApplicationComposer.class)
+public class ManagedExecutorServiceTest {
+    @Module
+    public Class<?>[] bean() {
+        return new Class<?>[] { ExecutorFacade.class, CdiExecutorFacade.class };
+    }
+
+    @EJB
+    private ExecutorFacade facade;
+
+    @Inject
+    private CdiExecutorFacade cdiFacade;
+
+    @Before
+    public void cleanUpContext() {
+        ThreadContext.exit(null);
+    }
+
+    @Test
+    public void checkEjbContext() throws Exception {
+        assertTrue(facade.submit().get());
+    }
+
+    @Test
+    public void checkCdiContext() throws Exception {
+        assertTrue(cdiFacade.submit().get());
+    }
+
+    @Singleton
+    @Typed(ExecutorFacade.class)
+    public static class ExecutorFacade extends CdiExecutorFacade {
+        @Resource
+        private ManagedExecutorService es;
+
+        @Override
+        protected boolean isValid() {
+            return ThreadContext.getThreadContext().getBeanContext().getBeanClass() == ExecutorFacade.class;
+        }
+    }
+
+    public static class CdiExecutorFacade {
+        @Resource
+        private ManagedExecutorService es;
+
+        public Future<Boolean> submit() {
+            return es.submit(new Callable<Boolean>() {
+                @Override
+                public Boolean call() throws Exception {
+                    return isValid();
+                }
+            });
+        }
+
+        protected boolean isValid() {
+            return ThreadContext.getThreadContext() == null;
+        }
+    }
+}