-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractGenericModule.java
100 lines (89 loc) · 3.22 KB
/
AbstractGenericModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package org.eclipse.emf.parsley.runtime.service;
/*******************************************************************************
* Copyright (c) 2008, 2013 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* @author Heiko Behrens - Initial contribution and API
* @author Sven Efftinge
* @author Sebastian Zarnekow
*******************************************************************************/
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.name.Names;
/**
* @author Heiko Behrens - Initial contribution and API
* @author Sven Efftinge
* @author Sebastian Zarnekow
*/
public abstract class AbstractGenericModule implements Module {
private static final Logger LOGGER = Logger.getLogger(AbstractGenericModule.class);
@Override
public void configure(Binder binder) {
Module compound = getBindings();
compound.configure(binder);
}
public final CompoundModule getBindings() {
Method[] methods = this.getClass().getMethods();
CompoundModule result = new CompoundModule();
for (Method method : methods) {
try {
if (method.getName().startsWith("bind")) {
result.add(new BindModule(method, this));
} else if (method.getName().startsWith("provide")) {
result.add(new ProviderModule(method, this));
} else if (method.getName().startsWith("configure")) {
if (!method.getName().equals("configure") && method.getParameterTypes().length == 1
&& method.getParameterTypes()[0].equals(Binder.class)) {
result.add(new FreeModule(method, this));
}
} else if (method.getName().startsWith(FieldSetterModule.METHOD_PREFIX)) {
if (!method.getName().equals(FieldSetterModule.METHOD_PREFIX)
&& method.getParameterTypes().length == 0) {
result.add(new FieldSetterModule(method, this));
}
}
} catch (Exception e) {
LOGGER.warn("Trying to use method " + method.toGenericString() + " for configuration failed", e);
}
}
return result;
}
protected void bindProperties(Binder binder, String propertyFilePath) {
try {
InputStream in = getClass().getClassLoader().getResourceAsStream(propertyFilePath);
if (in != null) {
Properties properties = new Properties();
properties.load(in);
Names.bindProperties(binder, properties);
} else {
throw new IllegalStateException("Couldn't find property file : " + propertyFilePath);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected Properties tryBindProperties(Binder binder, String propertyFilePath) {
try {
InputStream in = getClass().getClassLoader().getResourceAsStream(propertyFilePath);
if (in != null) {
Properties properties = new Properties();
properties.load(in);
Names.bindProperties(binder, properties);
return properties;
} else {
return null;
}
} catch (IOException e) {
return null;
}
}
}