Skip to content

Commit 800b82e

Browse files
author
Arun Gupta
committed
New sample to show basic usage of Mongo in a Java EE application
1 parent 963f38f commit 800b82e

11 files changed

Lines changed: 641 additions & 0 deletions

File tree

extra/mongo/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.extra</groupId>
6+
<artifactId>extra-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.extra</groupId>
12+
<artifactId>mongo</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.mongodb</groupId>
19+
<artifactId>mongo-java-driver</artifactId>
20+
<version>1.3</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>de.flapdoodle.embed</groupId>
24+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
25+
<version>1.36</version>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.extra.mongo;
41+
42+
import com.mongodb.BasicDBObject;
43+
import com.mongodb.DBObject;
44+
import javax.enterprise.inject.Model;
45+
import javax.validation.constraints.Size;
46+
47+
/**
48+
* @author Arun Gupta
49+
*/
50+
@Model
51+
public class Person {
52+
53+
@Size(min = 1, max = 20)
54+
private String name;
55+
56+
private int age;
57+
58+
public Person() {
59+
}
60+
61+
public Person(String name, int age) {
62+
this.name = name;
63+
this.age = age;
64+
}
65+
66+
public String getName() {
67+
return name;
68+
}
69+
70+
public void setName(String name) {
71+
this.name = name;
72+
}
73+
74+
public int getAge() {
75+
return age;
76+
}
77+
78+
public void setAge(int age) {
79+
this.age = age;
80+
}
81+
82+
public BasicDBObject toDBObject() {
83+
BasicDBObject doc = new BasicDBObject();
84+
85+
doc.put("name", name);
86+
doc.put("age", age);
87+
88+
return doc;
89+
}
90+
91+
public static Person fromDBObject(DBObject doc) {
92+
Person m = new Person();
93+
94+
m.name = (String) doc.get("name");
95+
m.age = (int) doc.get("age");
96+
97+
return m;
98+
}
99+
100+
@Override
101+
public String toString() {
102+
return name + ", " + age;
103+
}
104+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.extra.mongo;
41+
42+
import com.mongodb.BasicDBObject;
43+
import com.mongodb.DB;
44+
import com.mongodb.DBCollection;
45+
import com.mongodb.DBCursor;
46+
import com.mongodb.DBObject;
47+
import com.mongodb.Mongo;
48+
import com.mongodb.MongoException;
49+
import de.flapdoodle.embed.mongo.MongodExecutable;
50+
import de.flapdoodle.embed.mongo.MongodProcess;
51+
import de.flapdoodle.embed.mongo.MongodStarter;
52+
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
53+
import de.flapdoodle.embed.mongo.config.Net;
54+
import de.flapdoodle.embed.mongo.distribution.Version;
55+
import de.flapdoodle.embed.process.runtime.Network;
56+
import java.io.IOException;
57+
import java.util.ArrayList;
58+
import java.util.List;
59+
import java.util.logging.Level;
60+
import java.util.logging.Logger;
61+
import javax.annotation.PostConstruct;
62+
import javax.annotation.PreDestroy;
63+
import javax.ejb.Stateless;
64+
import javax.inject.Inject;
65+
import javax.inject.Named;
66+
67+
/**
68+
* @author Arun Gupta
69+
*/
70+
@Named
71+
@Stateless
72+
public class PersonSessionBean {
73+
74+
@Inject
75+
Person person;
76+
77+
DBCollection personCollection;
78+
79+
private MongodExecutable mongodExe;
80+
private MongodProcess mongod;
81+
private final int MONGO_PORT = 12345;
82+
83+
@PostConstruct
84+
private void initDB() {
85+
try {
86+
// Start embedded Mongo
87+
MongodStarter runtime = MongodStarter.getDefaultInstance();
88+
mongodExe = runtime.prepare(new MongodConfigBuilder()
89+
.version(Version.Main.PRODUCTION)
90+
.net(new Net(MONGO_PORT, Network.localhostIsIPv6()))
91+
.build());
92+
mongod = mongodExe.start();
93+
94+
// Get an instance of Mongo
95+
Mongo m = new Mongo("localhost", MONGO_PORT);
96+
DB db = m.getDB("personDB");
97+
personCollection = db.getCollection("persons");
98+
if (personCollection == null) {
99+
personCollection = db.createCollection("persons", null);
100+
}
101+
} catch (MongoException | IOException ex) {
102+
Logger.getLogger(PersonSessionBean.class.getName()).log(Level.SEVERE, null, ex);
103+
}
104+
}
105+
106+
@PreDestroy
107+
private void stopDB() {
108+
mongod.stop();
109+
mongodExe.stop();
110+
}
111+
112+
public void createPerson() {
113+
BasicDBObject doc = person.toDBObject();
114+
personCollection.insert(doc);
115+
}
116+
117+
public List<Person> getPersons() {
118+
List<Person> persons = new ArrayList();
119+
DBCursor cur = personCollection.find();
120+
System.out.println("getPersons: Found " + cur.length() + " person(s)");
121+
for (DBObject dbo : cur.toArray()) {
122+
persons.add(Person.fromDBObject(dbo));
123+
}
124+
125+
return persons;
126+
}
127+
}

0 commit comments

Comments
 (0)