/
MongoShellCommandManager.java
167 lines (144 loc) · 5.56 KB
/
MongoShellCommandManager.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
package fr.opensagres.nosql.ide.mongodb.core.shell;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoURI;
import com.mongodb.gridfs.GridFS;
import com.mongodb.tools.driver.DBObjectHelper;
import com.mongodb.tools.driver.MongoInstanceManager;
import com.mongodb.tools.driver.pagination.Page;
import com.mongodb.tools.driver.pagination.PaginationHelper;
import com.mongodb.tools.driver.pagination.SortOrder;
import fr.opensagres.nosql.ide.core.model.IServer;
import fr.opensagres.nosql.ide.core.shell.AbstractShellCommandManager;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.CollectionFindShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.ConnectShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.DBAuthenticateShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.DisconnectShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.DropDatabaseShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.GetSystemUsersShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.ShowCollectionsShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.ShowDbsShellCommand;
import fr.opensagres.nosql.ide.mongodb.core.internal.shell.UseShellCommand;
public class MongoShellCommandManager extends AbstractShellCommandManager {
private static final MongoShellCommandManager INSTANCE = new MongoShellCommandManager();
public static MongoShellCommandManager getInstance() {
return INSTANCE;
}
public Mongo connect(IServer server, MongoURI mongoURI)
throws UnknownHostException, MongoException {
Mongo mongo = MongoInstanceManager.getInstance().createMongo(mongoURI);
// MongoDriverHelper.tryConnection(mongo);
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new ConnectShellCommand(server, mongoURI));
}
return mongo;
}
public void disconnect(IServer server, Mongo mongo) {
if (mongo == null) {
return;
}
MongoInstanceManager.getInstance().dispose(mongo);
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new DisconnectShellCommand(server));
}
}
public List<String> showDbs(IServer server, Mongo mongo) {
List<String> names = mongo.getDatabaseNames();
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new ShowDbsShellCommand(server, names));
}
return names;
}
public DB use(IServer server, Mongo mongo, String dbname) {
DB db = mongo.getDB(dbname);
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new UseShellCommand(server, dbname));
}
return db;
}
public boolean authenticate(IServer server, DB db, String username,
char[] passwd) {
boolean result = db.authenticate(username, passwd);
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new DBAuthenticateShellCommand(server, username, passwd));
}
return result;
}
public Set<String> showCollections(IServer server, DB db) {
Set<String> names = db.getCollectionNames();
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new ShowCollectionsShellCommand(server));
}
return names;
}
public CommandResult getDBCollectionGetStats(DBCollection collection) {
CommandResult result = collection.getStats();
return result;
}
public List<DBObject> getDBCollectionGetIndexes(IServer server,
DBCollection dbCollection) {
return dbCollection.getIndexInfo();
}
public Page paginate(IServer server, DBCollection collection,
int pageNumber, int itemsPerPage) {
return paginate(server, collection, pageNumber, itemsPerPage, null,
null);
}
public Page paginate(IServer server, DBCollection collection,
int pageNumber, int itemsPerPage, String sortName, SortOrder order) {
Page page = PaginationHelper.paginate(collection, pageNumber,
itemsPerPage, sortName, order);
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new CollectionFindShellCommand(server,
collection.getName(), pageNumber, itemsPerPage,
sortName, order));
}
return page;
}
public Page paginate(GridFS gridFS, int pageNumber, int itemsPerPage) {
return paginate(gridFS, pageNumber, itemsPerPage, null, null);
}
public Page paginate(GridFS gridFS, int pageNumber, int itemsPerPage,
String sortName, SortOrder order) {
Page page = PaginationHelper.paginate(gridFS, pageNumber, itemsPerPage,
sortName, order);
if (hasListeners()) {
// getShellNotificationManager().broadcastChange(
// new CollectionFindShellCommand(collection, pageNumber,
// itemsPerPage, sortName, order));
}
return page;
}
public List<DBObject> getSystemUsers(IServer server, DB db) {
List<DBObject> users = DBObjectHelper.getSystemUsers(db);
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new GetSystemUsersShellCommand(server));
}
return users;
}
public void dropDatabase(IServer server, DB db) {
db.dropDatabase();
if (hasListeners()) {
getShellNotificationManager().broadcastChange(
new DropDatabaseShellCommand(server));
}
}
public void createCollection(DB db, String collectionName, DBObject options) {
db.createCollection(collectionName, options);
}
}