de.fhg.iais.cortex.guice.abstractModules.AbstractCortexServerModule.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.cortex.guice.abstractModules.AbstractCortexServerModule.java

Source

package de.fhg.iais.cortex.guice.abstractModules;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * 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.                                             *
 ******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import de.fhg.iais.commons.configuration.Home;
import de.fhg.iais.commons.dbc.DbcException;
import de.fhg.iais.cortex.rest.server.ICortexServer;

public abstract class AbstractCortexServerModule extends AbstractCortexModule {
    public final static int DEFAULT_PRIORITY = 100;

    private Class<? extends ICortexServer> implClass;

    private final List<String> moduleNameOrder;

    @SuppressWarnings("unused")
    private AbstractCortexServerModule() {
        this.moduleNameOrder = Lists.newArrayList();
    }

    public AbstractCortexServerModule(Home home) {
        FileInputStream fis = null;
        this.moduleNameOrder = Lists.newArrayList();
        File startServersConfig = home.getResourceLocationAsFile("startServers.txt");
        try {
            fis = new FileInputStream(startServersConfig);

            for (String line : IOUtils.readLines(fis)) {
                if (Strings.isNullOrEmpty(line) || line.startsWith("#")) {
                    continue;
                }
                this.moduleNameOrder.add(line);
            }
        } catch (FileNotFoundException e) {
            throw new DbcException("Unable to read configuration at " + startServersConfig.getAbsolutePath() + ".",
                    e);
        } catch (IOException e) {
            throw new DbcException("Unable to read configuration at " + startServersConfig.getAbsolutePath() + ".",
                    e);
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }

    protected void bindCortexServer(Class<? extends ICortexServer> implClass) {
        this.implClass = implClass;
        bindSingleton(implClass);
    }

    public Class<? extends ICortexServer> getServerClass() {
        return this.implClass;
    }

    public int getPriority() {
        for (int i = 0; i < this.moduleNameOrder.size(); i++) {
            if (this.getClass().getSimpleName().startsWith(this.moduleNameOrder.get(i))) {
                return i;
            }
        }
        return DEFAULT_PRIORITY;
    }
}