http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/ProcessUtil.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/ProcessUtil.java b/utils/src/com/cloud/utils/ProcessUtil.java deleted file mode 100644 index 53137c4..0000000 --- a/utils/src/com/cloud/utils/ProcessUtil.java +++ /dev/null @@ -1,112 +0,0 @@ -// -// 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 com.cloud.utils; - -import java.io.File; -import java.io.IOException; -import java.util.Properties; - -import javax.naming.ConfigurationException; - -import org.apache.commons.io.FileUtils; -import org.apache.log4j.Logger; - -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.script.OutputInterpreter; -import com.cloud.utils.script.Script; - -public class ProcessUtil { - private static final Logger s_logger = Logger.getLogger(ProcessUtil.class.getName()); - - // paths cannot be hardcoded - public static void pidCheck(String pidDir, String run) throws ConfigurationException { - - String dir = pidDir == null ? "/var/run" : pidDir; - - try { - final File propsFile = PropertiesUtil.findConfigFile("environment.properties"); - if (propsFile == null) { - s_logger.debug("environment.properties could not be opened"); - } else { - final Properties props = PropertiesUtil.loadFromFile(propsFile); - dir = props.getProperty("paths.pid"); - if (dir == null) { - dir = pidDir == null ? "/var/run" : pidDir; - } - } - } catch (IOException e) { - s_logger.debug("environment.properties could not be opened"); - } - - final File pidFile = new File(dir + File.separator + run); - try { - if (!pidFile.createNewFile()) { - if (!pidFile.exists()) { - throw new ConfigurationException("Unable to write to " + pidFile.getAbsolutePath() + ". Are you sure you're running as root?"); - } - - final String pidLine = FileUtils.readFileToString(pidFile).trim(); - if (pidLine.isEmpty()) { - throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); - } - try { - final long pid = Long.parseLong(pidLine); - final Script script = new Script("bash", 120000, s_logger); - script.add("-c", "ps -p " + pid); - final String result = script.execute(); - if (result == null) { - throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); - } - if (!pidFile.delete()) { - throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); - } - if (!pidFile.createNewFile()) { - throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); - } - } catch (final NumberFormatException e) { - throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); - } - } - pidFile.deleteOnExit(); - - final Script script = new Script("bash", 120000, s_logger); - script.add("-c", "echo $PPID"); - final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser(); - script.execute(parser); - - final String pid = parser.getLine(); - - FileUtils.writeStringToFile(pidFile, pid + "\n"); - } catch (final IOException e) { - throw new CloudRuntimeException("Unable to create the " + pidFile.getAbsolutePath() + ". Are you running as root?", e); - } - } - - public static String dumpStack() { - StringBuilder sb = new StringBuilder(); - StackTraceElement[] elems = Thread.currentThread().getStackTrace(); - if (elems != null && elems.length > 0) { - for (StackTraceElement elem : elems) { - sb.append("\tat ").append(elem.getMethodName()).append("(").append(elem.getFileName()).append(":").append(elem.getLineNumber()).append(")\n"); - } - } - return sb.toString(); - } -}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/Profiler.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/Profiler.java b/utils/src/com/cloud/utils/Profiler.java deleted file mode 100644 index f8e44bd..0000000 --- a/utils/src/com/cloud/utils/Profiler.java +++ /dev/null @@ -1,91 +0,0 @@ -// -// 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 com.cloud.utils; - -public class Profiler { - - private static final long MILLIS_FACTOR = 1000l; - private static final double EXPONENT = 2d; - - - private Long startTickNanoSeconds; - private Long stopTickNanoSeconds; - - public long start() { - startTickNanoSeconds = System.nanoTime(); - return startTickNanoSeconds; - } - - public long stop() { - stopTickNanoSeconds = System.nanoTime(); - return stopTickNanoSeconds; - } - - /** - * 1 millisecond = 1e+6 nanoseconds - * 1 second = 1000 milliseconds = 1e+9 nanoseconds - * - * @return the duration in nanoseconds. - */ - public long getDuration() { - if (startTickNanoSeconds != null && stopTickNanoSeconds != null) { - final long timeInNanoSeconds = stopTickNanoSeconds - startTickNanoSeconds; - return timeInNanoSeconds; - } - - return -1; - } - - /** - * 1 millisecond = 1e+6 nanoseconds - * 1 second = 1000 millisecond = 1e+9 nanoseconds - * - * @return the duration in milliseconds. - */ - public long getDurationInMillis() { - if (startTickNanoSeconds != null && stopTickNanoSeconds != null) { - final long timeInMillis = (stopTickNanoSeconds - startTickNanoSeconds) / (long)Math.pow(MILLIS_FACTOR, EXPONENT); - return timeInMillis; - } - - return -1; - } - - public boolean isStarted() { - return startTickNanoSeconds != null; - } - - public boolean isStopped() { - return stopTickNanoSeconds != null; - } - - @Override - public String toString() { - if (startTickNanoSeconds == null) { - return "Not Started"; - } - - if (stopTickNanoSeconds == null) { - return "Started but not stopped"; - } - - return "Done. Duration: " + getDurationInMillis() + "ms"; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/PropertiesUtil.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/PropertiesUtil.java b/utils/src/com/cloud/utils/PropertiesUtil.java deleted file mode 100644 index 4cb89f7..0000000 --- a/utils/src/com/cloud/utils/PropertiesUtil.java +++ /dev/null @@ -1,196 +0,0 @@ -// -// 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 com.cloud.utils; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import org.apache.log4j.Logger; - -public class PropertiesUtil { - private static final Logger s_logger = Logger.getLogger(PropertiesUtil.class); - - /** - * Searches the class path and local paths to find the config file. - * @param path path to find. if it starts with / then it's absolute path. - * @return File or null if not found at all. - */ - - public static File findConfigFile(String path) { - ClassLoader cl = PropertiesUtil.class.getClassLoader(); - URL url = cl.getResource(path); - - if (url != null && "file".equals(url.getProtocol())) { - return new File(url.getFile()); - } - - url = ClassLoader.getSystemResource(path); - if (url != null && "file".equals(url.getProtocol())) { - return new File(url.getFile()); - } - - File file = new File(path); - if (file.exists()) { - return file; - } - - String newPath = "conf" + (path.startsWith(File.separator) ? "" : "/") + path; - url = ClassLoader.getSystemResource(newPath); - if (url != null && "file".equals(url.getProtocol())) { - return new File(url.getFile()); - } - - url = cl.getResource(newPath); - if (url != null && "file".equals(url.getProtocol())) { - return new File(url.getFile()); - } - - newPath = "conf" + (path.startsWith(File.separator) ? "" : File.separator) + path; - file = new File(newPath); - if (file.exists()) { - return file; - } - - newPath = System.getProperty("catalina.home"); - if (newPath == null) { - newPath = System.getenv("CATALINA_HOME"); - } - - if (newPath == null) { - newPath = System.getenv("CATALINA_BASE"); - } - - if (newPath == null) { - return null; - } - - file = new File(newPath + File.separator + "conf" + File.separator + path); - if (file.exists()) { - return file; - } - - return null; - } - - public static Map<String, Object> toMap(Properties props) { - Set<String> names = props.stringPropertyNames(); - HashMap<String, Object> map = new HashMap<String, Object>(names.size()); - for (String name : names) { - map.put(name, props.getProperty(name)); - } - - return map; - } - - /* - * Returns an InputStream for the given resource - * This is needed to read the files within a jar in classpath. - */ - public static InputStream openStreamFromURL(String path) { - ClassLoader cl = PropertiesUtil.class.getClassLoader(); - URL url = cl.getResource(path); - if (url != null) { - try { - InputStream stream = url.openStream(); - return stream; - } catch (IOException ioex) { - return null; - } - } - return null; - } - - public static void loadFromJar(Properties properties, String configFile) throws IOException { - InputStream stream = PropertiesUtil.openStreamFromURL(configFile); - if (stream != null) { - properties.load(stream); - } else { - s_logger.error("Unable to find properties file: " + configFile); - } - } - - // Returns key=value pairs by parsing a commands.properties/config file - // with syntax; key=cmd;value (with this syntax cmd is stripped) and key=value - public static Map<String, String> processConfigFile(String[] configFiles) { - Map<String, String> configMap = new HashMap<String, String>(); - Properties preProcessedCommands = new Properties(); - for (String configFile : configFiles) { - File commandsFile = findConfigFile(configFile); - if (commandsFile != null) { - try { - loadFromFile(preProcessedCommands, commandsFile); - } catch (IOException ioe) { - s_logger.error("IO Exception loading properties file", ioe); - } - } - else { - // in case of a file within a jar in classpath, try to open stream using url - try { - loadFromJar(preProcessedCommands, configFile); - } catch (IOException e) { - s_logger.error("IO Exception loading properties file from jar", e); - } - } - } - - for (Object key : preProcessedCommands.keySet()) { - String preProcessedCommand = preProcessedCommands.getProperty((String)key); - int splitIndex = preProcessedCommand.lastIndexOf(";"); - String value = preProcessedCommand.substring(splitIndex + 1); - configMap.put((String)key, value); - } - - return configMap; - } - - /** - * Load a Properties object with contents from a File. - * @param properties the properties object to be loaded - * @param file the file to load from - * @throws IOException - */ - public static void loadFromFile(final Properties properties, final File file) - throws IOException { - try (final InputStream stream = new FileInputStream(file)) { - properties.load(stream); - } - } - - /** - * Load the file and return the contents as a Properties object. - * @param file the file to load - * @return A Properties object populated - * @throws IOException - */ - public static Properties loadFromFile(final File file) - throws IOException { - final Properties properties = new Properties(); - loadFromFile(properties, file); - return properties; - } - -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/ReflectUtil.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/ReflectUtil.java b/utils/src/com/cloud/utils/ReflectUtil.java deleted file mode 100644 index c8ae954..0000000 --- a/utils/src/com/cloud/utils/ReflectUtil.java +++ /dev/null @@ -1,213 +0,0 @@ -// -// 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 com.cloud.utils; - -import static java.beans.Introspector.getBeanInfo; -import static java.util.Collections.emptyList; -import static java.util.Collections.unmodifiableList; - -import java.beans.BeanInfo; -import java.beans.IntrospectionException; -import java.beans.PropertyDescriptor; -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.apache.log4j.Logger; -import org.reflections.Reflections; -import org.reflections.util.ConfigurationBuilder; -import org.reflections.util.ClasspathHelper; -import org.reflections.scanners.SubTypesScanner; -import org.reflections.scanners.TypeAnnotationsScanner; - -import com.google.common.collect.ImmutableSet; - -import com.cloud.utils.exception.CloudRuntimeException; - -public class ReflectUtil { - - private static final Logger s_logger = Logger.getLogger(ReflectUtil.class); - private static final Logger logger = Logger.getLogger(Reflections.class); - - public static Pair<Class<?>, Field> getAnyField(Class<?> clazz, String fieldName) { - try { - return new Pair<Class<?>, Field>(clazz, clazz.getDeclaredField(fieldName)); - } catch (SecurityException e) { - throw new CloudRuntimeException("How the heck?", e); - } catch (NoSuchFieldException e) { - // Do I really want this? No I don't but what can I do? It only throws the NoSuchFieldException. - Class<?> parent = clazz.getSuperclass(); - if (parent != null) { - return getAnyField(parent, fieldName); - } - return null; - } - } - - // Gets all classes with some annotation from a package - public static Set<Class<?>> getClassesWithAnnotation(Class<? extends Annotation> annotation, String[] packageNames) { - Reflections reflections; - Set<Class<?>> classes = new HashSet<Class<?>>(); - ConfigurationBuilder builder=new ConfigurationBuilder(); - for (String packageName : packageNames) { - builder.addUrls(ClasspathHelper.forPackage(packageName)); - } - builder.setScanners(new SubTypesScanner(),new TypeAnnotationsScanner()); - reflections = new Reflections(builder); - classes.addAll(reflections.getTypesAnnotatedWith(annotation)); - return classes; - } - - // Checks against posted search classes if cmd is async - public static boolean isCmdClassAsync(Class<?> cmdClass, Class<?>[] searchClasses) { - boolean isAsync = false; - Class<?> superClass = cmdClass; - - while (superClass != null && superClass != Object.class) { - String superName = superClass.getName(); - for (Class<?> baseClass : searchClasses) { - if (superName.equals(baseClass.getName())) { - isAsync = true; - break; - } - } - if (isAsync) - break; - superClass = superClass.getSuperclass(); - } - return isAsync; - } - - // Returns all fields until a base class for a cmd class - public static List<Field> getAllFieldsForClass(Class<?> cmdClass, Class<?> baseClass) { - List<Field> fields = new ArrayList<Field>(); - Collections.addAll(fields, cmdClass.getDeclaredFields()); - Class<?> superClass = cmdClass.getSuperclass(); - while (baseClass.isAssignableFrom(superClass) && baseClass != superClass) { - Field[] superClassFields = superClass.getDeclaredFields(); - if (superClassFields != null) - Collections.addAll(fields, superClassFields); - superClass = superClass.getSuperclass(); - } - return fields; - } - - /** - * Returns all unique fields except excludeClasses for a cmd class - * @param cmdClass the class in which fields should be collected - * @param excludeClasses the classes whose fields must be ignored - * @return list of fields - */ - public static Set<Field> getAllFieldsForClass(Class<?> cmdClass, Class<?>[] excludeClasses) { - Set<Field> fields = new HashSet<Field>(); - Collections.addAll(fields, cmdClass.getDeclaredFields()); - Class<?> superClass = cmdClass.getSuperclass(); - - while (superClass != null && superClass != Object.class) { - String superName = superClass.getName(); - boolean isNameEqualToSuperName = false; - for (Class<?> baseClass : excludeClasses) { - if (superName.equals(baseClass.getName())) { - isNameEqualToSuperName = true; - } - } - - if (!isNameEqualToSuperName) { - Field[] superClassFields = superClass.getDeclaredFields(); - if (superClassFields != null) { - Collections.addAll(fields, superClassFields); - } - } - superClass = superClass.getSuperclass(); - } - return fields; - } - - public static List<String> flattenProperties(final Object target, final Class<?> clazz) { - return flattenPropeties(target, clazz, "class"); - } - - public static List<String> flattenPropeties(final Object target, final Class<?> clazz, final String... excludedProperties) { - return flattenProperties(target, clazz, ImmutableSet.copyOf(excludedProperties)); - } - - private static List<String> flattenProperties(final Object target, final Class<?> clazz, final ImmutableSet<String> excludedProperties) { - - assert clazz != null; - - if (target == null) { - return emptyList(); - } - - assert clazz.isAssignableFrom(target.getClass()); - - try { - - final BeanInfo beanInfo = getBeanInfo(clazz); - final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); - - final List<String> serializedProperties = new ArrayList<String>(); - for (final PropertyDescriptor descriptor : descriptors) { - - if (excludedProperties.contains(descriptor.getName())) { - continue; - } - - serializedProperties.add(descriptor.getName()); - final Object value = descriptor.getReadMethod().invoke(target); - serializedProperties.add(value != null ? value.toString() : "null"); - - } - - return unmodifiableList(serializedProperties); - - } catch (IntrospectionException e) { - s_logger.warn("Ignored IntrospectionException when serializing class " + target.getClass().getCanonicalName(), e); - } catch (IllegalArgumentException e) { - s_logger.warn("Ignored IllegalArgumentException when serializing class " + target.getClass().getCanonicalName(), e); - } catch (IllegalAccessException e) { - s_logger.warn("Ignored IllegalAccessException when serializing class " + target.getClass().getCanonicalName(), e); - } catch (InvocationTargetException e) { - s_logger.warn("Ignored InvocationTargetException when serializing class " + target.getClass().getCanonicalName(), e); - } - - return emptyList(); - - } - - public static String getEntityName(Class clz){ - if(clz == null) - return null; - - String entityName = clz.getName(); - int index = entityName.lastIndexOf("."); - if (index != -1) { - return entityName.substring(index + 1); - }else{ - return entityName; - } - } - -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/ReflectionUse.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/ReflectionUse.java b/utils/src/com/cloud/utils/ReflectionUse.java deleted file mode 100644 index a5a78e2..0000000 --- a/utils/src/com/cloud/utils/ReflectionUse.java +++ /dev/null @@ -1,31 +0,0 @@ -// -// 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 com.cloud.utils; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@Target({METHOD}) -@Retention(RUNTIME) -public @interface ReflectionUse { -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/S3Utils.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/S3Utils.java b/utils/src/com/cloud/utils/S3Utils.java deleted file mode 100644 index 6efe76b..0000000 --- a/utils/src/com/cloud/utils/S3Utils.java +++ /dev/null @@ -1,603 +0,0 @@ -// -// 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 com.cloud.utils; - -import static com.amazonaws.Protocol.HTTP; -import static com.amazonaws.Protocol.HTTPS; -import static com.cloud.utils.StringUtils.join; -import static java.io.File.createTempFile; -import static java.lang.String.format; -import static java.lang.System.currentTimeMillis; -import static java.util.Collections.emptyList; -import static java.util.Collections.singletonList; -import static java.util.Collections.unmodifiableList; -import static org.apache.commons.lang.ArrayUtils.isEmpty; -import static org.apache.commons.lang.StringUtils.isBlank; -import static org.apache.commons.lang.StringUtils.isNotBlank; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FilenameFilter; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -import org.apache.commons.lang.ArrayUtils; -import org.apache.log4j.Logger; - -import com.amazonaws.AmazonClientException; -import com.amazonaws.ClientConfiguration; -import com.amazonaws.HttpMethod; -import com.amazonaws.auth.AWSCredentials; -import com.amazonaws.auth.BasicAWSCredentials; -import com.amazonaws.services.s3.AmazonS3; -import com.amazonaws.services.s3.AmazonS3Client; -import com.amazonaws.services.s3.model.Bucket; -import com.amazonaws.services.s3.model.CannedAccessControlList; -import com.amazonaws.services.s3.model.GetObjectRequest; -import com.amazonaws.services.s3.model.ListObjectsRequest; -import com.amazonaws.services.s3.model.ObjectListing; -import com.amazonaws.services.s3.model.ObjectMetadata; -import com.amazonaws.services.s3.model.PutObjectRequest; -import com.amazonaws.services.s3.model.S3Object; -import com.amazonaws.services.s3.model.S3ObjectSummary; -import com.amazonaws.services.s3.transfer.TransferManager; -import com.amazonaws.services.s3.transfer.Upload; -import com.cloud.utils.exception.CloudRuntimeException; - -public final class S3Utils { - - private static final Logger LOGGER = Logger.getLogger(S3Utils.class); - - public static final String SEPARATOR = "/"; - - private static final int MIN_BUCKET_NAME_LENGTH = 3; - private static final int MAX_BUCKET_NAME_LENGTH = 63; - - private S3Utils() { - super(); - } - - public static AmazonS3 acquireClient(final ClientOptions clientOptions) { - - final AWSCredentials credentials = new BasicAWSCredentials(clientOptions.getAccessKey(), clientOptions.getSecretKey()); - - final ClientConfiguration configuration = new ClientConfiguration(); - - if (clientOptions.isHttps() != null) { - configuration.setProtocol(clientOptions.isHttps() == true ? HTTPS : HTTP); - } - - if (clientOptions.getConnectionTimeout() != null) { - configuration.setConnectionTimeout(clientOptions.getConnectionTimeout()); - } - - if (clientOptions.getMaxErrorRetry() != null) { - configuration.setMaxErrorRetry(clientOptions.getMaxErrorRetry()); - } - - if (clientOptions.getSocketTimeout() != null) { - configuration.setSocketTimeout(clientOptions.getSocketTimeout()); - } - - if (clientOptions.getUseTCPKeepAlive() != null) { - //configuration.setUseTcpKeepAlive(clientOptions.getUseTCPKeepAlive()); - LOGGER.debug("useTCPKeepAlive not supported by old AWS SDK"); - } - - if (clientOptions.getConnectionTtl() != null) { - //configuration.setConnectionTTL(clientOptions.getConnectionTtl()); - LOGGER.debug("connectionTtl not supported by old AWS SDK"); - } - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Creating S3 client with configuration: [protocol: %1$s, connectionTimeOut: " + "%2$s, maxErrorRetry: %3$s, socketTimeout: %4$s, useTCPKeepAlive: %5$s, connectionTtl: %6$s]", - configuration.getProtocol(), configuration.getConnectionTimeout(), configuration.getMaxErrorRetry(), configuration.getSocketTimeout(), - -1, -1)); - } - - final AmazonS3Client client = new AmazonS3Client(credentials, configuration); - - if (isNotBlank(clientOptions.getEndPoint())) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Setting the end point for S3 client %1$s to %2$s.", client, clientOptions.getEndPoint())); - } - client.setEndpoint(clientOptions.getEndPoint()); - } - - return client; - - } - - public static void putFile(final ClientOptions clientOptions, final File sourceFile, final String bucketName, final String key) { - - assert clientOptions != null; - assert sourceFile != null; - assert !isBlank(bucketName); - assert !isBlank(key); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Sending file %1$s as S3 object %2$s in " + "bucket %3$s", sourceFile.getName(), key, bucketName)); - } - - acquireClient(clientOptions).putObject(bucketName, key, sourceFile); - - } - - public static void putObject(final ClientOptions clientOptions, final InputStream sourceStream, final String bucketName, final String key) { - - assert clientOptions != null; - assert sourceStream != null; - assert !isBlank(bucketName); - assert !isBlank(key); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Sending stream as S3 object %1$s in " + "bucket %2$s", key, bucketName)); - } - - acquireClient(clientOptions).putObject(bucketName, key, sourceStream, null); - - } - - public static void putObject(final ClientOptions clientOptions, final PutObjectRequest req) { - - assert clientOptions != null; - assert req != null; - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Sending stream as S3 object using PutObjectRequest")); - } - - acquireClient(clientOptions).putObject(req); - - } - - // multi-part upload file - public static void mputFile(final ClientOptions clientOptions, final File sourceFile, final String bucketName, final String key) throws InterruptedException { - - assert clientOptions != null; - assert sourceFile != null; - assert !isBlank(bucketName); - assert !isBlank(key); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Multipart sending file %1$s as S3 object %2$s in " + "bucket %3$s", sourceFile.getName(), key, bucketName)); - } - TransferManager tm = new TransferManager(S3Utils.acquireClient(clientOptions)); - Upload upload = tm.upload(bucketName, key, sourceFile); - upload.waitForCompletion(); - } - - // multi-part upload object - public static void mputObject(final ClientOptions clientOptions, final InputStream sourceStream, final String bucketName, final String key) - throws InterruptedException { - - assert clientOptions != null; - assert sourceStream != null; - assert !isBlank(bucketName); - assert !isBlank(key); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Multipart sending stream as S3 object %1$s in " + "bucket %2$s", key, bucketName)); - } - TransferManager tm = new TransferManager(S3Utils.acquireClient(clientOptions)); - Upload upload = tm.upload(bucketName, key, sourceStream, null); - upload.waitForCompletion(); - } - - // multi-part upload object - public static void mputObject(final ClientOptions clientOptions, final PutObjectRequest req) throws InterruptedException { - - assert clientOptions != null; - assert req != null; - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Multipart sending object to S3 using PutObjectRequest"); - } - TransferManager tm = new TransferManager(S3Utils.acquireClient(clientOptions)); - Upload upload = tm.upload(req); - upload.waitForCompletion(); - - } - - public static void setObjectAcl(final ClientOptions clientOptions, final String bucketName, final String key, final CannedAccessControlList acl) { - - assert clientOptions != null; - assert acl != null; - - acquireClient(clientOptions).setObjectAcl(bucketName, key, acl); - - } - - public static URL generatePresignedUrl(final ClientOptions clientOptions, final String bucketName, final String key, final Date expiration) { - - assert clientOptions != null; - assert !isBlank(bucketName); - assert !isBlank(key); - - return acquireClient(clientOptions).generatePresignedUrl(bucketName, key, expiration, HttpMethod.GET); - - } - - // Note that whenever S3Object is returned, client code needs to close the internal stream to avoid resource leak. - public static S3Object getObject(final ClientOptions clientOptions, final String bucketName, final String key) { - - assert clientOptions != null; - assert !isBlank(bucketName); - assert !isBlank(key); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Get S3 object %1$s in " + "bucket %2$s", key, bucketName)); - } - - return acquireClient(clientOptions).getObject(bucketName, key); - - } - - @SuppressWarnings("unchecked") - public static File getFile(final ClientOptions clientOptions, final String bucketName, final String key, final File targetDirectory, - final FileNamingStrategy namingStrategy) { - - assert clientOptions != null; - assert isNotBlank(bucketName); - assert isNotBlank(key); - assert targetDirectory != null && targetDirectory.isDirectory(); - assert namingStrategy != null; - - final AmazonS3 connection = acquireClient(clientOptions); - - File tempFile = null; - try { - - tempFile = createTempFile(join("-", targetDirectory.getName(), currentTimeMillis(), "part"), "tmp", targetDirectory); - tempFile.deleteOnExit(); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Downloading object %1$s from bucket %2$s to temp file %3$s", key, bucketName, tempFile.getName())); - } - - try { - connection.getObject(new GetObjectRequest(bucketName, key), tempFile); - } catch (AmazonClientException ex) { - // hack to handle different ETAG format generated from RiakCS for multi-part uploaded object - String msg = ex.getMessage(); - if (!msg.contains("verify integrity")) { - throw ex; - } - } - - final File targetFile = new File(targetDirectory, namingStrategy.determineFileName(key)); - tempFile.renameTo(targetFile); - - return targetFile; - - } catch (FileNotFoundException e) { - - throw new CloudRuntimeException(format("Failed open file %1$s in order to get object %2$s from bucket %3$s.", targetDirectory.getAbsoluteFile(), bucketName, - key), e); - - } catch (IOException e) { - - throw new CloudRuntimeException(format("Unable to allocate temporary file in directory %1$s to download %2$s:%3$s from S3", - targetDirectory.getAbsolutePath(), bucketName, key), e); - - } finally { - - if (tempFile != null) { - tempFile.delete(); - } - - } - - } - - public static List<File> getDirectory(final ClientOptions clientOptions, final String bucketName, final String sourcePath, final File targetDirectory, - final FileNamingStrategy namingStrategy) { - - assert clientOptions != null; - assert isNotBlank(bucketName); - assert isNotBlank(sourcePath); - assert targetDirectory != null; - - final AmazonS3 connection = acquireClient(clientOptions); - - // List the objects in the source directory on S3 - final List<S3ObjectSummary> objectSummaries = listDirectory(bucketName, sourcePath, connection); - final List<File> files = new ArrayList<File>(); - - for (final S3ObjectSummary objectSummary : objectSummaries) { - - files.add(getFile(clientOptions, bucketName, objectSummary.getKey(), targetDirectory, namingStrategy)); - - } - - return unmodifiableList(files); - - } - - public static List<S3ObjectSummary> getDirectory(final ClientOptions clientOptions, final String bucketName, final String sourcePath) { - assert clientOptions != null; - assert isNotBlank(bucketName); - assert isNotBlank(sourcePath); - - final AmazonS3 connection = acquireClient(clientOptions); - - // List the objects in the source directory on S3 - return listDirectory(bucketName, sourcePath, connection); - } - - private static List<S3ObjectSummary> listDirectory(final String bucketName, final String directory, final AmazonS3 client) { - - List<S3ObjectSummary> objects = new ArrayList<S3ObjectSummary>(); - ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(directory + SEPARATOR); - - ObjectListing ol = client.listObjects(listObjectsRequest); - if(ol.isTruncated()) { - do { - objects.addAll(ol.getObjectSummaries()); - listObjectsRequest.setMarker(ol.getNextMarker()); - ol = client.listObjects(listObjectsRequest); - } while (ol.isTruncated()); - } - else { - objects.addAll(ol.getObjectSummaries()); - } - - if (objects.isEmpty()) { - return emptyList(); - } - - return unmodifiableList(objects); - } - - public static void putDirectory(final ClientOptions clientOptions, final String bucketName, final File directory, final FilenameFilter fileNameFilter, - final ObjectNamingStrategy namingStrategy) { - - assert clientOptions != null; - assert isNotBlank(bucketName); - assert directory != null && directory.isDirectory(); - assert fileNameFilter != null; - assert namingStrategy != null; - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Putting directory %1$s in S3 bucket %2$s.", directory.getAbsolutePath(), bucketName)); - } - - // Determine the list of files to be sent using the passed filter ... - final File[] files = directory.listFiles(fileNameFilter); - - if (LOGGER.isTraceEnabled()) { - LOGGER.trace(format("Putting files (%1$s) in S3 bucket %2$s.", ArrayUtils.toString(files, "no files found"), bucketName)); - } - - // Skip spinning up an S3 connection when no files will be sent ... - if (isEmpty(files)) { - return; - } - - final AmazonS3 client = acquireClient(clientOptions); - - // Send the files to S3 using the passed ObjectNaming strategy to - // determine the key ... - for (final File file : files) { - final String key = namingStrategy.determineKey(file); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format("Putting file %1$s into bucket %2$s with key %3$s.", file.getAbsolutePath(), bucketName, key)); - } - client.putObject(bucketName, key, file); - } - - } - - public static void deleteObject(final ClientOptions clientOptions, final String bucketName, final String key) { - - assert clientOptions != null; - assert isNotBlank(bucketName); - assert isNotBlank(key); - - final AmazonS3 client = acquireClient(clientOptions); - - client.deleteObject(bucketName, key); - - } - - public static void deleteDirectory(final ClientOptions clientOptions, final String bucketName, final String directoryName) { - - assert clientOptions != null; - assert isNotBlank(bucketName); - assert isNotBlank(directoryName); - - final AmazonS3 client = acquireClient(clientOptions); - - final List<S3ObjectSummary> objects = listDirectory(bucketName, directoryName, client); - - for (final S3ObjectSummary object : objects) { - - client.deleteObject(bucketName, object.getKey()); - - } - - client.deleteObject(bucketName, directoryName); - - } - - public static boolean canConnect(final ClientOptions clientOptions) { - - try { - - acquireClient(clientOptions); - return true; - - } catch (AmazonClientException e) { - - LOGGER.warn("Ignored Exception while checking connection options", e); - return false; - - } - - } - - public static boolean doesBucketExist(final ClientOptions clientOptions, final String bucketName) { - - assert clientOptions != null; - assert !isBlank(bucketName); - - try { - - final List<Bucket> buckets = acquireClient(clientOptions).listBuckets(); - - for (Bucket bucket : buckets) { - if (bucket.getName().equals(bucketName)) { - return true; - } - } - - return false; - - } catch (AmazonClientException e) { - - LOGGER.warn("Ignored Exception while checking bucket existence", e); - return false; - - } - - } - - public static boolean canReadWriteBucket(final ClientOptions clientOptions, final String bucketName) { - - assert clientOptions != null; - assert isNotBlank(bucketName); - - try { - - final AmazonS3 client = acquireClient(clientOptions); - - final String fileContent = "testing put and delete"; - final InputStream inputStream = new ByteArrayInputStream(fileContent.getBytes()); - final String key = UUID.randomUUID().toString() + ".txt"; - - final ObjectMetadata metadata = new ObjectMetadata(); - metadata.setContentLength(fileContent.length()); - - client.putObject(bucketName, key, inputStream, metadata); - client.deleteObject(bucketName, key); - - return true; - - } catch (AmazonClientException e) { - - return false; - - } - - } - - public static List<String> checkClientOptions(ClientOptions clientOptions) { - - assert clientOptions != null; - - List<String> errorMessages = new ArrayList<String>(); - - errorMessages.addAll(checkRequiredField("access key", clientOptions.getAccessKey())); - errorMessages.addAll(checkRequiredField("secret key", clientOptions.getSecretKey())); - - errorMessages.addAll(checkOptionalField("connection timeout", clientOptions.getConnectionTimeout())); - errorMessages.addAll(checkOptionalField("socket timeout", clientOptions.getSocketTimeout())); - errorMessages.addAll(checkOptionalField("max error retries", clientOptions.getMaxErrorRetry())); - errorMessages.addAll(checkOptionalField("connection ttl", clientOptions.getConnectionTtl())); - - return unmodifiableList(errorMessages); - - } - - public static List<String> checkBucketName(final String bucketLabel, final String bucket) { - - assert isNotBlank(bucketLabel); - assert isNotBlank(bucket); - - final List<String> errorMessages = new ArrayList<String>(); - - if (bucket.length() < MIN_BUCKET_NAME_LENGTH) { - errorMessages.add(format("The length of %1$s " + "for the %2$s must have a length of at least %3$s " + "characters", bucket, bucketLabel, - MIN_BUCKET_NAME_LENGTH)); - } - - if (bucket.length() > MAX_BUCKET_NAME_LENGTH) { - errorMessages.add(format("The length of %1$s " + "for the %2$s must not have a length of at greater" + " than %3$s characters", bucket, bucketLabel, - MAX_BUCKET_NAME_LENGTH)); - } - - return unmodifiableList(errorMessages); - - } - - private static List<String> checkOptionalField(final String fieldName, final Integer fieldValue) { - if (fieldValue != null && fieldValue < 0) { - return singletonList(format("The value of %1$s must " + "be greater than zero.", fieldName)); - } - return emptyList(); - } - - private static List<String> checkRequiredField(String fieldName, String fieldValue) { - if (isBlank(fieldValue)) { - return singletonList(format("A %1$s must be specified.", fieldName)); - } - return emptyList(); - } - - public interface ClientOptions { - - String getAccessKey(); - - String getSecretKey(); - - String getEndPoint(); - - Boolean isHttps(); - - Integer getConnectionTimeout(); - - Integer getMaxErrorRetry(); - - Integer getSocketTimeout(); - - Boolean getUseTCPKeepAlive(); - - Integer getConnectionTtl(); - } - - public interface ObjectNamingStrategy { - - String determineKey(File file); - - } - - public interface FileNamingStrategy { - - String determineFileName(String key); - - } - -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/SerialVersionUID.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/SerialVersionUID.java b/utils/src/com/cloud/utils/SerialVersionUID.java deleted file mode 100644 index e4ea217..0000000 --- a/utils/src/com/cloud/utils/SerialVersionUID.java +++ /dev/null @@ -1,69 +0,0 @@ -// -// 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 com.cloud.utils; - -/** - * purposes. This is purely on an honor system though. You should always - **/ -public interface SerialVersionUID { - public static final long Base = 0x564D4F70 << 32; // 100 brownie points if you guess what this is and tell me. - - public static final long UUID = Base | 0x1; - public static final long CloudRuntimeException = Base | 0x2; - public static final long CloudStartupServlet = Base | 0x3; - public static final long CloudServiceImpl = Base | 0x4; - public static final long AccountLimitException = Base | 0x5; - public static final long InsufficientVirtualNetworkCapacityException = Base | 0x7; - public static final long NetworkUnavailableException = Base | 0x8; - public static final long Ip = Base | 0x9; - public static final long UnsupportedVersionException = Base | 0xb; - public static final long DataCenterIpAddressPK = Base | 0xc; - public static final long UnableToExecuteException = Base | 0xd; - public static final long ExecutionException = Base | 0xe; - public static final long VnetKey = Base | 0xf; - public static final long InsufficientServerCapacityException = Base | 0x10; - public static final long InsufficientAddressCapacityException = Base | 0x11; - public static final long ManagementServerException = Base | 0x12; - public static final long HAStateException = Base | 0x13; - public static final long InsufficientStorageCapacityException = Base | 0x14; - public static final long InsufficientCapacityException = Base | 0x15; - public static final long ConcurrentOperationException = Base | 0x16; - public static final long AgentUnavailableException = Base | 0x17; - public static final long OperationTimedoutException = Base | 0x18; - public static final long StorageUnavailableException = Base | 0x19; - public static final long InfficientVirtualNetworkCapacityException = Base | 0x1a; - public static final long DiscoveryException = Base | 0x1b; - public static final long ConflictingNetworkSettingException = Base | 0x1c; - public static final long CloudAuthenticationException = Base | 0x1d; - public static final long AsyncCommandQueued = Base | 0x1e; - public static final long ResourceUnavailableException = Base | 0x1f; - public static final long ConnectionException = Base | 0x20; - public static final long PermissionDeniedException = Base | 0x21; - public static final long sshException = Base | 0x22; - public static final long HttpCallException = Base | 0x23; - public static final long VirtualMachineMigrationException = Base | 0x24; - public static final long DiscoveredWithErrorException = Base | 0x25; - public static final long NoTransitionException = Base | 0x26; - public static final long CloudExecutionException = Base | 0x27; - public static final long CallFailedException = Base | 0x28; - public static final long UnableDeleteHostException = Base | 0x29; - public static final long AffinityConflictException = Base | 0x2a; - public static final long JobCancellationException = Base | 0x2b; -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/StringUtils.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/StringUtils.java b/utils/src/com/cloud/utils/StringUtils.java deleted file mode 100644 index c598be8..0000000 --- a/utils/src/com/cloud/utils/StringUtils.java +++ /dev/null @@ -1,323 +0,0 @@ -// -// 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 com.cloud.utils; - -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.owasp.esapi.StringUtilities; - -public class StringUtils { - private static final char[] hexChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - - private static Charset preferredACSCharset; - private static final String UTF8 = "UTF-8"; - - static { - if (isUtf8Supported()) { - preferredACSCharset = Charset.forName(UTF8); - } else { - preferredACSCharset = Charset.defaultCharset(); - } - } - - public static Charset getPreferredCharset() { - return preferredACSCharset; - } - - public static boolean isUtf8Supported() { - return Charset.isSupported(UTF8); - } - - protected static Charset getDefaultCharset() { - return Charset.defaultCharset(); - } - - public static String join(final Iterable<? extends Object> iterable, final String delim) { - final StringBuilder sb = new StringBuilder(); - if (iterable != null) { - final Iterator<? extends Object> iter = iterable.iterator(); - if (iter.hasNext()) { - final Object next = iter.next(); - sb.append(next.toString()); - } - while (iter.hasNext()) { - final Object next = iter.next(); - sb.append(delim + next.toString()); - } - } - return sb.toString(); - } - - public static String join(final String delimiter, final Object... components) { - return org.apache.commons.lang.StringUtils.join(components, delimiter); - } - - public static boolean isNotBlank(final String str) { - if (str != null && str.trim().length() > 0) { - return true; - } - - return false; - } - - public static String cleanupTags(String tags) { - if (tags != null) { - final String[] tokens = tags.split(","); - final StringBuilder t = new StringBuilder(); - for (int i = 0; i < tokens.length; i++) { - t.append(tokens[i].trim()).append(","); - } - t.delete(t.length() - 1, t.length()); - tags = t.toString(); - } - - return tags; - } - - /** - * @param tags - * @return List of tags - */ - public static List<String> csvTagsToList(final String tags) { - final List<String> tagsList = new ArrayList<String>(); - - if (tags != null) { - final String[] tokens = tags.split(","); - for (int i = 0; i < tokens.length; i++) { - tagsList.add(tokens[i].trim()); - } - } - - return tagsList; - } - - /** - * Converts a List of tags to a comma separated list - * @param tags - * @return String containing a comma separated list of tags - */ - - public static String listToCsvTags(final List<String> tagsList) { - final StringBuilder tags = new StringBuilder(); - if (tagsList.size() > 0) { - for (int i = 0; i < tagsList.size(); i++) { - tags.append(tagsList.get(i)); - if (i != tagsList.size() - 1) { - tags.append(','); - } - } - } - - return tags.toString(); - } - - public static String getExceptionStackInfo(final Throwable e) { - final StringBuffer sb = new StringBuffer(); - - sb.append(e.toString()).append("\n"); - final StackTraceElement[] elemnents = e.getStackTrace(); - for (final StackTraceElement element : elemnents) { - sb.append(element.getClassName()).append("."); - sb.append(element.getMethodName()).append("("); - sb.append(element.getFileName()).append(":"); - sb.append(element.getLineNumber()).append(")"); - sb.append("\n"); - } - - return sb.toString(); - } - - public static String unicodeEscape(final String s) { - final StringBuilder sb = new StringBuilder(); - for (int i = 0; i < s.length(); i++) { - final char c = s.charAt(i); - if (c >> 7 > 0) { - sb.append("\\u"); - sb.append(hexChar[c >> 12 & 0xF]); // append the hex character for the left-most 4-bits - sb.append(hexChar[c >> 8 & 0xF]); // hex for the second group of 4-bits from the left - sb.append(hexChar[c >> 4 & 0xF]); // hex for the third group - sb.append(hexChar[c & 0xF]); // hex for the last group, e.g., the right most 4-bits - } else { - sb.append(c); - } - } - return sb.toString(); - } - - public static String getMaskedPasswordForDisplay(final String password) { - if (password == null || password.isEmpty()) { - return "*"; - } - - final StringBuffer sb = new StringBuffer(); - sb.append(password.charAt(0)); - for (int i = 1; i < password.length(); i++) { - sb.append("*"); - } - - return sb.toString(); - } - - // removes a password request param and it's value, also considering password is in query parameter value which has been url encoded - private static final Pattern REGEX_PASSWORD_QUERYSTRING = Pattern.compile("(&|%26)?[^(&|%26)]*((p|P)assword|accesskey|secretkey)(=|%3D).*?(?=(%26|[&'\"]|$))"); - - // removes a password/accesskey/ property from a response json object - private static final Pattern REGEX_PASSWORD_JSON = Pattern.compile("\"((p|P)assword|accesskey|secretkey)\":\\s?\".*?\",?"); - - private static final Pattern REGEX_PASSWORD_DETAILS = Pattern.compile("(&|%26)?details(\\[|%5B)\\d*(\\]|%5D)\\.key(=|%3D)((p|P)assword|accesskey|secretkey)(?=(%26|[&'\"]))"); - - private static final Pattern REGEX_PASSWORD_DETAILS_INDEX = Pattern.compile("details(\\[|%5B)\\d*(\\]|%5D)"); - - private static final Pattern REGEX_REDUNDANT_AND = Pattern.compile("(&|%26)(&|%26)+"); - - // Responsible for stripping sensitive content from request and response strings - public static String cleanString(final String stringToClean) { - String cleanResult = ""; - if (stringToClean != null) { - cleanResult = REGEX_PASSWORD_QUERYSTRING.matcher(stringToClean).replaceAll(""); - cleanResult = REGEX_PASSWORD_JSON.matcher(cleanResult).replaceAll(""); - final Matcher detailsMatcher = REGEX_PASSWORD_DETAILS.matcher(cleanResult); - while (detailsMatcher.find()) { - final Matcher detailsIndexMatcher = REGEX_PASSWORD_DETAILS_INDEX.matcher(detailsMatcher.group()); - if (detailsIndexMatcher.find()) { - cleanResult = cleanDetails(cleanResult, detailsIndexMatcher.group()); - } - } - } - return cleanResult; - } - - public static String cleanDetails(final String stringToClean, final String detailsIndexSting) { - String cleanResult = stringToClean; - for (final String log : stringToClean.split("&|%26")) { - if (log.contains(detailsIndexSting)) { - cleanResult = cleanResult.replace(log, ""); - } - } - cleanResult = REGEX_REDUNDANT_AND.matcher(cleanResult).replaceAll("&"); - return cleanResult; - } - - public static boolean areTagsEqual(final String tags1, final String tags2) { - if (tags1 == null && tags2 == null) { - return true; - } - - if (tags1 != null && tags2 == null) { - return false; - } - - if (tags1 == null && tags2 != null) { - return false; - } - - final String delimiter = ","; - - final List<String> lstTags1 = new ArrayList<String>(); - final String[] aTags1 = tags1.split(delimiter); - - for (final String tag1 : aTags1) { - lstTags1.add(tag1.toLowerCase()); - } - - final List<String> lstTags2 = new ArrayList<String>(); - final String[] aTags2 = tags2.split(delimiter); - - for (final String tag2 : aTags2) { - lstTags2.add(tag2.toLowerCase()); - } - - return lstTags1.containsAll(lstTags2) && lstTags2.containsAll(lstTags1); - } - - public static String stripControlCharacters(final String s) { - return StringUtilities.stripControls(s); - } - - public static int formatForOutput(final String text, final int start, final int columns, final char separator) { - if (start >= text.length()) { - return -1; - } - - int end = start + columns; - if (end > text.length()) { - end = text.length(); - } - final String searchable = text.substring(start, end); - final int found = searchable.lastIndexOf(separator); - return found > 0 ? found : end - start; - } - - public static Map<String, String> stringToMap(final String s) { - final Map<String, String> map = new HashMap<String, String>(); - final String[] elements = s.split(";"); - for (final String parts : elements) { - final String[] keyValue = parts.split(":"); - map.put(keyValue[0], keyValue[1]); - } - return map; - } - - public static String mapToString(final Map<String, String> map) { - String s = ""; - for (final Map.Entry<String, String> entry : map.entrySet()) { - s += entry.getKey() + ":" + entry.getValue() + ";"; - } - if (s.length() > 0) { - s = s.substring(0, s.length() - 1); - } - return s; - } - - public static <T> List<T> applyPagination(final List<T> originalList, final Long startIndex, final Long pageSizeVal) { - // Most likely pageSize will never exceed int value, and we need integer to partition the listToReturn - final boolean applyPagination = startIndex != null && pageSizeVal != null - && startIndex <= Integer.MAX_VALUE && startIndex >= Integer.MIN_VALUE && pageSizeVal <= Integer.MAX_VALUE - && pageSizeVal >= Integer.MIN_VALUE; - List<T> listWPagination = null; - if (applyPagination) { - listWPagination = new ArrayList<>(); - final int index = startIndex.intValue() == 0 ? 0 : startIndex.intValue() / pageSizeVal.intValue(); - final List<List<T>> partitions = StringUtils.partitionList(originalList, pageSizeVal.intValue()); - if (index < partitions.size()) { - listWPagination = partitions.get(index); - } - } - return listWPagination; - } - - private static <T> List<List<T>> partitionList(final List<T> originalList, final int chunkSize) { - final List<List<T>> listOfChunks = new ArrayList<List<T>>(); - for (int i = 0; i < originalList.size() / chunkSize; i++) { - listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize + chunkSize)); - } - if (originalList.size() % chunkSize != 0) { - listOfChunks.add(originalList.subList(originalList.size() - originalList.size() % chunkSize, originalList.size())); - } - return listOfChunks; - } -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/SwiftUtil.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/SwiftUtil.java b/utils/src/com/cloud/utils/SwiftUtil.java deleted file mode 100644 index 1136818..0000000 --- a/utils/src/com/cloud/utils/SwiftUtil.java +++ /dev/null @@ -1,239 +0,0 @@ -// -// 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 com.cloud.utils; - -import java.io.File; -import java.util.Arrays; -import java.util.Map; - -import org.apache.log4j.Logger; - -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.script.OutputInterpreter; -import com.cloud.utils.script.Script; - -public class SwiftUtil { - private static Logger logger = Logger.getLogger(SwiftUtil.class); - private static final long SWIFT_MAX_SIZE = 5L * 1024L * 1024L * 1024L; - - public interface SwiftClientCfg { - String getAccount(); - - String getUserName(); - - String getKey(); - - String getEndPoint(); - } - - private static String getSwiftCLIPath() { - String swiftCLI = Script.findScript("scripts/storage/secondary", "swift"); - if (swiftCLI == null) { - logger.debug("Can't find swift cli at scripts/storage/secondary/swift"); - throw new CloudRuntimeException("Can't find swift cli at scripts/storage/secondary/swift"); - } - return swiftCLI; - } - - public static boolean postMeta(SwiftClientCfg cfg, String container, String object, Map<String, String> metas) { - String swiftCli = getSwiftCLIPath(); - StringBuilder cms = new StringBuilder(); - for (Map.Entry<String, String> entry : metas.entrySet()) { - cms.append(" -m "); - cms.append(entry.getKey()); - cms.append(":"); - cms.append(entry.getValue()); - cms.append(" "); - } - Script command = new Script("/bin/bash", logger); - command.add("-c"); - command.add("/usr/bin/python " + swiftCli + " -A " + cfg.getEndPoint() + " -U " + cfg.getAccount() + ":" + cfg.getUserName() + " -K " + cfg.getKey() + " post " + - container + " " + object + " " + cms.toString()); - OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser(); - String result = command.execute(parser); - if (result != null) { - throw new CloudRuntimeException("Failed to post meta" + result); - } - return true; - } - - public static String putObject(SwiftClientCfg cfg, File srcFile, String container, String fileName) { - String swiftCli = getSwiftCLIPath(); - if (fileName == null) { - fileName = srcFile.getName(); - } - String srcDirectory = srcFile.getParent(); - Script command = new Script("/bin/bash", logger); - long size = srcFile.length(); - command.add("-c"); - if (size <= SWIFT_MAX_SIZE) { - command.add("cd " + srcDirectory + ";/usr/bin/python " + swiftCli + " -A " + cfg.getEndPoint() + " -U " + cfg.getAccount() + ":" + cfg.getUserName() + - " -K " + cfg.getKey() + " upload " + container + " " + fileName); - } else { - command.add("cd " + srcDirectory + ";/usr/bin/python " + swiftCli + " -A " + cfg.getEndPoint() + " -U " + cfg.getAccount() + ":" + cfg.getUserName() + - " -K " + cfg.getKey() + " upload -S " + SWIFT_MAX_SIZE + " " + container + " " + fileName); - } - OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); - String result = command.execute(parser); - if (result != null) { - throw new CloudRuntimeException("Failed to upload file: " + result); - } - - if (parser.getLines() != null) { - String[] lines = parser.getLines().split("\\n"); - for (String line : lines) { - if (line.contains("Errno") || line.contains("failed") || line.contains("not found")) { - throw new CloudRuntimeException("Failed to upload file: " + Arrays.toString(lines)); - } - } - } - - return container + File.separator + srcFile.getName(); - } - - private static StringBuilder buildSwiftCmd(SwiftClientCfg swift) { - String swiftCli = getSwiftCLIPath(); - StringBuilder sb = new StringBuilder(); - sb.append(" /usr/bin/python "); - sb.append(swiftCli); - sb.append(" -A "); - sb.append(swift.getEndPoint()); - sb.append(" -U "); - sb.append(swift.getAccount()); - sb.append(":"); - sb.append(swift.getUserName()); - sb.append(" -K "); - sb.append(swift.getKey()); - sb.append(" "); - return sb; - } - - public static String[] list(SwiftClientCfg swift, String container, String rFilename) { - getSwiftCLIPath(); - Script command = new Script("/bin/bash", logger); - command.add("-c"); - - StringBuilder swiftCmdBuilder = buildSwiftCmd(swift); - swiftCmdBuilder.append(" list "); - swiftCmdBuilder.append(container); - - if (rFilename != null) { - swiftCmdBuilder.append(" -p "); - swiftCmdBuilder.append(rFilename); - } - - command.add(swiftCmdBuilder.toString()); - OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); - String result = command.execute(parser); - if (result == null && parser.getLines() != null && !parser.getLines().equalsIgnoreCase("")) { - String[] lines = parser.getLines().split("\\n"); - return lines; - } else { - if (result != null) { - String errMsg = "swiftList failed , err=" + result; - logger.debug("Failed to list " + errMsg); - } else { - String errMsg = "swiftList failed, no lines returns"; - logger.debug("Failed to list " + errMsg); - } - } - return new String[0]; - } - - public static File getObject(SwiftClientCfg cfg, File destDirectory, String swiftPath) { - int firstIndexOfSeparator = swiftPath.indexOf(File.separator); - String container = swiftPath.substring(0, firstIndexOfSeparator); - String srcPath = swiftPath.substring(firstIndexOfSeparator + 1); - String destFilePath = null; - if (destDirectory.isDirectory()) { - destFilePath = destDirectory.getAbsolutePath() + File.separator + srcPath; - } else { - destFilePath = destDirectory.getAbsolutePath(); - } - String swiftCli = getSwiftCLIPath(); - Script command = new Script("/bin/bash", logger); - command.add("-c"); - command.add("/usr/bin/python " + swiftCli + " -A " + cfg.getEndPoint() + " -U " + cfg.getAccount() + ":" + cfg.getUserName() + " -K " + cfg.getKey() + - " download " + container + " " + srcPath + " -o " + destFilePath); - OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); - String result = command.execute(parser); - if (result != null) { - String errMsg = "swiftDownload failed err=" + result; - logger.debug(errMsg); - throw new CloudRuntimeException("failed to get object: " + swiftPath); - } - if (parser.getLines() != null) { - String[] lines = parser.getLines().split("\\n"); - for (String line : lines) { - if (line.contains("Errno") || line.contains("failed")) { - String errMsg = "swiftDownload failed , err=" + Arrays.toString(lines); - logger.debug(errMsg); - throw new CloudRuntimeException("Failed to get object: " + swiftPath); - } - } - } - return new File(destFilePath); - } - - public static String getContainerName(String type, Long id) { - if (type.startsWith("T")) { - return "T-" + id; - } else if (type.startsWith("S")) { - return "S-" + id; - } else if (type.startsWith("V")) { - return "V-" + id; - } - return null; - } - - public static String[] splitSwiftPath(String path) { - int index = path.indexOf(File.separator); - if (index == -1) { - return null; - } - String[] paths = new String[2]; - paths[0] = path.substring(0, index); - paths[1] = path.substring(index + 1); - return paths; - } - - public static boolean deleteObject(SwiftClientCfg cfg, String path) { - Script command = new Script("/bin/bash", logger); - command.add("-c"); - - String[] paths = splitSwiftPath(path); - if (paths == null) { - return false; - } - String container = paths[0]; - String objectName = paths[1]; - - StringBuilder swiftCmdBuilder = buildSwiftCmd(cfg); - swiftCmdBuilder.append(" delete "); - swiftCmdBuilder.append(container); - swiftCmdBuilder.append(" "); - swiftCmdBuilder.append(objectName); - - command.add(swiftCmdBuilder.toString()); - OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); - command.execute(parser); - return true; - } -} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/83fd8f60/utils/src/com/cloud/utils/Ternary.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/Ternary.java b/utils/src/com/cloud/utils/Ternary.java deleted file mode 100644 index fdc2fc9..0000000 --- a/utils/src/com/cloud/utils/Ternary.java +++ /dev/null @@ -1,85 +0,0 @@ -// -// 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 com.cloud.utils; - -public class Ternary<T, U, V> { - private T t; - private U u; - private V v; - - public Ternary(T t, U u, V v) { - this.t = t; - this.u = u; - this.v = v; - } - - public T first() { - return t; - } - - public void first(T t) { - this.t = t; - } - - public U second() { - return u; - } - - public void second(U u) { - this.u = u; - } - - public V third() { - return v; - } - - public void third(V v) { - this.v = v; - } - - @Override - // Note: This means any two pairs with null for both values will match each - // other but what can I do? This is due to stupid type erasure. - public - int hashCode() { - return (t != null ? t.hashCode() : 0) | (u != null ? u.hashCode() : 0) | (v != null ? v.hashCode() : 0); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Ternary)) { - return false; - } - Ternary<?, ?, ?> that = (Ternary<?, ?, ?>)obj; - return (t != null ? t.equals(that.t) : that.t == null) && (u != null ? u.equals(that.u) : that.u == null) && (v != null ? v.equals(that.v) : that.v == null); - } - - @Override - public String toString() { - StringBuilder b = new StringBuilder("T["); - b.append(t != null ? t.toString() : "null"); - b.append(":"); - b.append(u != null ? u.toString() : "null"); - b.append(":"); - b.append(v != null ? v.toString() : "null"); - b.append("]"); - return b.toString(); - } -}