All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neogroup.warp.Warp Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package org.neogroup.warp;

import org.neogroup.warp.controllers.Controllers;
import org.neogroup.warp.data.*;
import org.neogroup.warp.messages.Messages;
import org.neogroup.warp.properties.Properties;
import org.neogroup.warp.resources.Resource;
import org.neogroup.warp.resources.ResourceProxy;
import org.neogroup.warp.resources.Resources;
import org.neogroup.warp.views.View;
import org.neogroup.warp.views.ViewFactory;
import org.neogroup.warp.views.Views;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public abstract class Warp {

    private static final String DEFAULT_LOGGER_NAME = "Warp";

    private static final Map contexts;

    static {
        contexts = new HashMap<>();
    }

    public static void registerController(String basePath, Class controllerClass) {
        Controllers.registerController(basePath, controllerClass);
    }

    public static void registerViewFactory(String viewFactoryName, Class viewFactoryClass) {
        Views.registerViewFactory(viewFactoryName, viewFactoryClass);
    }

    public static void registerDataSource(String dataSourceName, Class dataSourceClass) {
        DataSources.registerDataSource(dataSourceName, dataSourceClass);
    }

    public static void registerResource(String resourceName, Class resourceClass) {
        Resources.register(resourceName, resourceClass);
    }

    public static  R getProperty(String property) {
        return (R) Properties.get(property);
    }

    public static boolean hasProperty(String property) {
        return Properties.has(property);
    }

    public static void setProperty(String property, Object value) {
        Properties.set(property, value);
    }

    public static Logger getLogger() {
        return getLogger(DEFAULT_LOGGER_NAME);
    }

    public static Logger getLogger(String name) {
        return LoggerFactory.getLogger(name);
    }

    public static Logger getLogger(Class clazz) {
        return LoggerFactory.getLogger(clazz);
    }

    public static String getMessage(String key, Object... args) {
        return Messages.get(key, args);
    }

    public static String getMessage(String bundleName, String key, Object... args) {
        return Messages.get(bundleName, key, args);
    }

    public static String getMessage(Locale locale, String key, Object... args) {
        return Messages.get(locale, key, args);
    }

    public static String getMessage(String bundleName, Locale locale, String key, Object... args) {
        return Messages.get(bundleName, locale, key, args);
    }

    public static void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
        long threadId = Thread.currentThread().getId();
        try {
            Request request = new Request(servletRequest);
            Response response = new Response(servletResponse);
            WarpContext context = new WarpContext(request, response);
            contexts.put(threadId, context);
            Controllers.handle(context);
        }
        finally {
            WarpContext context = contexts.remove(threadId);
            try { context.release(); } catch (Exception ex) {}
        }
    }

    public static  C getController(Class controllerClass) {
        return Controllers.get(controllerClass);
    }

    public static  ResourceProxy getResource(Class modelClass) {
        return Resources.get(modelClass);
    }

    public static ResourceProxy getResource(String resourceName) {
        return Resources.get(resourceName);
    }

    public static  F getViewFactory(Class viewFactoryClass) {
        return Views.getViewFactory(viewFactoryClass);
    }

    public static  F getViewFactory(String name) {
        return Views.getViewFactory(name);
    }

    public static  V createView(String name) {
        return Views.createView(name);
    }

    public static  V createView(String viewFactoryName, String viewName) {
        return Views.createView(viewFactoryName, viewName);
    }

    public static  V createView(String name, Map viewParameters) {
        return Views.createView(name, viewParameters);
    }

    public static  V createView(String viewFactoryName, String viewName, Map viewParameters) {
        return Views.createView(viewFactoryName, viewName, viewParameters);
    }

    public static WarpContext getContext() {
        return contexts.get(Thread.currentThread().getId());
    }

    public static Request getRequest() {
        return getContext().getRequest();
    }

    public static Response getResponse() {
        return getContext().getResponse();
    }

    public static void stopRouting () {
        getContext().stopRouting();
    }

    public static DataConnection getConnection() {
        return getContext().getConnection();
    }

    public static DataConnection getConnection(String dataSourceName) {
        return getContext().getConnection(dataSourceName);
    }

    public static DataTable getTable (String table) {
        return getConnection().getTable(table);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy