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

com.hurence.logisland.processor.useragent.PooledUserAgentAnalyzerFactory Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2016 Hurence ([email protected])
 *
 * 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.hurence.logisland.processor.useragent;

import nl.basjes.parse.useragent.UserAgentAnalyzer;
import nl.basjes.parse.useragent.UserAgentAnalyzer.Builder;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import java.util.List;


/**
 * Created by mathieu on 18/05/17.
 */
public class PooledUserAgentAnalyzerFactory extends BasePooledObjectFactory {

    private boolean useCache = false;
    private int cacheSize = -1;
    private List selectedFields;

    public PooledUserAgentAnalyzerFactory(List selectedFields) {
        this.selectedFields = selectedFields;
    }

    public PooledUserAgentAnalyzerFactory(List selectedFields, int cacheSize) {
        this.selectedFields = selectedFields;
        this.useCache = true;
        this.cacheSize = cacheSize;
    }

    @Override
    public UserAgentAnalyzer create() throws Exception {

        Builder builder = UserAgentAnalyzer.newBuilder();
        if (useCache) {
            builder.withCache(cacheSize);
        } else {
            builder.withoutCache();
        }
        builder.withFields(selectedFields);
        return builder.build();
    }

    @Override
    public PooledObject wrap(UserAgentAnalyzer uaapo) {
        return new DefaultPooledObject(uaapo);
    }

    @Override
    public void passivateObject(PooledObject uaapo) throws Exception {
        // does nothing
    }

    @Override
    public boolean validateObject(PooledObject uaapo) {
        // Does no check
        return true;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy