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 org.apache.commons.discovery.ResourceNameDiscover;
20  import org.apache.commons.discovery.ResourceNameIterator;
21  import org.apache.commons.discovery.tools.ManagedProperties;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * Recover resource name from Managed Properties.
27   * @see org.apache.commons.discovery.tools.ManagedProperties
28   */
29  public class DiscoverNamesInManagedProperties extends ResourceNameDiscoverImpl implements ResourceNameDiscover {
30  
31      private static Log log = LogFactory.getLog(DiscoverNamesInManagedProperties.class);
32  
33      /**
34       * Sets the {@code Log} for this class.
35       *
36       * @param _log This class {@code Log}
37       * @deprecated This method is not thread-safe
38       */
39      @Deprecated
40      public static void setLog(Log _log) {
41          log = _log;
42      }
43  
44      private final String _prefix;
45  
46      private final String _suffix;
47  
48      /**
49       * Construct a new resource discoverer.
50       */
51      public DiscoverNamesInManagedProperties() {
52          this(null, null);
53      }
54  
55      /**
56       * Construct a new resource discoverer.
57       *
58       * @param prefix The resource name prefix
59       * @param suffix The resource name suffix
60       */
61      public DiscoverNamesInManagedProperties(String prefix, String suffix) {
62          _prefix = prefix;
63          _suffix = suffix;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public ResourceNameIterator findResourceNames(final String resourceName) {
71          String name;
72          if (_prefix != null && _prefix.length() > 0) {
73              name = _prefix + resourceName;
74          } else {
75              name = resourceName;
76          }
77  
78          if (_suffix != null && _suffix.length() > 0) {
79              name = name + _suffix;
80          }
81  
82          if (log.isDebugEnabled()) {
83              if (_prefix != null  &&  _suffix != null) {
84                  log.debug("find: resourceName='" + resourceName + "' as '" + name + "'");
85              } else {
86                  log.debug("find: resourceName = '" + name + "'");
87              }
88          }
89  
90          final String newResourcName = name;
91  
92          return new ResourceNameIterator() {
93  
94              private String resource = ManagedProperties.getProperty(newResourcName);
95  
96              public boolean hasNext() {
97                  return resource != null;
98              }
99  
100             public String nextResourceName() {
101                 String element = resource;
102                 resource = null;
103                 return element;
104             }
105         };
106     }
107 
108 }