summaryrefslogtreecommitdiff
path: root/java/broker-plugins/experimental/info/src/main
diff options
context:
space:
mode:
authorMartin Ritchie <ritchiem@apache.org>2010-05-24 12:53:28 +0000
committerMartin Ritchie <ritchiem@apache.org>2010-05-24 12:53:28 +0000
commit47f7386bc2e4305c387d9a9147cd43d92cf3b615 (patch)
treef183311f6e51fa5757b3e084122dc6906e1a2868 /java/broker-plugins/experimental/info/src/main
parent1a4016c7c44a532c1ffe1df65a70b08dbddbc9fc (diff)
downloadqpid-python-47f7386bc2e4305c387d9a9147cd43d92cf3b615.tar.gz
QPID-2555 : Commit Info OSGi plugin provided by Sorin Suciu.
Adds two new libraries to lib dir both ASL licensed. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@947629 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/broker-plugins/experimental/info/src/main')
-rw-r--r--java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java78
-rw-r--r--java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java27
-rw-r--r--java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java120
-rw-r--r--java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java99
-rw-r--r--java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java97
-rw-r--r--java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/XMLWriter.java101
6 files changed, 522 insertions, 0 deletions
diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java
new file mode 100644
index 0000000000..167a53fdc5
--- /dev/null
+++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java
@@ -0,0 +1,78 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+/**
+ *
+ * @author sorin
+ *
+ * Activator class for the tracking services
+ */
+
+package org.apache.qpid.info;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.Properties;
+
+import org.apache.qpid.info.util.HttpPoster;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class Activator implements BundleActivator
+{
+
+ BundleContext _ctx = null;
+
+ public void start(BundleContext ctx) throws Exception
+ {
+ if (null != ctx)
+ {
+ BrokerInfoServiceImpl service = new BrokerInfoServiceImpl(ctx);
+ ctx.registerService(BrokerInfoService.class.getName(), service,
+ null);
+ _ctx = ctx;
+ HttpPoster hp;
+ try
+ {
+ Properties props = new Properties();
+ String QPID_WORK = System.getenv("QPID_WORK");
+ props.load(new FileInputStream(QPID_WORK + File.separator
+ + "etc" + File.separator + "qpidinfo.properties"));
+ hp = new HttpPoster(props, service.invoke().toXML());
+ hp.run();
+ } catch (Exception ex)
+ {
+ // Silently drop any exception
+ }
+ }
+ }
+
+ public BundleContext getBundleContext()
+ {
+ return _ctx;
+ }
+
+ public void stop(BundleContext ctx) throws Exception
+ {
+ // no need to do anything here, osgi will unregister the service for us
+ }
+
+}
diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java
new file mode 100644
index 0000000000..4c3c95d385
--- /dev/null
+++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java
@@ -0,0 +1,27 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.info;
+
+public interface BrokerInfoService
+{
+ public Info<?> invoke();
+}
diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java
new file mode 100644
index 0000000000..fe10d55dea
--- /dev/null
+++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java
@@ -0,0 +1,120 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+/**
+ *
+ * @author sorin
+ *
+ * Implementation for Info service
+ */
+
+package org.apache.qpid.info;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.Map.Entry;
+
+import org.apache.qpid.server.configuration.ServerConfiguration;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+public class BrokerInfoServiceImpl implements BrokerInfoService
+{
+
+ SortedMap<String, String> brokerInfoMap = new TreeMap<String, String>();
+
+ private final List<String> qpidProps = Arrays.asList("QPID_HOME",
+ "QPID_WORK", "java.class.path", "java.vm.name",
+ "java.class.version", "os.arch", "os.name", "os.version",
+ "sun.arch.data.model", "user.dir", "user.name", "user.timezone");
+
+ private final BundleContext _ctx;
+
+ public BrokerInfoServiceImpl(BundleContext ctx)
+ {
+ _ctx = ctx;
+ }
+
+ public Info<? extends Map<String, ?>> invoke()
+ {
+ // Get current time
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
+ brokerInfoMap.put("time", sdf.format(Calendar.getInstance().getTime()));
+ // Get the hostname
+ try
+ {
+ InetAddress addr = InetAddress.getLocalHost();
+ String hostname = addr.getHostName();
+ brokerInfoMap.put("hostname", hostname);
+ brokerInfoMap.put("ip", addr.getHostAddress());
+ } catch (UnknownHostException e)
+ {
+ //
+ }
+ // Dump system props
+ Properties sysprops = System.getProperties();
+ String propName;
+ for (Iterator<Entry<Object, Object>> it = sysprops.entrySet()
+ .iterator(); it.hasNext();)
+ {
+ Entry<Object, Object> en = it.next();
+ propName = en.getKey().toString();
+ if (qpidProps.indexOf(propName) >= 0)
+ {
+ brokerInfoMap.put(propName, en.getValue().toString());
+ }
+ }
+
+ if (null == _ctx)
+ {
+ return new Info<SortedMap<String, String>>(brokerInfoMap);
+ }
+
+ ServiceReference sref;
+ ServerConfiguration sc;
+ try
+ {
+ sref = _ctx
+ .getServiceReference(ServerConfiguration.class.getName());
+ sc = (ServerConfiguration) _ctx.getService(sref);
+ if (null != sc)
+ {
+ brokerInfoMap.put("port", sc.getPorts().toString());
+ }
+ }
+ catch (Exception e)
+ {
+ return new Info<SortedMap<String, String>>(brokerInfoMap);
+ }
+
+ return new Info<SortedMap<String, String>>(brokerInfoMap);
+ }
+
+}
diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java
new file mode 100644
index 0000000000..e4d33817a1
--- /dev/null
+++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java
@@ -0,0 +1,99 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+/**
+ *
+ * @author sorin
+ *
+ * Info object
+ */
+
+package org.apache.qpid.info;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.qpid.info.util.XMLWriter;
+
+public class Info<T extends Map<String, ?>>
+{
+ private T _info;
+
+ public Info(T info)
+ {
+ _info = info;
+ }
+
+ public String toString()
+ {
+ String result = "";
+ for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();)
+ {
+ String str = it.next();
+ result += str + "=" + _info.get(str).toString() + "\n";
+ }
+ return result;
+ }
+
+ public Properties toProps()
+ {
+ Properties props = new Properties();
+ if (null == _info)
+ return null;
+ for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();)
+ {
+ String key = it.next();
+ props.put(key, _info.get(key));
+ }
+ return props;
+ }
+
+ public StringBuffer toStringBuffer()
+ {
+ StringBuffer sb = new StringBuffer();
+ for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();)
+ {
+ String str = it.next();
+ sb.append(str + "=" + _info.get(str).toString() + "\n");
+ }
+ return sb;
+ }
+
+ public StringBuffer toXML()
+ {
+ XMLWriter xw = new XMLWriter(new StringBuffer());
+ xw.writeXMLHeader();
+ Map<String, String> attr = new HashMap<String, String>();
+ xw.writeOpenTag("qpidinfo", attr);
+ String key;
+ for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();)
+ {
+ attr.clear();
+ key = it.next();
+ xw.writeTag(key, attr, _info.get(key).toString());
+ }
+ xw.writeCloseTag("qpidinfo");
+ return xw.getXML();
+ }
+
+}
diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java
new file mode 100644
index 0000000000..c67a7682f3
--- /dev/null
+++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java
@@ -0,0 +1,97 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+/**
+ *
+ * @author sorin
+ *
+ * An simple Http post class for qpid info service
+ */
+
+package org.apache.qpid.info.util;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+import java.net.URLConnection;
+
+public class HttpPoster implements Runnable {
+ private final String url;
+ private final Hashtable<String,String> header;
+ private final List<String> response = new ArrayList<String>();
+ private final StringBuffer _buf;
+ //
+ public HttpPoster(Properties props, StringBuffer buf)
+ {
+ _buf = buf;
+ if (null!= props) {
+ url = props.getProperty("URL");
+ header = new Hashtable<String, String>();
+ String hostname = props.getProperty("hostname");
+ if (null!= hostname) header.put("hostname", hostname);
+ } else {
+ url = null;
+ header = null;
+ }
+ }
+ //
+ @Override
+ public void run()
+ {
+ if (null==url) return;
+ String line;
+ URL urlDest;
+ URLConnection urlConn;
+ try {
+ urlDest = new URL(url);
+ urlConn = urlDest.openConnection();
+ urlConn.setDoOutput(true);
+ urlConn.setUseCaches(false);
+ for (Iterator<String> it=header.keySet().iterator(); it.hasNext();) {
+ String prop = (String)it.next();
+ urlConn.setRequestProperty(prop, header.get(prop));
+ }
+ OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
+ wr.write(_buf.toString());
+ wr.flush();
+ // Get the response
+ BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
+ while ((line = rd.readLine()) != null) {
+ response.add(line);
+ }
+ } catch (Exception ex) {
+ return;
+ }
+ }
+
+ public List<String> getResponse()
+ {
+ return response;
+ }
+
+}
+
diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/XMLWriter.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/XMLWriter.java
new file mode 100644
index 0000000000..81a41e812c
--- /dev/null
+++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/XMLWriter.java
@@ -0,0 +1,101 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+/**
+ *
+ * @author sorin
+ *
+ * Naive and rudimentary XML writer
+ */
+
+package org.apache.qpid.info.util;
+
+import java.util.Map;
+
+public class XMLWriter
+{
+
+ private final StringBuffer _sb;
+
+ private final String INDENT = " ";
+
+ public XMLWriter(StringBuffer sb)
+ {
+ _sb = sb;
+ }
+
+ public StringBuffer getXML()
+ {
+ return _sb;
+ }
+
+ public void writeXMLHeader()
+ {
+ _sb.append("<?xml version=\"1.0\"?>\n");
+ }
+
+ public void writeTag(String tagName, Map<String, String> attributes,
+ String value)
+ {
+ writeOpenTag(tagName, attributes);
+ writeValue(value);
+ writeCloseTag(tagName);
+ }
+
+ public void writeOpenTag(String tagName, Map<String, String> attributes)
+ {
+ _sb.append("<").append(tagName);
+ if (null == attributes)
+ {
+ _sb.append(">\n");
+ return;
+ }
+ for (String key : attributes.keySet())
+ {
+ _sb.append(" ").append(key + "=\"" + attributes.get(key) + "\"");
+ }
+ _sb.append(">\n");
+
+ }
+
+ private void writeValue(String val)
+ {
+ _sb.append(INDENT).append(escapeXML(val) + "\n");
+ }
+
+ public void writeCloseTag(String tagName)
+ {
+ _sb.append("</" + tagName + ">\n");
+ }
+
+ private String escapeXML(String xmlStr)
+ {
+ if (null == xmlStr)
+ return null;
+ xmlStr = xmlStr.replaceAll("&", "&amp;");
+ xmlStr = xmlStr.replace("<", "&lt;");
+ xmlStr = xmlStr.replace(">", "&gt;");
+ xmlStr = xmlStr.replace("\"", "&quot;");
+ xmlStr = xmlStr.replace("'", "&apos;");
+ return xmlStr;
+ }
+
+}