diff options
author | Rajith Muditha Attapattu <rajith@apache.org> | 2011-05-27 15:44:23 +0000 |
---|---|---|
committer | Rajith Muditha Attapattu <rajith@apache.org> | 2011-05-27 15:44:23 +0000 |
commit | 66765100f4257159622cefe57bed50125a5ad017 (patch) | |
tree | a88ee23bb194eb91f0ebb2d9b23ff423e3ea8e37 /qpid/java/broker-plugins/experimental/info/src/main/java | |
parent | 1aeaa7b16e5ce54f10c901d75c4d40f9f88b9db6 (diff) | |
parent | 88b98b2f4152ef59a671fad55a0d08338b6b78ca (diff) | |
download | qpid-python-66765100f4257159622cefe57bed50125a5ad017.tar.gz |
Creating a branch for experimenting with some ideas for JMS client.rajith_jms_client
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/rajith_jms_client@1128369 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker-plugins/experimental/info/src/main/java')
10 files changed, 1206 insertions, 0 deletions
diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java new file mode 100644 index 0000000000..c7d3fd38ff --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java @@ -0,0 +1,204 @@ +/* + * + * 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; + +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; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** The Activator class for the OSGI info service */ +public class Activator implements BundleActivator +{ + + private final List<String> _soapPropList = Arrays.asList("soap.hostname", + "soap.port", "soap.path", "soap.action", "soap.envelope"); + + private final List<String> _httpPropList = Arrays.asList("http.url", + "http.envelope"); + + InfoServiceImpl _service = null; + + BundleContext _ctx = null; + + /** + * Start bundle method + * + * @param ctx the bundle context + */ + public void start(BundleContext ctx) throws Exception + { + if (null != ctx) + { + _ctx = ctx; + _service = new InfoServiceImpl(); + ctx.registerService(InfoService.class.getName(), _service, null); + sendInfo("STARTUP"); + } + } + + /** + * Stop the bundle method + * + * @param ctx the bundle context + */ + public void stop(BundleContext ctx) throws Exception + { + 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.ini"; + ifr.load(cfgFilePath); + } + catch (Throwable ex) + { + // drop everything to be silent + return; + } + + // Only send Messages if we have some sections. + if (ifr.getSections().size() != 0) + { + Info<? extends Map<String, ?>> 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<? extends Map<String, ?>> 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(); + } + } + } + } + } + + /** + * 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 + * @return boolean + */ + 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/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/AppInfo.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/AppInfo.java new file mode 100644 index 0000000000..a5d267282b --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/AppInfo.java @@ -0,0 +1,94 @@ +/* + * + * 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; + +import org.apache.qpid.common.QpidProperties; +import org.apache.qpid.server.configuration.ServerConfiguration; +import org.apache.qpid.server.registry.ApplicationRegistry; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.TreeMap; + +/** AppInfo class is gathering application specific information */ +public class AppInfo +{ + + private static final List<String> appProps = Arrays.asList("QPID_HOME", + "QPID_WORK"); + + private static Map<String, String> appInfoMap = new TreeMap<String, String>(); + + /** + * getInfo method retrieves a key-value map for specific application properties + * + * @return Map<String,String> + */ + public static Map<String, String> getInfo() + { + + // Gather the selected app 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 (appProps.indexOf(propName) >= 0) + { + appInfoMap.put(propName, en.getValue().toString()); + } + } + + ServerConfiguration sc; + try + { + sc = ApplicationRegistry.getInstance().getConfiguration(); + if (null != sc) + { + appInfoMap.put("jmxport", sc.getJMXManagementPort() + ""); + appInfoMap.put("port", sc.getPorts().toString()); + appInfoMap.put("version", QpidProperties.getReleaseVersion()); + appInfoMap.put("vhosts", "standalone"); + appInfoMap.put("JMXPrincipalDatabase", sc + .getJMXPrincipalDatabase()); + appInfoMap.put("KeystorePath", sc.getKeystorePath()); + appInfoMap.put("PluginDirectory", sc.getPluginDirectory()); + appInfoMap.put("CertType", sc.getCertType()); + appInfoMap.put("QpidWork", sc.getQpidWork()); + appInfoMap.put("Bind", sc.getBind()); + } + } + catch (Exception e) + { + // drop everything to be silent + } + return appInfoMap; + + } + +} diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java new file mode 100644 index 0000000000..2fb9382526 --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java @@ -0,0 +1,143 @@ +/* + * + * 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 org.apache.qpid.info.util.XMLWriter; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +/** + * The Info class encapsulates all the information we are collecting + * and it is able to render it in different data representations + */ +public class Info<T extends Map<String, ?>> +{ + private T _info; + + /** + * Constructor. + * + * @param info instantiates the object with a Map<String,?> + */ + public Info(T info) + { + _info = info; + } + + @Override + 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; + } + + /** + * 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<String> it = _info.keySet().iterator(); it.hasNext();) + { + String key = it.next(); + props.put(key, _info.get(key)); + } + return props; + } + + /** + * Renders Info map to a StringBuffer + * + * @return A StringBuffer object representing the Info map + */ + 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; + } + + /** + * 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<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(); + } + + /** + * Renders Info map to a HashMap + * + * @return A HashMap object representing the Info map + */ + public HashMap<String, String> toMap() + { + String key; + HashMap<String, String> infoMap = new HashMap<String, String>(); + for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();) + { + key = it.next(); + infoMap.put(key, _info.get(key).toString()); + } + return infoMap; + } + +} diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/InfoService.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/InfoService.java new file mode 100644 index 0000000000..2804dfb1b4 --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/InfoService.java @@ -0,0 +1,30 @@ +/* + * + * 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. + * + */ + + /** + * Interface exposing the service methods + */ + package org.apache.qpid.info; + + public interface InfoService + { + public Info<?> invoke(String action); + } diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/InfoServiceImpl.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/InfoServiceImpl.java new file mode 100644 index 0000000000..5522f2701e --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/InfoServiceImpl.java @@ -0,0 +1,66 @@ +/* + * + * 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.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + + +public class InfoServiceImpl implements InfoService +{ + + SortedMap<String, String> infoMap = new TreeMap<String, String>(); + + /** + * invoke method collects all the information from System and Application + * and encapsulates them in an Info object + * @return An instance of an Info object + */ + public Info<? extends Map<String,?>> invoke(String action) + { + // Record the action (STARTUP/SHUTDOWN) + infoMap.put("action",action); + + // Record the current time stamp + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ"); + infoMap.put("time", sdf.format(Calendar.getInstance().getTime())); + + // Add the system specific properties + infoMap.putAll(SystemInfo.getInfo()); + + // Add the application specific properties + infoMap.putAll(AppInfo.getInfo()); + + return new Info<SortedMap<String, String>>(infoMap); + } + +} diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/SystemInfo.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/SystemInfo.java new file mode 100644 index 0000000000..8bd94fe14d --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/SystemInfo.java @@ -0,0 +1,91 @@ +/* + * + * 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; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.TreeMap; +import java.util.Map.Entry; + +/** + * Collector for system specific information + */ +public class SystemInfo +{ + + private static Map<String, String> sysInfoMap = new TreeMap<String, String>(); + + private static final List<String> sysProps = Arrays.asList( + "java.class.path", "java.home", "java.vm.name", "java.vm.vendor", + "java.vm.version", "java.class.version", "java.runtime.version", + "os.arch", "os.name", "os.version", "sun.arch.data.model", + "user.home", "user.dir", "user.name", "user.timezone"); + + /** + * getInfo collects all the properties specified in sysprops list + * @return A Map<String,String> + */ + public static Map<String, String> getInfo() + { + + // Get the hostname + try + { + InetAddress addr = InetAddress.getLocalHost(); + String hostname = addr.getHostName(); + sysInfoMap.put("hostname", hostname); + sysInfoMap.put("ip", addr.getHostAddress()); + } + catch (UnknownHostException e) + { + // drop everything to be silent + } + // Get the runtime info + sysInfoMap.put("CPUCores", Runtime.getRuntime().availableProcessors() + + ""); + sysInfoMap.put("Maximum_Memory", Runtime.getRuntime().maxMemory() + ""); + sysInfoMap.put("Free_Memory", Runtime.getRuntime().freeMemory() + ""); + + // Gather the selected 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 (sysProps.indexOf(propName) >= 0) + { + sysInfoMap.put(propName, en.getValue().toString()); + } + } + + return sysInfoMap; + + } + +} diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java new file mode 100644 index 0000000000..d27980be05 --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java @@ -0,0 +1,130 @@ +/*
+ *
+ * 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.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.InetAddress;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.UnknownHostException;
+
+/**
+ *
+ * An simple Http post class for qpid info service
+ */
+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;
+
+ /**
+ * 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<String, String>();
+ 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()
+ {
+ 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 = 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)
+ {
+ // Silently ignoring the error ;)
+ }
+ }
+
+ /**
+ * Retrieves the response from the http server
+ *
+ * @return List<String> response received from the http server
+ */
+ public List<String> get_response()
+ {
+ return _response;
+ }
+
+}
diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/IniFileReader.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/IniFileReader.java new file mode 100644 index 0000000000..60a025d322 --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/IniFileReader.java @@ -0,0 +1,193 @@ +/* + * + * 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.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * This class is simple implementation of an ini file reader. It expects a + * file with the following structure: + * + * ; global values, can be overwritten in sections + * key1=value1 + * key2=value2 + * + * [Section1] + * key1=value1_new ; overwriting the global key1 + * key3=value3 + * key4=value4 + * + * [Section2] + * key5=value5 + * key6=value6 + * key7=value7 + * + * Note: Commentaries are preceded by ; or # and are supported throughout + * A commentary line at the end of section is interpreted as + * a section end marker + * + * A structure <String,Properties> (section name, associated properties) + * is generated as a result of processing the ini file. + */ +public class IniFileReader +{ + private final Map<String, Properties> _sections; + + private final String COMMENT_SEMICOLON = ";"; + + private final String COMMENT_HASH = "#"; + + enum State + { + IN_SECTION, OFF_SECTION, GLOBAL + } + + /* + * IniFileReader constructor + */ + + public IniFileReader() + { + _sections = new HashMap<String, Properties>(); + } + + /** + * Cleans up the after comments or the empty spaces/tabs surrounding the given string + * + * @param str The String to be cleaned + * + * @return String Cleanup Version + */ + private String cleanUp(String str) + { + if (str.contains(COMMENT_SEMICOLON)) + { + str = str.substring(0, str.indexOf(COMMENT_SEMICOLON)); + } + if (str.contains(COMMENT_HASH)) + { + str = str.substring(0, str.indexOf(COMMENT_HASH)); + } + return str.trim(); + } + + /** + * Loads and parses the ini file with the full path specified in the argument + * + * @param fileName Full path to the ini file + * + * @throws IllegalArgumentException If the file cannot be processed + */ + public void load(String fileName) throws IllegalArgumentException + { + if (!new File(fileName).isFile()) + { + throw new IllegalArgumentException("File: " + fileName + " does not exist or cannot be read."); + } + State state = State.GLOBAL; + String line; + Properties sectionProps = new Properties(); + String sectionName = ""; + try + { + BufferedReader in = new BufferedReader(new FileReader(fileName)); + while ((line = in.readLine()) != null) + { + String str = cleanUp(line); + + // Did we get a section header? + if (str.startsWith("[")) + { + if (!str.endsWith("]")) + { + // Index of 1 to skip '[' + throw new IllegalArgumentException(str.substring(1) + + " is not closed"); + } + + // We encountered a new section header + if (state != State.IN_SECTION) + { + _sections.put(sectionName, sectionProps); + sectionProps = new Properties(); + sectionName = str.replace("[", "").replace("]", "") + .trim(); + state = State.IN_SECTION; + } + } + + // Any other line tested separately, ignore if out of a section + // and add if in section + if (str.length() == 0) + { + // We encountered a commented or an empty line, both cases + // mean we are off the section + if (state == State.IN_SECTION) + { + _sections.put(sectionName, sectionProps); + state = State.OFF_SECTION; + } + } + else + { + // proper line, add it to the props + if (state != State.OFF_SECTION) + { + if (str.contains("=")) + { + int ix = str.indexOf("="); + sectionProps.put(str.substring(0, ix).trim(), str + .substring(ix + 1).trim()); + } + } + } + } + in.close(); + } + catch (IOException e) + { + _sections.clear(); + return; + } + if (state != State.OFF_SECTION) + { + _sections.put(sectionName, sectionProps); + } + } + + /** + * Getter for the Sections Map + * + * @return Map<String,Properties> The parsed content of the ini file in this structure + */ + public Map<String, Properties> getSections() + { + return _sections; + } + +} diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/SoapClient.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/SoapClient.java new file mode 100644 index 0000000000..0f66085fc3 --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/SoapClient.java @@ -0,0 +1,155 @@ +/* + * + * 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 SOAP client for qpid info service + */ +package org.apache.qpid.info.util; + +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.net.InetAddress; +import java.net.Socket; +import java.util.HashMap; +import java.util.Properties; + +public class SoapClient +{ + + private final StringBuffer _xmlData; + + private final Properties _destprops; + + private final String _hostname; + + private final int _port; + + private final String _urlpath; + + private final String _soapenvelope; + + private final String _soapaction; + + private final StringBuffer _soapMessage = new StringBuffer(); + + + public SoapClient(HashMap<String, String> map, Properties destprops) + { + _destprops = destprops; + _hostname = (String) _destprops.get("soap.hostname"); + _port = Integer.parseInt((String) _destprops.get("soap.port")); + _urlpath = (String) destprops.get("soap.path"); + _soapenvelope = (String) destprops.get("soap.envelope"); + _soapaction = (String) destprops.get("soap.action"); + _xmlData = new StringBuffer(_soapenvelope); + replaceVariables(map); + } + + public StringBuffer getXMLData() + { + return _xmlData; + } + + public StringBuffer getSoapMessage() { + return _soapMessage; + } + + public String getSoapEnvelope() { + return _soapenvelope; + } + + /** + * Clears and sets new XML data + * @param sb the new data to set + */ + public void setXMLData(StringBuffer sb) + { + _xmlData.delete(0, _xmlData.length()); + _xmlData.append(sb); + } + + + public void replaceVariables(HashMap<String, String> vars) + { + int ix = 0; + for (String var : vars.keySet()) + { + while ((ix = _xmlData.indexOf("@" + var.toUpperCase())) >= 0) + { + _xmlData.replace(ix, ix + 1 + var.length(), vars.get(var)); + } + } + } + + public void replaceVariables(Properties varProps) + { + if (varProps == null) + { + return; + } + int ix = 0; + for (Object var : varProps.keySet()) + { + while ((ix = _xmlData.indexOf("@" + var)) >= 0) + { + _xmlData.replace(ix, ix + 1 + var.toString().length(), varProps + .get(var).toString()); + } + } + } + + + public void sendSOAPMessage() + { + + try + { + InetAddress addr = InetAddress.getByName(_hostname); + Socket sock = new Socket(addr, _port); + StringBuffer sb = new StringBuffer(); + sb.append("POST " + _urlpath + " HTTP/1.1\r\n"); + sb.append("Host: " + _hostname + ":" + _port + "\r\n"); + sb.append("Content-Length: " + _xmlData.length() + "\r\n"); + sb.append("Content-Type: text/xml; charset=\"utf-8\"\r\n"); + sb.append("SOAPAction: \"urn:"+ _soapaction +"\"\r\n"); + sb.append("User-Agent: Axis2\r\n"); + sb.append("\r\n"); + // Send header + BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock + .getOutputStream(), "UTF-8")); + synchronized(_soapMessage) { + _soapMessage.setLength(0); + _soapMessage.append(sb); + _soapMessage.append(_xmlData); + } + // Send data + wr.write(_soapMessage.toString()); + wr.flush(); + wr.close(); + + } catch (Exception ex) + { + // Drop any exception + } + } +} diff --git a/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/XMLWriter.java b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/XMLWriter.java new file mode 100644 index 0000000000..a266edae00 --- /dev/null +++ b/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/XMLWriter.java @@ -0,0 +1,100 @@ +/* + * + * 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.util; + +import java.util.Map; + +/** + * + * Naive and rudimentary XML writer + * It has methods to write the header, a tag with attributes + * and values. It escapes the XML special characters + */ +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("&", "&"); + xmlStr = xmlStr.replace("<", "<"); + xmlStr = xmlStr.replace(">", ">"); + xmlStr = xmlStr.replace("\"", """); + xmlStr = xmlStr.replace("'", "'"); + return xmlStr; + } + +} |