View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.testdoc.report.resource;
17  
18  import java.util.Locale;
19  import java.util.ServiceLoader;
20  
21  /**
22   * Service loader to load category resource descriptors.
23   *
24   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
25   * @version $Revision:591 $
26   */
27  public final class CategoryResourceLoader
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    /**
34     * The singleton instance of the resource loader.
35     */
36    public static final CategoryResourceLoader INSTANCE =
37        new CategoryResourceLoader();
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The internal service loader.
43     */
44    private final ServiceLoader<CategoryResourceProvider> loader = ServiceLoader
45        .load(CategoryResourceProvider.class);
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    /**
52     * Default constructor.
53     */
54    private CategoryResourceLoader()
55    {
56    }
57  
58    // ****************************** Inner Classes *****************************
59  
60    // ********************************* Methods ********************************
61  
62    // --- init -----------------------------------------------------------------
63  
64    // --- get&set --------------------------------------------------------------
65  
66    // --- business -------------------------------------------------------------
67  
68    /**
69     * Returns the category resource for the given category type.
70     *
71     * @param categoryId the type of the category the resource is requested.
72     * @return a category resource, never <code>null</code>.
73     */
74    public CategoryResource getResource(final String categoryId)
75    {
76      return getResource(Locale.getDefault(), categoryId);
77    }
78  
79    /**
80     * Returns the category resource for the given category type.
81     *
82     * @param locale the locale for which the resource is requested.
83     * @param categoryId the type of the category the resource is requested.
84     * @return a category resource, never <code>null</code>.
85     */
86    public CategoryResource getResource(final Locale locale,
87        final String categoryId)
88    {
89      try
90      {
91        final Class<?> categoryType = Class.forName(categoryId);
92        return getResource(locale, categoryType);
93      }
94      catch (final ClassNotFoundException e)
95      {
96        final int lastDotIndex = categoryId.lastIndexOf('.');
97        final String label =
98            lastDotIndex != -1 ? categoryId.substring(lastDotIndex + 1)
99                : categoryId;
100       return new CategoryResource(label, null);
101     }
102   }
103 
104   /**
105    * Returns the category resource for the given category type.
106    *
107    * @param categoryType the type of the category the resource is requested.
108    * @return a category resource, never <code>null</code>.
109    */
110   public CategoryResource getResource(final Class<?> categoryType)
111   {
112     return getResource(Locale.getDefault(), categoryType);
113   }
114 
115   /**
116    * Returns the category resource for the given category type.
117    *
118    * @param locale the locale for which the resource is requested.
119    * @param categoryType the type of the category the resource is requested.
120    * @return a category resource, never <code>null</code>.
121    */
122   public CategoryResource getResource(final Locale locale,
123       final Class<?> categoryType)
124   {
125     for (final CategoryResourceProvider resources : loader)
126     {
127       try
128       {
129         final CategoryResource resource =
130             resources.getResource(locale, categoryType);
131         if (resource != null)
132         {
133           return resource;
134         }
135       }
136       catch (final RuntimeException e)
137       {
138         continue;
139       }
140     }
141 
142     return new CategoryResource(categoryType.getSimpleName(), null);
143   }
144 
145   // --- object basics --------------------------------------------------------
146 
147 }