1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.onami.autobind.integrations.commons.configuration; |
20 | |
|
21 | |
import java.io.File; |
22 | |
import java.lang.annotation.Annotation; |
23 | |
import java.net.MalformedURLException; |
24 | |
import java.net.URL; |
25 | |
import java.util.Map; |
26 | |
import java.util.logging.Level; |
27 | |
import java.util.logging.Logger; |
28 | |
|
29 | |
import javax.inject.Named; |
30 | |
import javax.inject.Singleton; |
31 | |
|
32 | |
import org.apache.commons.configuration.ConfigurationException; |
33 | |
import org.apache.commons.configuration.FileConfiguration; |
34 | |
import org.apache.onami.autobind.configuration.Configuration; |
35 | |
import org.apache.onami.autobind.install.BindingStage; |
36 | |
import org.apache.onami.autobind.scanner.features.BindingScannerFeature; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@Singleton |
47 | 0 | public class CommonsConfigurationFeature extends BindingScannerFeature { |
48 | 0 | private Logger _logger = Logger.getLogger(CommonsConfigurationFeature.class.getName()); |
49 | |
|
50 | |
@Override |
51 | |
public BindingStage accept(Class<Object> annotatedClass, Map<String, Annotation> annotations) { |
52 | 0 | if (annotations.containsKey(Configuration.class.getName())) { |
53 | 0 | Configuration config = (Configuration) annotations.get(Configuration.class.getName()); |
54 | 0 | if (FileConfiguration.class.isAssignableFrom(config.to())) { |
55 | 0 | return BindingStage.BOOT_BEFORE; |
56 | |
} |
57 | |
} |
58 | 0 | return BindingStage.IGNORE; |
59 | |
} |
60 | |
|
61 | |
@SuppressWarnings("unchecked") |
62 | |
@Override |
63 | |
public void process(Class<Object> annotatedClass, Map<String, Annotation> annotations) { |
64 | 0 | Configuration config = (Configuration) annotations.get(Configuration.class.getName()); |
65 | 0 | Named name = config.name(); |
66 | |
|
67 | |
|
68 | |
URL url; |
69 | 0 | switch (config.location().type()) { |
70 | |
case FILE: |
71 | 0 | File file = new File(config.location().value()); |
72 | 0 | if (!file.exists()) { |
73 | 0 | _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " |
74 | |
+ config.location() + ". In the Path " + file.getAbsolutePath() |
75 | |
+ " no Configuration was found."); |
76 | 0 | return; |
77 | |
} |
78 | |
try { |
79 | 0 | url = file.toURI().toURL(); |
80 | 0 | } catch (MalformedURLException e) { |
81 | 0 | _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " |
82 | |
+ config.location() + ". It has an illegal URL-Format.", e); |
83 | 0 | return; |
84 | 0 | } |
85 | |
break; |
86 | |
case URL: |
87 | |
try { |
88 | 0 | url = new URL(config.location().value()); |
89 | 0 | } catch (MalformedURLException e) { |
90 | 0 | _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " |
91 | |
+ config.location() + ". It has an illegal URL-Format.", e); |
92 | 0 | return; |
93 | 0 | } |
94 | |
break; |
95 | |
case CLASSPATH: |
96 | |
default: |
97 | 0 | url = this.getClass().getResource(config.location().value()); |
98 | |
break; |
99 | |
} |
100 | |
|
101 | 0 | if (url == null) { |
102 | 0 | _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " |
103 | |
+ config.location() + ", because is couldn't be found in the Classpath."); |
104 | |
|
105 | 0 | return; |
106 | |
} |
107 | |
|
108 | 0 | Named named = null; |
109 | 0 | if (name.value().length() > 0) { |
110 | 0 | named = name; |
111 | |
} |
112 | |
|
113 | |
FileConfiguration configuration; |
114 | |
try { |
115 | |
|
116 | |
|
117 | 0 | Class<FileConfiguration> interf = (Class<FileConfiguration>) config.to(); |
118 | 0 | configuration = (FileConfiguration) injector.getInstance(interf); |
119 | 0 | configuration.load(url); |
120 | |
|
121 | 0 | bindInstance(configuration, interf, named, null); |
122 | 0 | bindInstance(configuration, FileConfiguration.class, named, null); |
123 | 0 | bindInstance(configuration, org.apache.commons.configuration.Configuration.class, |
124 | |
named, null); |
125 | 0 | } catch (ConfigurationException e) { |
126 | 0 | _logger.log(Level.WARNING, "Configuration " + name + " couldn't be loaded/bound: " |
127 | |
+ e.getMessage(), e); |
128 | 0 | } |
129 | 0 | } |
130 | |
} |