1 package de.dlr.shepard.common.neo4j;
2
3 import de.dlr.shepard.auth.apikey.entities.ApiKey;
4 import de.dlr.shepard.auth.permission.model.Permissions;
5 import de.dlr.shepard.auth.users.entities.User;
6 import de.dlr.shepard.common.subscription.entities.Subscription;
7 import de.dlr.shepard.common.util.IConnector;
8 import de.dlr.shepard.context.collection.entities.Collection;
9 import de.dlr.shepard.context.labJournal.entities.LabJournalEntry;
10 import de.dlr.shepard.context.references.dataobject.entities.CollectionReference;
11 import de.dlr.shepard.context.references.file.entities.FileReference;
12 import de.dlr.shepard.context.references.spatialdata.entities.SpatialDataReference;
13 import de.dlr.shepard.context.references.structureddata.entities.StructuredDataReference;
14 import de.dlr.shepard.context.references.timeseriesreference.model.TimeseriesReference;
15 import de.dlr.shepard.context.references.uri.entities.URIReference;
16 import de.dlr.shepard.context.semantic.entities.AnnotatableTimeseries;
17 import de.dlr.shepard.context.semantic.entities.SemanticAnnotation;
18 import de.dlr.shepard.context.version.entities.Version;
19 import de.dlr.shepard.data.file.entities.FileContainer;
20 import de.dlr.shepard.data.spatialdata.model.SpatialDataContainer;
21 import de.dlr.shepard.data.structureddata.entities.StructuredData;
22 import de.dlr.shepard.data.timeseries.model.Timeseries;
23 import io.quarkus.logging.Log;
24 import java.util.Collections;
25 import org.eclipse.microprofile.config.ConfigProvider;
26 import org.neo4j.ogm.config.Configuration;
27 import org.neo4j.ogm.exception.ConnectionException;
28 import org.neo4j.ogm.model.Result;
29 import org.neo4j.ogm.session.Session;
30 import org.neo4j.ogm.session.SessionFactory;
31
32
33
34
35
36
37 public class NeoConnector implements IConnector {
38
39 private SessionFactory sessionFactory = null;
40 private static NeoConnector instance = null;
41
42
43
44
45 private NeoConnector() {}
46
47
48
49
50
51
52 public static NeoConnector getInstance() {
53 if (instance == null) {
54 instance = new NeoConnector();
55 }
56 return instance;
57 }
58
59
60
61
62
63
64 @Override
65 public boolean connect() {
66 String username = ConfigProvider.getConfig().getValue("neo4j.username", String.class);
67 String password = ConfigProvider.getConfig().getValue("neo4j.password", String.class);
68 String host = ConfigProvider.getConfig().getValue("neo4j.host", String.class);
69 Configuration configuration = new Configuration.Builder()
70 .uri("neo4j://" + host)
71 .credentials(username, password)
72 .verifyConnection(true)
73 .useNativeTypes()
74 .build();
75 while (true) {
76 try {
77 sessionFactory = new SessionFactory(
78 configuration,
79 AnnotatableTimeseries.class.getPackageName(),
80 ApiKey.class.getPackageName(),
81 Collection.class.getPackageName(),
82 CollectionReference.class.getPackageName(),
83 FileContainer.class.getPackageName(),
84 FileReference.class.getPackageName(),
85 LabJournalEntry.class.getPackageName(),
86 Permissions.class.getPackageName(),
87 SemanticAnnotation.class.getPackageName(),
88 SpatialDataContainer.class.getPackageName(),
89 SpatialDataReference.class.getPackageName(),
90 StructuredData.class.getPackageName(),
91 StructuredDataReference.class.getPackageName(),
92 Subscription.class.getPackageName(),
93 Timeseries.class.getPackageName(),
94 TimeseriesReference.class.getPackageName(),
95 URIReference.class.getPackageName(),
96 User.class.getPackageName(),
97 Version.class.getPackageName()
98 );
99 return true;
100 } catch (ConnectionException ex) {
101 Log.warn("Cannot connect to neo4j database. Retrying...");
102 }
103 try {
104 Thread.sleep(1000);
105 } catch (InterruptedException e) {
106 Log.error("Cannot sleep while waiting for neo4j Connection");
107 Thread.currentThread().interrupt();
108 }
109 }
110 }
111
112 @Override
113 public boolean disconnect() {
114 if (sessionFactory != null) sessionFactory.close();
115 return true;
116 }
117
118 @Override
119 public boolean alive() {
120 Result result;
121 try {
122 result = sessionFactory.openSession().query("MATCH (n) RETURN count(*) as count", Collections.emptyMap());
123 } catch (ConnectionException ex) {
124 return false;
125 }
126 return result.iterator().hasNext() && result.iterator().next().containsKey("count");
127 }
128
129
130
131
132
133
134 public Session getNeo4jSession() {
135 if (sessionFactory == null) {
136 return null;
137 }
138 return sessionFactory.openSession();
139 }
140 }