View Javadoc

1   /*
2    * Copyright 2004-2005 Dan Washusen
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  
17  package org.reactive.beans.factory.config;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.springframework.util.ClassUtils;
22  
23  import java.util.List;
24  import java.util.Set;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  /***
29   * The default implementation of a placeholder.
30   *
31   * @author Dan Washusen
32   * @version $Id: DefaultPlaceholder.java,v 1.3 2005/06/16 07:02:06 dan_washusen Exp $
33   * @since 16.12.2004
34   */
35  public class DefaultPlaceholder
36          extends AbstractPlaceholder {
37      private static Log log = LogFactory.getLog(DefaultPlaceholder.class);
38  
39      /*** The default string used to seperate the type from the key. */
40      public static final String DEFAULT_TYPE_SEPERATOR = "|";
41  
42      /*** a string defined to signify the String class. */
43      public static final String TYPE_STRING = "String";
44      /*** a string defined to signify the String class. */
45      public static final String TYPE_ARRAY = "String[]";
46      /*** a string defined to signify the List class. */
47      public static final String TYPE_LIST = "List";
48      /*** a string defined to signify the Set class. */
49      public static final String TYPE_SET = "Set";
50      /*** a string defined to signify the Map class. */
51      public static final String TYPE_MAP = "Map";
52      /*** a string defined to signify the Properties class. */
53      public static final String TYPE_PROPS = "Props";
54  
55      protected String typeSeperator;
56  
57      public DefaultPlaceholder(String value) {
58          super(value);
59      }
60  
61      /***
62       * Returns the value used to seperate type from the key.
63       * @return the value used to seperate type from the key
64       */
65      public String getTypeSeperator() {
66          if (typeSeperator == null)
67              return DEFAULT_TYPE_SEPERATOR;
68  
69          return typeSeperator;
70      }
71  
72      /***
73       * Sets the value used to seperate type from the key.
74       * @param typeSeperator the value used to seperate type from the key
75       */
76      public void setTypeSeperator(String typeSeperator) {
77          this.typeSeperator = typeSeperator;
78      }
79  
80      protected Class determineType(String value) {
81          int typeSeperatorIndex = value.indexOf(getTypeSeperator());
82  
83          // if there is no type seperator in the supplied value then presume it's resolves to a string type
84          if (typeSeperatorIndex == -1)
85              return String.class;
86  
87          String typeName = value.substring(0, typeSeperatorIndex);
88  
89          // try and create a class from the typeName watching out for the various class loader issues
90          Class clazz = null;
91          try {
92              clazz = ClassUtils.forName(typeName);
93          }
94          catch (ClassNotFoundException e) {
95              log.error("Could not determine type for placeholder '" + value + "'; falling back to " + String.class);
96              clazz = String.class;
97          }
98  
99          return clazz;
100     }
101 
102     protected String determineKey(String value) {
103         int typeSeperatorIndex = value.indexOf(getTypeSeperator());
104 
105         // if there is no type seperator in the supplied value then presume the entire value is the key
106         if (typeSeperatorIndex == -1)
107             return value;
108         else {
109             // get the index of the type seperator plus one, because we don't actually want the seperator
110             return value.substring(typeSeperatorIndex + 1, value.length());
111         }
112     }
113 }