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

com.celum.dbplugin.resource.ClasspathResource Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
/*****************************************************************************
 * 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.dbplugin.resource;

import com.celum.dbplugin.installer.SqlScript;
import com.celum.dbplugin.installer.SqlScriptsResource;
import com.celum.dbplugin.installer.StringSqlScript;
import org.apache.maven.wagon.util.IoUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * provides access to SQL scripts on classpath.
 *
 * @author Zdenko Vrabel ([email protected])
 */
public class ClasspathResource extends SqlScriptsResource
{
	//-------------------------------------------------------------------------------------------------------------------
	// members
	//-------------------------------------------------------------------------------------------------------------------

	private final String[] paths;
	private final ClassLoader classLoader;


	//-------------------------------------------------------------------------------------------------------------------
	// constructors & methods
	//-------------------------------------------------------------------------------------------------------------------

	/**
	 * Constructor
	 * @param paths
	 */
	public ClasspathResource(String... paths)
	{
		this(ClasspathResource.class.getClassLoader(), paths);
	}


	/**
	 * Constructor
	 *
	 * @param classLoader
	 * @param paths
	 */
	public ClasspathResource(ClassLoader classLoader, String... paths)
	{
		this.paths = paths;
		this.classLoader = classLoader;
	}


	/**
	 * read all resources from classpath and returns sql scripts as a
	 * sorted collection
	 * @return
	 */
	@Override
	public Collection getSortedScripts()
	{
		try {
			List sqlScripts = new LinkedList();

			for (int i = 0; i < paths.length; ++i) {
				InputStream is = classLoader.getResourceAsStream(paths[i]);
				String script = IoUtils.toString(is);
				sqlScripts.add(new StringSqlScript(paths[i], script));
			}

			Collections.sort(sqlScripts);
			return sqlScripts;
		} catch (IOException e) {
			throw new IllegalStateException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy