This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
/
AbstractOrientDatabaseFactory.java
191 lines (158 loc) · 4.23 KB
/
AbstractOrientDatabaseFactory.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
package org.springframework.data.orient.commons.core;
import com.orientechnologies.orient.core.db.ODatabase;
import com.orientechnologies.orient.core.db.ODatabaseInternal;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import static org.springframework.util.Assert.notNull;
/**
* A base factory for creating {@link com.orientechnologies.orient.core.db.ODatabase} objects.
*
* @author Dzmitry_Naskou
* @param <T> the type of database to handle
*/
public abstract class AbstractOrientDatabaseFactory<T> implements OrientDatabaseFactory<T> {
/** The logger. */
private static Logger log = LoggerFactory.getLogger(AbstractOrientDatabaseFactory.class);
/** The username. */
protected String username = DEFAULT_USERNAME;
/** The password. */
protected String password = DEFAULT_PASSWORD;
/** The min pool size. */
protected int minPoolSize = DEFAULT_MIN_POOL_SIZE;
/** The max pool size. */
protected int maxPoolSize = DEFAULT_MAX_POOL_SIZE;
protected Boolean autoCreate;
protected String url;
@PostConstruct
public void init() {
notNull(url);
notNull(username);
notNull(password);
if(autoCreate==null) {
autoCreate = !getUrl().startsWith("remote:");
}
ODatabase<?> db = newDatabase();
createDatabase(db);
createPool();
}
protected abstract void createPool();
/**
* Open the database.
*
* @return the o database complex
*/
public abstract ODatabase<T> openDatabase();
protected abstract ODatabaseInternal<?> newDatabase();
public ODatabase<T> db() {
ODatabase<T> db;
if(!ODatabaseRecordThreadLocal.INSTANCE.isDefined()) {
db = openDatabase();
log.debug("acquire db from pool {}", db.hashCode());
} else {
db = (ODatabase<T>)ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner();
if(db.isClosed()) {
db = openDatabase();
log.debug("re-opened db {}", db.hashCode());
} else {
log.debug("use existing db {}", db.hashCode());
}
}
return db;
}
protected void createDatabase(ODatabase<?> db) {
if (autoCreate) {
if (!db.exists()) {
db.create();
db.close();
}
}
}
/**
* Gets the database url.
*
* @return the url
*/
public String getUrl() {
return url;
}
/**
* Sets the database url.
*
* @param url the new url
*/
public void setUrl(String url) {
this.url = url;
}
/**
* Gets the username.
*
* @return the username
*/
public String getUsername() {
return username;
}
/**
* Sets the username.
*
* @param username the new username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Gets the password.
*
* @return the password
*/
public String getPassword() {
return password;
}
/**
* Sets the password.
*
* @param password the new password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Gets the min pool size.
*
* @return the min pool size
*/
public int getMinPoolSize() {
return minPoolSize;
}
/**
* Sets the min pool size.
*
* @param minPoolSize the new min pool size
*/
public void setMinPoolSize(int minPoolSize) {
this.minPoolSize = minPoolSize;
}
/**
* Gets the max pool size.
*
* @return the max pool size
*/
public int getMaxPoolSize() {
return maxPoolSize;
}
/**
* Sets the max pool size.
*
* @param maxPoolSize the new max pool size
*/
public void setMaxPoolSize(int maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public Boolean getAutoCreate() {
return autoCreate;
}
public void setAutoCreate(Boolean autoCreate) {
this.autoCreate = autoCreate;
}
}