Child pages
  • Creating Client Plugins

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This is an example of naming a package: net.stuffcom.servoy.plugins.whois  - where 'net.stuffcom.servoy' is a prefix which follows the rule listed above, and 'whois' is the plugin name.

...

Code Block
public class WhoisPlugin implements IClientPlugin {

    public static final String PLUGIN_NAME = "whois";
    private WhoisPluginProvider provider;
    
    @Override
    public Properties getProperties() {
        Properties props = new Properties();
        props.put(DISPLAY_NAME, getName());
        return props;
    }

    @Override
    public void load() throws PluginException {
        // ignore
    }

    @Override
    public void unload() throws PluginException {
        provider = null;
    }

    @Override
    public void propertyChange(PropertyChangeEvent arg0) {
        // ignore
    }

    @Override
    public IScriptable getScriptObject() {
        if (provider == null) {
            provider = new WhoisPluginProvider();
        } 
        return provider;
    }

    @Override
    public Icon getImage() {
        URL iconUrl = getClass().getResource("images/whois.png"); //the image is added under a package 'net.stuffcom.servoy.plugins.whois.images' added to the WhoisPlugin project.
        if (iconUrl != null) {
            return new ImageIcon(iconUrl);
        } else {
            return null;
        }
    }

    @Override
    public String getName() {
        return PLUGIN_NAME;
    }

    @Override
    public void initialize(IClientPluginAccess arg0) throws PluginException {
        // ignore
    }
}

...

Code Block
@ServoyDocumented(publicName = WhoisPlugin.PLUGIN_NAME, scriptingName = "plugins." + WhoisPlugin.PLUGIN_NAME)
public class WhoisPluginProvider implements IScriptable, IReturnedTypesProvider {

    @Override
    public Class<?>[] getAllReturnedTypes() {
        return null;
    }
    
    private String server = "whois.networksolutions.com";
    private int port = 43;
    private int timeout = 30 * 1000; // unit is milliseconds
    
    @JSGetter
    public String getServer() {
        return server;
    }

    @JSSetter
    public void setServer(String server) {
        this.server = server;
    }

    @JSGetter
    public int getPort() {
        return port;
    }

    @JSSetter
    public void setPort(int port) {
        this.port = port;
    }

    @JSGetter
    public int getTimeout() {
        return timeout;
    }

    @JSSetter
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    /**
     * @clonedesc query(String, String, int, int)
     * @sampleas query(String, String, int, int)
     * @param domainName
     */
    @JSFunction
    public String query(String domainName) {
        return query(domainName, this.server, this.port, this.timeout);
    }
    
    /**
     * @clonedesc query(String, String, int, int)
     * @sampleas query(String, String, int, int)
     * @param domainName
     * @param server
     */
    @JSFunction
    public String query(String domainName, String server) {
        return query(domainName, server, this.port, this.timeout);
    }
    
    /**
     * @clonedesc query(String, String, int, int)
     * @sampleas query(String, String, int, int)
     * @param domaninName
     * @param server
     * @param port
     */
    @JSFunction
    public String query(String domainName, String server, int port) {
        return query(domainName, server, port, this.timeout);
    }
    
    /**
     * Calls a whois server to retrieve information about the domain name you provide
     * 
     * @sample
     * // you call a whois server by providing a domain name and get info in return
     * var result = plugins.whois.query('servoy.com');
     * // alternatively you can provide an alternate server (default is networksolutionnetworksolutions.com)
     * var result = plugins.whois.query('servoy.com', 'whois.internic.net/whois.html');
     * // you can also provide a port, if not standard (43 by deafault)
     * var result = plugins.whois.query('servoy.com', 'whois.internic.net', 43);
     * // and you can also provide a timeout length (unit is milliseconds, default is 30 seconds)
     * var result = plugins.whois.query('servoy.com', 'whois.internic.net', 43, 50000);
     * 
     * @param domainName
     * @param server
     * @param port
     * @param timeout
     * @return
     */
    @JSFunction
    public String query(String domainName, String server, int port, int timeout) {
        try {
            // create the socket
            Socket socket = new Socket(server, port);
            socket.setSoTimeout(timeout);
            // create a reader to get the response from the server
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // create an output stream to send our query to the server
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            // call the service with the domainName supplied
            // and terminate with carriage return)
            out.writeBytes(domainName + " \r\n");
            // read the response from the server
            String str1 = null;
            StringBuffer buffer = new StringBuffer();
            while ((str1 = in.readLine()) != null) {
                buffer.append(str1);
                buffer.append("\r\n");
            }
            // close our stream and reader
            out.close();
            in.close();
            // close the socket
            socket.close();
            // return the result as String
            return buffer.toString();
        } catch (IOException ioEx) {
            return ioEx.getLocalizedMessage();
        } catch (Exception ex) {
            return ex.getLocalizedMessage();
        }
    }
}

...

The plugin jar can use Java Service Provider to expose Servoy Plugin classes. There should be a file inside jar at path: META-INF/services/com.servoy.j2db.plugins.IPlugin which contains a line for each plugin that jar should expose (a class that implements IPlugin). The plugin should also have a default constructor (with no parameters). If file com.servoy.j2db.plugins.IPlugin is missing or contains invalid entries Servoy will scan the jar for all classes that implement interface IPlugin. An example of file content (for whois plugin) is:

Code Block
net.stuffcom.servoy.plugins.whois.WhoisPlugin

...