Permite definir @Producer para inyectar MongoClient y reducir el codigo del repositorio
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class JmoordbProducer {
@Produces
@ApplicationScoped
public MongoClient mongoClient() {
MongoClient mongo = new MongoClient("localhost", 27017);
try {
String username = (String) JmoordbContext.get("username");
String password = (String) JmoordbContext.get("password");
String database = (String) JmoordbContext.get("database");
String host = (String) JmoordbContext.get("host");
String port = (String) JmoordbContext.get("port");
Boolean security = (Boolean) JmoordbContext.get("security");
if (security == null && username == null) {
security = false;
}
if (security) {
char[] charArray = password.toCharArray();
MongoCredential credential = MongoCredential.createCredential(username, database, charArray);
ServerAddress serverAddress = new ServerAddress(host, Integer.parseInt(port));
// mongo = new MongoClient(serverAddress, new ArrayList<>());
mongo = new MongoClient(serverAddress, new ArrayList<MongoCredential>() {{ add(credential); }});
//
}else{
mongo = new MongoClient();
}
} catch (Exception e) {
JmoordbUtil.errorMessage("coneecction() "+e.getLocalizedMessage());
}
return mongo;
}
}
JmoordbConnection jmoordbConnection = new JmoordbConnection.Builder()
.withSecurity(false)
.withDatabase("")
.withHost("")
.withPort(0)
.withUsername("")
.withPassword(password)
.build();
@Stateless
public class RolRepository extends Repository<Rol> {
public RolRepository() {
super(Rol.class, "store", "rol");
}
}