[tuxdroid-svn] r4730 - software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java
Status: Beta
Brought to you by:
ks156
|
From: remi <c2m...@c2...> - 2009-06-03 09:34:07
|
Author: remi
Date: 2009-06-03 11:34:02 +0200 (Wed, 03 Jun 2009)
New Revision: 4730
Modified:
software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePlugin.java
software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePluginConfiguration.java
Log:
* code style
* methods order
Modified: software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePlugin.java
===================================================================
--- software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePlugin.java 2009-06-03 08:58:15 UTC (rev 4729)
+++ software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePlugin.java 2009-06-03 09:34:02 UTC (rev 4730)
@@ -40,9 +40,10 @@
* email)
* @since 3 juin 08
*/
-public abstract class SimplePlugin<CONFIGURATION extends SimplePluginConfiguration> {
+public abstract class SimplePlugin<CONFIGURATION extends SimplePluginConfiguration>
+{
public static final String ENVIRONEMENT_PREFIX = "tgp_";
- /** configuration. */
+ private String command = null;
private CONFIGURATION configuration;
private StdInCom stdInCom = null;
@@ -59,7 +60,8 @@
try
{
SimplePlugin.this.onPluginStop();
- } catch (Throwable e)
+ }
+ catch (Throwable e)
{
SimplePlugin.this.throwError(e);
}
@@ -73,112 +75,160 @@
try
{
SimplePlugin.this.onPluginEvent(eventName, eventValues);
- } catch (Throwable e)
+ }
+ catch (Throwable e)
{
SimplePlugin.this.throwError(e);
}
}
}
+
+ /**
+ * On plugin stop event.
+ * this method should be defined in your plugin class.
+ * @throws Throwable
+ * when something go wrong...
+ */
+ protected abstract void onPluginStop() throws Throwable;
+
+ /**
+ * On plugin event.
+ * this method should be defined in your plugin class.
+ * @throws Throwable
+ * when something go wrong...
+ */
+ protected abstract void onPluginEvent(String eventName, String[] eventValues) throws Throwable;
/**
+ * Stop the plugin.
+ */
+ public void stop()
+ {
+ if (this.stdInCom != null)
+ {
+ this.stdInCom.stopPipe();
+ }
+ try
+ {
+ this.onPluginStop();
+ }
+ catch (Throwable e)
+ {
+ this.throwError(e);
+ }
+ this.throwNotification("plugin", "exit");
+ }
+
+ /**
* @return the configuration
*/
- protected CONFIGURATION configuration() {
+ protected CONFIGURATION configuration()
+ {
return configuration;
}
/**
- * This is the generic method for throwing notifications to the server.
- *
- * @param notificationId
- * ID of the notification (this should match with declared
- * notifications in plugins.xml)
- * @param arguments
- * Notification arguments
+ * @return the command
*/
- protected void throwNotification(String notificationId, String... arguments) {
- StringBuffer buffer = new StringBuffer();
- buffer.append(notificationId);
- for (String argument : arguments) {
- buffer.append(" '");
- buffer.append(argument.replace("'", "\\'"));
- buffer.append("'");
- }
- System.out.println(buffer.toString());
- System.out.flush();
+ protected String getCommand()
+ {
+ return command;
}
- protected boolean isWindows() {
+ /**
+ * @param command
+ * the command to set
+ */
+ protected void setCommand(String command)
+ {
+ this.command = command;
+ }
+
+ /**
+ * Get if the platform is Windows or not.
+ * @return A boolean.
+ */
+ protected boolean isWindows()
+ {
String osName = System.getProperty("os.name").toLowerCase();
return osName.startsWith("windows");
}
-
- private void loadEnvironementData() throws SimplePluginException {
- if (configuration == null) {
+
+ /**
+ * Load the environement data to the plugin parameters.
+ * @throws SimplePluginException
+ */
+ private void loadEnvironementData() throws SimplePluginException
+ {
+ if (configuration == null)
+ {
return;
}
Map<String, String> environement = System.getenv();
throwTrace("Loading environement");
- for (String key : environement.keySet()) {
- if (key.startsWith(ENVIRONEMENT_PREFIX)) {
+ for (String key : environement.keySet())
+ {
+ if (key.startsWith(ENVIRONEMENT_PREFIX))
+ {
String stringValue = environement.get(key);
key = key.substring(ENVIRONEMENT_PREFIX.length());
throwTrace(" " + key + ":" + stringValue);
Field field = null;
Class<?> class1 = configuration.getClass();
- while (class1 != null) {
- try {
+ while (class1 != null)
+ {
+ try
+ {
field = class1.getDeclaredField(key);
- } catch (Exception e) {
}
+ catch (Exception e) {}
class1 = class1.getSuperclass();
}
- if (field == null) {
+ if (field == null)
+ {
continue;
}
field.setAccessible(true);
- try {
- if (field.getType() == String.class) {
+ try
+ {
+ if (field.getType() == String.class)
+ {
field.set(configuration, stringValue);
- } else if (field.getType() == int.class || field.getType() == Integer.class) {
+ }
+ else if (field.getType() == int.class || field.getType() == Integer.class)
+ {
field.set(configuration, Integer.parseInt(stringValue));
- } else if (field.getType() == double.class || field.getType() == Double.class) {
+ }
+ else if (field.getType() == double.class || field.getType() == Double.class)
+ {
field.set(configuration, Double.parseDouble(stringValue));
- } else if (field.getType() == boolean.class || field.getType() == Boolean.class) {
+ }
+ else if (field.getType() == boolean.class || field.getType() == Boolean.class)
+ {
field.set(configuration, Boolean.parseBoolean(stringValue));
- } else if (field.getType().getSuperclass() == Enum.class) {
+ }
+ else if (field.getType().getSuperclass() == Enum.class)
+ {
Enum newValue = Enum.valueOf((Class<Enum>) field.getType(), stringValue);
field.set(configuration, newValue);
- } else {
+ }
+ else
+ {
this.throwError(new SimplePluginException("Unable to find conversion for : " + field.getType()));
continue;
}
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
this.throwError(e);
continue;
}
}
}
}
-
+
/**
- * This is a special notification used to send "speakable" messages to the
- * server.
- *
- * @param content
- * message content
- */
- public void throwMessage(String content, Object... arguments) {
- String[] tmp = new String[arguments.length + 1];
- tmp[0] = content;
- for (int i = 0; i < arguments.length; i++) {
- tmp[i + 1] = String.valueOf(arguments[i]);
- }
- throwNotification("message", tmp);
- }
-
- /**
* This method is used a starting point for the plugin. The standard way to
* use this adding in your inherited plugin class something like this :
*
@@ -193,9 +243,12 @@
* @param configuration
* a new configuration object
*/
- protected void boot(String[] arguments, CONFIGURATION configuration) {
- try {
- if (arguments.length > 0) {
+ protected void boot(String[] arguments, CONFIGURATION configuration)
+ {
+ try
+ {
+ if (arguments.length > 0)
+ {
command = arguments[0];
}
this.configuration = configuration;
@@ -212,29 +265,14 @@
{
onPluginStop();
}
- } catch (Throwable e) {
+ }
+ catch (Throwable e)
+ {
throwError(e);
}
}
-
- private String command = null;
-
+
/**
- * @return the command
- */
- protected String getCommand() {
- return command;
- }
-
- /**
- * @param command
- * the command to set
- */
- protected void setCommand(String command) {
- this.command = command;
- }
-
- /**
* this method should be defined in your plugin class. This handle the main
* function of it. Be careful, if your make a plugin in "command mode" you
* should leave this as soon as possible. If your are making a "service"
@@ -246,47 +284,117 @@
protected abstract void start() throws Throwable;
/**
- * On plugin stop event.
- * this method should be defined in your plugin class.
- * @throws Throwable
- * when something go wrong...
+ * Get the full path of a file located in the "state" directory of a deployed
+ * plugin.
*/
- protected abstract void onPluginStop() throws Throwable;
-
+ protected File getStateFile(String name)
+ {
+ // State files should stay in the working folder of plugins in order
+ // to be deleted when the plugin is updated (re-deployed)
+ File sessionId = new File("states");
+ sessionId.mkdirs();
+ sessionId = new File(sessionId, name);
+ return sessionId;
+ }
+
/**
- * On plugin event.
- * this method should be defined in your plugin class.
- * @throws Throwable
- * when something go wrong...
+ * Get the full path of a file located in the "state" directory of a deployed
+ * plugin.
*/
- protected abstract void onPluginEvent(String eventName, String[] eventValues) throws Throwable;
-
+ protected <E> E readState(Class<E> objectClass, String sessionId)
+ {
+ try
+ {
+ File file = getStateFile(sessionId);
+ E result;
+ if (file.exists())
+ {
+ ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
+ result = (E) inputStream.readObject();
+ }
+ else
+ {
+ result = objectClass.newInstance();
+ }
+ return result;
+ }
+ catch (Exception e)
+ {
+ throwError(e);
+ return null;
+ }
+ }
+
/**
- * Stop the plugin.
+ * Read a serialized object from a file.
*/
- public void stop()
+ protected void writeState(Object object, String sessionId)
{
- if (this.stdInCom != null)
+ File file = getStateFile(sessionId);
+ try
{
- this.stdInCom.stopPipe();
+ ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
+ outputStream.writeObject(object);
+ outputStream.close();
}
- try {
- this.onPluginStop();
- } catch (Throwable e) {
- this.throwError(e);
+ catch (Exception e)
+ {
+ throwError(e);
}
- this.throwNotification("plugin", "exit");
}
/**
+ * This is the generic method for throwing notifications to the server.
+ *
+ * @param notificationId
+ * ID of the notification (this should match with declared
+ * notifications in plugins.xml)
+ * @param arguments
+ * Notification arguments
+ */
+ protected void throwNotification(String notificationId, String... arguments)
+ {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append(notificationId);
+ for (String argument : arguments)
+ {
+ buffer.append(" '");
+ buffer.append(argument.replace("'", "\\'"));
+ buffer.append("'");
+ }
+ System.out.println(buffer.toString());
+ System.out.flush();
+ }
+
+ /**
+ * This is a special notification used to send "speakable" messages to the
+ * server.
+ *
+ * @param content
+ * message content
+ */
+ public void throwMessage(String content, Object... arguments)
+ {
+ String[] tmp = new String[arguments.length + 1];
+ tmp[0] = content;
+ for (int i = 0; i < arguments.length; i++)
+ {
+ tmp[i + 1] = String.valueOf(arguments[i]);
+ }
+ throwNotification("message", tmp);
+ }
+
+ /**
* This function throw a debug trace to the server. The server should have
* activate traces for this plugin in order to display them.
*
* @param message
* trace message
*/
- protected void throwTrace(String message) {
- if (!configuration.isTraces()) {
+ protected void throwTrace(String message)
+ {
+ if (!configuration.isTraces())
+ {
return;
}
throwNotification("trace", message);
@@ -298,7 +406,8 @@
* @param result
* Check command result
*/
- protected void throwResult(boolean result) {
+ protected void throwResult(boolean result)
+ {
String resultValue;
if (result) resultValue = "true";
else resultValue = "false";
@@ -309,10 +418,12 @@
* This is a special notification used to send "actuation" to the
* server.
*/
- public void throwActuation(String actuationName, Object... arguments) {
+ public void throwActuation(String actuationName, Object... arguments)
+ {
String[] tmp = new String[arguments.length + 1];
tmp[0] = actuationName;
- for (int i = 0; i < arguments.length; i++) {
+ for (int i = 0; i < arguments.length; i++)
+ {
tmp[i + 1] = String.valueOf(arguments[i]);
}
throwNotification("actuation", tmp);
@@ -325,54 +436,18 @@
* @param throwable
* Exception to throw.
*/
- protected void throwError(Throwable throwable) {
- if (configuration.isTraces()) {
+ protected void throwError(Throwable throwable)
+ {
+ if (configuration.isTraces())
+ {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
throwable.printStackTrace(printWriter);
throwNotification("error", result.toString());
- } else {
+ }
+ else
+ {
throwNotification("error", throwable.getMessage());
}
-
}
-
- protected File getStateFile(String name) {
- // State files should stay in the working folder of plugins in order
- // to be deleted when the plugin is updated (re-deployed)
- File sessionId = new File("states");
- sessionId.mkdirs();
- sessionId = new File(sessionId, name);
- return sessionId;
- }
-
- protected <E> E readState(Class<E> objectClass, String sessionId) {
- try {
- File file = getStateFile(sessionId);
- E result;
- if (file.exists()) {
- ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
- result = (E) inputStream.readObject();
- } else {
-
- result = objectClass.newInstance();
- }
- return result;
- } catch (Exception e) {
- throwError(e);
- return null;
- }
- }
-
- protected void writeState(Object object, String sessionId) {
- File file = getStateFile(sessionId);
- try {
- ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
- outputStream.writeObject(object);
- outputStream.close();
- } catch (Exception e) {
- throwError(e);
- }
- }
-
}
Modified: software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePluginConfiguration.java
===================================================================
--- software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePluginConfiguration.java 2009-06-03 08:58:15 UTC (rev 4729)
+++ software_suite_v3/smart-core/smart-dev/plugin-toolkit/java/simpleplugin-java-kit/trunk/sources/com/kysoh/tuxdroid/plugin/framework/plugin/SimplePluginConfiguration.java 2009-06-03 09:34:02 UTC (rev 4730)
@@ -22,7 +22,8 @@
package com.kysoh.tuxdroid.plugin.framework.plugin;
-public class SimplePluginConfiguration {
+public class SimplePluginConfiguration
+{
private boolean traces = false;
private int pitch = 100;
private String language = "en";
@@ -34,7 +35,8 @@
/**
* @return the pitch
*/
- public int getPitch() {
+ public int getPitch()
+ {
return pitch;
}
@@ -42,14 +44,16 @@
* @param pitch
* the pitch to set
*/
- public void setPitch(int pitch) {
+ public void setPitch(int pitch)
+ {
this.pitch = pitch;
}
/**
* @return the language
*/
- public String getLanguage() {
+ public String getLanguage()
+ {
return language;
}
@@ -57,14 +61,16 @@
* @param language
* the language to set
*/
- public void setLanguage(String language) {
+ public void setLanguage(String language)
+ {
this.language = language;
}
/**
* @return the country
*/
- public String getCountry() {
+ public String getCountry()
+ {
return country;
}
@@ -72,14 +78,16 @@
* @param country
* the country to set
*/
- public void setCountry(String country) {
+ public void setCountry(String country)
+ {
this.country = country;
}
/**
* @return the locutor
*/
- public String getLocutor() {
+ public String getLocutor()
+ {
return locutor;
}
@@ -87,14 +95,16 @@
* @param locutor
* the locutor to set
*/
- public void setLocutor(String locutor) {
+ public void setLocutor(String locutor)
+ {
this.locutor = locutor;
}
/**
* @return the traces
*/
- public boolean isTraces() {
+ public boolean isTraces()
+ {
return traces;
}
@@ -102,7 +112,8 @@
* @param traces
* the traces to set
*/
- public void setTraces(boolean traces) {
+ public void setTraces(boolean traces)
+ {
this.traces = traces;
}
|