Package com.celum.dbtool.mojo

Source Code of com.celum.dbtool.mojo.AbstractDbMojo

/*****************************************************************************
* Copyright 2012 celum Slovakia s r.o.
*
* 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.celum.dbtool.mojo;

import com.celum.dbtool.installer.*;
import com.celum.dbtool.step.DbStep;
import com.celum.dbtool.step.Version;
import com.celum.dbtool.step.VersionFactory;
import com.celum.nyx.DataSourceWrapper;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.project.MavenProject;

import javax.sql.DataSource;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* @author Zdenko Vrabel (zdenko.vrabel@celum.com)
*/
public abstract class AbstractDbMojo extends AbstractMojo implements DbEventListener
{
    /**
     * @parameter
     */
    protected String jdbcDriver;

    /**
     * @parameter
     */
    protected String jdbcUrl;

    /**
     * @parameter
     */
    protected String user;

    /**
     * @parameter
     */
    protected String password;

    /**
     * @parameter default-value="PATCH"
     */
    protected String patchTable;

    /**
     * @parameter
     */
    protected Map<String, String> parameters;

    /**
     * @parameter
     */
    private String versionFactoryClass;


    /**
     * @parameter expression="${project}"
     * @required
     */
    private MavenProject project;

    /**
     * @parameter default-value="${project.build.resources}"
     */
    private Resource[] resources;


    /**
     * {@inheritDoc}
     */
  @Override
  public DbStep onProcessScript(DbStep script)
  {
    getLog().info("process step:" + script.getName());
    return script;
  }


    /**
     * {@inheritDoc}
     */
    @Override
    public void onError(DbStep script, Throwable e)
    {
        getLog().error("Error in step:" + script.getName(), e);
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public void onFinishProcessing(DbStep script)
    {
        getLog().info("step:" + script.getName() + " finished OK");
    }


    /**
     * Method register the version factory by 'versionFactoryClass' parameter.
     * The default is VersionFactory.DEFAULT.
     */
    protected void registerVersionFactoryFromSettings() throws ClassNotFoundException, IllegalAccessException, InstantiationException
    {
        if (versionFactoryClass == null || versionFactoryClass.isEmpty()) {
            return;
        }

        Class<?> vfClass = (Class<?>) Class.forName(versionFactoryClass);
        Version.registerVersionFactory( (VersionFactory) vfClass.newInstance() );
    }


    /**
     * returns the datasource
     */
    protected DataSource getDataSource() throws ClassNotFoundException, SQLException
    {
        getLog().info("DB " + this.jdbcUrl + " with " + this.user + " via driver " + jdbcDriver);
        Class.forName(jdbcDriver);
        Connection connection = DriverManager.getConnection(this.jdbcUrl, this.user, this.password);
        return new DataSourceWrapper(connection);
    }


    /**
     * returns classloader which can load scripts from project for you.
     * @return
     */
    protected ClassLoader getClassLoaderWithCompiledClasses()
    {
        try {
            List<URL> urls = new LinkedList<URL>();

            //add compiled classes
            for (Object object : project.getCompileClasspathElements()) {
                String path = (String) object;
                urls.add(new File(path).toURL());
            }

            //add resources
            for (Resource r : resources) {
                urls.add(new URL("file:" + r.getDirectory()));
            }

            ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[] {}), this.getClass().getClassLoader());
            return classLoader;
        } catch (Exception e) {
            getLog().error(e);
            throw new IllegalStateException(e);
        }
    }


    protected Map<String, String> getParameters()
    {
        Map<String, String> params = new HashMap<String, String>();
        params.putAll(this.parameters);
        params.put(DbVars.PATCHTABLE, this.patchTable);
        return params;
    }

}
TOP

Related Classes of com.celum.dbtool.mojo.AbstractDbMojo

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.