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

org.jboss.modules.ClassifyingModuleLoader Maven / Gradle / Ivy

There is a newer version: 2.1.5.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.jboss.modules;

import java.util.HashMap;
import java.util.Map;

/**
 * A module loader which selects a delegate module loader based upon the prefix of the module name.  Longer
 * names are matched first always.
 *
 * @author David M. Lloyd
 */
public final class ClassifyingModuleLoader extends ModuleLoader {
    private volatile Map delegates;
    private final ModuleLoader defaultLoader;
    private final String name;

    /**
     * Construct a new instance.  The given delegates map is copied.
     *
     * @param delegates the default delegates map to use
     * @param defaultLoader the default loader to use if no delegate mapping exists
     */
    public ClassifyingModuleLoader(final String name, final Map delegates, final ModuleLoader defaultLoader) {
        super(true, false);
        this.defaultLoader = defaultLoader;
        this.delegates = new HashMap(delegates);
        this.name = name;
    }

    /** {@inheritDoc} */
    protected Module preloadModule(String name) throws ModuleLoadException {
        int idx;
        final Map delegates = this.delegates;
        for (;;) {
            final ModuleLoader loader = delegates.get(name);
            if (loader != null) {
                return preloadModule(name, loader);
            }
            idx = name.lastIndexOf('.');
            if (idx == -1) {
                return preloadModule(name, defaultLoader);
            }
            name = name.substring(0, idx);
        }
    }

    /** {@inheritDoc} */
    protected ModuleSpec findModule(final String name) throws ModuleLoadException {
        // We have no modules of our own!
        return null;
    }

    /**
     * Change the delegates map.  A copy is made of the given map.
     *
     * @param delegates the new delegates map to use
     */
    public void setDelegates(Map delegates) {
        this.delegates = new HashMap(delegates);
    }

    public String toString() {
        return String.format("Classifying Module Loader @%x \"%s\"", Integer.valueOf(hashCode()), name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy