Skip to content

Commit 999e3f0

Browse files
committed
Use spring data to access MongoDB.
1 parent 63c0570 commit 999e3f0

File tree

120 files changed

+636
-463
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+636
-463
lines changed

cluster/auth/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.thefirstlineofcode.granite</groupId>
1010
<artifactId>granite-cluster</artifactId>
11-
<version>1.0.4-RELEASE</version>
11+
<version>1.0.5-RELEASE</version>
1212
<relativePath>../pom.xml</relativePath>
1313
</parent>
1414

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,57 @@
11
package com.thefirstlineofcode.granite.cluster.auth;
22

3-
import org.bson.Document;
43
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.mongodb.core.MongoTemplate;
5+
import org.springframework.data.mongodb.core.query.Criteria;
6+
import org.springframework.data.mongodb.core.query.Query;
57
import org.springframework.stereotype.Component;
8+
import org.springframework.transaction.annotation.Transactional;
69

7-
import com.mongodb.client.MongoCollection;
8-
import com.mongodb.client.MongoDatabase;
9-
import com.mongodb.client.model.Filters;
1010
import com.thefirstlineofcode.granite.framework.core.auth.Account;
1111
import com.thefirstlineofcode.granite.framework.core.auth.IAccountManager;
1212

1313
@Component
14+
@Transactional
1415
public class AccountManager implements IAccountManager {
16+
private static final String FIELD_NAME_NAME = "name";
17+
1518
@Autowired
16-
private MongoDatabase database;
19+
private MongoTemplate mongoTemplate;
1720

1821
@Override
1922
public void add(Account account) {
20-
Document doc = new Document().
21-
append("name", account.getName()).
22-
append("password", account.getPassword());
23-
getUsersCollection().insertOne(doc);
23+
if (!(account instanceof D_Account))
24+
throw new IllegalArgumentException(String.format("Must be type of '%s'", D_Account.class.getName()));
25+
26+
if (account.getName() == null || account.getPassword() == null)
27+
throw new IllegalArgumentException("Null user name or password.");
28+
29+
mongoTemplate.save(account);
2430
}
2531

2632
@Override
2733
public void remove(String name) {
28-
getUsersCollection().deleteOne(Filters.eq("name", name));
34+
mongoTemplate.findAndRemove(
35+
new Query().addCriteria(Criteria.where(FIELD_NAME_NAME).is(name)),
36+
D_Account.class);
2937
}
30-
38+
3139
@Override
3240
public boolean exists(String name) {
33-
return getUsersCollection().countDocuments(Filters.eq("name", name)) == 1;
41+
return mongoTemplate.count(
42+
new Query().addCriteria(Criteria.where(FIELD_NAME_NAME).is(name)),
43+
D_Account.class) != 0;
3444
}
35-
45+
3646
@Override
37-
public Account get(String name) {
38-
MongoCollection<Document> users = getUsersCollection();
39-
Document doc = users.find(Filters.eq("name", name)).first();
40-
if (doc == null)
41-
return null;
42-
43-
D_Account account = new D_Account();
44-
account.setId(doc.getObjectId("_id").toHexString());
45-
account.setName(doc.getString("name"));
46-
account.setPassword(doc.getString("password"));
47-
48-
return account;
47+
public D_Account get(String name) {
48+
return mongoTemplate.findOne(
49+
new Query().addCriteria(Criteria.where(FIELD_NAME_NAME).is(name)),
50+
D_Account.class);
4951
}
5052

51-
private MongoCollection<Document> getUsersCollection() {
52-
return database.getCollection("users");
53-
}
54-
5553
@Override
56-
public void add(String userName, String password) {
54+
public void add(String name, String password) {
5755
throw new UnsupportedOperationException();
5856
}
59-
6057
}
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
package com.thefirstlineofcode.granite.cluster.auth;
22

3-
import org.bson.Document;
43
import org.springframework.beans.factory.annotation.Autowired;
54
import org.springframework.stereotype.Component;
65

7-
import com.mongodb.client.MongoCollection;
8-
import com.mongodb.client.MongoDatabase;
9-
import com.mongodb.client.model.Filters;
6+
import com.thefirstlineofcode.granite.framework.core.auth.Account;
7+
import com.thefirstlineofcode.granite.framework.core.auth.IAccountManager;
108
import com.thefirstlineofcode.granite.framework.core.auth.IAuthenticator;
119
import com.thefirstlineofcode.granite.framework.core.auth.PrincipalNotFoundException;
1210

1311
@Component
1412
public class Authenticator implements IAuthenticator {
1513
@Autowired
16-
private MongoDatabase database;
14+
private IAccountManager accountManager;
1715

1816
@Override
1917
public Object getCredentials(Object principal) throws PrincipalNotFoundException {
20-
MongoCollection<Document> users = getUsersCollection();
21-
Document doc = users.find(Filters.eq("name", (String)principal)).projection(
22-
new Document("password", 1)).first();
18+
Account account = accountManager.get((String)principal);
19+
if (account == null)
20+
return new PrincipalNotFoundException(String.format("Account name %s wasn't existed.", principal));
2321

24-
return doc == null ? null : doc.get("password");
22+
return account.getPassword();
2523
}
2624

2725
@Override
2826
public boolean exists(Object principal) {
29-
return getUsersCollection().countDocuments(Filters.eq("name", (String)principal)) == 1;
27+
return accountManager.exists((String)principal);
3028
}
31-
32-
private MongoCollection<Document> getUsersCollection() {
33-
return database.getCollection("users");
34-
}
35-
3629
}

cluster/auth/src/main/java/com/thefirstlineofcode/granite/cluster/auth/D_Account.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.thefirstlineofcode.granite.cluster.auth;
22

3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.mapping.Document;
5+
36
import com.thefirstlineofcode.granite.framework.core.adf.data.IIdProvider;
47
import com.thefirstlineofcode.granite.framework.core.auth.Account;
58

9+
@Document(D_Account.COLLECTION_NAME_ACCOUNTS)
610
public class D_Account extends Account implements IIdProvider<String> {
11+
public static final String COLLECTION_NAME_ACCOUNTS = "accounts";
12+
13+
@Id
714
private String id;
815

916
@Override
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thefirstlineofcode.granite.cluster.auth;
2+
3+
import org.pf4j.Extension;
4+
5+
import com.thefirstlineofcode.granite.framework.adf.mongodb.DataContributorAdapter;
6+
7+
@Extension
8+
public class DataContributor extends DataContributorAdapter {
9+
@Override
10+
public Class<?>[] getDataObjects() {
11+
return new Class<?>[] {
12+
D_Account.class
13+
};
14+
}
15+
}

cluster/auth/src/main/java/com/thefirstlineofcode/granite/cluster/auth/DbInitializer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111

1212
@Extension
1313
public class DbInitializer extends AbstractDbInitializer {
14+
private static final String FIELD_NAME_NAME = "name";
1415

1516
@Override
1617
public void initialize(MongoDatabase database) {
17-
if (collectionExistsInDb(database, "users"))
18+
if (collectionExistsInDb(database, D_Account.COLLECTION_NAME_ACCOUNTS))
1819
return;
1920

20-
database.createCollection("users");
21-
MongoCollection<Document> users = database.getCollection("users");
22-
users.createIndex(Indexes.ascending("name"), new IndexOptions().unique(true));
21+
database.createCollection(D_Account.COLLECTION_NAME_ACCOUNTS);
22+
MongoCollection<Document> accounts = database.getCollection(D_Account.COLLECTION_NAME_ACCOUNTS);
23+
accounts.createIndex(Indexes.ascending(FIELD_NAME_NAME), new IndexOptions().unique(true));
2324
}
2425

2526
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
plugin.id=granite-cluster-auth
22
plugin.provider=TheFirstLineOfCode
3-
plugin.version=1.0.4-RELEASE
3+
plugin.version=1.0.5-RELEASE

cluster/cluster-debug/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.thefirstlineofcode.granite</groupId>
1010
<artifactId>granite-cluster</artifactId>
11-
<version>1.0.4-RELEASE</version>
11+
<version>1.0.5-RELEASE</version>
1212
<relativePath>../pom.xml</relativePath>
1313
</parent>
1414

cluster/dba/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.thefirstlineofcode.granite</groupId>
1010
<artifactId>granite-cluster</artifactId>
11-
<version>1.0.4-RELEASE</version>
11+
<version>1.0.5-RELEASE</version>
1212
<relativePath>../pom.xml</relativePath>
1313
</parent>
1414

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,95 @@
11
package com.thefirstlineofcode.granite.cluster.dba;
22

3+
import java.lang.reflect.InvocationTargetException;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.UUID;
8+
9+
import com.thefirstlineofcode.granite.framework.adf.mongodb.DataObjectMapping;
10+
import com.thefirstlineofcode.granite.framework.adf.mongodb.IDataContributor;
11+
import com.thefirstlineofcode.granite.framework.core.adf.IApplicationComponentService;
12+
import com.thefirstlineofcode.granite.framework.core.adf.IApplicationComponentServiceAware;
313
import com.thefirstlineofcode.granite.framework.core.adf.data.IDataObjectFactory;
14+
import com.thefirstlineofcode.granite.framework.core.adf.data.IIdProvider;
415
import com.thefirstlineofcode.granite.framework.core.annotations.AppComponent;
16+
import com.thefirstlineofcode.granite.framework.core.repository.IInitializable;
517

618
@AppComponent("data.object.factory")
7-
public class DataObjectFactory implements IDataObjectFactory {
8-
@SuppressWarnings("unchecked")
19+
public class DataObjectFactory implements IInitializable, IApplicationComponentServiceAware, IDataObjectFactory {
20+
private IApplicationComponentService appComponentService;
21+
private Map<Class<?>, Class<?>> dataObjectMappings;
22+
private volatile boolean inited;
23+
24+
public DataObjectFactory() {
25+
inited = false;
26+
dataObjectMappings = new HashMap<>();
27+
}
28+
29+
@SuppressWarnings({ "rawtypes", "unchecked" })
930
@Override
1031
public <K, V extends K> V create(Class<K> clazz) {
32+
if (!inited)
33+
init();
34+
1135
try {
12-
return (V)clazz.getDeclaredConstructor().newInstance();
36+
V object = doCreate(clazz);
37+
38+
if (object instanceof IIdProvider) {
39+
((IIdProvider)object).setId(UUID.randomUUID().toString());
40+
}
41+
42+
return object;
1343
} catch (Exception e) {
1444
throw new RuntimeException(String.format("Can't create data object for class '%s'.", clazz.getName()), e);
1545
}
1646
}
47+
48+
@SuppressWarnings("unchecked")
49+
private <K, V extends K> V doCreate(Class<K> domainType) throws InstantiationException, IllegalAccessException,
50+
IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
51+
Class<?> dataType = dataObjectMappings.get(domainType);
52+
if (dataType == null) {
53+
return (V)domainType.getDeclaredConstructor().newInstance();
54+
}
55+
56+
return (V)dataType.getDeclaredConstructor().newInstance();
57+
}
58+
59+
@Override
60+
public void setApplicationComponentService(IApplicationComponentService appComponentService) {
61+
this.appComponentService = appComponentService;
62+
}
63+
64+
@Override
65+
public void init() {
66+
if (inited)
67+
return;
68+
69+
synchronized (this) {
70+
if (inited)
71+
return;
72+
73+
List<IDataContributor> dataObjectsContributors = appComponentService.getPluginManager().
74+
getExtensions(IDataContributor.class);
75+
for (IDataContributor dataObjectsContributor : dataObjectsContributors) {
76+
DataObjectMapping<?>[] mappings = dataObjectsContributor.getDataObjectMappings();
77+
if (mappings == null || mappings.length == 0)
78+
continue;
79+
80+
for (DataObjectMapping<?> mapping : mappings) {
81+
Class<?> domainType = mapping.domainType;
82+
if (domainType == null)
83+
domainType = mapping.dataType.getSuperclass();
84+
85+
if (dataObjectMappings.containsKey(domainType))
86+
throw new IllegalArgumentException(String.format("Reduplicate domain data object type: '%s'.", mapping.domainType));
87+
88+
dataObjectMappings.put(domainType, mapping.dataType);
89+
}
90+
}
91+
92+
inited = true;
93+
}
94+
}
1795
}

0 commit comments

Comments
 (0)