1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
46
47
48
49
50
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
61
62
63
64
65
66
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
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
100 throw new MojoFailureException("NAR: Could not load custom properties file: '" + customPropertyLocation + "'.");
101 }
102 } catch (final IOException e) {
103
104 } finally {
105 try {
106 if (fis != null) {
107 fis.close();
108 }
109 } catch (final IOException e) {
110
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 }