View Javadoc

1   /*
2    * $Id: ActionMapperFactory.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher.mapper;
19  
20  import java.util.HashMap;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.struts2.StrutsConstants;
25  import org.apache.struts2.StrutsException;
26  import org.apache.struts2.config.Settings;
27  
28  import com.opensymphony.xwork2.ObjectFactory;
29  
30  /***
31   * <!-- START SNIPPET: javadoc -->
32   *
33   * Factory that creates {@link ActionMapper}s. This factory looks up the class name of the {@link ActionMapper} from
34   * Struts's configuration using the key <b>struts.mapper.class</b>.
35   *
36   * <!-- END SNIPPET: javadoc -->
37   *
38   */
39  public class ActionMapperFactory {
40      protected static final Log LOG = LogFactory.getLog(ActionMapperFactory.class);
41  
42      private static final HashMap<String,ActionMapper> classMap = new HashMap<String,ActionMapper>();
43  
44      /***
45       * Gets an instance of the ActionMapper
46       * 
47       * @return The action mapper
48       */
49      public static ActionMapper getMapper() {
50          synchronized (classMap) {
51              String clazz = (String) Settings.get(StrutsConstants.STRUTS_MAPPER_CLASS);
52              try {
53                  ActionMapper mapper = (ActionMapper) classMap.get(clazz);
54                  if (mapper == null) {
55                      mapper = (ActionMapper) ObjectFactory.getObjectFactory().buildBean(clazz, null);
56                      classMap.put(clazz, mapper);
57                  }
58  
59                  return mapper;
60              } catch (Exception e) {
61                  String msg = "Could not create ActionMapper: Struts will *not* work!";
62                  throw new StrutsException(msg, e);
63              }
64          }
65      }
66  }