001: /*
002: * Copyright (C) 2007 Jared Alexander Spigner
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * jspigner@openjx.org
019: *
020: * Plugin.java
021: *
022: * Created on June 8, 2007, 12:12 AM
023: *
024: */
025:
026: package org.openjx.conf;
027:
028: import java.io.BufferedInputStream;
029: import java.io.IOException;
030:
031: import java.net.MalformedURLException;
032: import java.net.URL;
033:
034: import java.util.Enumeration;
035: import java.util.Properties;
036:
037: import javax.swing.JOptionPane;
038:
039: import org.openjx.display.JXMessageBox;
040:
041: import org.openjx.core.VirtualMachine;
042:
043: /**
044: * This class is used to hold data about a given plugin.
045: *
046: * @author Jared Spigner
047: */
048: public class Plugin {
049: /** This is the url of the plugin. */
050: private String pluginURL;
051:
052: /** This is the complete url to the jar file which contains the plugin. */
053: private String pluginJar;
054:
055: /** This is the name of the plugin. */
056: private String pluginName;
057:
058: /** This is the name of the plugin schema. */
059: private String pluginSchema;
060:
061: /**
062: * True if this plugin is a control, false if it is simply an addition to
063: * the classpath.
064: */
065: private boolean pluginControl;
066:
067: /**
068: * This is the constructor for the Plugin class. It creates a new
069: * instance of Plugin.
070: */
071: public Plugin() {
072: this .setPluginJar("");
073: this .setPluginURL("");
074: this .setPluginName("");
075: this .setPluginSchema("");
076: this .setPluginControl(false);
077: }
078:
079: /**
080: * This is the constructor for the Plugin class. It creates a new
081: * instance of Plugin.
082: *
083: * @param jar is the jar we want to set.
084: * @param name is the name of the plugin.
085: */
086: public Plugin(String jar, String name) {
087: this .setPluginJar(jar);
088: this .setPluginURL(jar.substring(0, jar.length()
089: - (name.length() + 4)));
090: this .setPluginName(name);
091: }
092:
093: /**
094: * This method configures the plugin, by searching for a configuration file
095: * and applying the attributes.
096: *
097: * @param vm is a reference to the Virtual Machine.
098: *
099: * @return true on success, else false on failure.
100: */
101: public boolean configurePlugin(VirtualMachine vm) {
102: BufferedInputStream bis = null;
103:
104: try {
105: /** This means it is just a classpath enhancement. */
106: if (getClass().getResourceAsStream(
107: "/" + this .getPluginName() + ".properties") == null)
108: return true;
109:
110: Properties prop = new Properties();
111: prop.load(getClass().getResourceAsStream(
112: "/" + this .getPluginName() + ".properties"));
113:
114: String value = prop.getProperty("schema", "");
115:
116: if (value.equals("")) {
117: this .setPluginControl(false);
118: return true;
119: }
120:
121: this .setPluginControl(true);
122:
123: this .setPluginSchema(value);
124: } catch (IOException e) {
125: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
126: "Configure Plugin Failure", "" + e, vm.getViewer());
127: return false;
128: }
129:
130: return true;
131: }
132:
133: /**
134: * This method gets the plugin control value.
135: *
136: * @return true if it is a control, else false.
137: */
138: public boolean getPluginControl() {
139: return this .pluginControl;
140: }
141:
142: /**
143: * This method gets the plugin jar file.
144: *
145: * @return the complete url to the plugin jar file.
146: */
147: public String getPluginJar() {
148: return this .pluginJar;
149: }
150:
151: /**
152: * This method gets the plugin name.
153: *
154: * @return the plugin name.
155: */
156: public String getPluginName() {
157: return this .pluginName;
158: }
159:
160: /**
161: * This method gets the plugin schema.
162: *
163: * @return the plugin schema.
164: */
165: public String getPluginSchema() {
166: return this .pluginSchema;
167: }
168:
169: /**
170: * This method gets the plugin url.
171: *
172: * @return the plugin url.
173: */
174: public String getPluginURL() {
175: return this .pluginURL;
176: }
177:
178: /**
179: * This method installs this plugin into the classpath of the OpenJX
180: * application.
181: *
182: * @param vm is a reference to the Virtual Machine.
183: *
184: * @return true on success, else false on failure.
185: */
186: public boolean installPlugin(VirtualMachine vm) {
187: try {
188: vm.jxPluginLoader.addURL(new URL(this .getPluginJar()), vm
189: .getViewer());
190: } catch (MalformedURLException e) {
191: new JXMessageBox(JOptionPane.ERROR_MESSAGE,
192: "Plugin Failure", "" + e, vm.getViewer());
193: return false;
194: }
195:
196: return true;
197: }
198:
199: /**
200: * This method sets the plugin control state.
201: *
202: * @param state of the plugin control (true/control, false/no control).
203: */
204: public void setPluginControl(boolean state) {
205: this .pluginControl = state;
206: }
207:
208: /**
209: * This method sets the plugin jar file.
210: *
211: * @param jar is the complete url to the plugin jar file.
212: */
213: public void setPluginJar(String jar) {
214: this .pluginJar = jar;
215: }
216:
217: /**
218: * This method sets the plugin name.
219: *
220: * @param name is the name of the plugin.
221: */
222: public void setPluginName(String name) {
223: this .pluginName = name;
224: }
225:
226: /**
227: * This method sets the plugin schema.
228: *
229: * @param schema is the name of the schema.
230: */
231: public void setPluginSchema(String schema) {
232: this .pluginSchema = schema;
233: }
234:
235: /**
236: * This method sets the plugin url.
237: *
238: * @param url is the url of the plugin.
239: */
240: public void setPluginURL(String url) {
241: this.pluginURL = url;
242: }
243: }
|