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.HashMap;
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.discovery.tools.ManagedProperties;
25  import org.apache.commons.logging.Log;
26  
27  
28  /***
29   * Recover resource name from Managed Properties,
30   * using OLD property names.
31   * 
32   * This class maintains a mapping between old names and
33   * (new) the class names they represent.  The discovery
34   * mechanism uses the class names as property names.
35   * 
36   * @see org.apache.commons.discovery.tools.ManagedProperties
37   * 
38   * @author Richard A. Sitze
39   */
40  public class DiscoverNamesInAlternateManagedProperties
41      extends ResourceNameDiscoverImpl
42      implements ResourceNameDiscover
43  {
44      private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInAlternateManagedProperties.class);
45      public static void setLog(Log _log) {
46          log = _log;
47      }
48  
49      HashMap mapping = new HashMap();
50      
51      /*** Construct a new resource discoverer
52       */
53      public DiscoverNamesInAlternateManagedProperties() {
54      }
55      
56      /***
57       */
58      public void addClassToPropertyNameMapping(String className, String propertyName) {
59          mapping.put(className, propertyName);
60      }
61  
62      /***
63       * @return Enumeration of ResourceInfo
64       */
65      public ResourceNameIterator findResourceNames(final String resourceName) {
66          final String mappedName = (String)mapping.get(resourceName);
67  
68          if (log.isDebugEnabled()) {
69              if (mappedName == null) {
70                  log.debug("find: resourceName='" + resourceName + "', no mapping");
71              } else {
72                  log.debug("find: resourceName='" + resourceName + "', lookup property '" + mappedName + "'");
73              }
74          }
75  
76          return new ResourceNameIterator() {
77              private String resource =
78                  (mappedName == null) ? null : ManagedProperties.getProperty(mappedName);
79              
80              public boolean hasNext() {
81                  return resource != null;
82              }
83              
84              public String nextResourceName() {
85                  String element = resource;
86                  resource = null;
87                  return element;
88              }
89          };
90      }
91  }