/*
 *   Copyright 2010, Maarten Billemont
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.lyndir.love.webapp.listener;

import com.google.inject.*;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.lyndir.lhunath.opal.jpa.PersistFilter;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.love.webapp.data.service.ServiceModule;
import com.lyndir.love.webapp.resource.UserResource;
import com.lyndir.love.webapp.util.GsonJsonProvider;
import com.lyndir.love.webapp.util.ModelExceptionMapper;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;


/**
 * @author lhunath
 */
public class GuiceContext extends GuiceServletContextListener {

    static final Logger logger = Logger.get( GuiceContext.class );

    private static final String PATH_APP = "/app/*";
    private static final String PATH_APP_REST = "/app/rest/*";

    /**
     * {@inheritDoc}
     */
    @Override
    protected Injector getInjector() {
        return Guice.createInjector( Stage.DEVELOPMENT, new ServiceModule(), new ServletModule() {
            @Override
            protected void configureServlets() {
                filter( PATH_APP ).through( PersistFilter.class );

                bind( UserResource.class );
                bind( GsonJsonProvider.class );
                bind( ModelExceptionMapper.class );
                serve( PATH_APP_REST ).with( GuiceContainer.class );
            }
        } );
    }
}

@@ -657,16 +657,181 @@
 tener;%0A%0A
+import com.google.api.client.repackaged.com.google.common.base.Preconditions;%0Aimport com.google.common.base.Splitter;%0Aimport com.google.common.collect.ImmutableMap;%0A
 import c
@@ -850,16 +850,16 @@
 ject.*;%0A
-
 import c
@@ -952,32 +952,76 @@
 .ServletModule;%0A
+import com.lyndir.lhunath.opal.jpa.Persist;%0A
 import com.lyndi
@@ -1395,16 +1395,102 @@
 tainer;%0A
+import java.net.URI;%0Aimport java.util.Iterator;%0Aimport javax.persistence.Persistence;%0A
 %0A%0A/**%0A *
@@ -1684,16 +1684,21 @@
 ATH_APP 
+     
 = %22/app/
@@ -2021,24 +2021,1844 @@
 ervlets() %7B%0A
+                logger.dbg( %22Configuring persistence filter%22 );%0A                Persist persistence;%0A                String databaseURL = System.getenv( %22DATABASE_URL%22 );%0A                if (databaseURL != null) %7B%0A                    // Heroku container.%0A                    URI dbURI = URI.create( databaseURL );%0A%0A                    // Determine username and password.%0A                    Iterator%3CString%3E userInfoIt = Splitter.on( ':' ).split( dbURI.getUserInfo() ).iterator();%0A                    String username = userInfoIt.next();%0A                    String password = userInfoIt.next();%0A%0A                    // Determine JDBC connection URL.%0A                    String scheme = dbURI.getScheme();%0A                    Preconditions.checkState( %22postgres%22.equals( scheme ), %22Unsupported database provider: %25s%22, scheme );%0A                    String url = String.format( %22jdbc:postgresql://%25s:%25d%25s%22, dbURI.getHost(), dbURI.getPort(), dbURI.getPath() );%0A%0A                    // Build JPA connection properties.%0A                    ImmutableMap.Builder%3CString, String%3E properties = ImmutableMap.builder();%0A                    properties.put( %22javax.persistence.jdbc.url%22, url );%0A                    properties.put( %22javax.persistence.jdbc.user%22, username );%0A                    properties.put( %22javax.persistence.jdbc.password%22, password );%0A                    properties.put( %22javax.persistence.jdbc.driver%22, %22org.postgresql.Driver%22 );%0A                    properties.put( %22hibernate.dialect%22, %22org.hibernate.dialect.PostgreSQLDialect%22 );%0A                    properties.put( %22hibernate.hbm2ddl.auto%22, %22update%22 );%0A                    persistence = new Persist( Persistence.createEntityManagerFactory( Persist.DEFAULT_UNIT, properties.build() ) );%0A                %7D else%0A                    persistence = new Persist();%0A
             
@@ -3888,16 +3888,20 @@
 through(
+ new
  Persist
@@ -3906,27 +3906,95 @@
 stFilter
-.class
+( persistence ) );%0A%0A                logger.dbg( %22Configuring API resources%22
  );%0A
-%0A
         

Support Heroku's persistence configuration.