From 35b50aa3a8ba1b3b8abd2197a998c2622a763ac1 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 21 Feb 2009 11:39:25 +0100 Subject: [PATCH] bindings/java: Moved the `PluginData' and `ValueList' classes. And implemented various (trivial) `Collectd*Interface' classes. --- .../java/org/collectd/api/CollectdAPI.java | 4 +- .../collectd/api/CollectdConfigInterface.java | 27 ++++ .../collectd/api/CollectdInitInterface.java | 27 ++++ .../collectd/api/CollectdReadInterface.java | 27 ++++ .../api/CollectdShutdownInterface.java | 27 ++++ .../collectd/api/CollectdWriteInterface.java | 27 ++++ bindings/java/org/collectd/api/DataSet.java | 132 ++++++++++++++++ .../java/org/collectd/api/DataSource.java | 145 ++++++++++++++++++ .../java/org/collectd/api/Notification.java | 81 ++++++++++ .../{protocol => api}/PluginData.java | 2 +- .../collectd/{protocol => api}/ValueList.java | 10 +- 11 files changed, 503 insertions(+), 6 deletions(-) create mode 100644 bindings/java/org/collectd/api/CollectdConfigInterface.java create mode 100644 bindings/java/org/collectd/api/CollectdInitInterface.java create mode 100644 bindings/java/org/collectd/api/CollectdReadInterface.java create mode 100644 bindings/java/org/collectd/api/CollectdShutdownInterface.java create mode 100644 bindings/java/org/collectd/api/CollectdWriteInterface.java create mode 100644 bindings/java/org/collectd/api/DataSet.java create mode 100644 bindings/java/org/collectd/api/DataSource.java create mode 100644 bindings/java/org/collectd/api/Notification.java rename bindings/java/org/collectd/{protocol => api}/PluginData.java (99%) rename bindings/java/org/collectd/{protocol => api}/ValueList.java (94%) diff --git a/bindings/java/org/collectd/api/CollectdAPI.java b/bindings/java/org/collectd/api/CollectdAPI.java index 67c8e6ad..89d9c169 100644 --- a/bindings/java/org/collectd/api/CollectdAPI.java +++ b/bindings/java/org/collectd/api/CollectdAPI.java @@ -22,8 +22,8 @@ package org.collectd.api; import java.util.List; -import org.collectd.protocol.ValueList; -import org.collectd.protocol.DataSource; +import org.collectd.api.ValueList; +import org.collectd.api.DataSource; public class CollectdAPI { diff --git a/bindings/java/org/collectd/api/CollectdConfigInterface.java b/bindings/java/org/collectd/api/CollectdConfigInterface.java new file mode 100644 index 00000000..fae4aa5a --- /dev/null +++ b/bindings/java/org/collectd/api/CollectdConfigInterface.java @@ -0,0 +1,27 @@ +/* + * collectd/java - org/collectd/api/CollectdConfigInterface.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 + */ + +package org.collectd.api; + +public interface CollectdConfigInterface +{ + int Config (OConfigItem ci); +} diff --git a/bindings/java/org/collectd/api/CollectdInitInterface.java b/bindings/java/org/collectd/api/CollectdInitInterface.java new file mode 100644 index 00000000..46379fe8 --- /dev/null +++ b/bindings/java/org/collectd/api/CollectdInitInterface.java @@ -0,0 +1,27 @@ +/* + * collectd/java - org/collectd/api/CollectdInitInterface.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 + */ + +package org.collectd.api; + +public interface CollectdInitInterface +{ + int Init (); +} diff --git a/bindings/java/org/collectd/api/CollectdReadInterface.java b/bindings/java/org/collectd/api/CollectdReadInterface.java new file mode 100644 index 00000000..6b3e1ad8 --- /dev/null +++ b/bindings/java/org/collectd/api/CollectdReadInterface.java @@ -0,0 +1,27 @@ +/* + * collectd/java - org/collectd/api/CollectdReadInterface.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 + */ + +package org.collectd.api; + +public interface CollectdReadInterface +{ + int Read (); +} diff --git a/bindings/java/org/collectd/api/CollectdShutdownInterface.java b/bindings/java/org/collectd/api/CollectdShutdownInterface.java new file mode 100644 index 00000000..909f849e --- /dev/null +++ b/bindings/java/org/collectd/api/CollectdShutdownInterface.java @@ -0,0 +1,27 @@ +/* + * collectd/java - org/collectd/api/CollectdShutdownInterface.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 + */ + +package org.collectd.api; + +public interface CollectdShutdownInterface +{ + int Shutdown (); +} diff --git a/bindings/java/org/collectd/api/CollectdWriteInterface.java b/bindings/java/org/collectd/api/CollectdWriteInterface.java new file mode 100644 index 00000000..dd76f2c0 --- /dev/null +++ b/bindings/java/org/collectd/api/CollectdWriteInterface.java @@ -0,0 +1,27 @@ +/* + * collectd/java - org/collectd/api/CollectdWriteInterface.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 + */ + +package org.collectd.api; + +public interface CollectdWriteInterface +{ + public int Write (ValueList vl); +} diff --git a/bindings/java/org/collectd/api/DataSet.java b/bindings/java/org/collectd/api/DataSet.java new file mode 100644 index 00000000..ff5107de --- /dev/null +++ b/bindings/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 + */ + +package org.collectd.api; + +import java.util.List; +import java.util.ArrayList; + +public class DataSet +{ + private String _type; + private List _ds; + + private DataSet () + { + this._type = null; + this._ds = new ArrayList (); + } + + public DataSet (String type) + { + this._type = type; + this._ds = new ArrayList (); + } + + public DataSet (String type, DataSource dsrc) + { + this._type = type; + this._ds = new ArrayList (); + this._ds.add (dsrc); + } + + public DataSet (String type, List 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 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/bindings/java/org/collectd/api/DataSource.java b/bindings/java/org/collectd/api/DataSource.java new file mode 100644 index 00000000..bfe8e2d0 --- /dev/null +++ b/bindings/java/org/collectd/api/DataSource.java @@ -0,0 +1,145 @@ +/* + * jcollectd + * Copyright (C) 2009 Hyperic, Inc. + * + * 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 + */ + +package org.collectd.api; + +/** + * Java representation of collectd/src/plugin.h:data_source_t structure. + */ +public class DataSource { + public static final int TYPE_COUNTER = 0; + public static final int TYPE_GAUGE = 1; + + static final String COUNTER = "COUNTER"; + static final String GAUGE = "GAUGE"; + + static final String NAN = "U"; + private static final String[] TYPES = { COUNTER, GAUGE }; + + String _name; + int _type; + 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; + } + + public void setName(String name) { + _name = name; + } + + public int getType() { + return _type; + } + + public void setType(int type) { + _type = type; + } + + public double getMin() { + return _min; + } + + public void setMin(double min) { + _min = min; + } + + public double getMax() { + return _max; + } + + public void setMax(double max) { + _max = max; + } + + static double toDouble(String val) { + if (val.equals(NAN)) { + return Double.NaN; + } + else { + return Double.parseDouble(val); + } + } + + private String asString(double val) { + if (Double.isNaN(val)) { + return NAN; + } + else { + return String.valueOf(val); + } + } + + public String toString() { + StringBuffer sb = new StringBuffer(); + final char DLM = ':'; + sb.append(_name).append(DLM); + sb.append(TYPES[_type]).append(DLM); + sb.append(asString(_min)).append(DLM); + 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/bindings/java/org/collectd/api/Notification.java b/bindings/java/org/collectd/api/Notification.java new file mode 100644 index 00000000..cd2fed58 --- /dev/null +++ b/bindings/java/org/collectd/api/Notification.java @@ -0,0 +1,81 @@ +/* + * jcollectd + * Copyright (C) 2009 Hyperic, Inc. + * + * 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 + */ + +package org.collectd.api; + +/** + * Java representation of collectd/src/plugin.h:notfication_t structure. + */ +public class Notification extends PluginData { + public static final int FAILURE = 1; + public static final int WARNING = 2; + public static final int OKAY = 4; + + public static String[] SEVERITY = { + "FAILURE", + "WARNING", + "OKAY", + "UNKNOWN" + }; + + private int _severity; + private String _message; + + public Notification (PluginData pd) { + super (pd); + } + + public void setSeverity (int severity) { + if ((severity == FAILURE) + || (severity == WARNING) + || (severity == OKAY)) + this._severity = severity; + } + + public int getSeverity() { + return _severity; + } + + public String getSeverityString() { + switch (_severity) { + case FAILURE: + return SEVERITY[0]; + case WARNING: + return SEVERITY[1]; + case OKAY: + return SEVERITY[2]; + default: + return SEVERITY[3]; + } + } + + public void setMessage (String message) { + this._message = message; + } + + public String getMessage() { + return _message; + } + + public String toString() { + StringBuffer sb = new StringBuffer(super.toString()); + sb.append(" [").append(getSeverityString()).append("] "); + sb.append(_message); + return sb.toString(); + } +} diff --git a/bindings/java/org/collectd/protocol/PluginData.java b/bindings/java/org/collectd/api/PluginData.java similarity index 99% rename from bindings/java/org/collectd/protocol/PluginData.java rename to bindings/java/org/collectd/api/PluginData.java index 734a999c..45cd8362 100644 --- a/bindings/java/org/collectd/protocol/PluginData.java +++ b/bindings/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/bindings/java/org/collectd/protocol/ValueList.java b/bindings/java/org/collectd/api/ValueList.java similarity index 94% rename from bindings/java/org/collectd/protocol/ValueList.java rename to bindings/java/org/collectd/api/ValueList.java index 7ae2d3dc..61cfdd79 100644 --- a/bindings/java/org/collectd/protocol/ValueList.java +++ b/bindings/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 getDataSource() { if (_ds.size() > 0) { return _ds; } else { - TypesDB db = TypesDB.getInstance(); - return db.getType(_type); + return null; } } -- 2.30.2