View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.spi;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.ServiceLoader;
24  import javax.faces.FactoryFinder;
25  import javax.faces.context.ExternalContext;
26  
27  /**
28   * This class provides an interface to override SPI handling done by
29   * MyFaces.
30   * 
31   * This is useful on environments like in OSGi, because it allows to
32   * put custom code to find SPI interfaces under META-INF/services/
33   * 
34   * @author Leonardo Uribe
35   * @since 2.0.3
36   *
37   */
38  public abstract class ServiceProviderFinder
39  {
40      public static final String[] KNOWN_SERVICES = 
41      {
42          FactoryFinder.APPLICATION_FACTORY,
43          FactoryFinder.CLIENT_WINDOW_FACTORY,
44          FactoryFinder.EXCEPTION_HANDLER_FACTORY,
45          FactoryFinder.EXTERNAL_CONTEXT_FACTORY,
46          FactoryFinder.FACELET_CACHE_FACTORY,
47          FactoryFinder.FACES_CONTEXT_FACTORY,
48          FactoryFinder.FLASH_FACTORY,
49          FactoryFinder.FLOW_HANDLER_FACTORY,
50          FactoryFinder.LIFECYCLE_FACTORY,
51          FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY,
52          FactoryFinder.RENDER_KIT_FACTORY,
53          FactoryFinder.TAG_HANDLER_DELEGATE_FACTORY,
54          FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY,
55          FactoryFinder.VISIT_CONTEXT_FACTORY,
56          FactoryFinder.SEARCH_EXPRESSION_CONTEXT_FACTORY,
57          "org.apache.myfaces.spi.AnnotationProvider",
58          "org.apache.myfaces.spi.AnnotationProviderFactory",
59          "org.apache.myfaces.spi.FaceletConfigResourceProvider",
60          "org.apache.myfaces.spi.FaceletConfigResourceProviderFactory",
61          "org.apache.myfaces.spi.FacesConfigResourceProvider",
62          "org.apache.myfaces.spi.FacesConfigResourceProviderFactory",
63          "org.apache.myfaces.spi.FacesConfigurationMerger",
64          "org.apache.myfaces.spi.FacesConfigurationMergerFactory",
65          "org.apache.myfaces.spi.FacesConfigurationProvider",
66          "org.apache.myfaces.spi.FacesConfigurationProviderFactory",
67          "org.apache.myfaces.spi.FacesFlowProvider",
68          "org.apache.myfaces.spi.FacesFlowProviderFactory",
69          "org.apache.myfaces.spi.FactoryFinderProvider",
70          "org.apache.myfaces.spi.FactoryFinderProviderFactory",
71          "org.apache.myfaces.spi.InjectionProvider",
72          "org.apache.myfaces.spi.InjectionProviderFactory",
73          "org.apache.myfaces.spi.ResourceLibraryContractsProvider",
74          "org.apache.myfaces.spi.ResourceLibraryContractsProviderFactory",
75          "org.apache.myfaces.spi.ViewScopeProvider",
76          "org.apache.myfaces.spi.ViewScopeProviderFactory",
77          "org.apache.myfaces.spi.WebConfigProvider",
78          "org.apache.myfaces.spi.WebConfigProviderFactory",
79          "org.apache.myfaces.config.annotation.LifecycleProvider",
80          "org.apache.myfaces.config.annotation.LifecycleProviderFactory",
81      };
82      
83      /**
84       * Gets the list of classes bound to the spiClass key, looking
85       * for entries under META-INF/services/[spiClass]
86       * 
87       * @param spiClass
88       * @return
89       */
90      public abstract List<String> getServiceProviderList(String spiClass);
91      
92      public <S> ServiceLoader<S> load(Class<S> spiClass)
93      {
94          return ServiceLoader.load(spiClass);
95      }
96      
97      /**
98       * If ServiceProviderFinderFactory knows beforehand or has stored somewhere the
99       * known locations of the SPI interfaces, this method helps to set this config
100      * information so the implementation of this interface can use it. The idea is
101      * MyFaces initialization algorithm will call getKnownServiceProviderMapInfo
102      * method and if the value is not null it will call this method to pass the
103      * map back to the ServiceProviderFinder, so it can take it.
104      * 
105      * @param map 
106      */
107     public void initKnownServiceProviderMapInfo(ExternalContext ectx, Map<String, List<String>> map)
108     {
109     }
110 
111     public Map<String, List<String>> calculateKnownServiceProviderMapInfo(ExternalContext ectx, 
112         String[] knownServices)
113     {
114         //Map<String, List<String>> map = new HashMap<String, List<String>>();
115         //for (String service : knownServices)
116         //{
117         //    map.put(service, this.getServiceProviderList(service));
118         //}
119         //return map;
120         return null;
121     }
122 }