Java tutorial
/* * GLIMPSE: A generic and flexible monitoring infrastructure. * For further information: http://labsewiki.isti.cnr.it/labse/tools/glimpse/public/main * * Copyright (C) 2011 Software Engineering Laboratory - ISTI CNR - Pisa - Italy * * 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, either version 3 of the License, or * (at your option) any later version. * * 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, see <http://www.gnu.org/licenses/>. * */ package eu.learnpad.simulator.mon.utils; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import org.apache.commons.net.ntp.TimeStamp; import eu.learnpad.simulator.mon.probe.GlimpseAbstractProbe; /** * This class should be used only for debug purpose<br /> * because uses deprecated methods. * Helps to read text from files and to generate<br /> * Properties object from a .ini formed file * * @author Antonello Calabrò * */ public class Manager { @SuppressWarnings("deprecation") public static Properties Read(String fileName) { Properties readedProps = new Properties(); File file = new File(fileName); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; try { fis = new FileInputStream(file); // Here BufferedInputStream is added for fast reading. bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); // dis.available() returns 0 if the file does not have more lines. String property = ""; String key = ""; String value = ""; while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. property = dis.readLine().trim(); if (property.length() > 0) { key = property.substring(0, property.indexOf("=")); value = property.substring(property.indexOf("=") + 1, property.length()); readedProps.put(key.trim(), value.trim()); } } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (IOException e) { e.printStackTrace(); } return readedProps; } /** * It reads text from file and provides it on string * * * @param filePath the file to read path * @return a String containing all the file text */ @SuppressWarnings("deprecation") public static String ReadTextFromFile(String filePath) { File file = new File(filePath); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; StringBuilder strB = new StringBuilder(); try { fis = new FileInputStream(file); // Here BufferedInputStream is added for fast reading. bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. strB.append(dis.readLine()); } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (IOException e) { e.printStackTrace(); } return strB.toString(); } public static Properties createProbeSettingsPropertiesObject(String javaNamingFactoryInitial, String javaNamingProviderUrl, String javaNamingSecurityPrincipal, String javaNamingSecurityCredential, String connectionFactoryNames, String topicProbeTopic, boolean debug, String probeName, String probeChannel) { if (debug) DebugMessages.print(TimeStamp.getCurrentTime(), GlimpseAbstractProbe.class.getSimpleName(), "Creating Properties object "); Properties settings = new Properties(); settings.setProperty("java.naming.factory.initial", javaNamingFactoryInitial); settings.setProperty("java.naming.provider.url", javaNamingProviderUrl); settings.setProperty("java.naming.security.principal", javaNamingSecurityPrincipal); settings.setProperty("java.naming.security.credential", javaNamingSecurityCredential); settings.setProperty("connectionFactoryNames", connectionFactoryNames); settings.setProperty("topic.probeTopic", topicProbeTopic); settings.setProperty("probeName", probeName); settings.setProperty("probeChannel", probeChannel); if (debug) { DebugMessages.ok(); DebugMessages.line(); } return settings; } public static Properties createConsumerSettingsPropertiesObject(String javaNamingFactoryInitial, String javaNamingProviderUrl, String javaNamingSecurityPrincipal, String javaNamingSecurityCredential, String connectionFactoryNames, String topicServiceTopic, boolean debug, String consumerName) { if (debug) DebugMessages.print(TimeStamp.getCurrentTime(), "Consumer", "Creating Properties object "); Properties settings = new Properties(); settings.setProperty("java.naming.factory.initial", javaNamingFactoryInitial); settings.setProperty("java.naming.provider.url", javaNamingProviderUrl); settings.setProperty("java.naming.security.principal", javaNamingSecurityPrincipal); settings.setProperty("java.naming.security.credential", javaNamingSecurityCredential); settings.setProperty("connectionFactoryNames", connectionFactoryNames); settings.setProperty("topic.serviceTopic", topicServiceTopic); settings.setProperty("consumerName", consumerName); if (debug) { DebugMessages.ok(); DebugMessages.line(); } return settings; } }