001: package net.sourceforge.squirrel_sql.plugins.sessionscript;
002:
003: /*
004: * Copyright (C) 2002 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024:
025: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
026: import net.sourceforge.squirrel_sql.fw.util.DuplicateObjectException;
027: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
028: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
029: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
030: import net.sourceforge.squirrel_sql.fw.xml.XMLObjectCache;
031:
032: /**
033: * XML cache of SQL scripts.
034: *
035: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
036: */
037: public class AliasScriptCache {
038: /** Logger for this class. */
039: private static ILogger s_log = LoggerController
040: .createLogger(AliasScriptCache.class);
041:
042: /** Current plugin. */
043: private SessionScriptPlugin _plugin;
044:
045: /** Cache that contains data. */
046: private XMLObjectCache<AliasScript> _cache = new XMLObjectCache<AliasScript>();
047:
048: /** File name to save scripts to. */
049: private String _scriptsFileName;
050:
051: /**
052: * Ctor. Loads scripts from the XML document.
053: *
054: * @param sqlScriptPlugin Current plugin.
055: *
056: * @throws IllegalArgumentException
057: * Thrown if <TT>null</TT> <TT>SQLScriptPlugin</TT> passed.
058: */
059: public AliasScriptCache(SessionScriptPlugin plugin)
060: throws IOException {
061: super ();
062: if (plugin == null) {
063: throw new IllegalArgumentException(
064: "Null SessionScriptPlugin passed");
065: }
066:
067: _plugin = plugin;
068:
069: _scriptsFileName = _plugin.getPluginUserSettingsFolder()
070: .getAbsolutePath()
071: + File.separator + "session-scripts.xml";
072: }
073:
074: /**
075: * Return the <TT>AliasScript</TT> for the passed <TT>ISQLAlias</TT>.
076: *
077: * @param alias <TT>SQLALias</TT> to retrieve collection of scripts for.
078: *
079: * @throws IllegalArgumentException Thrown if null<TT>ISQLAlias</TT> passed.
080: *
081: * @throws InternalError Thrown if we try to add a script for an alias
082: * and one already exists. Programming error.
083: */
084: public synchronized AliasScript get(ISQLAlias alias) {
085: if (alias == null) {
086: throw new IllegalArgumentException("ISQLALias == null");
087: }
088:
089: AliasScript script = (AliasScript) _cache.get(
090: AliasScript.class, alias.getIdentifier());
091: if (script == null) {
092: script = new AliasScript(alias);
093: try {
094: _cache.add(script);
095: } catch (DuplicateObjectException ex) {
096: // This should never happen as we check above for the duplicate.
097: throw new InternalError(ex.getMessage());
098: }
099: }
100: return script;
101: }
102:
103: /**
104: * Load scripts from XML document.
105: */
106: public synchronized void load() {
107: try {
108: _cache.load(_scriptsFileName, getClass().getClassLoader());
109: } catch (FileNotFoundException ignore) { // first time user has run pgm.
110: } catch (XMLException ex) {
111: String msg = "Error loading scripts file: "
112: + _scriptsFileName;
113: s_log.error(msg, ex);
114: _plugin.getApplication().showErrorDialog(msg, ex);
115: } catch (DuplicateObjectException ex) {
116: String msg = "Error loading scripts file: "
117: + _scriptsFileName;
118: s_log.error(msg, ex);
119: _plugin.getApplication().showErrorDialog(msg, ex);
120: }
121: }
122:
123: /**
124: * Save scripts.
125: */
126: public synchronized void save() {
127: try {
128: _cache.save(_scriptsFileName);
129: } catch (IOException ex) {
130: String msg = "Error occured saving scripts to "
131: + _scriptsFileName;
132: s_log.error(msg, ex);
133: _plugin.getApplication().showErrorDialog(msg, ex);
134: } catch (XMLException ex) {
135: String msg = "Error occured saving scripts to "
136: + _scriptsFileName;
137: s_log.error(msg, ex);
138: _plugin.getApplication().showErrorDialog(msg, ex);
139: }
140: }
141: }
|