001: package net.sourceforge.squirrel_sql.plugins.userscript.kernel;
002:
003: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
004: import net.sourceforge.squirrel_sql.client.session.ISession;
005: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
006: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
007: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
008: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
009: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
010: import net.sourceforge.squirrel_sql.fw.util.StringManager;
011: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
012: import net.sourceforge.squirrel_sql.plugins.userscript.UserScriptPlugin;
013: import net.sourceforge.squirrel_sql.plugins.userscript.FrameWorkAcessor;
014:
015: import javax.swing.*;
016: import java.io.File;
017: import java.lang.reflect.Field;
018: import java.lang.reflect.Method;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import java.net.URLClassLoader;
022: import java.sql.Connection;
023:
024: public class UserScriptAdmin {
025: private static final StringManager s_stringMgr = StringManagerFactory
026: .getStringManager(UserScriptAdmin.class);
027:
028: public static final String SCRIPT_PROPERTIES_FILE = "UserScriptProperties.xml";
029:
030: public static final boolean TARGET_TYPE_DB_OBJECT = false;
031: public static final boolean TARGET_TYPE_SQL = true;
032:
033: private UserScriptPlugin m_plugin;
034: private ISession m_session;
035:
036: public UserScriptAdmin(UserScriptPlugin plugin, ISession session) {
037: try {
038: m_plugin = plugin;
039: m_session = session;
040:
041: ScriptProps props = readScriptProps();
042: if (null != props) {
043: for (int i = 0; i < props.getScripts().length; i++) {
044: if (props.getScripts()[i].isShowInStandard()) {
045:
046: GenericScriptPopupAction actDbObject = new GenericScriptPopupAction(
047: props.getScripts()[i], this ,
048: TARGET_TYPE_DB_OBJECT);
049:
050: //IObjectTreeAPI api = m_session.getObjectTreeAPI(m_plugin);
051: IObjectTreeAPI api = FrameWorkAcessor
052: .getObjectTreeAPI(m_session, m_plugin);
053:
054: api.addToPopup(DatabaseObjectType.TABLE,
055: actDbObject);
056: api.addToPopup(DatabaseObjectType.PROCEDURE,
057: actDbObject);
058: api.addToPopup(DatabaseObjectType.SESSION,
059: actDbObject);
060:
061: GenericScriptPopupAction actSql = new GenericScriptPopupAction(
062: props.getScripts()[i], this ,
063: TARGET_TYPE_SQL);
064:
065: //m_session.getSQLPanelAPI(m_plugin).addToSQLEntryAreaMenu(actSql);
066: FrameWorkAcessor.getSQLPanelAPI(m_session,
067: m_plugin).addToSQLEntryAreaMenu(actSql);
068: }
069: }
070: }
071: initUserScriptClassLoader(props);
072:
073: } catch (Exception e) {
074: throw new RuntimeException(e);
075: }
076: }
077:
078: ScriptProps readScriptProps() {
079: try {
080: if (getScriptPropertiesFile().exists()) {
081: XMLBeanReader br = new XMLBeanReader();
082: br.load(getScriptPropertiesFile(), this .getClass()
083: .getClassLoader());
084: return (ScriptProps) br.iterator().next();
085: } else {
086: return null;
087: }
088: } catch (Exception e) {
089: throw new RuntimeException(e);
090:
091: }
092: }
093:
094: private void initUserScriptClassLoader(ScriptProps props) {
095: try {
096: URL[] cp;
097: if (null == props) {
098: cp = new URL[0];
099: } else {
100: cp = new URL[props.getExtraClassPath().length];
101:
102: for (int i = 0; i < props.getExtraClassPath().length; i++) {
103: String path = props.getExtraClassPath()[i]
104: .getEntry();
105: cp[i] = (new File(path)).toURI().toURL();
106: }
107: }
108: m_plugin.setUserScriptClassLoader(URLClassLoader
109: .newInstance(cp));
110: } catch (MalformedURLException e) {
111: throw new RuntimeException(e);
112: }
113: }
114:
115: private File getScriptPropertiesFile() {
116: try {
117: return new File(m_plugin.getPluginUserSettingsFolder()
118: .getPath()
119: + File.separator + SCRIPT_PROPERTIES_FILE);
120: } catch (Exception e) {
121: throw new RuntimeException(e);
122: }
123: }
124:
125: public ScriptTargetCollection getTargets(boolean targetType) {
126: if (targetType == TARGET_TYPE_DB_OBJECT) {
127: //IObjectTreeAPI api = m_session.getObjectTreeAPI(m_plugin);
128: IObjectTreeAPI api = FrameWorkAcessor.getObjectTreeAPI(
129: m_session, m_plugin);
130:
131: IDatabaseObjectInfo[] dbObjs = api
132: .getSelectedDatabaseObjects();
133:
134: ScriptTargetCollection targets = new ScriptTargetCollection();
135: for (int i = 0; i < dbObjs.length; i++) {
136: if (dbObjs[i].getDatabaseObjectType().equals(
137: DatabaseObjectType.TABLE)) {
138: ITableInfo tableInfo = (ITableInfo) dbObjs[i];
139: if ("VIEW".equals(tableInfo.getType())) {
140: targets.add(new ScriptTarget(dbObjs[i]
141: .getSimpleName(),
142: ScriptTarget.DB_OBJECT_TYPE_VIEW));
143: } else if ("TABLE".equals(tableInfo.getType())
144: || "SYSTEM TABLE".equals(tableInfo
145: .getType())) {
146: targets.add(new ScriptTarget(dbObjs[i]
147: .getSimpleName(),
148: ScriptTarget.DB_OBJECT_TYPE_TABLE));
149: }
150: } else if (dbObjs[i].getDatabaseObjectType().equals(
151: DatabaseObjectType.PROCEDURE)) {
152: targets.add(new ScriptTarget(dbObjs[i]
153: .getSimpleName(),
154: ScriptTarget.DB_OBJECT_TYPE_PROCEDURE));
155: } else if (dbObjs[i].getDatabaseObjectType().equals(
156: DatabaseObjectType.SESSION)) {
157: targets.add(new ScriptTarget(dbObjs[i]
158: .getSimpleName(),
159: ScriptTarget.DB_OBJECT_TYPE_CONNECTION));
160: }
161: }
162: return targets;
163:
164: } else // targetType == TARGET_TYPE_SQL
165: {
166: ScriptTargetCollection targets = new ScriptTargetCollection();
167:
168: //String sql = m_session.getSQLPanelAPI(m_plugin).getSQLScriptToBeExecuted();
169: String sql = FrameWorkAcessor.getSQLPanelAPI(m_session,
170: m_plugin).getSQLScriptToBeExecuted();
171:
172: targets.add(new ScriptTarget(sql,
173: ScriptTarget.DB_OBJECT_TYPE_SQL_STATEMENT));
174: return targets;
175: }
176: }
177:
178: public UserScriptPlugin getPlugin() {
179: return m_plugin;
180: }
181:
182: public ISession getSession() {
183: return m_session;
184: }
185:
186: public void writeScriptProps(ScriptProps scriptProps) {
187: try {
188: XMLBeanWriter bw = new XMLBeanWriter(scriptProps);
189: bw.save(getScriptPropertiesFile());
190: } catch (Exception e) {
191: throw new RuntimeException(e);
192: }
193: }
194:
195: public void refreshExtraClassPath() {
196: initUserScriptClassLoader(readScriptProps());
197: }
198:
199: public void executeScript(JFrame ownerFrame, Script script,
200: ScriptTargetCollection targets) {
201: //ScriptEnvironment env = new ScriptEnvironment(m_session.getSQLPanelAPI(m_plugin), ownerFrame);
202: ScriptEnvironment env = new ScriptEnvironment(FrameWorkAcessor
203: .getSQLPanelAPI(m_session, m_plugin), ownerFrame);
204:
205: try {
206: ClassLoader loader = m_plugin.getUserScriptClassLoader();
207: Class<?> scriptClass = Class.forName(script
208: .getScriptClass(), false, loader);
209: Object scriptInst = scriptClass.newInstance();
210:
211: Field f = scriptInst.getClass().getField("environment");
212: f.set(scriptInst, env);
213: Method m = scriptInst.getClass().getMethod(
214: "execute",
215: new Class[] { String.class, String.class,
216: Connection.class });
217:
218: ScriptTarget[] buf = targets.getAll();
219: for (int i = 0; i < buf.length; i++) {
220: m.invoke(scriptInst, new Object[] {
221: buf[i].getTargetType(), buf[i].getTargetInfo(),
222: m_session.getSQLConnection().getConnection() });
223: env.flushAll();
224: }
225: env.setExecutionFinished(true);
226: } catch (Exception e) {
227: // i18n[userscript.scriptAdminErr=Err Msg]
228: e.printStackTrace(env.createPrintStream(s_stringMgr
229: .getString("userscript.scriptAdminErr")));
230: env.flushAll();
231: env.setExecutionFinished(false);
232: throw new RuntimeException(e);
233: }
234:
235: }
236:
237: }
|