Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC.
*
* ====================================================================
* Licensed 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.jclouds.util;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.base.Splitter.on;
import static com.google.common.base.Throwables.getCausalChain;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Closeables.closeQuietly;
import static org.jclouds.util.Patterns.CHAR_TO_PATTERN;
import static org.jclouds.util.Patterns.TOKEN_TO_PATTERN;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import org.jclouds.PropertiesBuilder;
import org.jclouds.crypto.Pems;
import org.jclouds.domain.Credentials;
import org.jclouds.logging.Logger;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.RestContextBuilder;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.io.OutputSupplier;
import com.google.inject.Module;
import com.google.inject.ProvisionException;
import com.google.inject.spi.Message;
/**
* General utilities used in jclouds code.
*
* @author Adrian Cole
*/
public class Utils {
public static Suppliercharset}
* @return properly encoded String.
*/
public static byte[] encodeString(String str, String charsetName) {
try {
return str.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
logger.warn(e, "Failed to encode string to bytes with encoding " + charsetName
+ ". Falling back to system's default encoding");
return str.getBytes();
}
}
/**
* Encode the given string with the UTF-8 encoding, the sane default. In the very unlikely event
* the encoding fails with {@link UnsupportedEncodingException}, log a warning and fall back to
* the system's default encoding.
*
* @param str
* what to encode
* @return properly encoded String.
*/
public static byte[] encodeString(String str) {
return encodeString(str, UTF8_ENCODING);
}
/**
* replaces tokens that are expressed as {token}
*
*
* ex. if input is "hello {where}"
* and replacements is "where" -> "world"
* then replaceTokens returns "hello world"
*
* @param input
* source to replace
* @param replacements
* token/value pairs
*/
public static String replaceTokens(String input, Map replacements) {
Matcher matcher = Patterns.TOKEN_PATTERN.matcher(input);
StringBuilder builder = new StringBuilder();
int i = 0;
while (matcher.find()) {
String replacement = replacements.get(matcher.group(1));
builder.append(input.substring(i, matcher.start()));
if (replacement == null)
builder.append(matcher.group(0));
else
builder.append(replacement);
i = matcher.end();
}
builder.append(input.substring(i, input.length()));
return builder.toString();
}
/**
* Will throw an exception if the argument is null or empty.
*
* @param nullableString
* string to verify. Can be null or empty.
*/
public static void checkNotEmpty(String nullableString) {
checkNotEmpty(nullableString, "Argument can't be null or empty");
}
/**
* Will throw an exception if the argument is null or empty. Accepts a custom error message.
*
* @param nullableString
* string to verify. Can be null or empty.
* @param message
* message to show in case of exception
*/
public static void checkNotEmpty(String nullableString, String message) {
checkArgument(nullableString != null && nullableString.length() > 0, message);
}
/**
* Gets a set of supported providers. Idea stolen from pallets (supported-clouds). Uses
* rest.properties to populate the set.
*
*/
public static Iterable getSupportedProviders() {
return getSupportedProvidersOfType(RestContextBuilder.class);
}
/**
* Gets a set of supported providers. Idea stolen from pallets (supported-clouds). Uses
* rest.properties to populate the set.
*
*/
@SuppressWarnings("unchecked")
public static Iterable getSupportedProvidersOfType(Class extends RestContextBuilder> type) {
Properties properties = new Properties();
try {
properties.load(Utils.class.getResourceAsStream("/rest.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
return getSupportedProvidersOfTypeInProperties(type, properties);
}
@SuppressWarnings("unchecked")
public static Iterable getSupportedProvidersOfTypeInProperties(
final Class extends RestContextBuilder> type, final Properties properties) {
return filter(transform(filter(properties.entrySet(), new Predicate>() {
@Override
public boolean apply(Entry