Java tutorial
/** * 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.hadoop.io.crypto.bee; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class BeeUtil { private static final Log LOG = LogFactory.getLog(BeeUtil.class); public static String getStackTrace(Throwable throwable) { // for LOG, you can directly use LOG.error("", e). Writer writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); throwable.printStackTrace(printWriter); return writer.toString(); } /** * To retrieve property from * BeeConstants.BEE_CRYPTO_PROPERTIES_FILE(/usr/lib/bee * /bee-crypto/bee.properties) * * @param propertyName * @return property value or null for Exception FileNotFoundException or * IOException * @throws IOException */ public static String getCryptoProperty(String propertyName) throws IOException { FileInputStream fiStream = null; try { Properties prop = new Properties(); fiStream = new FileInputStream(new File(BeeConstants.BEE_CRYPTO_PROPERTIES_FILE)); prop.load(fiStream); return prop.getProperty(propertyName); } catch (FileNotFoundException e) { LOG.warn("bee-crypto perperties file not found: " + BeeConstants.BEE_CRYPTO_PROPERTIES_FILE); } catch (IOException e) { LOG.warn("Fail to retrieve property:" + propertyName, e); } finally { if (fiStream != null) fiStream.close(); } return null; } }