From a249dbcb7d2d3211ef3f5a32b49a867ab5e9407c Mon Sep 17 00:00:00 2001 From: Robert Greig Date: Thu, 15 Mar 2007 10:12:57 +0000 Subject: Commit of interop test stuff prior to M2 branch. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@518559 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/qpid/util/PropertiesUtils.java | 199 ++++++++++++++++++ .../java/org/apache/qpid/util/ReflectionUtils.java | 228 +++++++++++++++++++++ .../apache/qpid/util/ReflectionUtilsException.java | 44 ++++ 3 files changed, 471 insertions(+) create mode 100644 java/common/src/main/java/org/apache/qpid/util/PropertiesUtils.java create mode 100644 java/common/src/main/java/org/apache/qpid/util/ReflectionUtils.java create mode 100644 java/common/src/main/java/org/apache/qpid/util/ReflectionUtilsException.java (limited to 'java/common/src') diff --git a/java/common/src/main/java/org/apache/qpid/util/PropertiesUtils.java b/java/common/src/main/java/org/apache/qpid/util/PropertiesUtils.java new file mode 100644 index 0000000000..aa21841256 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/util/PropertiesUtils.java @@ -0,0 +1,199 @@ +/* + * + * 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.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Iterator; +import java.util.Properties; + +import org.apache.log4j.Logger; + +/** + * PropertiesHelper defines some static methods which are useful when working with properties + * files. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
Read properties from an input stream + *
Read properties from a file + *
Read properties from a URL + *
Read properties given a path to a file + *
Trim any whitespace from property values + *
+ */ +public class PropertiesUtils +{ + /** Used for logging. */ + private static final Logger log = Logger.getLogger(PropertiesUtils.class); + + /** + * Get properties from an input stream. + * + * @param is The input stream. + * + * @return The properties loaded from the input stream. + * + * @throws IOException If the is an I/O error reading from the stream. + */ + public static Properties getProperties(InputStream is) throws IOException + { + log.debug("getProperties(InputStream): called"); + + // Create properties object laoded from input stream + Properties properties = new Properties(); + + properties.load(is); + + return properties; + } + + /** + * Get properties from a file. + * + * @param file The file. + * + * @return The properties loaded from the file. + * + * @throws IOException If there is an I/O error reading from the file. + */ + public static Properties getProperties(File file) throws IOException + { + log.debug("getProperties(File): called"); + + // Open the file as an input stream + InputStream is = new FileInputStream(file); + + // Create properties object loaded from the stream + Properties properties = getProperties(is); + + // Close the file + is.close(); + + return properties; + } + + /** + * Get properties from a url. + * + * @param url The URL. + * + * @return The properties loaded from the url. + * + * @throws IOException If there is an I/O error reading from the URL. + */ + public static Properties getProperties(URL url) throws IOException + { + log.debug("getProperties(URL): called"); + + // Open the URL as an input stream + InputStream is = url.openStream(); + + // Create properties object loaded from the stream + Properties properties = getProperties(is); + + // Close the url + is.close(); + + return properties; + } + + /** + * Get properties from a path name. The path name may refer to either a file or a URL. + * + * @param pathname The path name. + * + * @return The properties loaded from the file or URL. + * + * @throws IOException If there is an I/O error reading from the URL or file named by the path. + */ + public static Properties getProperties(String pathname) throws IOException + { + log.debug("getProperties(String): called"); + + // Check that the path is not null + if (pathname == null) + { + return null; + } + + // Check if the path is a URL + if (isURL(pathname)) + { + // The path is a URL + return getProperties(new URL(pathname)); + } + else + { + // Assume the path is a file name + return getProperties(new File(pathname)); + } + } + + /** + * Trims whitespace from property values. This method returns a new set of properties + * the same as the properties specified as an argument but with any white space removed by + * the {@link java.lang.String#trim} method. + * + * @param properties The properties to trim whitespace from. + * + * @return The white space trimmed properties. + */ + public static Properties trim(Properties properties) + { + Properties trimmedProperties = new Properties(); + + // Loop over all the properties + for (Iterator i = properties.keySet().iterator(); i.hasNext();) + { + String next = (String) i.next(); + String nextValue = properties.getProperty(next); + + // Trim the value if it is not null + if (nextValue != null) + { + nextValue.trim(); + } + + // Store the trimmed value in the trimmed properties + trimmedProperties.setProperty(next, nextValue); + } + + return trimmedProperties; + } + + /** + * Helper method. Guesses whether a string is a URL or not. A String is considered to be a url if it begins with + * http:, ftp:, or uucp:. + * + * @param name The string to test for being a URL. + * + * @return True if the string is a URL and false if not. + */ + private static boolean isURL(String name) + { + return (name.toLowerCase().startsWith("http:") || name.toLowerCase().startsWith("ftp:") + || name.toLowerCase().startsWith("uucp:")); + } +} diff --git a/java/common/src/main/java/org/apache/qpid/util/ReflectionUtils.java b/java/common/src/main/java/org/apache/qpid/util/ReflectionUtils.java new file mode 100644 index 0000000000..724f504474 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/util/ReflectionUtils.java @@ -0,0 +1,228 @@ +/* + * + * 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.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Provides helper methods for operating on classes and methods using reflection. Reflection methods tend to return + * a lot of checked exception so writing code to use them can be tedious and harder to read, especially when such errors + * are not expected to occur. This class always works with {@link ReflectionUtilsException}, which is a runtime exception, + * to wrap the checked exceptions raised by the standard Java reflection methods. Code using it does not normally + * expect these errors to occur, usually does not have a recovery mechanism for them when they do, but is cleaner, + * quicker to write and easier to read in the majority of cases. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
Look up Classes by name. + *
Instantiate Classes by no-arg constructor. + *
+ */ +public class ReflectionUtils +{ + /** + * Gets the Class object for a named class. + * + * @param className The class to get the Class object for. + * + * @return The Class object for the named class. + */ + public static Class forName(String className) + { + try + { + return Class.forName(className); + } + catch (ClassNotFoundException e) + { + throw new ReflectionUtilsException("ClassNotFoundException whilst finding class.", e); + } + } + + /** + * Creates an instance of a Class, instantiated through its no-args constructor. + * + * @param cls The Class to instantiate. + * @param The Class type. + * + * @return An instance of the class. + */ + public static T newInstance(Class cls) + { + try + { + return cls.newInstance(); + } + catch (InstantiationException e) + { + throw new ReflectionUtilsException("InstantiationException whilst instantiating class.", e); + } + catch (IllegalAccessException e) + { + throw new ReflectionUtilsException("IllegalAccessException whilst instantiating class.", e); + } + } + + /** + * Calls a named method on an object with a specified set of parameters, any Java access modifier are overridden. + * + * @param o The object to call. + * @param method The method name to call. + * @param params The parameters to pass. + * @param paramClasses The argument types. + * + * @return The return value from the method call. + */ + public static Object callMethodOverridingIllegalAccess(Object o, String method, Object[] params, Class[] paramClasses) + { + // Get the objects class. + Class cls = o.getClass(); + + // Get the classes of the parameters. + /*Class[] paramClasses = new Class[params.length]; + + for (int i = 0; i < params.length; i++) + { + paramClasses[i] = params[i].getClass(); + }*/ + + try + { + // Try to find the matching method on the class. + Method m = cls.getDeclaredMethod(method, paramClasses); + + // Make it accessible. + m.setAccessible(true); + + // Invoke it with the parameters. + return m.invoke(o, params); + } + catch (NoSuchMethodException e) + { + throw new RuntimeException(e); + } + catch (IllegalAccessException e) + { + throw new RuntimeException(e); + } + catch (InvocationTargetException e) + { + throw new RuntimeException(e); + } + } + + /** + * Calls a named method on an object with a specified set of parameters. + * + * @param o The object to call. + * @param method The method name to call. + * @param params The parameters to pass. + * + * @return The return value from the method call. + */ + public static Object callMethod(Object o, String method, Object[] params) + { + // Get the objects class. + Class cls = o.getClass(); + + // Get the classes of the parameters. + Class[] paramClasses = new Class[params.length]; + + for (int i = 0; i < params.length; i++) + { + paramClasses[i] = params[i].getClass(); + } + + try + { + // Try to find the matching method on the class. + Method m = cls.getMethod(method, paramClasses); + + // Invoke it with the parameters. + return m.invoke(o, params); + } + catch (NoSuchMethodException e) + { + throw new RuntimeException(e); + } + catch (IllegalAccessException e) + { + throw new RuntimeException(e); + } + catch (InvocationTargetException e) + { + throw new RuntimeException(e); + } + } + + /** + * Calls a constuctor witht the specified arguments. + * + * @param constructor The constructor. + * @param args The arguments. + * @param The Class type. + * + * @return An instance of the class that the constructor is for. + */ + public static T newInstance(Constructor constructor, Object[] args) + { + try + { + return constructor.newInstance(args); + } + catch (InstantiationException e) + { + throw new RuntimeException(e); + } + catch (IllegalAccessException e) + { + throw new RuntimeException(e); + } + catch (InvocationTargetException e) + { + throw new RuntimeException(e); + } + } + + /** + * Gets the constructor of a class that takes the specified set of arguments if any matches. If no matching + * constructor is found then a runtime exception is raised. + * + * @param cls The class to get a constructor from. + * @param args The arguments to match. + * @param The class type. + * + * @return The constructor. + */ + public static Constructor getConstructor(Class cls, Class[] args) + { + try + { + return cls.getConstructor(args); + } + catch (NoSuchMethodException e) + { + throw new RuntimeException(e); + } + } +} diff --git a/java/common/src/main/java/org/apache/qpid/util/ReflectionUtilsException.java b/java/common/src/main/java/org/apache/qpid/util/ReflectionUtilsException.java new file mode 100644 index 0000000000..20499641ac --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/util/ReflectionUtilsException.java @@ -0,0 +1,44 @@ +/* + * + * 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; + +/** + * Wraps a checked exception that occurs when {@link ReflectionUtils} encounters checked exceptions using standard + * Java reflection methods. + * + *

+ *
CRC Card
Responsibilities Collaborations + *
Wrap a checked reflection exception. + *
+ */ +public class ReflectionUtilsException extends RuntimeException +{ + /** + * Creates a runtime reflection exception, from a checked one. + * + * @param message The message. + * @param cause The causing exception. + */ + public ReflectionUtilsException(String message, Throwable cause) + { + super(message, cause); + } +} -- cgit v1.2.1