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.log.DiscoveryLogFactory;
22  import org.apache.commons.discovery.tools.ManagedProperties;
23  import org.apache.commons.logging.Log;
24  
25  
26  /***
27   * Recover resource name from Managed Properties.
28   * @see org.apache.commons.discovery.tools.ManagedProperties
29   * 
30   * @author Richard A. Sitze
31   */
32  public class DiscoverNamesInManagedProperties
33      extends ResourceNameDiscoverImpl
34      implements ResourceNameDiscover
35  {
36      private static Log log = DiscoveryLogFactory.newLog(DiscoverNamesInManagedProperties.class);
37      public static void setLog(Log _log) {
38          log = _log;
39      }
40  
41      
42      private final String _prefix;
43      private final String _suffix;
44      
45      /*** Construct a new resource discoverer
46       */
47      public DiscoverNamesInManagedProperties() {
48          _prefix = null;
49          _suffix = null;
50      }
51      
52      /*** Construct a new resource discoverer
53       */
54      public DiscoverNamesInManagedProperties(String prefix, String suffix) {
55          _prefix = prefix;
56          _suffix = suffix;
57      }
58  
59      /***
60       * @return Enumeration of ResourceInfo
61       */
62      public ResourceNameIterator findResourceNames(final String resourceName) {
63          String name;
64          if (_prefix != null && _prefix.length() > 0) {
65              name = _prefix + resourceName;
66          } else {
67              name = resourceName;
68          }
69  
70          if (_suffix != null && _suffix.length() > 0) {
71              name = name + _suffix;
72          }
73  
74          if (log.isDebugEnabled()) {
75              if (_prefix != null  &&  _suffix != null) {
76                  log.debug("find: resourceName='" + resourceName + "' as '" + name + "'");
77              } else {
78                  log.debug("find: resourceName = '" + name + "'");
79              }
80          }
81  
82          final String newResourcName = name;
83          return new ResourceNameIterator() {
84              private String resource = ManagedProperties.getProperty(newResourcName);
85              public boolean hasNext() {
86                  return resource != null;
87              }
88              
89              public String nextResourceName() {
90                  String element = resource;
91                  resource = null;
92                  return element;
93              }
94          };
95      }
96  }