View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.discovery.resource.names;
18  
19  import java.util.Hashtable;
20  
21  import org.apache.commons.discovery.ResourceNameDiscover;
22  import org.apache.commons.discovery.ResourceNameIterator;
23  import org.apache.commons.discovery.log.DiscoveryLogFactory;
24  import org.apache.commons.logging.Log;
25  
26  
27  /***
28   * Recover resource name from Managed Properties,
29   * using OLD property names.
30   * 
31   * This class maintains a mapping between old names and
32   * (new) the class names they represent.  The discovery
33   * mechanism uses the class names as property names.
34   * 
35   * @see org.apache.commons.discovery.tools.ManagedProperties
36   * 
37   * @author Richard A. Sitze
38   */
39  public class DiscoverMappedNames
40      extends ResourceNameDiscoverImpl
41      implements ResourceNameDiscover
42  {
43      private static Log log = DiscoveryLogFactory.newLog(DiscoverMappedNames.class);
44      public static void setLog(Log _log) {
45          log = _log;
46      }
47      
48      private Hashtable mapping = new Hashtable();  // String name ==> String[] newNames
49      
50      /*** Construct a new resource discoverer
51       */
52      public DiscoverMappedNames() {
53      }
54      
55      public void map(String fromName, String toName) {
56          mapping.put(fromName, toName);
57      }
58      
59      public void map(String fromName, String [] toNames) {
60          mapping.put(fromName, toNames);
61      }
62  
63      /***
64       * @return Enumeration of ResourceInfo
65       */
66      public ResourceNameIterator findResourceNames(final String resourceName) {
67          if (log.isDebugEnabled()) {
68              log.debug("find: resourceName='" + resourceName + "', mapping to constants");
69          }
70          
71          final Object obj = mapping.get(resourceName);
72  
73          final String[] names;
74          if (obj instanceof String) {
75              names = new String[] { (String)obj };
76          } else if (obj instanceof String[]) {
77              names = (String[])obj;
78          } else {
79              names = null;
80          }
81          
82          return new ResourceNameIterator() {
83  
84              private int idx = 0;
85              
86              public boolean hasNext() {
87                  if (names != null) {
88                      while (idx < names.length  &&  names[idx] == null) {
89                          idx++;
90                      }
91                      return idx < names.length;
92                  }
93                  return false;
94              }
95              
96              public String nextResourceName() {
97                  return hasNext() ? names[idx++] : null;
98              }
99          };
100     }
101 }