View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.web.breadcrumb;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  /***
25   * @author jmccrindle
26   */
27  public class MapCrumbFactory extends CrumbFactoryWrapper implements CrumbFactory {
28  
29      private Map factories = new HashMap();
30      
31      /***
32       * 
33       */
34      public MapCrumbFactory() {
35          super();
36      }
37  
38      /***
39       * @param crumbFactory
40       */
41      public MapCrumbFactory(CrumbFactory crumbFactory) {
42          super(crumbFactory);
43      }
44  
45      /* (non-Javadoc)
46       * @see org.jdiagnose.library.web.breadcrumb.CrumbFactory#getCrumb(javax.servlet.http.HttpServletRequest, java.lang.String)
47       */
48      public Crumb getCrumb(HttpServletRequest request, String url) {
49          CrumbFactory factory = getCrumbFactory(url);
50          if(factory != null ){
51              return factory.getCrumb(request, url);
52          }
53          CrumbFactory delegate = getCrumbFactory();
54          if(delegate != null) {
55              return delegate.getCrumb(request, url);
56          }
57          return null;
58      }
59      
60      protected CrumbFactory getCrumbFactory(String url) {
61          for (Iterator entryIterator = factories.entrySet().iterator(); entryIterator.hasNext();) {
62              Map.Entry entry = (Map.Entry) entryIterator.next();
63              String key = (String) entry.getKey();
64              if(url.endsWith(key)) {
65                  return (CrumbFactory) entry.getValue();
66              }
67          }
68          return null;
69      }
70      
71      public void setFactories(Map factories) {
72          this.factories = factories;
73      }
74  }