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  import java.util.Map;
21  
22  import org.apache.commons.discovery.ResourceNameDiscover;
23  import org.apache.commons.discovery.ResourceNameIterator;
24  import org.apache.commons.discovery.tools.ManagedProperties;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
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  public class DiscoverNamesInAlternateManagedProperties
39          extends ResourceNameDiscoverImpl
40          implements ResourceNameDiscover {
41  
42      private static Log log = LogFactory.getLog(DiscoverNamesInAlternateManagedProperties.class);
43  
44      /**
45       * Sets the {@code Log} for this class.
46       *
47       * @param _log This class {@code Log}
48       * @deprecated This method is not thread-safe
49       */
50      @Deprecated
51      public static void setLog(Log _log) {
52          log = _log;
53      }
54  
55      private final Map<String, String> mapping = new HashMap<String, String>();
56  
57      /**
58       * Construct a new resource discoverer.
59       */
60      public DiscoverNamesInAlternateManagedProperties() {
61      }
62  
63      /**
64       * Add a class name/property name mapping.
65       *
66       * @param className The class name
67       * @param propertyName The property name
68       */
69      public void addClassToPropertyNameMapping(String className, String propertyName) {
70          mapping.put(className, propertyName);
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public ResourceNameIterator findResourceNames(final String resourceName) {
78          final String mappedName = mapping.get(resourceName);
79  
80          if (log.isDebugEnabled()) {
81              if (mappedName == null) {
82                  log.debug("find: resourceName='" + resourceName + "', no mapping");
83              } else {
84                  log.debug("find: resourceName='" + resourceName + "', lookup property '" + mappedName + "'");
85              }
86          }
87  
88          return new ResourceNameIterator() {
89              private String resource =
90                  (mappedName == null) ? null : ManagedProperties.getProperty(mappedName);
91  
92              public boolean hasNext() {
93                  return resource != null;
94              }
95  
96              public String nextResourceName() {
97                  String element = resource;
98                  resource = null;
99                  return element;
100             }
101         };
102     }
103 
104 }