Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. public class DatabaseConnectorModule extends ServletModule {
  2.  
  3. @Override
  4. protected void configureServlets() {
  5. // need a context for jndi to use.
  6. bind(Context.class).to(InitialContext.class);
  7.  
  8. // actual jndi name is "jdbc/exist".
  9. bind(Database.class).toProvider(JndiIntegration.fromJndi(Database.class, "java:/comp/env/jdbc/exist")).asEagerSingleton();
  10. serve("/*").with(DatabaseConnectorImpl.class);
  11. }
  12. }
  13.  
  14.  
  15. @Singleton
  16. public class DatabaseConnectorImpl extends HttpServlet {
  17.  
  18. private final Database database;
  19.  
  20. @Inject
  21. public DatabaseConnectorImpl(Database database) {
  22. this.database = database;
  23. try {
  24. DatabaseManager.registerDatabase(database);
  25. } catch (XMLDBException e) {
  26. System.err.println("Error in DatabaseConnectorImpl: " + e.getMessage());
  27. e.printStackTrace();
  28. }
  29. }
  30.  
  31. /**
  32. * Dole out the collections here. Anyone calling this class should already be validated because the class extracts the database username and password as a property.
  33. * <br> This is a convenience class because
  34. * @throws XMLDBException
  35. * if username, password, or something else is incorrect.
  36. */
  37. public Collection getCollection(String URI) throws XMLDBException {
  38. if(this.database == null){
  39. System.err.println("Error in DatabaseConnectorImpl: database is null");
  40. throw new NullPointerException("database is null in DatabaseConnectorImpl.getCollection");
  41. }
  42. String username = database.getProperty("username");
  43. String password = database.getProperty("password");
  44. return DatabaseManager.getCollection(URI, username, password);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement