1 | /** |
2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
3 | */ |
4 | package org.sourceforge.xradar.config; |
5 | |
6 | import java.io.File; |
7 | import java.io.FileInputStream; |
8 | import java.io.FileNotFoundException; |
9 | import java.io.IOException; |
10 | import java.io.InputStream; |
11 | import java.util.Properties; |
12 | |
13 | |
14 | |
15 | |
16 | import java.util.logging.Logger; |
17 | |
18 | import org.sourceforge.xradar.XRadarException; |
19 | import org.sourceforge.xradar.util.StreamUtils; |
20 | |
21 | /** |
22 | * <p>Loads all the necessary data (such as Version, Release date...) concerning the Release</p> |
23 | * |
24 | * @author Romain PELISSE, belaran@gmail.com |
25 | * |
26 | */ |
27 | public class ReleaseDataLoader { |
28 | |
29 | private static final Logger logger = Logger.getLogger(ReleaseDataLoader.class.getSimpleName()); |
30 | private String filename; |
31 | |
32 | public ReleaseDataLoader() { |
33 | super(); |
34 | } |
35 | |
36 | /** |
37 | * @return the filename |
38 | */ |
39 | public String getFilename() { |
40 | return filename; |
41 | } |
42 | |
43 | /** |
44 | * @param filename |
45 | * the filename to set |
46 | */ |
47 | public void setFilename(final String filename) { |
48 | this.filename = filename; |
49 | } |
50 | |
51 | public ReleaseDataLoader(final String filename) { |
52 | this.setFilename(filename); |
53 | } |
54 | |
55 | public ReleaseData load() { |
56 | // PropertiesAccessor propertiesAccessor = new PropertiesAccessor(this.filename); |
57 | // create and load default properties |
58 | final ReleaseData release = new ReleaseData(); |
59 | try { |
60 | final Properties propertiesAccessor = loadProperties(filename); |
61 | release.setSystem(propertiesAccessor.getProperty("version.label")); |
62 | release.setDate(propertiesAccessor.getProperty("date.label")); |
63 | release.setVersion(propertiesAccessor.getProperty("days.since.last.release")); |
64 | release.setSequencdeId(Integer.valueOf(propertiesAccessor.getProperty("sequence.id"))); |
65 | } catch (final XRadarException e) { |
66 | // TODO Don't know if xradar should fail or use default values |
67 | logger.fine("Can't load release properties file" + e.getMessage()); |
68 | } |
69 | return release; |
70 | } |
71 | |
72 | private Properties loadProperties(final String filename) throws XRadarException { |
73 | notNull(filename, "Missing release properties file"); |
74 | final Properties propertiesAccessor = new Properties(); |
75 | final File localFile = new File(filename); |
76 | InputStream in; |
77 | // Test if file exists locally |
78 | if (localFile.exists() && localFile.isFile()) { |
79 | try { |
80 | in = new FileInputStream(this.filename); |
81 | } catch (final FileNotFoundException e) { |
82 | throw new XRadarException("Can't find release properties file",e); |
83 | } |
84 | } else { |
85 | // Try to load from classpath |
86 | in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename); |
87 | if (in == null) { |
88 | throw new XRadarException("Can't find release properties file"); |
89 | } |
90 | } |
91 | try { |
92 | propertiesAccessor.load(in); |
93 | } catch (IOException e) { |
94 | throw new XRadarException("Can't find load data from properties file",e); |
95 | } finally { |
96 | StreamUtils.closeStream(in); |
97 | } |
98 | return propertiesAccessor; |
99 | } |
100 | |
101 | public ReleaseData load(final String filename) { |
102 | // Assign local variable to field before calling load() |
103 | this.filename = filename; |
104 | return this.load(); |
105 | } |
106 | |
107 | //TODO: Move to a static lib ? |
108 | public static void notNull(Object object, String message) { |
109 | if (object == null) { |
110 | throw new IllegalArgumentException(message); |
111 | } |
112 | } |
113 | } |
114 | |
115 | |
116 | |
117 | |