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

net.sourceforge.squirrel_sql.plugins.sessionscript.AliasScriptCache Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
package net.sourceforge.squirrel_sql.plugins.sessionscript;
/*
 * Copyright (C) 2002 Colin Bell
 * [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
import net.sourceforge.squirrel_sql.fw.util.DuplicateObjectException;
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
import net.sourceforge.squirrel_sql.fw.xml.XMLException;
import net.sourceforge.squirrel_sql.fw.xml.XMLObjectCache;
/**
 * XML cache of SQL scripts.
 *
 * @author Colin Bell
 */
public class AliasScriptCache
{
	/** Logger for this class. */
	private static ILogger s_log =
		LoggerController.createLogger(AliasScriptCache.class);

	/** Current plugin. */
	private SessionScriptPlugin _plugin;

	/** Cache that contains data. */
	private XMLObjectCache _cache = new XMLObjectCache();

	/** File name to save scripts to. */
	private String _scriptsFileName;

	/**
	 * Ctor. Loads scripts from the XML document.
	 *
	 * @param	sqlScriptPlugin	Current plugin.
	 *
	 * @throws	IllegalArgumentException
	 *			Thrown if null SQLScriptPlugin passed.
	 */
	public AliasScriptCache(SessionScriptPlugin plugin)
		throws IOException
	{
		super();
		if (plugin == null)
		{
			throw new IllegalArgumentException("Null SessionScriptPlugin passed");
		}

		_plugin = plugin;

		_scriptsFileName = _plugin.getPluginUserSettingsFolder().getAbsolutePath()
							+ File.separator + "session-scripts.xml";
	}

	/**
	 * Return the AliasScript for the passed ISQLAlias.
	 *
	 * @param	alias	SQLALias to retrieve collection of scripts for.
	 *
	 * @throws	IllegalArgumentException	Thrown if nullISQLAlias passed.
	 *
	 * @throws	InternalError	Thrown if we try to add a script for an alias
	 * 							and one already exists. Programming error.
	 */
	public synchronized AliasScript get(ISQLAlias alias)
	{
		if (alias == null)
		{
			throw new IllegalArgumentException("ISQLALias == null");
		}

		AliasScript script = (AliasScript)_cache.get(AliasScript.class, alias.getIdentifier());
		if (script == null)
		{
			script = new AliasScript(alias);
			try
			{
				_cache.add(script);
			}
			catch (DuplicateObjectException ex)
			{
				// This should never happen as we check above for the duplicate.
				throw new InternalError(ex.getMessage());
			}
		}
		return script;
	}

	/**
	 * Load scripts from XML document.
	 */
	public synchronized void load()
	{
		try
		{
			_cache.load(_scriptsFileName, getClass().getClassLoader());
		}
		catch (FileNotFoundException ignore)
		{ // first time user has run pgm.
		}
		catch (XMLException ex)
		{
			String msg = "Error loading scripts file: " + _scriptsFileName;
			s_log.error(msg, ex);
			_plugin.getApplication().showErrorDialog(msg, ex);
		}
		catch (DuplicateObjectException ex)
		{
			String msg = "Error loading scripts file: " + _scriptsFileName;
			s_log.error(msg, ex);
			_plugin.getApplication().showErrorDialog(msg, ex);
		}
	}

	/**
	 * Save scripts.
	 */
	public synchronized void save()
	{
		try
		{
			_cache.save(_scriptsFileName);
		}
		catch (IOException ex)
		{
			String msg = "Error occured saving scripts to " + _scriptsFileName;
			s_log.error(msg, ex);
			_plugin.getApplication().showErrorDialog(msg, ex);
		}
		catch (XMLException ex)
		{
			String msg = "Error occured saving scripts to " + _scriptsFileName;
			s_log.error(msg, ex);
			_plugin.getApplication().showErrorDialog(msg, ex);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy