View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Collection;
28  import java.util.Enumeration;
29  import java.util.LinkedHashSet;
30  import java.util.Properties;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.util.PropertyUtils;
37  
38  public class NarProperties {
39  
40    private final static String AOL_PROPERTIES = "aol.properties";
41    private final static String CUSTOM_AOL_PROPERTY_KEY = "nar.aolProperties";
42    private static NarProperties instance;
43  
44    /**
45     * Retrieve the NarProperties
46     * 
47     * @param project
48     *          may be null
49     * @return
50     * @throws MojoFailureException
51     */
52    public static NarProperties getInstance(final MavenProject project) throws MojoFailureException {
53      if (instance == null) {
54        instance = new NarProperties(project);
55      }
56      return instance;
57    }
58  
59    /**
60     * Programmatically inject properties (and possibly overwrite existing
61     * properties)
62     * 
63     * @param project
64     *          the current maven project
65     * @param properties
66     *          the properties from input stream
67     */
68    public static void inject(final MavenProject project, final InputStream properties) throws MojoFailureException {
69      final Properties defaults = PropertyUtils.loadProperties(properties);
70      final NarProperties nar = getInstance(project);
71      nar.properties.putAll(defaults);
72    }
73  
74    private final Properties properties;
75  
76    private NarProperties(final MavenProject project) throws MojoFailureException {
77  
78      final Properties defaults = PropertyUtils.loadProperties(NarUtil.class.getResourceAsStream(AOL_PROPERTIES));
79      if (defaults == null) {
80        throw new MojoFailureException("NAR: Could not load default properties file: '" + AOL_PROPERTIES + "'.");
81      }
82  
83      this.properties = new Properties(defaults);
84      FileInputStream fis = null;
85      String customPropertyLocation = null;
86      try {
87        if (project != null) {
88          customPropertyLocation = project.getProperties().getProperty(CUSTOM_AOL_PROPERTY_KEY);
89          if (customPropertyLocation == null) {
90            // Try and read from the system property in case it's specified there
91            customPropertyLocation = System.getProperties().getProperty(CUSTOM_AOL_PROPERTY_KEY);
92          }
93          fis = new FileInputStream(customPropertyLocation != null ? customPropertyLocation : project.getBasedir()
94              + File.separator + AOL_PROPERTIES);
95          this.properties.load(fis);
96        }
97      } catch (final FileNotFoundException e) {
98        if (customPropertyLocation != null) {
99          // We tried loading from a custom location - so throw the exception
100         throw new MojoFailureException("NAR: Could not load custom properties file: '" + customPropertyLocation + "'.");
101       }
102     } catch (final IOException e) {
103       // ignore (FIXME)
104     } finally {
105       try {
106         if (fis != null) {
107           fis.close();
108         }
109       } catch (final IOException e) {
110         // ignore
111       }
112     }
113 
114   }
115 
116   public Collection<String> getKnownAOLs() {
117     final Collection<String> result = new LinkedHashSet<>();
118     final Pattern pattern = Pattern.compile("([^.]+)\\.([^.]+)\\.([^.]+).*");
119     final Enumeration<?> e = this.properties.propertyNames();
120     while (e.hasMoreElements()) {
121       final Object key = e.nextElement();
122       if (key instanceof String) {
123         final Matcher matcher = pattern.matcher((String) key);
124         if (matcher.matches()) {
125           result.add(matcher.group(1) + "-" + matcher.group(2) + "-" + matcher.group(3));
126         }
127       }
128     }
129     return result;
130   }
131 
132   public String getProperty(final String key) {
133     return this.properties.getProperty(key);
134   }
135 }