View Javadoc

    1   /*
    2    * Copyright 2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * $Header:$
    17   */
    18  package org.apache.struts2.s1;
    19
    20  import java.util.HashMap;
    21  import java.util.Iterator;
    22  import java.util.Map;
    23
    24  import org.apache.struts.config.ActionConfig;
    25  import org.apache.struts.config.ControllerConfig;
    26  import org.apache.struts.config.ExceptionConfig;
    27  import org.apache.struts.config.FormBeanConfig;
    28  import org.apache.struts.config.ForwardConfig;
    29  import org.apache.struts.config.MessageResourcesConfig;
    30  import org.apache.struts.config.ModuleConfig;
    31  import org.apache.struts.config.PlugInConfig;
    32
    33  import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
    34  import com.opensymphony.xwork2.config.entities.PackageConfig;
    35  import com.opensymphony.xwork2.config.entities.ResultConfig;
    36
    37  /***
    38   * Wrapper for a Struts 1.x ModuleConfig based on an XWork PackageConfig.  Using a wrapper object
    39   * allows us to be explicit about what is and isn't implemented.
    40   */
    41  class WrapperModuleConfig implements ModuleConfig {
    42
    43      private Struts1Factory strutsFactory;
    44      private PackageConfig delegate;
    45      private Map _actionMappings;
    46      private Map _exceptionConfigs;
    47      private Map _actionForwards;
    48
    49      public WrapperModuleConfig(Struts1Factory factory, PackageConfig config) {
    50          delegate = config;
    51          this.strutsFactory = factory;
    52      }
    53
    54      /***
    55       * Add Struts ActionMappings (from XWork ExceptionConfigs).
    56       */
    57      private void initActionMappings() {
    58
    59          if (_actionMappings == null) {
    60              _actionMappings = new HashMap();
    61              for (Iterator i = delegate.getActionConfigs().entrySet().iterator(); i.hasNext();) {
    62                  Map.Entry entry = (Map.Entry) i.next();
    63                  String actionPath = '/' + (String) entry.getKey();
    64                  com.opensymphony.xwork2.config.entities.ActionConfig actionConfig =
    65                          (com.opensymphony.xwork2.config.entities.ActionConfig) entry.getValue();
    66                  _actionMappings.put(actionPath, strutsFactory.createActionMapping(actionConfig, actionPath, this));
    67              }
    68          }
    69      }
    70
    71      /***
    72       * Add Struts ExceptionConfigs (from XWork ExceptionMappingConfigs).
    73       */
    74      private void initExceptionConfigs() {
    75          if (_exceptionConfigs == null) {
    76              _exceptionConfigs = new HashMap();
    77              for (Iterator i = delegate.getGlobalExceptionMappingConfigs().iterator(); i.hasNext();) {
    78                  ExceptionMappingConfig config = (ExceptionMappingConfig) i.next();
    79                  _exceptionConfigs.put(config.getExceptionClassName(), strutsFactory.createExceptionConfig(config));
    80              }
    81          }
    82      }
    83
    84      /***
    85       * Add Struts ActionForwards (from XWork ResultConfigs).
    86       */
    87      private void initActionForwards() {
    88          if (_actionForwards == null) {
    89              _actionForwards = new HashMap();
    90              for (Iterator i = delegate.getGlobalResultConfigs().entrySet().iterator(); i.hasNext();) {
    91                  Map.Entry entry = (Map.Entry) i.next();
    92                  String name = (String) entry.getKey();
    93                  ResultConfig config = (ResultConfig) entry.getValue();
    94                  _actionForwards.put(name, strutsFactory.createActionForward(config));
    95              }
    96          }
    97      }
    98
    99      public String getPrefix() {
    100         return delegate.getNamespace();
    101     }
    102
    103     public void setPrefix(String prefix) {
    104         throw new UnsupportedOperationException("Not implemented - immutable");
    105     }
    106
    107     public boolean getConfigured() {
    108         return true;
    109     }
    110
    111     public ControllerConfig getControllerConfig() {
    112         throw new UnsupportedOperationException("NYI");
    113     }
    114
    115     public void setControllerConfig(ControllerConfig cc) {
    116         throw new UnsupportedOperationException("Not implemented - immutable");
    117     }
    118
    119     public String getActionFormBeanClass() {
    120         throw new UnsupportedOperationException("NYI");
    121     }
    122
    123     public void setActionFormBeanClass(String actionFormBeanClass) {
    124         throw new UnsupportedOperationException("Not implemented - immutable");
    125     }
    126
    127     public String getActionMappingClass() {
    128         throw new UnsupportedOperationException("NYI");
    129     }
    130
    131     public void setActionMappingClass(String actionMappingClass) {
    132         throw new UnsupportedOperationException("Not implemented - immutable");
    133     }
    134
    135     public void addActionConfig(ActionConfig config) {
    136         throw new UnsupportedOperationException("Not implemented - immutable");
    137     }
    138
    139     public void addExceptionConfig(ExceptionConfig config) {
    140         throw new UnsupportedOperationException("Not implemented - immutable");
    141     }
    142
    143     public void addFormBeanConfig(FormBeanConfig config) {
    144         throw new UnsupportedOperationException("Not implemented - immutable");
    145     }
    146
    147     public String getActionForwardClass() {
    148         throw new UnsupportedOperationException("NYI");
    149     }
    150
    151     public void setActionForwardClass(String actionForwardClass) {
    152         throw new UnsupportedOperationException("Not implemented - immutable");
    153     }
    154
    155     public void addForwardConfig(ForwardConfig config) {
    156         throw new UnsupportedOperationException("NYI");
    157     }
    158
    159     public void addMessageResourcesConfig(MessageResourcesConfig config) {
    160         throw new UnsupportedOperationException("Not implemented - immutable");
    161     }
    162
    163     public void addPlugInConfig(PlugInConfig plugInConfig) {
    164         throw new UnsupportedOperationException("Not implemented - immutable");
    165     }
    166
    167     public ActionConfig findActionConfig(String path) {
    168         initActionMappings();
    169         return (ActionConfig) _actionMappings.get(path);
    170     }
    171
    172     public ActionConfig[] findActionConfigs() {
    173         initActionMappings();
    174         return (ActionConfig[]) _actionMappings.values().toArray(new ActionConfig[_actionMappings.size()]);
    175     }
    176
    177     public ExceptionConfig findExceptionConfig(String type) {
    178         initExceptionConfigs();
    179         return (ExceptionConfig) _exceptionConfigs.get(type);
    180     }
    181
    182     public ExceptionConfig[] findExceptionConfigs() {
    183         initExceptionConfigs();
    184         return (ExceptionConfig[]) _exceptionConfigs.values().toArray(new ExceptionConfig[_exceptionConfigs.size()]);
    185     }
    186
    187     public FormBeanConfig findFormBeanConfig(String name) {
    188         throw new UnsupportedOperationException("NYI");
    189     }
    190
    191     public FormBeanConfig[] findFormBeanConfigs() {
    192         throw new UnsupportedOperationException("NYI");
    193     }
    194
    195     public ForwardConfig findForwardConfig(String name) {
    196         initActionForwards();
    197         return (ForwardConfig) _actionForwards.get(name);
    198     }
    199
    200     public ForwardConfig[] findForwardConfigs() {
    201         initActionForwards();
    202         return (ForwardConfig[]) _actionForwards.values().toArray(new ForwardConfig[_actionForwards.size()]);
    203     }
    204
    205     public MessageResourcesConfig findMessageResourcesConfig(String key) {
    206         throw new UnsupportedOperationException("NYI");
    207     }
    208
    209     public MessageResourcesConfig[] findMessageResourcesConfigs() {
    210         throw new UnsupportedOperationException("NYI");
    211     }
    212
    213     public PlugInConfig[] findPlugInConfigs() {
    214         throw new UnsupportedOperationException("NYI");
    215     }
    216
    217     public void freeze() {
    218         throw new UnsupportedOperationException("Not implemented - immutable");
    219     }
    220
    221     public void removeActionConfig(ActionConfig config) {
    222         throw new UnsupportedOperationException("Not implemented - immutable");
    223     }
    224
    225     public void removeExceptionConfig(ExceptionConfig config) {
    226         throw new UnsupportedOperationException("Not implemented - immutable");
    227     }
    228
    229     public void removeFormBeanConfig(FormBeanConfig config) {
    230         throw new UnsupportedOperationException("Not implemented - immutable");
    231     }
    232
    233     public void removeForwardConfig(ForwardConfig config) {
    234         throw new UnsupportedOperationException("Not implemented - immutable");
    235     }
    236
    237     public void removeMessageResourcesConfig(MessageResourcesConfig config) {
    238         throw new UnsupportedOperationException("Not implemented - immutable");
    239     }
    240
    241     public ExceptionConfig findException(Class arg0) {
    242         throw new UnsupportedOperationException("NYI");
    243     }
    244 }