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

com.tcdng.unify.core.RequestContextManagerImpl Maven / Gradle / Ivy

There is a newer version: 2.1.64
Show newest version
/*
 * Copyright 2018-2020 The Code Department.
 * 
 * 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.tcdng.unify.core;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.TimeZone;

import com.tcdng.unify.core.annotation.Component;
import com.tcdng.unify.core.constant.UserPlatform;
import com.tcdng.unify.core.util.ApplicationUtils;

/**
 * Default implementation of request context manager.
 * 
 * @author Lateef Ojulari
 * @since 1.0
 */
@Component(ApplicationComponents.APPLICATION_REQUESTCONTEXTMANAGER)
public class RequestContextManagerImpl extends AbstractUnifyComponent implements RequestContextManager {

    private static Locale applicationLocale;

    private static TimeZone applicationTimeZone;

    private static final ThreadLocal requestContextThreadLocal =
            new ThreadLocal() {

                @Override
                protected ThreadRequestContextInfo initialValue() {
                    return new ThreadRequestContextInfo(newDefaultContext());
                }
            };

    @Override
    public RequestContext getRequestContext() {
        RequestContext requestContext = requestContextThreadLocal.get().getRequestContext();
        return requestContext != null ? requestContext : requestContextThreadLocal.get().getDefaultRequestContext();
    }

    @Override
    public void loadRequestContext(UserSession userSession, String requestPath) throws UnifyException {
        requestContextThreadLocal.get().setRequestContext(
                new RequestContext(requestPath != null ? requestPath : "", userSession.getSessionContext()));
    }

    @Override
    public void loadRequestContext(RequestContext requestContext) throws UnifyException {
        requestContextThreadLocal.get().setRequestContext(requestContext);
    }

    @Override
    public void unloadRequestContext() {
        RequestContext requestContext = requestContextThreadLocal.get().getRequestContext();
        if (requestContext != null) {
            requestContext.removeAllAttributes();
        }

        requestContextThreadLocal.remove();
    }

    @Override
    public void reset() throws UnifyException {

    }

    @Override
    protected void onInitialize() throws UnifyException {
        applicationLocale = getApplicationLocale();
        applicationTimeZone = getApplicationTimeZone();
    }

    @Override
    protected void onTerminate() throws UnifyException {

    }

    private static RequestContext newDefaultContext() {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            return new RequestContext(null,
                    new SessionContext(ApplicationUtils.generateSessionContextId(), applicationLocale,
                            applicationTimeZone, "http://localhost", "/default", inetAddress.getHostName(),
                            inetAddress.getHostAddress(), null, UserPlatform.DEFAULT));
        } catch (UnknownHostException e) {
        }
        return null;
    }

    private static class ThreadRequestContextInfo {

        private RequestContext requestContext;

        private RequestContext defaultRequestContext;

        public ThreadRequestContextInfo(RequestContext defaultRequestContext) {
            this.defaultRequestContext = defaultRequestContext;
        }

        public RequestContext getRequestContext() {
            return requestContext;
        }

        public void setRequestContext(RequestContext requestContext) {
            this.requestContext = requestContext;
        }

        public RequestContext getDefaultRequestContext() {
            return defaultRequestContext;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy