From 875ac5d246878b199c393e9700c8a085c8939333 Mon Sep 17 00:00:00 2001 From: antelder Date: Thu, 19 Mar 2009 17:02:07 +0000 Subject: [maven-release-plugin] copy for branch tb3 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@756088 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/sca/branches/tb3/modules/stripes/LICENSE | 241 +++++++++++++++++ .../tb3/modules/stripes/META-INF/MANIFEST.MF | 31 +++ .../ant/sca/branches/tb3/modules/stripes/NOTICE | 12 + .../ant/sca/branches/tb3/modules/stripes/pom.xml | 60 +++++ .../apache/tuscany/sca/stripes/TuscanyHelper.java | 298 +++++++++++++++++++++ .../tuscany/sca/stripes/TuscanyInterceptor.java | 73 +++++ .../sca/stripes/TuscanyInterceptorSupport.java | 57 ++++ 7 files changed, 772 insertions(+) create mode 100644 sandbox/ant/sca/branches/tb3/modules/stripes/LICENSE create mode 100644 sandbox/ant/sca/branches/tb3/modules/stripes/META-INF/MANIFEST.MF create mode 100644 sandbox/ant/sca/branches/tb3/modules/stripes/NOTICE create mode 100644 sandbox/ant/sca/branches/tb3/modules/stripes/pom.xml create mode 100644 sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java create mode 100644 sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptor.java create mode 100644 sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptorSupport.java (limited to 'sandbox/ant/sca/branches/tb3/modules/stripes') diff --git a/sandbox/ant/sca/branches/tb3/modules/stripes/LICENSE b/sandbox/ant/sca/branches/tb3/modules/stripes/LICENSE new file mode 100644 index 0000000000..c77d3fe856 --- /dev/null +++ b/sandbox/ant/sca/branches/tb3/modules/stripes/LICENSE @@ -0,0 +1,241 @@ + + 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. + +=============================================================================== + +Apache Tuscany SCA for Java Subcomponents +=========================================: + +The Tuscany SCA for Java release includes a number of subcomponents with +separate copyright notices and license terms. Your use of the source +code for the these subcomponents is subject to the terms and +conditions of the following licenses. + +=============================================================================== +The module host-webapp includes files under the following license: + +Permission to copy, display and distribute the Service Component Architecture Specification and/or +portions thereof, without modification, in any medium without fee or royalty is hereby granted, provided +that you include the following on ALL copies of the Service Component Architecture Specification, or +portions thereof, that you make: + +1. A link or URL to the Service Component Architecture Specification at this location: +· http://www.osoa.org/display/Main/Service+Component+Architecture+Specifications + +2. The full text of the copyright notice as shown in the Service Component Architecture Specification. + +BEA, Cape Clear, IBM, Interface21, IONA, Oracle, Primeton, Progress Software, Red Hat, Rogue Wave, +SAP, Siemens, Software AG., Sun, Sybase, TIBCO (collectively, the "Authors") agree to grant you a +royalty-free license, under reasonable, non-discriminatory terms and conditions to patents that they deem +necessary to implement the Service Component Architecture Specification. +THE Service Component Architecture SPECIFICATION IS PROVIDED "AS IS," AND THE +AUTHORS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, +REGARDING THIS SPECIFICATION AND THE IMPLEMENTATION OF ITS CONTENTS, +INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE, NON-INFRINGEMENT OR TITLE. +THE AUTHORS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL +OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR RELATING TO ANY USE OR +DISTRIBUTION OF THE Service Components Architecture SPECIFICATION. +The name and trademarks of the Authors may NOT be used in any manner, including advertising or +publicity pertaining to the Service Component Architecture Specification or its contents without specific, + diff --git a/sandbox/ant/sca/branches/tb3/modules/stripes/META-INF/MANIFEST.MF b/sandbox/ant/sca/branches/tb3/modules/stripes/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..002b57b46e --- /dev/null +++ b/sandbox/ant/sca/branches/tb3/modules/stripes/META-INF/MANIFEST.MF @@ -0,0 +1,31 @@ +Manifest-Version: 1.0 +Tool: Bnd-0.0.255 +Bundle-Name: Apache Tuscany SCA Webapp Host +Created-By: 1.6.0_07 (Sun Microsystems Inc.) +Bundle-Vendor: The Apache Software Foundation +Bundle-Version: 2.0 +Bnd-LastModified: 1222833658000 +Bundle-ManifestVersion: 2 +Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt +Bundle-Description: Apache Tuscany SCA Stripes +Import-Package: javax.naming, + javax.servlet, + javax.servlet.http, + javax.servlet.jsp, + javax.servlet.jsp.tagext, + net.sourceforge.stripes;version="1.5", + net.sourceforge.stripes.action;version="1.5", + net.sourceforge.stripes.config;version="1.5", + net.sourceforge.stripes.controller;version="1.5", + net.sourceforge.stripes.exception;version="1.5", + net.sourceforge.stripes.util;version="1.5", + org.apache.tuscany.sca.core;version="2.0", + org.apache.tuscany.sca.host.http;version="2.0", + org.apache.tuscany.sca.host.webapp;version="2.0", + org.apache.tuscany.sca.implementation.web.runtime.utils;version="2.0", + org.apache.tuscany.sca.node;version="2.0.0", + org.oasisopen.sca;version="2.0", + org.oasisopen.sca.annotation;version="2.0" +Bundle-SymbolicName: org.apache.tuscany.sca.stripes +Bundle-DocURL: http://www.apache.org/ + diff --git a/sandbox/ant/sca/branches/tb3/modules/stripes/NOTICE b/sandbox/ant/sca/branches/tb3/modules/stripes/NOTICE new file mode 100644 index 0000000000..dfde08644b --- /dev/null +++ b/sandbox/ant/sca/branches/tb3/modules/stripes/NOTICE @@ -0,0 +1,12 @@ +${pom.name} +Copyright (c) 2005 - 2008 The Apache Software Foundation + +This product includes software developed by +The Apache Software Foundation (http://www.apache.org/). + +This product also includes software under the Service Component Architecture specification license +(see the LICENSE file contained in this distribution) with the following copyright + +(c) Copyright BEA Systems, Inc., Cape Clear Software, International Business Machines Corp, Interface21, IONA +Technologies, Oracle, Primeton Technologies, Progress Software, Red Hat, Rogue Wave Software, SAP AG., Siemens +AG., Software AG., Sun Microsystems, Inc., Sybase Inc., TIBCO Software Inc., 2005, 2008. All rights reserved. diff --git a/sandbox/ant/sca/branches/tb3/modules/stripes/pom.xml b/sandbox/ant/sca/branches/tb3/modules/stripes/pom.xml new file mode 100644 index 0000000000..502591794d --- /dev/null +++ b/sandbox/ant/sca/branches/tb3/modules/stripes/pom.xml @@ -0,0 +1,60 @@ + + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-modules + 2.0-M5-SNAPSHOT + ../pom.xml + + + tuscany-stripes + Apache Tuscany SCA Stripes Web Framework Integration + + + + org.apache.tuscany.sca + tuscany-host-webapp + 2.0-M5-SNAPSHOT + + + org.apache.tuscany.sca + tuscany-implementation-web-runtime + 2.0-M5-SNAPSHOT + + + + net.sourceforge.stripes + stripes + 1.5 + + + + javax.servlet + servlet-api + 2.5 + provided + + + + + diff --git a/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java b/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java new file mode 100644 index 0000000000..f4d7f48dae --- /dev/null +++ b/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyHelper.java @@ -0,0 +1,298 @@ +/* + * 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.tuscany.sca.stripes; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import javax.servlet.ServletContext; + +import net.sourceforge.stripes.action.ActionBeanContext; +import net.sourceforge.stripes.controller.StripesFilter; +import net.sourceforge.stripes.exception.StripesRuntimeException; +import net.sourceforge.stripes.util.Log; +import net.sourceforge.stripes.util.ReflectUtil; + +import org.apache.tuscany.sca.implementation.web.runtime.utils.ContextHelper; +import org.oasisopen.sca.annotation.Reference; + +/** + *

Static helper class that is used to lookup SCA references and inject them into objects + * (often ActionBeans). Is capable of injecting references through setter methods (property access) + * and also through direct field access if the security policy allows it. Methods and fields + * must be annotated using the SCA {@code @Reference} annotation.

+ * + *

Methods and fields may be public, protected, package-access or private. If they are not + * public an attempt is made to call {@link Method#setAccessible(boolean)} in order to make + * them accessible from this class. If the attempt fails, an exception will be thrown.

+ * + *

Method names can take any form. For example {@code setSomeBean(Bean b)} or + * {@code someBean(bean b)}. In both cases, if a specific Reference name is not supplied, + * the default name of {@code someBean} will be used.

+ * + *

The value of the {@code @Reference} annotation should be the reference on the + * SCA component with an {@code } componentType. + * + *

The first time that any of the injection methods in this class is called with a specific type + * of object, the object's class is examined for annotated fields and methods. The discovered + * fields and methods are then cached for future usage.

+ * + * Created for Tuscany from the Stripes SpringHelper written by Dan Hayes and Tim Fennell + */ +public class TuscanyHelper { + private static final Log log = Log.getInstance(TuscanyHelper.class); + + /** Lazily filled in map of Class to methods annotated with Reference. */ + private static Map, Collection> methodMap = + new ConcurrentHashMap, Collection>(); + + /** Lazily filled in map of Class to fields annotated with Reference. */ + private static Map, Collection> fieldMap = + new ConcurrentHashMap, Collection>(); + + /** + * Injects SCA References using the ComponentContext that is + * derived from the ServletContext, which is in turn looked up using the + * ActionBeanContext. + * + * @param bean the object into which to inject SCA reference + * @param context the ActionBeanContext represented by the current request + */ + public static void injectBeans(Object bean, ActionBeanContext context) { + injectBeans(bean, StripesFilter.getConfiguration().getServletContext()); + } + + /** + * Looks for all methods and fields annotated with {@code @Reference} and attempts + * to lookup and inject a managed bean into the field/property. If any annotated + * element cannot be injected an exception is thrown. + * + * @param bean the bean into which to inject SCA reference + * @param ctx the SCA ComponentContext + */ + public static void injectBeans(Object bean, ServletContext ctx) { + // First inject any values using annotated methods + for (Method m : getMethods(bean.getClass())) { + try { + Reference scaReference = m.getAnnotation(Reference.class); + boolean nameSupplied = !"".equals(scaReference.name()); + String name = nameSupplied ? scaReference.name() : methodToPropertyName(m); + Class beanType = m.getParameterTypes()[0]; + Object managedBean = findReference(ctx, name, beanType, !nameSupplied); + m.invoke(bean, managedBean); + } + catch (Exception e) { + throw new StripesRuntimeException("Exception while trying to lookup and inject " + + "an SCA Reference into a bean of type " + bean.getClass().getSimpleName() + + " using method " + m.toString(), e); + } + } + + // And then inject any properties that are annotated + for (Field f : getFields(bean.getClass())) { + try { + Reference scaReference = f.getAnnotation(Reference.class); + boolean nameSupplied = !"".equals(scaReference.name()); + String name = nameSupplied ? scaReference.name() : f.getName(); + Object managedBean = findReference(ctx, name, f.getType(), !nameSupplied); + f.set(bean, managedBean); + } + catch (Exception e) { + throw new StripesRuntimeException("Exception while trying to lookup and inject " + + "an SCA Referenceinto a bean of type " + bean.getClass().getSimpleName() + + " using field access on field " + f.toString(), e); + } + } + } + + /** + * Fetches the methods on a class that are annotated with Reference. The first time it + * is called for a particular class it will introspect the class and cache the results. + * All non-overridden methods are examined, including protected and private methods. + * If a method is not public an attempt it made to make it accessible - if it fails + * it is removed from the collection and an error is logged. + * + * @param clazz the class on which to look for Reference annotated methods + * @return the collection of methods with the annotation + */ + protected static Collection getMethods(Class clazz) { + Collection methods = methodMap.get(clazz); + if (methods == null) { + methods = ReflectUtil.getMethods(clazz); + Iterator iterator = methods.iterator(); + + while (iterator.hasNext()) { + Method method = iterator.next(); + if (!method.isAnnotationPresent(Reference.class)) { + iterator.remove(); + } + else { + // If the method isn't public, try to make it accessible + if (!method.isAccessible()) { + try { + method.setAccessible(true); + } + catch (SecurityException se) { + throw new StripesRuntimeException( + "Method " + clazz.getName() + "." + method.getName() + "is marked " + + "with @Reference and is not public. An attempt to call " + + "setAccessible(true) resulted in a SecurityException. Please " + + "either make the method public or modify your JVM security " + + "policy to allow Stripes to setAccessible(true).", se); + } + } + + // Ensure the method has only the one parameter + if (method.getParameterTypes().length != 1) { + throw new StripesRuntimeException( + "A method marked with @Reference must have exactly one parameter: " + + "the bean to be injected. Method [" + method.toGenericString() + "] has " + + method.getParameterTypes().length + " parameters." + ); + } + } + } + + methodMap.put(clazz, methods); + } + + return methods; + } + + /** + * Fetches the fields on a class that are annotated with Refernece. The first time it + * is called for a particular class it will introspect the class and cache the results. + * All non-overridden fields are examined, including protected and private fields. + * If a field is not public an attempt it made to make it accessible - if it fails + * it is removed from the collection and an error is logged. + * + * @param clazz the class on which to look for Reference annotated fields + * @return the collection of methods with the annotation + */ + protected static Collection getFields(Class clazz) { + Collection fields = fieldMap.get(clazz); + if (fields == null) { + fields = ReflectUtil.getFields(clazz); + Iterator iterator = fields.iterator(); + + while (iterator.hasNext()) { + Field field = iterator.next(); + if (!field.isAnnotationPresent(Reference.class)) { + iterator.remove(); + } + else if (!field.isAccessible()) { + // If the field isn't public, try to make it accessible + try { + field.setAccessible(true); + } + catch (SecurityException se) { + throw new StripesRuntimeException( + "Field " + clazz.getName() + "." + field.getName() + "is marked " + + "with @Reference and is not public. An attempt to call " + + "setAccessible(true) resulted in a SecurityException. Please " + + "either make the field public, annotate a public setter instead " + + "or modify your JVM security policy to allow Stripes to " + + "setAccessible(true).", se); + } + } + } + + fieldMap.put(clazz, fields); + } + + return fields; + } + + /** + * Looks up an SCA Reference from a ComponentContext. First looks for a bean + * with name specified. If no such bean exists, looks for a bean by type. If there is + * only one bean of the appropriate type, it is returned. If zero or more than one bean + * of the correct type exists, an exception is thrown. + * + * @param ctx the SCA ComponentContext + * @param name the name of the reference to look for + * @param type the type of bean to look for + * @param allowFindByType true to indicate that finding a bean by type is acceptable + * if find by name fails. + * @exception RuntimeException various subclasses of RuntimeException are thrown if it + * is not possible to find a unique matching bean in the ComponentContext given + * the constraints supplied. + */ + protected static Object findReference(ServletContext ctx, + String name, + Class type, + boolean allowFindByType) { + // First try to lookup using the name provided + Object bean = ContextHelper.getReference(name, type, ctx); + if (bean == null) { + throw new StripesRuntimeException("no reference defined:" + name); + } + + log.debug("Found sca reference with name [", name, "] and type [", + bean.getClass().getName(), "]"); + return bean; + +// TODO: Support get by type (sca autowire?) + +// // If we got here then we didn't find a bean yet, try by type +// String[] beanNames = ctx.getBeanNamesForType(type); +// if (beanNames.length == 0) { +// throw new StripesRuntimeException( +// "Unable to find SpringBean with name [" + name + "] or type [" + +// type.getName() + "] in the Spring application context."); +// } +// else if (beanNames.length > 1) { +// throw new StripesRuntimeException( +// "Unable to find SpringBean with name [" + name + "] or unique bean with type [" + +// type.getName() + "] in the Spring application context. Found " + beanNames.length + +// "beans of matching type."); +// } +// else { +// log.warn("Found unique SpringBean with type [" + type.getName() + "]. Matching on ", +// "type is a little risky so watch out!"); +// return ctx.getBean(beanNames[0], type); +// } + } + + /** + * A slightly unusual, and somewhat "loose" conversion of a method name to a property + * name. Assumes that the name is in fact a mutator for a property and will do the + * usual {@code setFoo} to {@code foo} conversion if the method follows the normal + * syntax, otherwise will just return the method name. + * + * @param m the method to determine the property name of + * @return a String property name + */ + protected static String methodToPropertyName(Method m) { + String name = m.getName(); + if (name.startsWith("set") && name.length() > 3) { + String ret = name.substring(3,4).toLowerCase(); + if (name.length() > 4) ret += name.substring(4); + return ret; + } + else { + return name; + } + } +} diff --git a/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptor.java b/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptor.java new file mode 100644 index 0000000000..1381266c9e --- /dev/null +++ b/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptor.java @@ -0,0 +1,73 @@ +/* + * 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.tuscany.sca.stripes; + +import net.sourceforge.stripes.action.Resolution; +import net.sourceforge.stripes.controller.ExecutionContext; +import net.sourceforge.stripes.controller.Interceptor; +import net.sourceforge.stripes.controller.Intercepts; +import net.sourceforge.stripes.controller.LifecycleStage; +import net.sourceforge.stripes.util.Log; + + +/** + *

An {@link Interceptor} that uses the implementation.web ComponentContext to inject reference + * proxies into newly created ActionBeans immediately following ActionBeanResolution. For more + * information on how the injection is performed see {@link TuscanyHelper#injectBeans(Object, + * net.sourceforge.stripes.action.ActionBeanContext)}.

+ * + *

To configure the TuscanyInterceptor for use you will need to add the following to your + * web.xml (assuming no other interceptors are yet configured):

+ * + *
+ * <init-param>
+ *     <param-name>Interceptor.Classes</param-name>
+ *     <param-value>
+ *         org.apache.tuscany.sca.stripes.TuscanyInterceptor,
+ *         net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor
+ *     </param-value>
+ * </init-param>
+ * 
+ * + *

If one or more interceptors are already configured in your web.xml simply separate the + * fully qualified names of the interceptors with commas (additional whitespace is ok).

+ * + * Created for Tuscany from the Stripes SpringInterceptor written by Tim Fennell + */ +@Intercepts(LifecycleStage.ActionBeanResolution) +public class TuscanyInterceptor implements Interceptor { + private static final Log log = Log.getInstance(TuscanyInterceptor.class); + + /** + * Allows ActionBean resolution to proceed and then once the ActionBean has been + * located invokes the {@link TuscanyHelper} to perform SCA reference injection. + * + * @param context the current execution context + * @return the Resolution produced by calling context.proceed() + * @throws Exception if the Tuscany injection process produced unrecoverable errors + */ + public Resolution intercept(ExecutionContext context) throws Exception { + Resolution resolution = context.proceed(); + log.debug("Running Tuscany dependency injection for instance of ", + context.getActionBean().getClass().getSimpleName()); + TuscanyHelper.injectBeans(context.getActionBean(), context.getActionBeanContext()); + return resolution; + } +} diff --git a/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptorSupport.java b/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptorSupport.java new file mode 100644 index 0000000000..4c73f28a08 --- /dev/null +++ b/sandbox/ant/sca/branches/tb3/modules/stripes/src/main/java/org/apache/tuscany/sca/stripes/TuscanyInterceptorSupport.java @@ -0,0 +1,57 @@ +/* + * 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.tuscany.sca.stripes; + +import net.sourceforge.stripes.config.ConfigurableComponent; +import net.sourceforge.stripes.config.Configuration; +import net.sourceforge.stripes.controller.Interceptor; + +import javax.servlet.ServletContext; + +/** + *

Base class for developing Interceptors with dependencies on SCA component references. Not + * to be confused with {@link TuscanyInterceptor} which injects SCA reference proxies into + * ActionBeans. For example, you may wish to subclass this class in order to write an + * interceptor with access to Tuscany ???.

+ * + * TODO: does Tuscany really need this? + * + *

Since Interceptors are long-lived objects that are instantiated at application startup + * time, and not per-request, the Tuscany wiring takes place in the init() method and happens + * only once when the interceptor is first created and initialized.

+ * + * Created for Tuscany from the Stripes SpringInterceptorSupport written by Tim Fennell + */ +public abstract class TuscanyInterceptorSupport implements Interceptor, ConfigurableComponent { + + /** + * Fetches the ServletContext and invokes TuscanyHelper.injectBeans() to auto-wire any + * Tuscany dependencies prior to being placed into service. + * + * @param configuration the Stripes Configuration + * @throws Exception if there are problems with the Tuscany configuration/wiring + */ + public void init(Configuration configuration) throws Exception { + ServletContext ctx = configuration.getBootstrapPropertyResolver() + .getFilterConfig().getServletContext(); + + TuscanyHelper.injectBeans(this, ctx); + } +} -- cgit v1.2.3