Package com.alphacsp.cit

Source Code of com.alphacsp.cit.DefaultInterperterFactoryBean

/*
* Copyright 2006 the original author or authors.
*
* 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.alphacsp.cit;

import com.alphacsp.cit.commands.CommandsTemplate;
import com.alphacsp.cit.commands.XmlCommandsReader;
import com.alphacsp.cit.validators.ValidationException;
import com.alphacsp.common.io.DefaultResource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
* @author Yoav Hakman
*/
public class DefaultInterperterFactoryBean extends AbstractFactoryBean implements InterperterFactory {

    private static final Log LOG = LogFactory.getLog(DefaultInterperterFactoryBean.class);

    private String environmentFileLocation;
    private String environmentTemplateFileLocation;
    private String commandsFileLocation;
    private UserInterface userInterface;

    public String getEnvironmentFileLocation() {
        return environmentFileLocation;
    }

    public void setEnvironmentFileLocation(String environmentFileLocation) {
        this.environmentFileLocation = environmentFileLocation;
    }

    public String getEnvironmentTemplateFileLocation() {
        return environmentTemplateFileLocation;
    }

    public void setEnvironmentTemplateFileLocation(String environmentTemplateFileLocation) {
        this.environmentTemplateFileLocation = environmentTemplateFileLocation;
    }

    public String getCommandsFileLocation() {
        return commandsFileLocation;
    }

    public void setCommandsFileLocation(String commandsFileLocation) {
        this.commandsFileLocation = commandsFileLocation;
    }

    public UserInterface getUserInterface() {
        return userInterface;
    }

    public void setUserInterface(UserInterface userInterface) {
        this.userInterface = userInterface;
    }

    public Interperter createInterperter() throws ValidationException {
        return (Interperter) createInstance();
    }

    protected Object createInstance()  {
        Assert.notNull(environmentTemplateFileLocation);
        Assert.notNull(environmentFileLocation);
        Assert.notNull(commandsFileLocation);
        Assert.notNull(userInterface);

        Interperter interperter = new DefaultInterperter();
        interperter.setUserInterface(userInterface);

        ClassLoader classLoader = ClassUtils.getDefaultClassLoader();

        XmlEnvironmentTemplateReader environmentTemplateReader = new XmlEnvironmentTemplateReader();
        environmentTemplateReader.setClassLoader(classLoader);
        environmentTemplateReader.setXmlFile(new DefaultResource(environmentTemplateFileLocation));
        EnvironmentTemplate environmentTemplate = environmentTemplateReader.readObject();

        PropertiesFileEnvironmentFactoryBean environmentFactory = new PropertiesFileEnvironmentFactoryBean(environmentTemplate);
        environmentFactory.setEnvironmentFile(new DefaultResource(environmentFileLocation));
        Environment environment = environmentFactory.createEnvironment();
        interperter.setEnvironment(environment);

        XmlCommandsReader commandsReader = new XmlCommandsReader();
        commandsReader.setClassLoader(classLoader);
        commandsReader.setXmlFile(new DefaultResource(commandsFileLocation));
        CommandsTemplate groups = commandsReader.readObject();
        interperter.setCommandsTemplate(groups);

        return interperter;
    }

    public Class getObjectType() {
        return Interperter.class;
    }
}
TOP

Related Classes of com.alphacsp.cit.DefaultInterperterFactoryBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.