Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyGuiceServletContextListener extends GuiceServletContextListener {
- @Override
- protected Injector getInjector() {
- Injector injector = Guice.createInjector(
- new ServerModule(), new DispatchServletModule(), new DatabaseConnectorModule());// , new DatabaseConnectorServletModule()
- // Initialize the connection pool. This should be first, before any requests begin.
- DatabaseConnectorImpl DatabaseConnectionPool = injector.getInstance(Key.get(DatabaseConnectorImpl.class));
- try {
- DatabaseConnectionPool.init();// FIXME: Is this necessary?
- } catch (ServletException e) {
- e.printStackTrace();
- }
- return injector;
- }
- }
- public class DatabaseConnectorModule extends ServletModule {
- @Override
- protected void configureServlets() {
- // need a context for jndi to use.
- bind(Context.class).to(InitialContext.class);
- // actual jndi name is "jdbc/exist".
- bind(Database.class).toProvider(JndiIntegration.fromJndi(Database.class, "java:/comp/env/jdbc/exist")).asEagerSingleton();
- serve("/*").with(DatabaseConnectorImpl.class);
- }
- }
- @Singleton
- public class DatabaseConnectorImpl extends HttpServlet {
- private final Database database;
- @Inject
- public DatabaseConnectorImpl(Database database) {
- this.database = database;
- try {
- DatabaseManager.registerDatabase(database);
- } catch (XMLDBException e) {
- System.err.println("Error in DatabaseConnectorImpl: " + e.getMessage());
- e.printStackTrace();
- }
- }
- /**
- * 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.
- * <br> This is a convenience class because
- * @throws XMLDBException
- * if username, password, or something else is incorrect.
- */
- public Collection getCollection(String URI) throws XMLDBException {
- if(this.database == null){
- System.err.println("Error in DatabaseConnectorImpl: database is null");
- throw new NullPointerException("database is null in DatabaseConnectorImpl.getCollection");
- }
- String username = database.getProperty("username");
- String password = database.getProperty("password");
- return DatabaseManager.getCollection(URI, username, password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement