From 5951dfafe74a4115ef22dbb5d5d9b8c455bde0f5 Mon Sep 17 00:00:00 2001 From: Martin Ritchie Date: Tue, 22 Jun 2010 13:41:24 +0000 Subject: QPID-2555 : Modified the MANIFEST to include org.apache.qpid.common and org.apache.qpid.server.registry Patch provided by Sorin Suciu git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@956888 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/qpid/info/Activator.java | 196 ++++++++++++++++++--- .../org/apache/qpid/info/BrokerInfoService.java | 27 --- .../apache/qpid/info/BrokerInfoServiceImpl.java | 120 ------------- .../src/main/java/org/apache/qpid/info/Info.java | 64 +++++-- .../java/org/apache/qpid/info/util/HttpPoster.java | 124 ++++++++----- 5 files changed, 294 insertions(+), 237 deletions(-) delete mode 100644 java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java delete mode 100644 java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java (limited to 'java/broker-plugins/experimental/info/src/main') 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 index 167a53fdc5..a442cf1f28 100644 --- 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 @@ -19,60 +19,204 @@ * */ -/** - * - * @author sorin - * - * Activator class for the tracking services - */ - package org.apache.qpid.info; import java.io.File; -import java.io.FileInputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Properties; import org.apache.qpid.info.util.HttpPoster; +import org.apache.qpid.info.util.IniFileReader; +import org.apache.qpid.info.util.SoapClient; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +/** + * The Activator class for the OSGI info service + * + */ public class Activator implements BundleActivator { + private final List soapPropList = Arrays.asList("soap.hostname", + "soap.port", "soap.path", "soap.action", "soap.envelope"); + + private final List httpPropList = Arrays.asList("http.url", + "http.envelope"); + + InfoServiceImpl service = null; + BundleContext _ctx = null; + Map infoprops = new HashMap(); + + /** + * Start bundle method + * + * @param ctx + * the bundle context + */ 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 - } + service = new InfoServiceImpl(); + ctx.registerService(InfoService.class.getName(), service, null); + sendInfo("STARTUP"); } } + /** + * Getter for the bundle context + * + * @return BundleContext the bundle context + */ public BundleContext getBundleContext() { return _ctx; } + /** + * Stop the bundle method + * + * @param ctx + * the bundle context + */ public void stop(BundleContext ctx) throws Exception { - // no need to do anything here, osgi will unregister the service for us + sendInfo("SHUTDOWN"); + } + + /** + * Sends the information message + * + * @param action + * label that identifies if we are starting up or shutting down + */ + private void sendInfo(String action) + { + if ((null == _ctx) && (null == service)) + { + // invalid state + return; + } + + IniFileReader ifr = new IniFileReader(); + try + { + String QPID_HOME = System.getProperty("QPID_HOME"); + String cfgFilePath = QPID_HOME + File.separator + "etc" + + File.separator + "qpidinfo.properties"; + ifr.load(cfgFilePath); + } catch (Exception ex) + { + // drop the exception + return; + } + + // If we have no sections, something has gone really wrong, abort + if (ifr.getSections().size() == 0) + return; + + Info> info = service.invoke(action); + String protocol = ifr.getSections().get("").getProperty("protocol"); + sendMessages(protocol, ifr, info); + } + + /** + * Sends all the messages configured in the properties file + * + * @param protocol + * indicates what protocol to be used: http and soap implemented + * for now + * @param ifr + * an instance of IniFileReader class + * @param info + * an instance of an Info object, encapsulating the information + * we want to send + */ + private void sendMessages(String protocol, IniFileReader ifr, + Info> info) + { + if (null != protocol) + { + // Set the global properties first (as they are the defaults) + Properties defaultProps = ifr.getSections().get(""); + if (protocol.toLowerCase().startsWith("http")) + { + for (String section : ifr.getSections().keySet()) + { + // Skip the defaults + if (section.equals("")) + continue; + Properties props = new Properties(); + props.putAll(defaultProps); + props.putAll(ifr.getSections().get(section)); + if (isValid(protocol, props)) + { + new HttpPoster(props, info.toXML()).run(); + } + } + + } else if (protocol.toLowerCase().startsWith("soap")) + { + for (String section : ifr.getSections().keySet()) + { + Properties props = new Properties(); + props.putAll(defaultProps); + props.putAll(ifr.getSections().get(section)); + if (isValid(protocol, props)) + { + new SoapClient(info.toMap(), props).sendSOAPMessage(); + } + } + } + } else + { + return; + } + } + + /** + * Checks if the properties for a specified protocol are valid + * + * @param protocol + * String representing the protocol + * @param props + * The properties associate with the specified protocol + */ + private boolean isValid(String protocol, Properties props) + { + if (null == protocol) + return false; + String value = ""; + if (protocol.toLowerCase().startsWith("http")) + { + for (String prop : httpPropList) + { + if (null == props.get(prop)) + return false; + } + return true; + } + + if (protocol.toLowerCase().startsWith("soap")) + { + for (String prop : soapPropList) + { + value = props.getProperty(prop); + if (null == value) + { + return false; + } + } + return true; + } + return false; } +} // end class -} 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 deleted file mode 100644 index 4c3c95d385..0000000000 --- a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * - * 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 deleted file mode 100644 index fe10d55dea..0000000000 --- a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * - * 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 brokerInfoMap = new TreeMap(); - - private final List 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> 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> it = sysprops.entrySet() - .iterator(); it.hasNext();) - { - Entry en = it.next(); - propName = en.getKey().toString(); - if (qpidProps.indexOf(propName) >= 0) - { - brokerInfoMap.put(propName, en.getValue().toString()); - } - } - - if (null == _ctx) - { - return new Info>(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>(brokerInfoMap); - } - - return new Info>(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 index e4d33817a1..2c6db8df12 100644 --- 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 @@ -35,15 +35,24 @@ import java.util.Properties; import org.apache.qpid.info.util.XMLWriter; +/** + * The Info class encapsulates all the informations we are collecting + * and it is able to render it in different data representations + */ public class Info> { private T _info; + /** + * Constructor. + * @param info instantiates the object with a Map + */ public Info(T info) { _info = info; } + @Override public String toString() { String result = ""; @@ -55,21 +64,25 @@ public class Info> return result; } - public Properties toProps() - { + /** + * Renders Info map to a property object + * @return A Properties object representing the Info map + */ + public Properties toProps() { Properties props = new Properties(); - if (null == _info) - return null; - for (Iterator it = _info.keySet().iterator(); it.hasNext();) - { - String key = it.next(); - props.put(key, _info.get(key)); + if (null==_info) return null; + for (Iterator it = _info.keySet().iterator(); it.hasNext();) { + String key = it.next(); + props.put(key, _info.get(key)); } return props; } - - public StringBuffer toStringBuffer() - { + + /** + * Renders Info map to a StringBuffer + * @return A StringBuffer object representing the Info map + */ + public StringBuffer toStringBuffer() { StringBuffer sb = new StringBuffer(); for (Iterator it = _info.keySet().iterator(); it.hasNext();) { @@ -78,22 +91,39 @@ public class Info> } return sb; } - - public StringBuffer toXML() + + /** + * Renders Info map to a StringBuffer containing an XML string + * @return A StringBuffer object containing an XML representation of the Info map + */ + public StringBuffer toXML() { XMLWriter xw = new XMLWriter(new StringBuffer()); xw.writeXMLHeader(); Map attr = new HashMap(); xw.writeOpenTag("qpidinfo", attr); String key; - for (Iterator it = _info.keySet().iterator(); it.hasNext();) - { + for (Iterator it = _info.keySet().iterator(); it.hasNext();) { attr.clear(); key = it.next(); - xw.writeTag(key, attr, _info.get(key).toString()); - } + xw.writeTag(key, attr, _info.get(key).toString()); + } xw.writeCloseTag("qpidinfo"); return xw.getXML(); } + + /** + * Renders Info map to a HashMap + * @return A HashMap object representing the Info map + */ + public HashMap toMap() { + String key; + HashMap infoMap = new HashMap(); + for (Iterator it = _info.keySet().iterator(); it.hasNext();) { + key = it.next(); + infoMap.put(key, _info.get(key).toString()); + } + return infoMap; + } } 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 index c67a7682f3..d07f34dec0 100644 --- 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 @@ -36,62 +36,92 @@ import java.util.Properties; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.net.InetAddress; import java.net.URL; import java.net.URLConnection; +import java.net.UnknownHostException; + +public class HttpPoster implements Runnable +{ + private final String url; + + private final Hashtable header; -public class HttpPoster implements Runnable { - private final String url; - private final Hashtable header; private final List response = new ArrayList(); - private final StringBuffer _buf; - // - public HttpPoster(Properties props, StringBuffer buf) + + private final StringBuffer _buf; + + /** + * Constructor + * @param props Properties containing the URL + * @param buf Buffer containing the message to be posted + */ + public HttpPoster(Properties props, StringBuffer buf) + { + _buf = buf; + if (null != props) + { + url = props.getProperty("http.url"); + header = new Hashtable(); + try + { + String hostname = InetAddress.getLocalHost().getHostName(); + header.put("hostname", hostname); + } catch (UnknownHostException e) + { + // Silently ignoring the error ;) + } + } else + { + url = null; + header = null; + } + } + /** + * Posts the message from the _buf StringBuffer to the http server + */ + public void run() { - _buf = buf; - if (null!= props) { - url = props.getProperty("URL"); - header = new Hashtable(); - 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 { - 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 it=header.keySet().iterator(); it.hasNext();) { - String prop = (String)it.next(); - urlConn.setRequestProperty(prop, header.get(prop)); - } - OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream()); + urlDest = new URL(url); + urlConn = urlDest.openConnection(); + urlConn.setDoOutput(true); + urlConn.setUseCaches(false); + for (Iterator 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) { + BufferedReader rd = new BufferedReader(new InputStreamReader( + urlConn.getInputStream())); + while ((line = rd.readLine()) != null) + { response.add(line); - } - } catch (Exception ex) { - return; - } - } - - public List getResponse() + } + } catch (Exception ex) { - return response; - } - -} + return; + } + } + + /** + * Retrieves the response from the http server + * @return List response received from the http server + */ + public List getResponse() + { + return response; + } +} -- cgit v1.2.1