Utilities.java 4.32 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.InvalidSyntaxException
 *  org.osgi.framework.ServiceReference
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.bedrock.internal;

import com.adobe.aemds.bedrock.internal.OSGiUtils;
import com.adobe.aemds.datamanager.impl.UrlUtil;
import com.adobe.service.ConnectionFactory;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
import javax.naming.NameNotFoundException;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Utilities {
    private static final Logger logger = LoggerFactory.getLogger(Utilities.class);
    private static final String[] SERVICE_PREFIXES = new String[]{"", "com.adobe~", "com.adobe."};

    public static Properties getEnvironment() {
        Properties env = new Properties();
        env.putAll(System.getenv());
        return env;
    }

    public static ConnectionFactory serviceLookup(String inConnFactoryName) throws NameNotFoundException {
        ConnectionFactory svcConnection = null;
        for (int i = 0; i < SERVICE_PREFIXES.length; ++i) {
            block7 : {
                Object obj = null;
                String serviceName = SERVICE_PREFIXES[i] + inConnFactoryName;
                try {
                    BundleContext bc = OSGiUtils.getBundleContext();
                    String filter = "(bmc.service.name=" + serviceName + ")";
                    ServiceReference[] srs = null;
                    try {
                        srs = bc.getAllServiceReferences(ConnectionFactory.class.getName(), filter);
                    }
                    catch (InvalidSyntaxException e) {
                        throw new RuntimeException("Unexpected error parsing filter: " + filter, (Throwable)e);
                    }
                    if (srs == null || srs.length <= 0) break block7;
                    obj = bc.getService(srs[0]);
                    if (obj instanceof ConnectionFactory) {
                        svcConnection = (ConnectionFactory)obj;
                        break block7;
                    }
                    throw new IllegalArgumentException("Object instance is not of type ConnectionFactory: " + obj);
                }
                catch (ArrayIndexOutOfBoundsException exp) {
                    logger.debug("No service of type ConnectionFactory found with service-name " + serviceName + ", moving on...", (Throwable)exp);
                }
            }
            if (svcConnection != null) break;
        }
        if (svcConnection == null) {
            throw new NameNotFoundException(inConnFactoryName);
        }
        return svcConnection;
    }

    public static String replaceAll(String inOriginal, String inSubString, String inReplacement) {
        if (inOriginal == null || inSubString == null || inReplacement == null) {
            return inOriginal;
        }
        int last = 0;
        int pos = 0;
        StringBuffer stringValue = new StringBuffer(inOriginal);
        while ((pos = stringValue.toString().indexOf(inSubString, last)) >= 0) {
            last = pos;
            stringValue.replace(pos, pos + inSubString.length(), inReplacement);
        }
        return stringValue.toString();
    }

    public static String[] tokenizeString(String inStr) {
        StreamTokenizer st = new StreamTokenizer(new StringReader(inStr));
        st.resetSyntax();
        st.whitespaceChars(1, 32);
        st.wordChars(33, 126);
        st.quoteChar(34);
        st.quoteChar(39);
        ArrayList<String> al = new ArrayList<String>();
        try {
            while (st.nextToken() != -1) {
                al.add(st.sval);
            }
        }
        catch (IOException e) {
            logger.warn("Unexpected error encountered fetching next token for string " + inStr, (Throwable)e);
        }
        String[] resStrArray = al.toArray(new String[al.size()]);
        return resStrArray;
    }

    public static URL toURL(String url) throws IOException {
        return UrlUtil.toURL(url);
    }
}