[collectd] [PATCH] Import changes done to collectd:bindings/java.

Florian Forster octo at verplant.org
Sat Feb 21 12:09:35 CET 2009


From: Florian Forster <octo at leeloo.lan.home.verplant.org>

Hi Doug,

I've done some changes to the Java classes in collectd. Mostly I've
moved the classes representing data types in collectd to the
`org.collectd.api' namespace where they will be used by the upcoming
Java plugin.

I'd be glad if you could review these changes and add them to
`jcollectd' :)

The changes are:

 - DataSet: Added a new class to represent data sets. This class
   basically ties together a type and data sources (stored as
   List<DataSource>).
   The main reason for this new class is `parseDataSet' which is used in
   `TypesDB'.
 - DataSource: Implement `parseDataSource' (basically copied from
   `TypesDB').
 - TypesDB: Use `parseDataSet' to parse the `types.db' file. This makes
   the code much simpler and doesn't need any access to internal fields
   anymore.

Regards,
-octo

Signed-off-by: Florian Forster <octo at leeloo.lan.home.verplant.org>
---
 src/main/java/org/collectd/api/DataSet.java        |  132 ++++++++++++++++++++
 src/main/java/org/collectd/api/DataSource.java     |   49 +++++++-
 src/main/java/org/collectd/api/Notification.java   |   21 +++-
 src/main/java/org/collectd/api/PluginData.java     |    2 +-
 src/main/java/org/collectd/api/ValueList.java      |   10 +-
 .../java/org/collectd/protocol/Dispatcher.java     |    3 +
 .../java/org/collectd/protocol/PacketWriter.java   |    4 +
 src/main/java/org/collectd/protocol/Sender.java    |    4 +
 .../org/collectd/protocol/StdoutDispatcher.java    |    4 +
 src/main/java/org/collectd/protocol/TypesDB.java   |   53 ++------
 .../java/org/collectd/protocol/UdpReceiver.java    |   26 +++--
 src/main/java/org/collectd/protocol/UdpSender.java |    2 +
 12 files changed, 249 insertions(+), 61 deletions(-)
 create mode 100644 src/main/java/org/collectd/api/DataSet.java

diff --git a/src/main/java/org/collectd/api/DataSet.java b/src/main/java/org/collectd/api/DataSet.java
new file mode 100644
index 0000000..ff5107d
--- /dev/null
+++ b/src/main/java/org/collectd/api/DataSet.java
@@ -0,0 +1,132 @@
+/*
+ * collectd/java - org/collectd/api/OConfigItem.java
+ * Copyright (C) 2009  Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ */
+
+package org.collectd.api;
+
+import java.util.List;
+import java.util.ArrayList;
+
+public class DataSet
+{
+    private String _type;
+    private List<DataSource> _ds;
+
+    private DataSet ()
+    {
+        this._type = null;
+        this._ds = new ArrayList<DataSource> ();
+    }
+
+    public DataSet (String type)
+    {
+        this._type = type;
+        this._ds = new ArrayList<DataSource> ();
+    }
+
+    public DataSet (String type, DataSource dsrc)
+    {
+        this._type = type;
+        this._ds = new ArrayList<DataSource> ();
+        this._ds.add (dsrc);
+    }
+
+    public DataSet (String type, List<DataSource> ds)
+    {
+        this._type = type;
+        this._ds = ds;
+    }
+
+    public void setType (String type)
+    {
+        this._type = type;
+    }
+
+    public String getType ()
+    {
+        return (this._type);
+    }
+
+    public void addDataSource (DataSource dsrc)
+    {
+        this._ds.add (dsrc);
+    }
+
+    public List<DataSource> getDataSources ()
+    {
+        return (this._ds);
+    }
+
+    public String toString ()
+    {
+        StringBuffer sb = new StringBuffer ();
+        int i;
+
+        sb.append (this._type);
+        for (i = 0; i < this._ds.size (); i++)
+        {
+            if (i == 0)
+                sb.append ("\t");
+            else
+                sb.append (", ");
+            sb.append (this._ds.get (i).toString ());
+        }
+
+        return (sb.toString ());
+    }
+
+    static public DataSet parseDataSet (String str)
+    {
+        DataSet ds = new DataSet ();
+        String[] fields;
+        int i;
+
+        str = str.trim();
+        if (str.length() == 0) {
+            return (null);
+        }
+        if (str.charAt(0) == '#') {
+            return (null);
+        }
+
+        fields = str.split ("\\s+");
+        if (fields.length < 2)
+            return (null);
+
+        ds._type = fields[0];
+
+        for (i = 1; i < fields.length; i++) {
+            DataSource dsrc;
+
+            dsrc = DataSource.parseDataSource (fields[i]);
+            if (dsrc == null)
+                break;
+
+            ds._ds.add (dsrc);
+        }
+
+        if (i < fields.length)
+            return (null);
+
+        return (ds);
+    } /* DataSet parseDataSet */
+} /* class DataSet */
+
+/* vim: set sw=4 sts=4 et : */
diff --git a/src/main/java/org/collectd/api/DataSource.java b/src/main/java/org/collectd/api/DataSource.java
index c090cde..bfe8e2d 100644
--- a/src/main/java/org/collectd/api/DataSource.java
+++ b/src/main/java/org/collectd/api/DataSource.java
@@ -16,7 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-package org.collectd.protocol;
+package org.collectd.api;
 
 /**
  * Java representation of collectd/src/plugin.h:data_source_t structure. 
@@ -36,6 +36,21 @@ public class DataSource {
     double _min;
     double _max;
 
+    public DataSource (String name, int type, double min, double max) {
+        this._name = name;
+        this._type = TYPE_GAUGE;
+        if (type == TYPE_COUNTER)
+            this._type = TYPE_COUNTER;
+        this._min = min;
+        this._max = max;
+    }
+
+    /* Needed in parseDataSource below. Other code should use the above
+     * constructor or `parseDataSource'. */
+    private DataSource () {
+        this._type = TYPE_GAUGE;
+    }
+
     public String getName() {
         return _name;
     }
@@ -95,4 +110,36 @@ public class DataSource {
         sb.append(asString(_max));
         return sb.toString();
     }
+
+    static public DataSource parseDataSource (String str)
+    {
+        String[] fields;
+        int str_len = str.length ();
+        DataSource dsrc = new DataSource ();
+
+        /* Ignore trailing commas. This makes it easier for parsing code. */
+        if (str.charAt (str_len - 1) == ',') {
+            str = str.substring (0, str_len - 1);
+        }
+
+        fields = str.split(":");
+        if (fields.length != 4)
+            return (null);
+
+        dsrc._name = fields[0];
+
+        if (fields[1].equals (DataSource.GAUGE)) {
+            dsrc._type  = TYPE_GAUGE;
+        }
+        else {
+            dsrc._type  = TYPE_COUNTER;
+        }
+
+        dsrc._min =  toDouble (fields[2]);
+        dsrc._max =  toDouble (fields[3]);
+
+        return (dsrc);
+    } /* DataSource parseDataSource */
 }
+
+/* vim: set sw=4 sts=4 et : */
diff --git a/src/main/java/org/collectd/api/Notification.java b/src/main/java/org/collectd/api/Notification.java
index 6029aef..cd2fed5 100644
--- a/src/main/java/org/collectd/api/Notification.java
+++ b/src/main/java/org/collectd/api/Notification.java
@@ -16,7 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-package org.collectd.protocol;
+package org.collectd.api;
 
 /**
  * Java representation of collectd/src/plugin.h:notfication_t structure.
@@ -33,11 +33,18 @@ public class Notification extends PluginData {
         "UNKNOWN"
     };
 
-    int _severity;
-    String _message;
+    private int _severity;
+    private String _message;
 
-    Notification(PluginData pd) {
-        super(pd);
+    public Notification (PluginData pd) {
+        super (pd);
+    }
+
+    public void setSeverity (int severity) {
+        if ((severity == FAILURE)
+                || (severity == WARNING)
+                || (severity == OKAY))
+            this._severity = severity;
     }
 
     public int getSeverity() {
@@ -57,6 +64,10 @@ public class Notification extends PluginData {
         }
     }
 
+    public void setMessage (String message) {
+        this._message = message;
+    }
+
     public String getMessage() {
         return _message;
     }
diff --git a/src/main/java/org/collectd/api/PluginData.java b/src/main/java/org/collectd/api/PluginData.java
index 734a999..45cd836 100644
--- a/src/main/java/org/collectd/api/PluginData.java
+++ b/src/main/java/org/collectd/api/PluginData.java
@@ -16,7 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-package org.collectd.protocol;
+package org.collectd.api;
 
 import java.util.Date;
 
diff --git a/src/main/java/org/collectd/api/ValueList.java b/src/main/java/org/collectd/api/ValueList.java
index 7ae2d3d..61cfdd7 100644
--- a/src/main/java/org/collectd/api/ValueList.java
+++ b/src/main/java/org/collectd/api/ValueList.java
@@ -16,7 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-package org.collectd.protocol;
+package org.collectd.api;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -58,13 +58,17 @@ public class ValueList extends PluginData {
         _values.add(value);
     }
 
+    /* Used by the network parsing code */
+    public void clearValues () {
+        _values.clear ();
+    }
+
     public List<DataSource> getDataSource() {
         if (_ds.size() > 0) {
             return _ds;
         }
         else {
-            TypesDB db = TypesDB.getInstance();
-            return db.getType(_type);
+            return null;
         }
     }
 
diff --git a/src/main/java/org/collectd/protocol/Dispatcher.java b/src/main/java/org/collectd/protocol/Dispatcher.java
index f35cfbf..b5edfe3 100644
--- a/src/main/java/org/collectd/protocol/Dispatcher.java
+++ b/src/main/java/org/collectd/protocol/Dispatcher.java
@@ -18,6 +18,9 @@
 
 package org.collectd.protocol;
 
+import org.collectd.api.ValueList;
+import org.collectd.api.Notification;
+
 /**
  * Java interface equivalent of:
  * collectd/src/plugin.h:plugin_dispatch_values,plugin_dispatch_notification 
diff --git a/src/main/java/org/collectd/protocol/PacketWriter.java b/src/main/java/org/collectd/protocol/PacketWriter.java
index cf4ac5b..7bf0355 100644
--- a/src/main/java/org/collectd/protocol/PacketWriter.java
+++ b/src/main/java/org/collectd/protocol/PacketWriter.java
@@ -25,6 +25,10 @@ import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.List;
 
+import org.collectd.api.DataSource;
+import org.collectd.api.PluginData;
+import org.collectd.api.ValueList;
+
 /**
  * collectd/src/network.c:network_write 
  */
diff --git a/src/main/java/org/collectd/protocol/Sender.java b/src/main/java/org/collectd/protocol/Sender.java
index b605777..c9aa8c5 100644
--- a/src/main/java/org/collectd/protocol/Sender.java
+++ b/src/main/java/org/collectd/protocol/Sender.java
@@ -21,6 +21,10 @@ package org.collectd.protocol;
 import java.io.IOException;
 import java.net.InetAddress;
 
+import org.collectd.api.PluginData;
+import org.collectd.api.ValueList;
+import org.collectd.api.Notification;
+
 /**
  * Protocol independent Sender interface. 
  */
diff --git a/src/main/java/org/collectd/protocol/StdoutDispatcher.java b/src/main/java/org/collectd/protocol/StdoutDispatcher.java
index e12198a..2d469fe 100644
--- a/src/main/java/org/collectd/protocol/StdoutDispatcher.java
+++ b/src/main/java/org/collectd/protocol/StdoutDispatcher.java
@@ -21,6 +21,10 @@ package org.collectd.protocol;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.collectd.api.DataSource;
+import org.collectd.api.ValueList;
+import org.collectd.api.Notification;
+
 /**
  * Dispatch collectd data to stdout.
  * java -classpath collectd.jar org.collectd.protocol.UdpReceiver 
diff --git a/src/main/java/org/collectd/protocol/TypesDB.java b/src/main/java/org/collectd/protocol/TypesDB.java
index 05f971f..1c29223 100644
--- a/src/main/java/org/collectd/protocol/TypesDB.java
+++ b/src/main/java/org/collectd/protocol/TypesDB.java
@@ -24,12 +24,14 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
 
+import org.collectd.api.DataSource;
+import org.collectd.api.DataSet;
+
 /**
  * Parser for collectd/src/types.db format.
  */
@@ -101,47 +103,18 @@ public class TypesDB {
         String line;
 
         while ((line = reader.readLine()) != null) {
-            line = line.trim();
-            if (line.length() == 0) {
-                continue;
-            }
-            if (line.charAt(0) == '#') {
-                continue;
-            }
-            String[] fields = line.split("\\s+");
-            String type = fields[0];
-
-            for (int i=1; i<fields.length; i++) {
-                String entry = fields[i];
-                int len = entry.length();
-                if (entry.charAt(len-1) == ',') {
-                    entry = entry.substring(0, len-1);
-                }
-
-                String[] dsfields = entry.split(":");
-                DataSource ds = new DataSource();
-                ds._name = dsfields[0];
-
-                String dstype = dsfields[1];
-                if (dstype.equals(DataSource.GAUGE)) {
-                    ds._type = DataSource.TYPE_GAUGE;
-                }
-                else {
-                    ds._type = DataSource.TYPE_COUNTER;
-                }
-
-                ds._min = DataSource.toDouble(dsfields[2]);
-                ds._max = DataSource.toDouble(dsfields[3]);
-
-                List<DataSource> tds = _types.get(type);
-                if (tds == null) {
-                    tds = new ArrayList<DataSource>();
-                    _types.put(type, tds);
-                }
-                tds.add(ds);
+            DataSet ds;
+
+            ds = DataSet.parseDataSet (line);
+            if (ds != null)
+            {
+                String type = ds.getType ();
+                List<DataSource> dsrc = ds.getDataSources ();
+
+                this._types.put (type, dsrc);
             }
         }
-    }
+    } /* void load */
 
     public static void main(String[] args) throws Exception {
         TypesDB tl = new TypesDB();
diff --git a/src/main/java/org/collectd/protocol/UdpReceiver.java b/src/main/java/org/collectd/protocol/UdpReceiver.java
index 0069c2c..c9076d0 100644
--- a/src/main/java/org/collectd/protocol/UdpReceiver.java
+++ b/src/main/java/org/collectd/protocol/UdpReceiver.java
@@ -31,6 +31,10 @@ import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.logging.Logger;
 
+import org.collectd.api.PluginData;
+import org.collectd.api.ValueList;
+import org.collectd.api.Notification;
+
 /**
  * collectd UDP protocol receiver.
  * See collectd/src/network.c:parse_packet
@@ -160,12 +164,12 @@ public class UdpReceiver {
                 bb.order(ByteOrder.LITTLE_ENDIAN);
                 val = new Double(bb.getDouble());
             }
-            vl._values.add(val);
+            vl.addValue (val);
         }
         if (_dispatcher != null) {
             _dispatcher.dispatch(vl);
         }
-        vl._values.clear();        
+        vl.clearValues ();
     }
 
     //a union of sorts
@@ -214,35 +218,35 @@ public class UdpReceiver {
                 readValues(is, obj.getValueList());
             }
             else if (type == Network.TYPE_TIME) {
-                obj.pd._time = is.readLong() * 1000;
+                obj.pd.setTime (is.readLong() * 1000);
             }
             else if (type == Network.TYPE_INTERVAL) {
-                obj.getValueList()._interval = is.readLong();
+                obj.getValueList ().setInterval (is.readLong ());
             }
             else if (type == Network.TYPE_HOST) {
-                obj.pd._host = readString(is, len);
+                obj.pd.setHost (readString (is, len));
             }
             else if (type == Network.TYPE_PLUGIN) {
-                obj.pd._plugin = readString(is, len);
+                obj.pd.setPlugin (readString (is, len));
             }
             else if (type == Network.TYPE_PLUGIN_INSTANCE) {
-                obj.pd._pluginInstance = readString(is, len);
+                obj.pd.setPluginInstance (readString (is, len));
             }
             else if (type == Network.TYPE_TYPE) {
-                obj.pd._type = readString(is, len);
+                obj.pd.setType (readString (is, len));
             }
             else if (type == Network.TYPE_TYPE_INSTANCE) {
-                obj.pd._typeInstance = readString(is, len);
+                obj.pd.setTypeInstance (readString (is, len));
             }
             else if (type == Network.TYPE_MESSAGE) {
                 Notification notif = obj.getNotification();
-                notif._message = readString(is, len);
+                notif.setMessage (readString(is, len));
                 if (_dispatcher != null) {
                     _dispatcher.dispatch(notif);
                 }
             }
             else if (type == Network.TYPE_SEVERITY) {
-                obj.getNotification()._severity = (int)is.readLong();
+                obj.getNotification ().setSeverity ((int) is.readLong ());
             }
             else {
                 break;
diff --git a/src/main/java/org/collectd/protocol/UdpSender.java b/src/main/java/org/collectd/protocol/UdpSender.java
index b5eea98..00a053e 100644
--- a/src/main/java/org/collectd/protocol/UdpSender.java
+++ b/src/main/java/org/collectd/protocol/UdpSender.java
@@ -27,6 +27,8 @@ import java.net.SocketException;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.collectd.api.PluginData;
+
 /**
  * collectd UDP protocol sender.
  * See collectd/src/network.c:network_write
-- 
1.5.6.5




More information about the collectd mailing list