Search in sources :

Example 1 with DatastoreDao

use of com.example.getstarted.daos.DatastoreDao in project getting-started-java by GoogleCloudPlatform.

the class ListBookServlet method init.

@Override
public void init() throws ServletException {
    BookDao dao = null;
    CloudStorageHelper storageHelper = new CloudStorageHelper();
    // Creates the DAO based on the Context Parameters
    String storageType = this.getServletContext().getInitParameter("bookshelf.storageType");
    switch(storageType) {
        case "datastore":
            dao = new DatastoreDao();
            break;
        case "cloudsql":
            try {
                // Use this url when using dev appserver, but connecting to Cloud SQL
                String connect = this.getServletContext().getInitParameter("sql.urlRemote");
                if (connect.contains("localhost")) {
                    // Use this url when using a local mysql server
                    connect = this.getServletContext().getInitParameter("sql.urlLocal");
                } else if (System.getProperty("com.google.appengine.runtime.version").startsWith("Google App Engine/")) {
                    // Use this url when on App Engine, connecting to Cloud SQL.
                    // Uses a special adapter because of the App Engine sandbox.
                    connect = this.getServletContext().getInitParameter("sql.urlRemoteGAE");
                }
                dao = new CloudSqlDao(connect);
            } catch (SQLException e) {
                throw new ServletException("SQL error", e);
            }
            break;
        default:
            throw new IllegalStateException("Invalid storage type. Check if bookshelf.storageType property is set.");
    }
    this.getServletContext().setAttribute("dao", dao);
    this.getServletContext().setAttribute("storageHelper", storageHelper);
    this.getServletContext().setAttribute(// Hide upload when Cloud Storage is not configured.
    "isCloudStorageConfigured", !Strings.isNullOrEmpty(getServletContext().getInitParameter("bookshelf.bucket")));
}
Also used : CloudStorageHelper(com.example.getstarted.util.CloudStorageHelper) ServletException(javax.servlet.ServletException) CloudSqlDao(com.example.getstarted.daos.CloudSqlDao) SQLException(java.sql.SQLException) BookDao(com.example.getstarted.daos.BookDao) DatastoreDao(com.example.getstarted.daos.DatastoreDao)

Aggregations

BookDao (com.example.getstarted.daos.BookDao)1 CloudSqlDao (com.example.getstarted.daos.CloudSqlDao)1 DatastoreDao (com.example.getstarted.daos.DatastoreDao)1 CloudStorageHelper (com.example.getstarted.util.CloudStorageHelper)1 SQLException (java.sql.SQLException)1 ServletException (javax.servlet.ServletException)1