View Javadoc

1   /*
2    * Copyright 2012-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.maven.bugzilla;
17  
18  import java.io.BufferedInputStream;
19  import java.io.InputStream;
20  import java.util.List;
21  
22  import org.apache.commons.io.FilenameUtils;
23  import org.apache.commons.io.IOUtils;
24  import org.apache.maven.plugin.MojoExecutionException;
25  
26  import de.smartics.maven.issue.config.Component;
27  import de.smartics.maven.issue.config.Configuration;
28  import de.smartics.maven.issue.config.ConfigurationLoader;
29  
30  /**
31   * Loads default descriptions for components.
32   */
33  public final class ComponentDescriptionLoader
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    /**
40     * If no configuration is specified or the specified configuration does not
41     * exist, the system configuration is used as default. Without the
42     * <code>.xml</code> file name extension.
43     * <p>
44     * The value of this constant is {@value}.
45     * </p>
46     */
47    private static final String SYSTEM_CONFIG_PREFIX =
48        "bugzilla-configuration-system";
49  
50    // --- members --------------------------------------------------------------
51  
52    /**
53     * The name of the configuration file to load from the class path.
54     */
55    private final String configurationName;
56  
57    /**
58     * The name of the locale to use if standard component descriptions are used.
59     */
60    private final String locale;
61  
62    // ****************************** Initializer *******************************
63  
64    // ****************************** Constructors ******************************
65  
66    /**
67     * Default constructor.
68     *
69     * @param configurationName the name of the configuration file to load from
70     *          the class path.
71     * @param locale the name of the locale to use if standard component
72     *          descriptions are used.
73     */
74    public ComponentDescriptionLoader(final String configurationName,
75        final String locale)
76    {
77      this.configurationName = configurationName;
78      this.locale = locale;
79    }
80  
81    // ****************************** Inner Classes *****************************
82  
83    // ********************************* Methods ********************************
84  
85    // --- init -----------------------------------------------------------------
86  
87    // --- get&set --------------------------------------------------------------
88  
89    // --- business -------------------------------------------------------------
90  
91    List<Component> loadDefaultComponents() throws MojoExecutionException
92    {
93      final InputStream input = openDefaultComponentsStream();
94  
95      try
96      {
97        final ConfigurationLoader loader = new ConfigurationLoader();
98        final Configuration configuration = loader.load(input);
99        return configuration.getDefaultComponents();
100     }
101     finally
102     {
103       IOUtils.closeQuietly(input);
104     }
105   }
106 
107   private InputStream openDefaultComponentsStream()
108     throws MojoExecutionException
109   {
110     final ClassLoader classLoader =
111         Thread.currentThread().getContextClassLoader();
112     InputStream in = openStream(classLoader);
113 
114     if (in == null)
115     {
116       in = openStream(classLoader, SYSTEM_CONFIG_PREFIX);
117     }
118 
119     if (in != null)
120     {
121       return new BufferedInputStream(in);
122     }
123 
124     throw new MojoExecutionException(
125         "Cannot find configuration for default components. Neither resource '"
126             + configurationName + "' nor the system default resource '"
127             + SYSTEM_CONFIG_PREFIX + ".xml' has been found on the class path.");
128   }
129 
130   private InputStream openStream(final ClassLoader classLoader)
131   {
132     final String stem = FilenameUtils.getBaseName(configurationName);
133     return openStream(classLoader, stem);
134   }
135 
136   private InputStream openStream(final ClassLoader classLoader,
137       final String stem)
138   {
139     final String localizedName = stem + '-' + locale + ".xml";
140     InputStream in = classLoader.getResourceAsStream(localizedName);
141     if (in == null)
142     {
143       in = classLoader.getResourceAsStream(stem + "-en.xml");
144     }
145     return in;
146   }
147 
148   // --- object basics --------------------------------------------------------
149 
150 }