-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIonicState.java
More file actions
78 lines (70 loc) · 2.87 KB
/
IonicState.java
File metadata and controls
78 lines (70 loc) · 2.87 KB
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
package com.ionic.sdk.addon.jdbc.impl;
import com.ionic.sdk.agent.Agent;
import com.ionic.sdk.agent.AgentSdk;
import com.ionic.sdk.core.res.Resource;
import com.ionic.sdk.device.profile.persistor.DeviceProfilePersistorPlainText;
import com.ionic.sdk.device.profile.persistor.ProfilePersistor;
import com.ionic.sdk.error.IonicException;
import com.ionic.sdk.error.SdkError;
import java.io.File;
import java.net.URL;
import java.security.Security;
import java.util.Properties;
/**
* Cache an initialized {@link Agent} loaded on the first call to {@link #getAgent(Properties)}.
*/
public class IonicState {
/**
* Cache an initialized {@link Agent} loaded on the first call to {@link #getAgent(Properties)}.
*/
public static Agent getAgent(final Properties properties) throws IonicException {
AgentSdk.initialize(Security.getProvider("SunJCE"));
return Agent.clone(SingletonHelper.getInstance(properties.getProperty("ionic.sep")));
}
/**
* Helper to guard against double init.
* <p>
* http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
*/
private static class SingletonHelper {
/**
* The per-process singleton of this object. The first fetch of this object should trigger its implicit
* initialization. Subsequent fetches will return the cached value.
*/
private static volatile Agent instance;
/**
* @return the per-process singleton of this object
*/
private static Agent getInstance(final String location) throws IonicException {
if (instance == null) {
synchronized (SingletonHelper.class) {
if (instance == null) {
instance = getIonicAgent(location);
}
}
}
return instance;
}
/**
* Template {@link Agent} is initialized from {@link ProfilePersistor}.
*
* @param location path of {@link ProfilePersistor} information
* @return initialized {@link Agent}
* @throws IonicException on failure to initialize {@link Agent}
*/
private static Agent getIonicAgent(final String location) throws IonicException {
// interpret setting as a classpath resource
final URL url = Resource.resolve(location);
// interpret setting as a filesystem file
final File file = new File(location);
// load the profile from the indicated location
if (url != null) {
return new Agent(new DeviceProfilePersistorPlainText(url));
} else if (file.exists()) {
return new Agent(new DeviceProfilePersistorPlainText(file.getPath()));
} else {
throw new IonicException(SdkError.ISAGENT_RESOURCE_NOT_FOUND, location);
}
}
}
}