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

org.glassfish.grizzly.AbstractTransformer Maven / Gradle / Ivy

/*
 * Copyright (c) 2008, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.grizzly;

import org.glassfish.grizzly.attributes.Attribute;
import org.glassfish.grizzly.attributes.AttributeBuilder;
import org.glassfish.grizzly.attributes.AttributeStorage;
import org.glassfish.grizzly.memory.MemoryManager;

/**
 *
 * @author Alexey Stashok
 */
public abstract class AbstractTransformer implements Transformer {
    protected final AttributeBuilder attributeBuilder = Grizzly.DEFAULT_ATTRIBUTE_BUILDER;

    protected final Attribute> stateAttr;

    private MemoryManager memoryManager;

    public AbstractTransformer() {
        String namePrefix = getNamePrefix();

        stateAttr = attributeBuilder.createAttribute(namePrefix + ".state");
    }

    protected String getNamePrefix() {
        return getClass().getName();
    }

    @Override
    public final TransformationResult transform(AttributeStorage storage, K input) throws TransformationException {
        return saveLastResult(storage, transformImpl(storage, input));
    }

    protected abstract TransformationResult transformImpl(AttributeStorage storage, K input) throws TransformationException;

    @Override
    public final TransformationResult getLastResult(final AttributeStorage storage) {
        final LastResultAwareState state = stateAttr.get(storage);
        if (state != null) {
            return state.getLastResult();
        }

        return null;
    }

    protected final TransformationResult saveLastResult(final AttributeStorage storage, final TransformationResult result) {
        obtainStateObject(storage).setLastResult(result);
        return result;
    }

    @Override
    public void release(AttributeStorage storage) {
        stateAttr.remove(storage);
    }

    protected MemoryManager obtainMemoryManager(AttributeStorage storage) {
        if (memoryManager != null) {
            return memoryManager;
        }

        if (storage instanceof Connection) {
            Connection connection = (Connection) storage;
            return connection.getMemoryManager();
        }

        return MemoryManager.DEFAULT_MEMORY_MANAGER;
    }

    public MemoryManager getMemoryManager() {
        return memoryManager;
    }

    public void setMemoryManager(MemoryManager memoryManager) {
        this.memoryManager = memoryManager;
    }

    public static  T getValue(final AttributeStorage storage, final Attribute attribute, final T defaultValue) {
        final T value = attribute.get(storage);
        if (value != null) {
            return value;
        }

        return defaultValue;
    }

    protected final LastResultAwareState obtainStateObject(final AttributeStorage storage) {

        LastResultAwareState value = stateAttr.get(storage);
        if (value == null) {
            value = createStateObject();
            stateAttr.set(storage, value);
        }

        return value;
    }

    protected LastResultAwareState createStateObject() {
        return new LastResultAwareState<>();
    }

    public static class LastResultAwareState {
        protected TransformationResult lastResult;

        public TransformationResult getLastResult() {
            return lastResult;
        }

        public void setLastResult(TransformationResult lastResult) {
            this.lastResult = lastResult;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy