/
Push.java
226 lines (187 loc) · 6.69 KB
/
Push.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.baasbox.configuration;
import org.codehaus.jackson.map.ObjectMapper;
import play.Logger;
import com.baasbox.configuration.index.IndexPushConfiguration;
import com.baasbox.util.ConfigurationFileContainer;
public enum Push implements IProperties{
PUSH_SANDBOX_ENABLE("push.sandbox.enable", "The value to verify if BaasBox needs to contact the SANDBOX server or the PRODUCTION server", Boolean.class),
PUSH_APPLE_TIMEOUT("push.apple.timeout", "The timeout for push notifications on Apple devices", Integer.class),
SANDBOX_ANDROID_API_KEY("sandbox.android.api.key", "The key to send push notifications to Android devices in SANDBOX mode", String.class),
SANDBOX_IOS_CERTIFICATE("sandbox.ios.certificate", "The Apple certificate in SANDBOX mode", ConfigurationFileContainer.class,new IosCertificateHandler()),
SANDBOX_IOS_CERTIFICATE_PASSWORD("sandbox.ios.certificate.password", "The password of the Apple certificate in SANDBOX mode", String.class),
PRODUCTION_ANDROID_API_KEY("production.android.api.key", "The key to send push notifications to Android devices in PRODUCTION mode", String.class),
PRODUCTION_IOS_CERTIFICATE("production.ios.certificate", "The Apple certificate in PRODUCTION mode", ConfigurationFileContainer.class,new IosCertificateHandler()),
PRODUCTION_IOS_CERTIFICATE_PASSWORD("production.ios.certificate.password", "The password of the Apple certificate in PRODUCTION mode", String.class);
private final String key;
private final Class<?> type;
private String description;
private IPropertyChangeCallback changeCallback = null;
//override
private boolean editable=true;
private boolean visible=true;
private Object overriddenValue=null;
private boolean overridden=false;
Push(final String iKey, final String iDescription, final Class<?> iType,
final IPropertyChangeCallback iChangeAction) {
this(iKey, iDescription, iType);
changeCallback = iChangeAction;
}
Push(final String iKey, final String iDescription, final Class<?> iType) {
key = iKey;
description = iDescription;
type = iType;
}
@Override
public void setValue(Object newValue) throws IllegalStateException{
if (!editable) throw new IllegalStateException("The value cannot be changed");
_setValue(newValue);
}
@Override
public void _setValue(Object newValue) {
Object parsedValue=null;
if (Logger.isDebugEnabled()) Logger.debug("Type:"+type+" Setting "+newValue.toString() + "of class: "+newValue.getClass().toString());
try{
if (newValue != null)
if (type == Boolean.class)
parsedValue = Boolean.parseBoolean(newValue.toString());
else if (type == Integer.class)
parsedValue = Integer.parseInt(newValue.toString());
else if (type == Float.class)
parsedValue = Float.parseFloat(newValue.toString());
else if (type == String.class)
parsedValue = newValue.toString();
else if (type == ConfigurationFileContainer.class){
parsedValue = (ConfigurationFileContainer)newValue;
}
else
parsedValue = newValue;
}catch (Exception e){
Logger.warn(newValue + " value is invalid for key " + key + "\nNULL will be stored");
}
if (changeCallback != null) changeCallback.change(getValue(), newValue);
IndexPushConfiguration idx;
try {
idx = new IndexPushConfiguration();
if(type == ConfigurationFileContainer.class && parsedValue!=null){
ConfigurationFileContainer cfc = (ConfigurationFileContainer)parsedValue;
ObjectMapper om = new ObjectMapper();
idx.put(key, om.writeValueAsString(cfc));
}else{
idx.put(key, parsedValue);
}
} catch (Exception e) {
Logger.error("Could not store key " + key, e);
throw new RuntimeException("Could not store key " + key,e);
}
}
@Override
public Object getValue() {
if (overridden) return overriddenValue;
return _getValue();
}
@Override
public Object _getValue() {
IndexPushConfiguration idx;
try {
idx = new IndexPushConfiguration();
return idx.get(key);
} catch (Exception e) {
Logger.error("Could not retrieve key " + key, e);
}
return null;
}
@Override
public boolean getValueAsBoolean() {
Object v = getValue();
return v instanceof Boolean ? ((Boolean) v).booleanValue() : Boolean.parseBoolean(v.toString());
}
@Override
public String getValueAsString() {
Object v = getValue();
return v != null ? v.toString() : null;
}
public ConfigurationFileContainer getValueAsFileContainer() {
Object v = getValue();
ConfigurationFileContainer result = null;
if(v!=null){
ObjectMapper om = new ObjectMapper();
try {
result = om.readValue(v.toString(), ConfigurationFileContainer.class);
} catch (Exception e) {
e.printStackTrace();
return result;
}
}
return result;
}
@Override
public int getValueAsInteger() {
Object v = getValue();
return (int) (v instanceof Number ? ((Number) v).intValue() : Integer.parseInt(v.toString()));
}
@Override
public long getValueAsLong() {
Object v = getValue();
return (long) (v instanceof Number ? ((Number) v).longValue() : Long.parseLong(v.toString()));
}
@Override
public float getValueAsFloat() {
Object v = getValue();
return (float) (v instanceof Number ? ((Number) v).floatValue() : Float.parseFloat(v.toString()));
}
@Override
public String getKey() {
return key;
}
@Override
public Class<?> getType() {
return type;
}
@Override
public String getValueDescription() {
return description;
}
public static String getEnumDescription() {
return "Configurations for push related properties";
}
@Override
public void override(Object newValue) {
Object parsedValue=null;
if (Logger.isDebugEnabled()) Logger.debug("New setting value, key: " + this.key + ", type: "+ this.type + ", new value: " + newValue);
if (changeCallback != null) changeCallback.change(getValue(), newValue);
if (newValue != null)
if (type == Boolean.class)
parsedValue = Boolean.parseBoolean(newValue.toString());
else if (type == Integer.class)
parsedValue = Integer.parseInt(newValue.toString());
else if (type == Float.class)
parsedValue = Float.parseFloat(newValue.toString());
else if (type == String.class)
parsedValue = newValue.toString();
else
parsedValue = newValue;
this.overriddenValue=parsedValue;
this.overridden=true;
this.editable=false;
}
@Override
public void setEditable(boolean editable) {
this.editable = editable;
}
@Override
public void setVisible(boolean visible) {
this.visible = visible;
}
@Override
public boolean isOverridden() {
return overridden;
}
@Override
public boolean isVisible() {
return visible;
}
@Override
public boolean isEditable() {
return editable;
}
}