1 | package org.greenstone.atea;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.mongodb.client.MongoDatabase;
|
---|
5 | import com.mongodb.MongoClient;
|
---|
6 | import com.mongodb.MongoCredential;
|
---|
7 |
|
---|
8 | import java.io.BufferedReader;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.FileReader;
|
---|
11 | import java.util.Properties;
|
---|
12 |
|
---|
13 | import org.apache.log4j.Logger;
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * https://www.tutorialspoint.com/mongodb/mongodb_java.htm
|
---|
18 | *
|
---|
19 | * TO COMPILE:
|
---|
20 | * maori-lang-detection/src$
|
---|
21 | * javac -cp ".:../conf:../lib/*" org/greenstone/atea/MongoDBAccess.java
|
---|
22 | *
|
---|
23 | * TO RUN:
|
---|
24 | * java -cp ".:../conf:../lib/*" org.greenstone.atea.MongoDBAccess
|
---|
25 | */
|
---|
26 | public class MongoDBAccess {
|
---|
27 |
|
---|
28 | private static Logger logger = Logger.getLogger(org.greenstone.atea.MongoDBAccess.class.getName());
|
---|
29 |
|
---|
30 | final static String HOST = "localhost";
|
---|
31 | final static int PORT = 27017; // mongodb port
|
---|
32 | final static String PROPS_FILENAME = "config.properties";
|
---|
33 | final static String DB_NAME = "ateacrawldata";
|
---|
34 |
|
---|
35 | private String USERNAME;
|
---|
36 | private String PASSWORD;
|
---|
37 |
|
---|
38 |
|
---|
39 | public MongoDBAccess() throws Exception {
|
---|
40 | boolean success = false;
|
---|
41 |
|
---|
42 | // Read in the username and password from our props file
|
---|
43 | Properties props = new Properties();
|
---|
44 |
|
---|
45 | //File propsFile = new File(PROPS_FILENAME);
|
---|
46 | //logger.debug("*** Conf props filename: " + propsFile.getAbsolutePath());
|
---|
47 | try {
|
---|
48 | props.load(getClass().getClassLoader().getResourceAsStream(PROPS_FILENAME));
|
---|
49 | } catch(Exception e) {
|
---|
50 | logger.error(e);
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | USERNAME = props.getProperty("mongodb.user", "");
|
---|
55 | if(USERNAME.equals("")) {
|
---|
56 | USERNAME = "root";
|
---|
57 | logger.warn("WARNING: No sensible value for mongodb.user specified in " + PROPS_FILENAME + " defaulting to: " + USERNAME);
|
---|
58 | }
|
---|
59 | PASSWORD = props.getProperty("mongodb.pwd");
|
---|
60 |
|
---|
61 | logger.debug("Got pwd: " + PASSWORD);
|
---|
62 |
|
---|
63 | if(PASSWORD != null && PASSWORD.equals("CHANGEME")) {
|
---|
64 |
|
---|
65 | success = false;
|
---|
66 | throw new Exception("************ FATAL ERROR: Change DB password in properties file " + PROPS_FILENAME);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | public void connectToDB() throws Exception {
|
---|
72 | // Creating a Mongo client
|
---|
73 | MongoClient mongo = new MongoClient( HOST, PORT );
|
---|
74 |
|
---|
75 | // Creating Credentials
|
---|
76 | MongoCredential credential;
|
---|
77 | credential = MongoCredential.createCredential(USERNAME, DB_NAME, PASSWORD.toCharArray());
|
---|
78 | System.out.println("Connected to the database successfully");
|
---|
79 |
|
---|
80 | // Accessing the database
|
---|
81 | MongoDatabase database = mongo.getDatabase(DB_NAME);
|
---|
82 | //System.out.println("Credentials: "+ credential);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | public static void main(String args[]) {
|
---|
87 | try {
|
---|
88 | MongoDBAccess mongodbCon = new MongoDBAccess();
|
---|
89 | //mongodbCon.connectToDB();
|
---|
90 | }catch(Exception e) {
|
---|
91 | e.printStackTrace();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|