From 53dfa7e61494fc38de8b527a91dfdb6051260e2a Mon Sep 17 00:00:00 2001 From: Robert Greig Date: Tue, 20 Feb 2007 16:51:32 +0000 Subject: (Path submitted by Rupert Smith) Qpid-338. Custom SASL implementation for Java 1.4 retrotranslation of the Java client. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@509642 13f79535-47bb-0310-9956-ffa450edef68 --- java/common/pom.xml | 74 +++++----- .../main/java/org/apache/qpid/util/FileUtils.java | 161 +++++++++++++++++++++ .../org/apache/qpid/util/PrettyPrintingUtils.java | 73 ++++++++++ 3 files changed, 272 insertions(+), 36 deletions(-) create mode 100644 java/common/src/main/java/org/apache/qpid/util/FileUtils.java create mode 100644 java/common/src/main/java/org/apache/qpid/util/PrettyPrintingUtils.java (limited to 'java/common') diff --git a/java/common/pom.xml b/java/common/pom.xml index c8168c34bb..5e94153346 100644 --- a/java/common/pom.xml +++ b/java/common/pom.xml @@ -15,7 +15,7 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - --> +--> @@ -67,35 +67,37 @@ - org.codehaus.mojo - retrotranslator-maven-plugin - - - package - - translate - - - ${project.build.directory}/${project.build.finalName}-java1.4.jar - ${retrotranslator.verify} - - ${retrotranslator.1.4-rt-path} - ${retrotranslator.1.4-jce-path} - ${retrotranslator.1.4-jsse-path} - - - - ${project.build.directory} - ${project.build.finalName}.jar - - - - + org.codehaus.mojo + retrotranslator-maven-plugin + + + package + + translate + + + ${project.build.directory}/${project.build.finalName}-java14.jar + ${retrotranslator.verify} + + ${retrotranslator.1.4-rt-path} + ${retrotranslator.1.4-jce-path} + ${retrotranslator.1.4-jsse-path} + ${retrotranslator.1.4-sasl-path} + + false + + + ${project.build.directory} + ${project.build.finalName}.jar + + + + - + - - + + org.codehaus.mojo build-helper-maven-plugin @@ -109,9 +111,9 @@ - ${project.build.directory}/${project.build.finalName}-java1.4.jar + ${project.build.directory}/${project.build.finalName}-java14.jar jar - java1.4 + java14 @@ -156,12 +158,12 @@ test - - - net.sf.retrotranslator - retrotranslator-runtime - provided - + + + net.sf.retrotranslator + retrotranslator-runtime + provided + diff --git a/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/java/common/src/main/java/org/apache/qpid/util/FileUtils.java new file mode 100644 index 0000000000..ba79a6e8d4 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/util/FileUtils.java @@ -0,0 +1,161 @@ +/* + * + * 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.util; + +import java.io.*; + +import org.apache.log4j.Logger; + +/** + * FileUtils provides some simple helper methods for working with files. It follows the convention of wrapping all + * checked exceptions as runtimes, so code using these methods is free of try-catch blocks but does not expect to + * recover from errors. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
Read a text file as a string. + *
Open a file or default resource as an input stream. + *
+ */ +public class FileUtils +{ + /** + * Reads a text file as a string. + * + * @param filename The name of the file. + * + * @return The contents of the file. + */ + public static String readFileAsString(String filename) + { + BufferedInputStream is = null; + + try + { + is = new BufferedInputStream(new FileInputStream(filename)); + } + catch (FileNotFoundException e) + { + throw new RuntimeException(e); + } + + return readStreamAsString(is); + } + + /** + * Reads a text file as a string. + * + * @param file The file. + * + * @return The contents of the file. + */ + public static String readFileAsString(File file) + { + BufferedInputStream is = null; + + try + { + is = new BufferedInputStream(new FileInputStream(file)); + } + catch (FileNotFoundException e) + { + throw new RuntimeException(e); + } + + return readStreamAsString(is); + } + + /** + * Reads the contents of a reader, one line at a time until the end of stream is encountered, and returns all + * together as a string. + * + * @param is The reader. + * + * @return The contents of the reader. + */ + private static String readStreamAsString(BufferedInputStream is) + { + try + { + byte[] data = new byte[4096]; + + StringBuffer inBuffer = new StringBuffer(); + + String line; + int read; + + while ((read = is.read(data)) != -1) + { + String s = new String(data, 0, read); + inBuffer.append(s); + } + + return inBuffer.toString(); + } + catch (IOException e) + { + throw new RuntimeException(e); + } + } + + /** + * Either opens the specified filename as an input stream, or uses the default resource loaded using the + * specified class loader, if opening the file fails or no file name is specified. + * + * @param filename The name of the file to open. + * @param defaultResource The name of the default resource on the classpath if the file cannot be opened. + * @param cl The classloader to load the default resource with. + * + * @return An input stream for the file or resource, or null if one could not be opened. + */ + public static InputStream openFileOrDefaultResource(String filename, String defaultResource, ClassLoader cl) + { + InputStream is = null; + + // Flag to indicate whether the default resource should be used. By default this is true, so that the default + // is used when opening the file fails. + boolean useDefault = true; + + // Try to open the file if one was specified. + if (filename != null) + { + try + { + is = new BufferedInputStream(new FileInputStream(new File(filename))); + + // Clear the default flag because the file was succesfully opened. + useDefault = false; + } + catch (FileNotFoundException e) + { + // Ignore this exception, the default will be used instead. + } + } + + // Load the default resource if a file was not specified, or if opening the file failed. + if (useDefault) + { + is = cl.getResourceAsStream(defaultResource); + } + + return is; + } +} diff --git a/java/common/src/main/java/org/apache/qpid/util/PrettyPrintingUtils.java b/java/common/src/main/java/org/apache/qpid/util/PrettyPrintingUtils.java new file mode 100644 index 0000000000..faeb9d7167 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/util/PrettyPrintingUtils.java @@ -0,0 +1,73 @@ +/* + * + * 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.util; + +/** + * Contains pretty printing convenienve methods for producing formatted logging output, mostly for debugging purposes. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
+ */ +public class PrettyPrintingUtils +{ + /** + * Pretty prints an array of ints as a string. + * + * @param array The array to pretty print. + * + * @return The pretty printed string. + */ + public static String printArray(int[] array) + { + String result = "["; + for (int i = 0; i < array.length; i++) + { + result += array[i]; + result += (i < (array.length - 1)) ? ", " : ""; + } + + result += "]"; + + return result; + } + + /** + * Pretty prints an array of strings as a string. + * + * @param array The array to pretty print. + * + * @return The pretty printed string. + */ + public static String printArray(String[] array) + { + String result = "["; + for (int i = 0; i < array.length; i++) + { + result += array[i]; + result += (i < (array.length - 1)) ? ", " : ""; + } + + result += "]"; + + return result; + } +} -- cgit v1.2.1