Author: musachy Date: Mon May 19 10:55:00 2008 New Revision: 657889 URL: http://svn.apache.org/viewvc?rev=657889&view=rev Log: @Action can be applied to a class (used from REST plugin) Add flags to control what is scanned (used from REST plugin)
Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/AbstractClassLoaderResolver.java struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/AbstractClassLoaderResolver.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/AbstractClassLoaderResolver.java?rev=657889&r1=657888&r2=657889&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/AbstractClassLoaderResolver.java (original) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/AbstractClassLoaderResolver.java Mon May 19 10:55:00 2008 @@ -354,10 +354,15 @@ File[] files = location.listFiles(); for (File file : files) { - if (file.isDirectory() && recursive) { - loadResourcesInDirectory(test, recursive, baseURLSpec, dirName, parent + "/" + file.getName(), file); - } else if (!file.isDirectory()) { - addIfMatching(test, baseURLSpec, parent, file.getName()); + try { + if (file.isDirectory() && recursive) { + loadResourcesInDirectory(test, recursive, baseURLSpec, dirName, parent + "/" + file.getName(), file); + } else if (!file.isDirectory()) { + addIfMatching(test, baseURLSpec, parent, file.getName()); + } + } catch (Exception ex) { + if (LOG.isErrorEnabled()) + LOG.error("Unable to scan [#0] for resources", ex, file.toString()); } } } @@ -420,7 +425,9 @@ } for (String exclusion : exclusions) { - if (exclusion.endsWith("/*") && name.startsWith(exclusion.substring(0, exclusion.length() - 2)) || + String tmpName = name.endsWith("/") ? name : name + "/"; + //adding "/" to the name, otherwise "org/apache/struts/*" will filter "org/apache/struts2" out + if (exclusion.endsWith("/*") && tmpName.startsWith(exclusion.substring(0, exclusion.length() - 1)) || name.equals(exclusion)) { return true; } Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java?rev=657889&r1=657888&r2=657889&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java (original) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/PackageBasedActionConfigBuilder.java Mon May 19 10:55:00 2008 @@ -69,8 +69,10 @@ private String[] excludePackages; private String[] packageLocators; private boolean disableActionScanning = false; + private boolean disableGlobalActionScanning = false; private String actionSuffix = "Action"; private boolean checkImplementsAction = true; + private boolean mapAllMatches = false; /** * Constructs actions based on a list of packages. @@ -112,6 +114,22 @@ } /** + * @param disableActionScanning Disable scanning for actions + */ + @Inject(value = "struts.convention.action.disableScanning", required = false) + public void setDisableActionScanning(String disableActionScanning) { + this.disableActionScanning = "true".equals(disableActionScanning); + } + + /** + * @param disableActionScanning If set to true, only the named packages will be scanned + */ + @Inject(value = "struts.convention.action.disableGlobalScanning", required = false) + public void setDisableGlobalActionScanning(String disableGlobalActionScanning) { + this.disableGlobalActionScanning = "true".equals(disableGlobalActionScanning); + } + + /** * @param actionPackages (Optional) An optional list of action packages that this should create * configuration for. */ @@ -162,6 +180,16 @@ } /** + * @param packageLocators (Optional) Map actions that match the "*${Suffix}" pattern + * even if they don't have a default method. The mapping from + * the url to the action will be delegated the action mapper. + */ + @Inject(value = "struts.convention.action.mapAllMatches", required = false) + public void setMapAllMatches(String mapAllMatches) { + this.mapAllMatches = "true".equals(mapAllMatches); + } + + /** * Builds the action configurations by loading all classes in the packages specified by the * property <b>struts.convention.action.packages</b> and then figuring out which classes implement Action * or have Action in their name. Next, if this class is in a Java package that hasn't been @@ -197,7 +225,7 @@ classes.addAll(findActionsInNamedPackages()); } - if (packageLocators != null) { + if (packageLocators != null && !disableGlobalActionScanning) { classes.addAll(findActionsUsingPackageLocators()); } @@ -311,6 +339,13 @@ createActionConfig(pkgCfg, actionClass, defaultActionName, method, action); } } + + // some actions will not have any @Action or a default method, like the rest actions + // where the action mapper is the one that finds the right method at runtime + if (map.isEmpty() && mapAllMatches) { + Action actionAnnotation = actionClass.getAnnotation(Action.class); + createActionConfig(defaultPackageConfig, actionClass, defaultActionName, null, actionAnnotation); + } } buildIndexActions(packageConfigs); @@ -325,7 +360,7 @@ /** * Determines the namespace for the action based on the action class. If there is a [EMAIL PROTECTED] Namespace} * annotation on the class (including parent classes) or on the package that the class is in, than - * it is used. Otherwise, the Java package name that the class is in is used inconjunction with + * it is used. Otherwise, the Java package name that the class is in is used in conjunction with * either the <b>struts.convention.action.packages</b> or <b>struts.convention.package.locators</b> * configuration values. These are used to determine which part of the Java package name should * be converted into the namespace for the XWork PackageConfig. @@ -585,8 +620,4 @@ } } } - - public void setDisableActionScanning(boolean disableActionScanning) { - this.disableActionScanning = disableActionScanning; - } } \ No newline at end of file Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java?rev=657889&r1=657888&r2=657889&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java (original) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/java/org/apache/struts2/convention/annotation/Action.java Mon May 19 10:55:00 2008 @@ -52,7 +52,7 @@ * </pre> * <!-- END SNIPPET: javadoc --> */ [EMAIL PROTECTED](ElementType.METHOD) [EMAIL PROTECTED]({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Action { String DEFAULT_VALUE = "DEFAULT_VALUE"; Modified: struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml?rev=657889&r1=657888&r2=657889&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml (original) +++ struts/sandbox/trunk/struts2-convention-plugin/src/main/resources/struts-plugin.xml Mon May 19 10:55:00 2008 @@ -39,6 +39,8 @@ <constant name="struts.convention.result.path" value="/WEB-INF/content/"/> <constant name="struts.convention.action.suffix" value="Action"/> + <constant name="struts.convention.action.disableScanning" value="false"/> + <constant name="struts.convention.action.mapAllMatches" value="false"/> <constant name="struts.convention.action.checkImplementsAction" value="true"/> <constant name="struts.convention.default.parent.package" value="convention-default"/> <constant name="struts.convention.action.name.lowercase" value="true"/>