You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2008/03/07 20:56:28 UTC
svn commit: r634792 [2/9] - in /geronimo/sandbox/concurrent: ./
concurrent-deployer/ concurrent-deployer/src/ concurrent-deployer/src/main/
concurrent-deployer/src/main/plan/ concurrent/ concurrent/src/
concurrent/src/main/ concurrent/src/main/plan/ ge...
Added: geronimo/sandbox/concurrent/geronimo-concurrent-builder/src/main/java/org/apache/geronimo/concurrent/builder/ResourceRefBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-builder/src/main/java/org/apache/geronimo/concurrent/builder/ResourceRefBuilder.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-builder/src/main/java/org/apache/geronimo/concurrent/builder/ResourceRefBuilder.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-builder/src/main/java/org/apache/geronimo/concurrent/builder/ResourceRefBuilder.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,333 @@
+/**
+ * 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.geronimo.concurrent.builder;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.Resource;
+import javax.util.concurrent.ContextService;
+import javax.util.concurrent.ManagedExecutorService;
+import javax.util.concurrent.ManagedScheduledExecutorService;
+import javax.util.concurrent.ManagedThreadFactory;
+import javax.xml.namespace.QName;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.common.DeploymentException;
+import org.apache.geronimo.common.UnresolvedReferenceException;
+import org.apache.geronimo.concurrent.naming.ResourceReferenceFactory;
+import org.apache.geronimo.gbean.AbstractName;
+import org.apache.geronimo.gbean.AbstractNameQuery;
+import org.apache.geronimo.gbean.GBeanInfo;
+import org.apache.geronimo.gbean.GBeanInfoBuilder;
+import org.apache.geronimo.j2ee.deployment.Module;
+import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedApp;
+import org.apache.geronimo.j2ee.deployment.annotation.ResourceAnnotationHelper;
+import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
+import org.apache.geronimo.kernel.GBeanNotFoundException;
+import org.apache.geronimo.kernel.Kernel;
+import org.apache.geronimo.kernel.config.Configuration;
+import org.apache.geronimo.kernel.repository.Environment;
+import org.apache.geronimo.management.ManagedConstants;
+import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
+import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
+import org.apache.geronimo.xbeans.geronimo.naming.GerResourceEnvRefDocument;
+import org.apache.geronimo.xbeans.geronimo.naming.GerResourceEnvRefType;
+import org.apache.geronimo.xbeans.javaee.DescriptionType;
+import org.apache.geronimo.xbeans.javaee.FullyQualifiedClassType;
+import org.apache.geronimo.xbeans.javaee.InjectionTargetType;
+import org.apache.geronimo.xbeans.javaee.JndiNameType;
+import org.apache.geronimo.xbeans.javaee.ResourceEnvRefType;
+import org.apache.geronimo.xbeans.javaee.XsdStringType;
+import org.apache.xmlbeans.QNameSet;
+import org.apache.xmlbeans.XmlObject;
+
+/**
+ * @version $Rev: 587764 $ $Date: 2008/03/06 22:05:03 $
+ */
+public class ResourceRefBuilder extends AbstractNamingBuilder {
+ private final static Log log = LogFactory.getLog(ResourceRefBuilder.class);
+
+ private static final QName GER_MANAGED_OBJECT_REF_QNAME = GerResourceEnvRefDocument.type.getDocumentElementName();
+ private static final QNameSet GER_MANAGED_OBJECT_REF_QNAME_SET = QNameSet.singleton(GER_MANAGED_OBJECT_REF_QNAME);
+
+ private final QNameSet resourceRefQNameSet;
+ private final Kernel kernel;
+
+ public ResourceRefBuilder(Kernel kernel, Environment defaultEnvironment, String[] eeNamespaces) {
+ super(defaultEnvironment);
+ this.kernel = kernel;
+ this.resourceRefQNameSet = buildQNameSet(eeNamespaces, "resource-env-ref");
+ }
+
+ protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
+ return specDD.selectChildren(resourceRefQNameSet).length > 0;
+ }
+
+ static Key<Map<String, GerResourceEnvRefType>> DEFAULT_MAPPINGS_KEY = new Key<Map<String, GerResourceEnvRefType>>() {
+ public Map<String, GerResourceEnvRefType> get(Map context) {
+ Map<String, GerResourceEnvRefType> result =
+ (Map<String, GerResourceEnvRefType>) context.get(this);
+ if (result == null) {
+ result = new HashMap<String, GerResourceEnvRefType>();
+ context.put(this, result);
+ }
+ return result;
+ }
+ };
+
+ public void buildNaming(XmlObject specDD, XmlObject plan, Module module, Map componentContext) throws DeploymentException {
+ XmlObject[] gerResourceEnvRefsUntyped = plan == null ? NO_REFS : plan.selectChildren(GER_MANAGED_OBJECT_REF_QNAME_SET);
+ Map<String, GerResourceEnvRefType> refMap = mapResourceEnvRefs(gerResourceEnvRefsUntyped);
+
+ // Discover and process any @Resource annotations (if !metadata-complete)
+ if (module.getClassFinder() != null) {
+ // Process all the annotations for this naming builder type
+ try {
+ ManagedResourceRefProcessor processor = new ManagedResourceRefProcessor(refMap, module.getSharedContext());
+ ResourceAnnotationHelper.processAnnotations(module.getAnnotatedApp(), module.getClassFinder(), processor);
+ } catch (Exception e) {
+ log.warn("Unable to process @Resource annotations for module " + module.getName(), e);
+ }
+ }
+
+ Map<String, GerResourceEnvRefType> defaultMappings = DEFAULT_MAPPINGS_KEY.get(module.getSharedContext());
+ refMap.putAll(defaultMappings);
+
+ List<ResourceEnvRefType> resourceEnvRefsUntyped = convert(specDD.selectChildren(resourceRefQNameSet), JEE_CONVERTER, ResourceEnvRefType.class, ResourceEnvRefType.type);
+ ClassLoader cl = module.getEarContext().getClassLoader();
+ for (ResourceEnvRefType resourceEnvRef : resourceEnvRefsUntyped) {
+ String name = getStringValue(resourceEnvRef.getResourceEnvRefName());
+ String type = getStringValue(resourceEnvRef.getResourceEnvRefType());
+
+ addInjections(name, resourceEnvRef.getInjectionTargetArray(), componentContext);
+
+ Class iface;
+ try {
+ iface = cl.loadClass(type);
+ } catch (ClassNotFoundException e) {
+ throw new DeploymentException("could not load class " + type, e);
+ }
+
+ GerResourceEnvRefType gerResourceEnvRef = refMap.remove(name);
+
+ String j2eeType = null;
+ if (iface == ContextService.class) {
+
+ } else if (iface == ManagedThreadFactory.class) {
+ j2eeType = ManagedConstants.MANAGED_THREAD_FACTORY;
+ } else if (iface == ManagedExecutorService.class) {
+ j2eeType = ManagedConstants.MANAGED_EXECUTOR_SERVICE;
+ } else if (iface == ManagedScheduledExecutorService.class) {
+ j2eeType = ManagedConstants.MANAGED_EXECUTOR_SERVICE;
+ } else {
+ log.debug("Ignoring non-managed resource reference type: " + iface);
+ continue;
+ }
+
+ try {
+ // TODO: should we also pass interfaces to discover the right gbeans?
+ AbstractNameQuery containerId = getAdminObjectContainerId(name, j2eeType, gerResourceEnvRef);
+ ResourceReferenceFactory ref = buildManagedObjectReference(module, containerId, iface);
+ getJndiContextMap(componentContext).put(ENV + name, ref);
+ } catch (UnresolvedReferenceException e) {
+ throw new DeploymentException(
+ "Unable to resolve resource env reference '"
+ + name
+ + "' ("
+ + (e.isMultiple() ? "found multiple matching resources"
+ : "no matching resources found") + ")", e);
+ }
+ }
+ }
+
+ private ResourceReferenceFactory buildManagedObjectReference(Module module, AbstractNameQuery containerId, Class iface) throws DeploymentException {
+ Configuration localConfiguration = module.getEarContext().getConfiguration();
+ try {
+ // first, lookup in configuration
+ localConfiguration.findGBean(containerId);
+ } catch (GBeanNotFoundException e) {
+ // second, lookup in kernel
+ Set results = this.kernel.listGBeans(containerId);
+ if (results == null || results.isEmpty()) {
+ throw new DeploymentException("Cannot resolve managed object ref " + containerId);
+ } else if (results.size() > 1) {
+ throw new DeploymentException("Managed object ref resolved to multiple results " + containerId);
+ }
+ }
+ return new ResourceReferenceFactory(module.getConfigId(), containerId, iface, module.getModuleName());
+ }
+
+ private static AbstractNameQuery getAdminObjectContainerId(String name, String j2eeType, GerResourceEnvRefType gerResourceEnvRef) {
+ AbstractNameQuery containerId;
+ if (gerResourceEnvRef == null) {
+ containerId = buildAbstractNameQuery(null, null, name, j2eeType, null);
+ } else {
+ //construct name from components
+ GerPatternType patternType = gerResourceEnvRef.getPattern();
+ containerId = buildAbstractNameQuery(patternType, j2eeType, null, null);
+ }
+ return containerId;
+ }
+
+ private static Map<String, GerResourceEnvRefType> mapResourceEnvRefs(XmlObject[] refs) {
+ Map<String, GerResourceEnvRefType> refMap = new HashMap<String, GerResourceEnvRefType>();
+ if (refs != null) {
+ for (XmlObject ref1 : refs) {
+ GerResourceEnvRefType ref = (GerResourceEnvRefType) ref1.copy().changeType(GerResourceEnvRefType.type);
+ refMap.put(ref.getRefName().trim(), ref);
+ }
+ }
+ return refMap;
+ }
+
+ public QNameSet getSpecQNameSet() {
+ return resourceRefQNameSet;
+ }
+
+ public QNameSet getPlanQNameSet() {
+ return GER_MANAGED_OBJECT_REF_QNAME_SET;
+ }
+
+ private static class ManagedResourceRefProcessor extends ResourceAnnotationHelper.ResourceProcessor {
+
+ private Map<String, GerResourceEnvRefType> refMap;
+ private Map sharedContext;
+
+ public ManagedResourceRefProcessor(Map<String, GerResourceEnvRefType> refMap,
+ Map sharedContext) {
+ this.refMap = refMap;
+ this.sharedContext = sharedContext;
+ }
+
+ private static String getDefaultServiceMapping(String resourceType) {
+ if (resourceType.equals(ContextService.class.getName())) {
+ return "DefaultContextService";
+ } else if (resourceType.equals(ManagedThreadFactory.class.getName())) {
+ return "DefaultManagedThreadFactory";
+ } else if (resourceType.equals(ManagedExecutorService.class.getName())) {
+ return "DefaultManagedExecutorService";
+ } else if (resourceType.equals(ManagedScheduledExecutorService.class.getName())) {
+ return "DefaultManagedScheduledExecutorService";
+ } else {
+ throw new IllegalArgumentException("Invalid resource type: " + resourceType);
+ }
+ }
+
+ public boolean processResource(AnnotatedApp annotatedApp, Resource annotation, Class cls, Method method, Field field) throws DeploymentException {
+ String resourceName = getResourceName(annotation, method, field);
+ String resourceType = getResourceType(annotation, method, field);
+
+ if (resourceType.equals(ContextService.class.getName()) ||
+ resourceType.equals(ManagedThreadFactory.class.getName()) ||
+ resourceType.equals(ManagedExecutorService.class.getName()) ||
+ resourceType.equals(ManagedScheduledExecutorService.class.getName())) {
+
+ ResourceEnvRefType resourceEnvRef = null;
+
+ ResourceEnvRefType[] ResourceEnvRefs = annotatedApp.getResourceEnvRefArray();
+ for (ResourceEnvRefType resourceEnvRefType : ResourceEnvRefs) {
+ if (resourceEnvRefType.getResourceEnvRefName().getStringValue().trim().equals(resourceName)) {
+ resourceEnvRef = resourceEnvRefType;
+ break;
+ }
+ }
+
+ if (resourceEnvRef == null) {
+ resourceEnvRef = annotatedApp.addNewResourceEnvRef();
+
+ // resource-env-ref-name
+ JndiNameType resourceEnvRefName = resourceEnvRef.addNewResourceEnvRefName();
+ resourceEnvRefName.setStringValue(resourceName);
+ resourceEnvRef.setResourceEnvRefName(resourceEnvRefName);
+ }
+
+ // resource-env-ref-type
+ if (!resourceEnvRef.isSetResourceEnvRefType() && !resourceType.equals("")) {
+ FullyQualifiedClassType qualifiedClass = resourceEnvRef.addNewResourceEnvRefType();
+ qualifiedClass.setStringValue(resourceType);
+ resourceEnvRef.setResourceEnvRefType(qualifiedClass);
+ }
+
+ // description
+ if ((resourceEnvRef.getDescriptionArray() == null ||
+ resourceEnvRef.getDescriptionArray().length == 0) &&
+ annotation.description().trim().length() > 0) {
+ DescriptionType description = resourceEnvRef.addNewDescription();
+ String descriptionAnnotation = annotation.description();
+ description.setStringValue(descriptionAnnotation);
+
+ }
+
+ // mapped-name
+ if (!resourceEnvRef.isSetMappedName() && annotation.mappedName().trim().length() > 0) {
+ XsdStringType mappedName = resourceEnvRef.addNewMappedName();
+ mappedName.setStringValue(annotation.mappedName().trim());
+ resourceEnvRef.setMappedName(mappedName);
+ }
+
+ // injection target
+ if (method != null || field != null) {
+ InjectionTargetType[] targets = resourceEnvRef.getInjectionTargetArray();
+ if (!hasTarget(method, field, targets)) {
+ configureInjectionTarget(resourceEnvRef.addNewInjectionTarget(), method, field);
+ }
+ }
+
+ // automatically map to default services
+ if (annotation.name().trim().length() == 0 && !refMap.containsKey(resourceName)) {
+ GerResourceEnvRefType gerResourceEnvRefType = GerResourceEnvRefType.Factory.newInstance();
+ gerResourceEnvRefType.setRefName(resourceName);
+
+ GerPatternType patternType = gerResourceEnvRefType.addNewPattern();
+
+ patternType.setName(getDefaultServiceMapping(resourceType));
+
+ DEFAULT_MAPPINGS_KEY.get(sharedContext).put(resourceName, gerResourceEnvRefType);
+ }
+ }
+
+ return true;
+ }
+ }
+
+ public static final GBeanInfo GBEAN_INFO;
+
+ static {
+ GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(ResourceRefBuilder.class, NameFactory.MODULE_BUILDER);
+ infoBuilder.addAttribute("kernel", Kernel.class, false, false);
+ infoBuilder.addAttribute("eeNamespaces", String[].class, true, true);
+ infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
+
+ infoBuilder.setConstructor(new String[]{ "kernel",
+ "defaultEnvironment",
+ "eeNamespaces" });
+
+ GBEAN_INFO = infoBuilder.getBeanInfo();
+ }
+
+ public static GBeanInfo getGBeanInfo() {
+ return GBEAN_INFO;
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-builder/src/main/java/org/apache/geronimo/concurrent/builder/ResourceRefBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-builder/src/main/java/org/apache/geronimo/concurrent/builder/ResourceRefBuilder.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/LICENSE.txt
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/LICENSE.txt?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/LICENSE.txt (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/LICENSE.txt Fri Mar 7 11:56:14 2008
@@ -0,0 +1,203 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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.
+
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/LICENSE.txt
------------------------------------------------------------------------------
svn:eol-style = native
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/NOTICE.txt
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/NOTICE.txt?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/NOTICE.txt (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/NOTICE.txt Fri Mar 7 11:56:14 2008
@@ -0,0 +1,53 @@
+Apache Geronimo
+Copyright 2003-2007 The Apache Software Foundation
+
+This product includes software developed by
+The Apache Software Foundation (http://www.apache.org/).
+
+=========================================================================
+== Apache Harmony Notice ==
+=========================================================================
+
+This product includes software developed by The Apache Software
+Foundation (http://www.apache.org/).
+
+Portions of Harmony were originally developed by
+Intel Corporation and are licensed to the Apache Software
+Foundation under the "Software Grant and Corporate Contribution
+License Agreement", informally known as the "Intel Harmony CLA".
+
+Portions of Apache Harmony Classlib reference RFC 3280 - "Internet X.509 Public
+Key Infrastructure, Certificate and Certificate Revocation List (CRL) Profile".
+Portions of RFC 3280 are included in Harmony Javadoc for reference, and in
+accordance with the licenssing terms for RFC3280, the license is included here :
+
+ Copyright (C) The Internet Society (2002). All Rights Reserved.
+
+ This document and translations of it may be copied and furnished to
+ others, and derivative works that comment on or otherwise explain it
+ or assist in its implementation may be prepared, copied, published
+ and distributed, in whole or in part, without restriction of any
+ kind, provided that the above copyright notice and this paragraph are
+ included on all such copies and derivative works. However, this
+ document itself may not be modified in any way, such as by removing
+ the copyright notice or references to the Internet Society or other
+ Internet organizations, except as needed for the purpose of
+ developing Internet standards in which case the procedures for
+ copyrights defined in the Internet Standards process must be
+ followed, or as required to translate it into languages other than
+ English.
+
+ The limited permissions granted above are perpetual and will not be
+ revoked by the Internet Society or its successors or assigns.
+
+ This document and the information contained herein is provided on an
+ "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
+ TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
+ BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
+ HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+
+
+This software includes software developed by Doug Lea with assistance from
+members of JCP JSR-166 which has been released to the public domain,
+as explained at: http://creativecommons.org/licenses/publicdomain.
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/NOTICE.txt
------------------------------------------------------------------------------
svn:eol-style = native
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml Fri Mar 7 11:56:14 2008
@@ -0,0 +1,49 @@
+<?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.
+-->
+
+<!-- $Rev: 487122 $ $Date: 2008/03/04 19:02:07 $ -->
+
+<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/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.geronimo.plugins</groupId>
+ <artifactId>concurrent</artifactId>
+ <version>2.2-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.apache.geronimo.modules</groupId>
+ <artifactId>geronimo-concurrent-core</artifactId>
+ <name>Concurrency Utilities for Java EE: Base Implementation</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-concurrent_spec</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,70 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.concurrent.ManagedContextHandler;
+
+/*
+ * Managed context handler that for thread classloader.
+ */
+public class ClassLoaderContextHandler implements ManagedContextHandler {
+
+ private final static Log LOG = LogFactory.getLog(ClassLoaderContextHandler.class);
+
+ private final static String OLD_CLASSLOADER =
+ ClassLoaderContextHandler.class.getName() + ".oldClassloader";
+
+ private final static String NEW_CLASSLOADER =
+ ClassLoaderContextHandler.class.getName() + ".newClassloader";
+
+ public void saveContext(Map<String, Object> context) {
+ LOG.debug("saveContext");
+
+ Thread thread = Thread.currentThread();
+ context.put(NEW_CLASSLOADER,
+ thread.getContextClassLoader());
+ }
+
+ public void setContext(Map<String, Object> threadContext) {
+ LOG.debug("setContext");
+
+ Thread thread = Thread.currentThread();
+
+ // save existing classloader
+ threadContext.put(OLD_CLASSLOADER,
+ thread.getContextClassLoader());
+
+ // set new classloader
+ ClassLoader classLoader = (ClassLoader)threadContext.get(NEW_CLASSLOADER);
+ thread.setContextClassLoader(classLoader);
+ }
+
+ public void unsetContext(Map<String, Object> threadContext) {
+ LOG.debug("unsetContext");
+
+ Thread thread = Thread.currentThread();
+
+ // restore old classloader
+ ClassLoader classLoader = (ClassLoader)threadContext.get(OLD_CLASSLOADER);
+ thread.setContextClassLoader(classLoader);;
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContext.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContext.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContext.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContext.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,71 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.geronimo.concurrent.spi.ManagedContextBuilder;
+
+public abstract class ManagedContext {
+
+ protected ManagedContextHandler contextHandler;
+ protected Map<String, Object> context;
+
+ public ManagedContext(ManagedContextHandler handler,
+ Map<String, Object> context) {
+ if (handler == null || context == null) {
+ throw new NullPointerException("executor or listener is null");
+ }
+ this.contextHandler = handler;
+ this.context = context;
+ }
+
+ public static ManagedContext captureContext(ManagedContextHandler contextHandler) {
+ ManagedContextBuilder builder = ManagedContextBuilder.getManagedContextBuilder();
+ return builder.buildManagedContext(contextHandler);
+ }
+
+ /**
+ * Checks if the context is still valid. The context becomes invalid
+ * when the component that was used to create this context is stopped or
+ * was undeployed.
+ *
+ * @return true if the context's component is running. False, if the context's components
+ * is not running.
+ */
+ public abstract boolean isValid();
+
+ public Map<String, Object> set() {
+ Map<String, Object> threadContext = new HashMap<String, Object>();
+ set(threadContext);
+ return threadContext;
+ }
+
+ public void set(Map<String, Object> threadContext) {
+ if (!isValid()) {
+ throw new IllegalStateException("Context is not valid: context's component is not running");
+ }
+ threadContext.putAll(this.context);
+ this.contextHandler.setContext(threadContext);
+ }
+
+ public void unset(Map<String, Object> threadContext) {
+ this.contextHandler.unsetContext(threadContext);
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContext.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandler.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandler.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandler.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,61 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.Map;
+
+/**
+ * The ManagedContextHandler is used for capturing context information in one thread and
+ * restoring it on a different thread.
+ * ManagedContextHandlers are stateless. All state information must be stored in the
+ * passed context object.
+ *
+ * {@link #saveContext(Map)} will usually be called on a different thread then
+ * {@link #setContext(Map)} or {@link #unsetContext(Map)} but
+ * {@link #setContext(Map)} and {@link #unsetContext(Map)} will always be called
+ * on the same thread.
+ */
+public interface ManagedContextHandler {
+
+ /**
+ * Captures and saves the context information of the current environment.
+ * That context information will later be set on the thread in the
+ * {@link #setContext(Map)} function.
+ *
+ * @param context
+ */
+ void saveContext(Map<String, Object> context);
+
+ /**
+ * Sets the context information captured by {@link #saveContext(Map)} on the
+ * current thread.
+ * This function must also save current thread context information which will
+ * be restored by {@link #unsetContext(Map)}.
+ *
+ * @param threadContext
+ */
+ void setContext(Map<String, Object> threadContext);
+
+ /**
+ * Restores thread context information that was in place when
+ * {@link #setContext(Map)} was called.
+ *
+ * @param threadContext
+ */
+ void unsetContext(Map<String, Object> threadContext);
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandler.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,97 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class ManagedContextHandlerChain implements ManagedContextHandler {
+
+ private final static Log LOG = LogFactory.getLog(ManagedContextHandlerChain.class);
+
+ private final static String INVOKED_HANDLERS =
+ ManagedContextHandlerChain.class.getName() + ".invokedHandlers";
+
+ private List<ManagedContextHandler> chain = new ArrayList<ManagedContextHandler>();
+
+ public ManagedContextHandlerChain() {
+ }
+
+ public ManagedContextHandlerChain(List<ManagedContextHandler> handlers) {
+ if (handlers != null) {
+ this.chain.addAll(handlers);
+ }
+ }
+
+ public void addManagedContextHandler(ManagedContextHandler handler) {
+ this.chain.add(handler);
+ }
+
+ public void removeManagedContextHandler(ManagedContextHandler handler) {
+ this.chain.remove(handler);
+ }
+
+ public void saveContext(Map<String, Object> context) {
+ // call in proper order
+ ListIterator<ManagedContextHandler> iter = this.chain.listIterator();
+ while (iter.hasNext()) {
+ ManagedContextHandler handler = iter.next();
+ handler.saveContext(context);
+ }
+ }
+
+ public void setContext(Map<String, Object> threadContext) {
+ // keep track of invoked handlers
+ List<ManagedContextHandler> invokedHandlers = new ArrayList<ManagedContextHandler>();
+ threadContext.put(INVOKED_HANDLERS, invokedHandlers);
+
+ // call in proper order
+ ListIterator<ManagedContextHandler> iter = this.chain.listIterator();
+ while (iter.hasNext()) {
+ ManagedContextHandler handler = iter.next();
+ try {
+ handler.setContext(threadContext);
+ } finally {
+ invokedHandlers.add(handler);
+ }
+ }
+ }
+
+ public void unsetContext(Map<String, Object> threadContext) {
+ List<ManagedContextHandler> invokedHandlers =
+ (List<ManagedContextHandler>)threadContext.get(INVOKED_HANDLERS);
+ // call in reverse order
+ ListIterator<ManagedContextHandler> iter =
+ invokedHandlers.listIterator(invokedHandlers.size());
+ while (iter.hasPrevious()) {
+ ManagedContextHandler handler = iter.previous();
+ try {
+ handler.unsetContext(threadContext);
+ } catch (RuntimeException e) {
+ LOG.error("Error occurred in unsetContext function: " + e.getMessage(), e);
+ }
+ }
+
+ invokedHandlers.clear();
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTask.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTask.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTask.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTask.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,27 @@
+/**
+ * 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.geronimo.concurrent;
+
+public interface ManagedTask {
+
+ boolean cancel();
+
+ String getIdentityDescription(String locale);
+
+ String getIdentityName();
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTask.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTask.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskListenerSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskListenerSupport.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskListenerSupport.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskListenerSupport.java Fri Mar 7 11:56:14 2008
@@ -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.geronimo.concurrent;
+
+import java.util.concurrent.Future;
+
+import javax.util.concurrent.ManagedExecutorService;
+import javax.util.concurrent.ManagedTaskListener;
+
+public class ManagedTaskListenerSupport {
+
+ private ManagedExecutorService executor;
+ private ManagedTaskListener listener;
+
+ public ManagedTaskListenerSupport(ManagedExecutorService executor,
+ ManagedTaskListener listener) {
+ if (executor == null || listener == null) {
+ throw new NullPointerException("executor or listener is null");
+ }
+ this.executor = executor;
+ this.listener = listener;
+ }
+
+ public void taskAborted(Future<?> future, Throwable exception) {
+ this.listener.taskAborted(future, this.executor, exception);
+ }
+
+ public void taskDone(Future<?> future, Throwable exception) {
+ this.listener.taskDone(future, this.executor, exception);
+ }
+
+ public void taskStarting(Future<?> future) {
+ this.listener.taskStarting(future, this.executor);
+ }
+
+ public void taskSubmitted(Future<?> future) {
+ this.listener.taskSubmitted(future, this.executor);
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskListenerSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskListenerSupport.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskUtils.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskUtils.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskUtils.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskUtils.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,67 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.Locale;
+
+import javax.util.concurrent.Identifiable;
+
+public class ManagedTaskUtils {
+
+ public static String getTaskDescription(Object task, String localeStr) {
+ if (task == null) {
+ return null;
+ } else if (task instanceof Identifiable) {
+ Locale locale = parseLocale(localeStr);
+ return ((Identifiable)task).getIdentityDescription(locale);
+ } else {
+ return task.toString();
+ }
+ }
+
+ public static String getTaskName(Object task) {
+ if (task == null) {
+ return null;
+ } else if (task instanceof Identifiable) {
+ return ((Identifiable)task).getIdentityName();
+ } else {
+ return task.toString();
+ }
+ }
+
+ public static Locale parseLocale(String localeStr) {
+ if (localeStr == null || localeStr.trim().length() == 0) {
+ throw new IllegalArgumentException("Invalid locale string:" + localeStr);
+ }
+
+ Locale locale = null;
+
+ String[] tokens = localeStr.split("_");
+ if (tokens.length == 1) {
+ locale = new Locale(tokens[0]);
+ } else if (tokens.length == 2) {
+ locale = new Locale(tokens[0], tokens[1]);
+ } else if (tokens.length == 3) {
+ locale = new Locale(tokens[0], tokens[1], tokens[2]);
+ } else {
+ throw new IllegalArgumentException("Invalid locale string:" + localeStr);
+ }
+
+ return locale;
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedTaskUtils.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/BasicContextService.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/BasicContextService.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/BasicContextService.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/BasicContextService.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,88 @@
+/**
+ * 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.geronimo.concurrent.context;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.util.concurrent.ContextService;
+
+import org.apache.geronimo.concurrent.ManagedContextHandler;
+
+/**
+ * Base implementation of ContextService using Java Dynamic Proxy.
+ */
+public class BasicContextService implements ContextService {
+
+ protected ManagedContextHandler contextHandler;
+
+ public BasicContextService(ManagedContextHandler contextHandler) {
+ if (contextHandler == null) {
+ throw new NullPointerException("contextHandler is required");
+ }
+ this.contextHandler = contextHandler;
+ }
+
+ public Object createContextObject(Object obj, Class<?>[] interfaces) {
+ return createContextObject(obj, interfaces, new HashMap<String, String>());
+ }
+
+ public Object createContextObject(Object obj, Class<?>[] interfaces, Map<String, String> props) {
+ checkInterfaces(obj, interfaces);
+ ManagedProxy invHandler = new ManagedProxy(obj, this.contextHandler);
+ invHandler.setProperties(props);
+ return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
+ interfaces,
+ invHandler);
+ }
+
+ public Map<String, String> getProperties(Object obj) {
+ ManagedProxy proxy = getContextProxy(obj);
+ return proxy.getProperties();
+ }
+
+ public void setProperties(Object obj, Map<String, String> props) {
+ ManagedProxy proxy = getContextProxy(obj);
+ proxy.setProperties(props);
+ }
+
+ private void checkInterfaces(Object obj, Class<?>[] interfaces) {
+ if (interfaces == null || interfaces.length == 0) {
+ throw new IllegalArgumentException("Must specify proxy interfaces");
+ }
+ for (Class clazz : interfaces) {
+ if (clazz == null) {
+ throw new IllegalArgumentException("Null proxy interface");
+ }
+ if (!clazz.isAssignableFrom(obj.getClass())) {
+ throw new IllegalArgumentException("Instance must implement " + clazz.getName() + " interface");
+ }
+ }
+ }
+
+ private ManagedProxy getContextProxy(Object obj) {
+ InvocationHandler invHandler = Proxy.getInvocationHandler(obj);
+ if (invHandler instanceof ManagedProxy) {
+ return (ManagedProxy)invHandler;
+ } else {
+ throw new IllegalArgumentException("Invalid proxy");
+ }
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/BasicContextService.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/BasicContextService.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/ManagedProxy.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/ManagedProxy.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/ManagedProxy.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/ManagedProxy.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,116 @@
+/**
+ * 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.geronimo.concurrent.context;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.concurrent.ManagedContext;
+import org.apache.geronimo.concurrent.ManagedContextHandler;
+
+public class ManagedProxy implements InvocationHandler, Serializable {
+
+ private final static Log LOG = LogFactory.getLog(ManagedProxy.class);
+
+ private static final Method HASHCODE_METHOD;
+ private static final Method EQUALS_METHOD;
+ private static final Method TOSTRING_METHOD;
+
+ static {
+ try {
+ HASHCODE_METHOD = Object.class.getMethod("hashCode", null);
+ EQUALS_METHOD =
+ Object.class.getMethod("equals", new Class[] { Object.class });
+ TOSTRING_METHOD = Object.class.getMethod("toString", null);
+ } catch (NoSuchMethodException e) {
+ throw new NoSuchMethodError(e.getMessage());
+ }
+ }
+
+ private Object obj;
+ private Map<String, String> props;
+ private ManagedContext managedContext;
+
+ public ManagedProxy(Object obj, ManagedContextHandler contextHandler) {
+ if (obj == null || contextHandler == null) {
+ throw new IllegalArgumentException("object or contextHandler is null");
+ }
+ this.obj = obj;
+
+ // save context now
+ this.managedContext = ManagedContext.captureContext(contextHandler);
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ Class declaringClass = method.getDeclaringClass();
+
+ if (declaringClass == Object.class) {
+ if (method.equals(HASHCODE_METHOD) ||
+ method.equals(EQUALS_METHOD) ||
+ method.equals(TOSTRING_METHOD)) {
+ // make call without context
+ return invokeMethod(proxy, method, args);
+ } else {
+ throw new InternalError("Unexpected Object method: " + method);
+ }
+ } else {
+ // make call with context
+ return invokeContextMethod(proxy, method, args);
+ }
+ }
+
+ protected Object invokeContextMethod(Object proxy, Method method, Object[] args) throws Throwable {
+ LOG.debug("Calling managed method " + method);
+
+ Map<String, Object> threadContext = new HashMap<String, Object>();
+ threadContext.putAll(this.props);
+
+ this.managedContext.set(threadContext);
+ try {
+ return method.invoke(this.obj, args);
+ } catch (InvocationTargetException e) {
+ throw e.getTargetException();
+ } finally {
+ this.managedContext.unset(threadContext);
+ }
+ }
+
+ protected Object invokeMethod(Object proxy, Method method, Object[] args) throws Throwable {
+ LOG.debug("Calling non-managed method " + method);
+
+ try {
+ return method.invoke(this.obj, args);
+ } catch (InvocationTargetException e) {
+ throw e.getTargetException();
+ }
+ }
+
+ public void setProperties(Map<String, String> props) {
+ this.props = props;
+ }
+
+ public Map<String, String> getProperties() {
+ return this.props;
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/ManagedProxy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/context/ManagedProxy.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/executor/AbstractManagedExecutorService.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/executor/AbstractManagedExecutorService.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/executor/AbstractManagedExecutorService.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/executor/AbstractManagedExecutorService.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,381 @@
+/**
+ * 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.
+ */
+
+/*
+ * This class extends java.util.concurrent.ThreadPoolExecutor and borrows some code from
+ * java.util.concurrent.AbstractExecutorService class in Apache Harmony.
+ */
+package org.apache.geronimo.concurrent.executor;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import javax.util.concurrent.ManagedExecutorService;
+import javax.util.concurrent.ManagedTaskListener;
+
+import org.apache.geronimo.concurrent.ManagedContext;
+import org.apache.geronimo.concurrent.ManagedTaskListenerSupport;
+import org.apache.geronimo.concurrent.harmony.ThreadPoolExecutor;
+import org.apache.geronimo.concurrent.thread.ManagedRunnable;
+
+public abstract class AbstractManagedExecutorService
+ extends ThreadPoolExecutor
+ implements ManagedExecutorService {
+
+ public AbstractManagedExecutorService(int corePoolSize,
+ int maximumPoolSize,
+ long keepAliveTime,
+ TimeUnit unit,
+ BlockingQueue<Runnable> workQueue,
+ ThreadFactory threadFactory) {
+ super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
+ }
+
+ protected abstract ManagedContext getManagedContext();
+
+ // invokeAny() functions
+
+ /**
+ * the main mechanics of invokeAny.
+ */
+ private <T> T doInvokeAny(Collection<Callable<T>> tasks,
+ boolean timed,
+ long nanos,
+ ManagedTaskListener listener)
+ throws InterruptedException,
+ ExecutionException,
+ TimeoutException {
+ if (tasks == null) {
+ throw new NullPointerException();
+ }
+ int ntasks = tasks.size();
+ if (ntasks == 0) {
+ throw new IllegalArgumentException();
+ }
+ List<Future<T>> futures= new ArrayList<Future<T>>(ntasks);
+ ManagedTaskListenerSupport listenerSupport = getManagedTaskListenerSupport(listener);
+ ManagedContext managedContext = getManagedContext();
+ ManagedExecutorCompletionService<T> ecs =
+ new ManagedExecutorCompletionService<T>(this, managedContext, listenerSupport);
+
+ // For efficiency, especially in executors with limited
+ // parallelism, check to see if previously submitted tasks are
+ // done before submitting more of them. This interleaving
+ // plus the exception mechanics account for messiness of main
+ // loop.
+
+ try {
+ // Record exceptions so that if we fail to obtain any
+ // result, we can throw the last exception we got.
+ ExecutionException ee = null;
+ long lastTime = (timed)? System.nanoTime() : 0;
+ Iterator<Callable<T>> it = tasks.iterator();
+
+ // Start one task for sure; the rest incrementally
+ futures.add(ecs.submit(it.next()));
+ --ntasks;
+ int active = 1;
+
+ for (;;) {
+ Future<T> f = ecs.poll();
+ if (f == null) {
+ if (ntasks > 0) {
+ --ntasks;
+ futures.add(ecs.submit(it.next()));
+ ++active;
+ } else if (active == 0) {
+ break;
+ } else if (timed) {
+ f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
+ if (f == null) {
+ throw new TimeoutException();
+ }
+ long now = System.nanoTime();
+ nanos -= now - lastTime;
+ lastTime = now;
+ } else {
+ f = ecs.take();
+ }
+ }
+ if (f != null) {
+ --active;
+ try {
+ return f.get();
+ } catch(InterruptedException ie) {
+ throw ie;
+ } catch(ExecutionException eex) {
+ ee = eex;
+ } catch(RuntimeException rex) {
+ ee = new ExecutionException(rex);
+ }
+ }
+ }
+
+ if (ee == null) {
+ ee = new ExecutionException(null);
+ }
+ throw ee;
+
+ } finally {
+ for (Future<T> f : futures) {
+ f.cancel(true);
+ }
+ }
+ }
+
+ public <T> T invokeAny(Collection<Callable<T>> tasks)
+ throws InterruptedException,
+ ExecutionException {
+ return invokeAny(tasks, null);
+ }
+
+ public <T> T invokeAny(Collection<Callable<T>> tasks,
+ ManagedTaskListener listener)
+ throws InterruptedException,
+ ExecutionException {
+ try {
+ return doInvokeAny(tasks, false, 0, listener);
+ } catch (TimeoutException cannotHappen) {
+ assert false;
+ return null;
+ }
+ }
+
+ public <T> T invokeAny(Collection<Callable<T>> tasks, long timeout, TimeUnit unit)
+ throws InterruptedException,
+ ExecutionException,
+ TimeoutException {
+ return invokeAny(tasks, timeout, unit, null);
+ }
+
+ public <T> T invokeAny(Collection<Callable<T>> tasks,
+ long timeout,
+ TimeUnit unit,
+ ManagedTaskListener listener)
+ throws InterruptedException,
+ ExecutionException,
+ TimeoutException {
+ return doInvokeAny(tasks, true, unit.toNanos(timeout), listener);
+ }
+
+ // invokeAll() functions
+
+ public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
+ throws InterruptedException {
+ return invokeAll(tasks, null);
+ }
+
+ public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
+ ManagedTaskListener listener)
+ throws InterruptedException {
+ if (tasks == null) {
+ throw new NullPointerException();
+ }
+ ManagedTaskListenerSupport listenerSupport = getManagedTaskListenerSupport(listener);
+ ManagedContext managedContext = getManagedContext();
+ List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
+ boolean done = false;
+ try {
+ for (Callable<T> task : tasks) {
+ ManagedFutureTask<T> future = createManagedTask(task, managedContext, listenerSupport);
+ futures.add(future);
+ executeTask(future);
+ }
+ for (Future<T> future : futures) {
+ if (!future.isDone()) {
+ try {
+ future.get();
+ } catch(CancellationException ignore) {
+ } catch(ExecutionException ignore) {
+ }
+ }
+ }
+ done = true;
+ return futures;
+ } finally {
+ if (!done) {
+ for (Future<T> future : futures) {
+ future.cancel(true);
+ }
+ }
+ }
+ }
+
+ public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
+ long timeout,
+ TimeUnit unit)
+ throws InterruptedException {
+ return invokeAll(tasks, timeout, unit, null);
+ }
+
+ public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
+ long timeout,
+ TimeUnit unit,
+ ManagedTaskListener listener)
+ throws InterruptedException {
+ if (tasks == null || unit == null) {
+ throw new NullPointerException();
+ }
+ ManagedTaskListenerSupport listenerSupport = getManagedTaskListenerSupport(listener);
+ ManagedContext managedContext = getManagedContext();
+ long nanos = unit.toNanos(timeout);
+ List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
+ boolean done = false;
+ try {
+ for (Callable<T> task : tasks) {
+ futures.add(createManagedTask(task, managedContext, listenerSupport));
+ }
+ long lastTime = System.nanoTime();
+
+ // Interleave time checks and calls to execute in case
+ // executor doesn't have any/much parallelism.
+ for (Future<T> future : futures) {
+ executeTask((ManagedFutureTask<T>)future);
+ long now = System.nanoTime();
+ nanos -= now - lastTime;
+ lastTime = now;
+ if (nanos <= 0) {
+ return futures;
+ }
+ }
+
+ for (Future<T> future : futures) {
+ if (!future.isDone()) {
+ if (nanos <= 0)
+ return futures;
+ try {
+ future.get(nanos, TimeUnit.NANOSECONDS);
+ } catch(CancellationException ignore) {
+ } catch(ExecutionException ignore) {
+ } catch(TimeoutException toe) {
+ return futures;
+ }
+ long now = System.nanoTime();
+ nanos -= now - lastTime;
+ lastTime = now;
+ }
+ }
+ done = true;
+ return futures;
+ } finally {
+ if (!done) {
+ for (Future<T> f : futures) {
+ f.cancel(true);
+ }
+ }
+ }
+ }
+
+ // execute and submit functions
+
+ public void execute(Runnable task) {
+ if (task == null) {
+ throw new NullPointerException();
+ }
+ ManagedRunnable managedRunnable = new ManagedRunnable(task, getManagedContext());
+ super.execute(managedRunnable);
+ }
+
+ public <T> Future<T> submit(Callable<T> task) {
+ return submit(task, null);
+ }
+
+ public Future<?> submit(Runnable task) {
+ return submit(task, null, null);
+ }
+
+ public <T> Future<T> submit(Runnable task, T result) {
+ return submit(task, result, null);
+ }
+
+ public Future<?> submit(Runnable task, ManagedTaskListener listener) {
+ return submit(task, null, listener);
+ }
+
+ public <T> Future<T> submit(Runnable task, T result, ManagedTaskListener listener) {
+ if (task == null) {
+ throw new NullPointerException();
+ }
+
+ ManagedTaskListenerSupport listenerSupport = getManagedTaskListenerSupport(listener);
+ ManagedContext managedContext = getManagedContext();
+ ManagedFutureTask<T> managedFuture = new ManagedFutureTask<T>(task,
+ result,
+ managedContext,
+ listenerSupport);
+ executeTask(managedFuture);
+
+ return managedFuture;
+ }
+
+ public <T> Future<T> submit(Callable<T> task, ManagedTaskListener listener) {
+ if (task == null) {
+ throw new NullPointerException();
+ }
+
+ ManagedTaskListenerSupport listenerSupport = getManagedTaskListenerSupport(listener);
+ ManagedContext managedContext = getManagedContext();
+ ManagedFutureTask<T> managedFuture = new ManagedFutureTask<T>(task,
+ managedContext,
+ listenerSupport);
+ executeTask(managedFuture);
+
+ return managedFuture;
+ }
+
+ private <T> ManagedFutureTask<T> createManagedTask(Callable<T> task,
+ ManagedContext managedContext,
+ ManagedTaskListenerSupport listenerSupport) {
+ ManagedFutureTask<T> managedFuture =
+ new ManagedFutureTask<T>(task, managedContext, listenerSupport);
+ return managedFuture;
+ }
+
+ protected ManagedTaskListenerSupport getManagedTaskListenerSupport(ManagedTaskListener listener) {
+ return (listener == null) ? null : new ManagedTaskListenerSupport(this, listener);
+ }
+
+ /*
+ * This is called by invokeAll/invokeAny/submit functions.
+ */
+ protected void executeTask(ManagedFutureTask<?> task) {
+ ManagedTaskListenerSupport listenerSupport = task.getManagedTaskListenerSupport();
+ if (listenerSupport != null) {
+ listenerSupport.taskSubmitted(task);
+ }
+ try {
+ super.execute(task);
+ } catch (RejectedExecutionException exception) {
+ if (listenerSupport != null) {
+ listenerSupport.taskDone(task, exception);
+ }
+ throw exception;
+ }
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/executor/AbstractManagedExecutorService.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/executor/AbstractManagedExecutorService.java
------------------------------------------------------------------------------
svn:executable =