/kitchensink-mongodb/src/main/java/org/jboss/as/quickstarts/kitchensink/util/Resources.java

https://bitbucket.org/diogo-rh/openshift-quickstarts · Java · 93 lines · 55 code · 10 blank · 28 comment · 2 complexity · 6d38dffab96ba809383d938317a45be7 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
  4. * contributors by the @authors tag. See the copyright.txt in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jboss.as.quickstarts.kitchensink.util;
  18. import com.mongodb.MongoClient;
  19. import com.mongodb.MongoClientOptions;
  20. import com.mongodb.MongoCredential;
  21. import com.mongodb.ServerAddress;
  22. import com.mongodb.client.MongoCollection;
  23. import com.mongodb.client.MongoDatabase;
  24. import org.bson.Document;
  25. import org.jboss.as.quickstarts.kitchensink.model.Member;
  26. import javax.enterprise.context.RequestScoped;
  27. import javax.enterprise.inject.Produces;
  28. import javax.enterprise.inject.spi.InjectionPoint;
  29. import javax.faces.context.FacesContext;
  30. import javax.inject.Inject;
  31. import java.util.Collections;
  32. import java.util.List;
  33. import java.util.logging.Logger;
  34. /**
  35. * This class uses CDI to alias Java EE resources, such as the persistence context, to CDI beans
  36. * <p>
  37. * <p>
  38. * Example injection on a managed bean field:
  39. * </p>
  40. * <p>
  41. * <pre>
  42. * &#064;Inject
  43. * private EntityManager em;
  44. * </pre>
  45. */
  46. public class Resources {
  47. public static final String DB_SERVICE_PREFIX_MAPPING = "DB_SERVICE_PREFIX_MAPPING";
  48. @Inject
  49. private DbServicePrefixMappingParser dbServicePrefixMappingParser;
  50. @Produces
  51. private MongoClient produceMongoClient() {
  52. List<DbServicePrefixMappingParser.DbServicePrefixMapping> mappings = dbServicePrefixMappingParser.parseDbServicePrefixMappingEnvVar(System.getenv(DB_SERVICE_PREFIX_MAPPING));
  53. for (DbServicePrefixMappingParser.DbServicePrefixMapping mapping : mappings) {
  54. if ("MONGODB".equals(mapping.getDatabaseType().toUpperCase())) {
  55. String hostname = System.getenv(mapping.getServiceName() + "_SERVICE_HOST");
  56. String port = System.getenv(mapping.getServiceName() + "_SERVICE_PORT");
  57. String database = System.getenv(mapping.getEnvPrefix() + "_DATABASE");
  58. String username = System.getenv(mapping.getEnvPrefix() + "_USERNAME");
  59. String password = System.getenv(mapping.getEnvPrefix() + "_PASSWORD");
  60. MongoCredential credential = MongoCredential.createCredential(username, database, password.toCharArray());
  61. return new MongoClient(new ServerAddress(hostname, Integer.parseInt(port)), Collections.singletonList(credential));
  62. }
  63. }
  64. throw new IllegalStateException("No MongoDB mapping in " + DB_SERVICE_PREFIX_MAPPING);
  65. }
  66. @Produces
  67. private MongoDatabase produceMongoDatabase(MongoClient mongoClient) {
  68. return mongoClient.getDatabase(System.getenv("DB_DATABASE"));
  69. }
  70. @Produces
  71. private MongoCollection<Document> produceMongoCollection(MongoDatabase mongoDatabase) {
  72. return mongoDatabase.getCollection(Member.class.getSimpleName());
  73. }
  74. @Produces
  75. public Logger produceLog(InjectionPoint injectionPoint) {
  76. return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
  77. }
  78. @Produces
  79. @RequestScoped
  80. public FacesContext produceFacesContext() {
  81. return FacesContext.getCurrentInstance();
  82. }
  83. }