Example usage for org.springframework.util ObjectUtils isEmpty

List of usage examples for org.springframework.util ObjectUtils isEmpty

Introduction

In this page you can find the example usage for org.springframework.util ObjectUtils isEmpty.

Prototype

@SuppressWarnings("rawtypes")
public static boolean isEmpty(@Nullable Object obj) 

Source Link

Document

Determine whether the given object is empty.

Usage

From source file:org.eclipse.gemini.blueprint.extender.support.ApplicationContextConfiguration.java

public ApplicationContextConfiguration(Bundle bundle, ConfigurationScanner configurationScanner) {
    Assert.notNull(bundle);/*from   w  ww.  j a va 2  s  .com*/
    Assert.notNull(configurationScanner);
    this.bundle = bundle;
    this.configurationScanner = configurationScanner;

    Dictionary headers = this.bundle.getHeaders();

    String[] configs = this.configurationScanner.getConfigurations(bundle);

    this.isSpringPoweredBundle = !ObjectUtils.isEmpty(configs);
    this.configurationLocations = configs;

    hasTimeout = ConfigUtils.isDirectiveDefined(headers, ConfigUtils.DIRECTIVE_TIMEOUT);

    long option = ConfigUtils.getTimeOut(headers);
    // translate into ms
    this.timeout = (option >= 0 ? option * 1000 : option);
    this.publishContextAsService = ConfigUtils.getPublishContext(headers);
    this.asyncCreation = ConfigUtils.getCreateAsync(headers);
    this.waitForDeps = ConfigUtils.getWaitForDependencies(headers);

    // create toString
    StringBuilder buf = new StringBuilder();
    buf.append("AppCtxCfg [Bundle=");
    buf.append(OsgiStringUtils.nullSafeSymbolicName(bundle));
    buf.append("]isSpringBundle=");
    buf.append(isSpringPoweredBundle());
    buf.append("|async=");
    buf.append(isCreateAsynchronously());
    buf.append("|wait-for-deps=");
    buf.append(isWaitForDependencies());
    buf.append("|publishCtx=");
    buf.append(isPublishContextAsService());
    buf.append("|timeout=");
    buf.append(getTimeout() / 1000);
    buf.append("s");
    toString = buf.toString();
    if (log.isTraceEnabled()) {
        log.trace("Configuration: " + toString);
    }
}

From source file:org.eclipse.gemini.blueprint.io.internal.resolver.PackageAdminResolver.java

public ImportedBundle[] getImportedBundles(Bundle bundle) {
    boolean trace = log.isTraceEnabled();

    PackageAdmin pa = getPackageAdmin();

    // create map with bundles as keys and a list of packages as value
    Map<Bundle, List<String>> importedBundles = new LinkedHashMap<Bundle, List<String>>(8);

    // 1. consider required bundles first

    // see if there are required bundle(s) defined
    String[] entries = OsgiHeaderUtils.getRequireBundle(bundle);

    // 1. if so, locate the bundles
    for (int i = 0; i < entries.length; i++) {
        String[] parsed = OsgiHeaderUtils.parseRequiredBundleString(entries[i]);
        // trim the strings just to be on the safe side (some implementations allows whitespaces, some don't)
        String symName = parsed[0].trim();
        String versionRange = parsed[1].trim();
        Bundle[] foundBundles = pa.getBundles(symName, versionRange);

        if (!ObjectUtils.isEmpty(foundBundles)) {
            Bundle requiredBundle = foundBundles[0];

            // find exported packages
            ExportedPackage[] exportedPackages = pa.getExportedPackages(requiredBundle);
            if (exportedPackages != null)
                addExportedPackages(importedBundles, requiredBundle, exportedPackages);
        } else {/*from w ww  .  j a  va  2 s.  c o  m*/
            if (trace) {
                log.trace("Cannot find required bundle " + symName + "|" + versionRange);
            }
        }
    }

    // 2. determine imported bundles 
    // get all bundles
    Bundle[] bundles = bundleContext.getBundles();

    for (int i = 0; i < bundles.length; i++) {
        Bundle analyzedBundle = bundles[i];
        // if the bundle is already included (it's a required one), there's no need to look at it again
        if (!importedBundles.containsKey(analyzedBundle)) {
            ExportedPackage[] epa = pa.getExportedPackages(analyzedBundle);
            if (epa != null)
                for (int j = 0; j < epa.length; j++) {
                    ExportedPackage exportedPackage = epa[j];
                    Bundle[] importingBundles = exportedPackage.getImportingBundles();
                    if (importingBundles != null)
                        for (int k = 0; k < importingBundles.length; k++) {
                            if (bundle.equals(importingBundles[k])) {
                                addImportedBundle(importedBundles, exportedPackage);
                            }
                        }
                }
        }
    }

    List<ImportedBundle> importedBundlesList = new ArrayList<ImportedBundle>(importedBundles.size());

    for (Map.Entry<Bundle, List<String>> entry : importedBundles.entrySet()) {
        Bundle importedBundle = entry.getKey();
        List<String> packages = entry.getValue();
        importedBundlesList.add(
                new ImportedBundle(importedBundle, (String[]) packages.toArray(new String[packages.size()])));
    }

    return (ImportedBundle[]) importedBundlesList.toArray(new ImportedBundle[importedBundlesList.size()]);
}

From source file:org.eclipse.gemini.blueprint.service.importer.support.AbstractOsgiServiceImportFactoryBean.java

public void afterPropertiesSet() {
    Assert.notNull(this.bundleContext, "Required 'bundleContext' property was not set.");
    Assert.notNull(classLoader, "Required 'classLoader' property was not set.");
    Assert.isTrue(/*from   w  w w.ja  va 2  s  . c om*/
            !ObjectUtils.isEmpty(interfaces) || StringUtils.hasText(filter)
                    || StringUtils.hasText(serviceBeanName),
            "At least the interface or filter or service name needs to be defined to import an OSGi service");
    if (ObjectUtils.isEmpty(interfaces)) {
        log.warn("OSGi importer [" + beanName + "] definition contains no interfaces: "
                + "all invocations will be executed on the proxy and not on the backing service");
    }

    // validate specified classes
    Assert.isTrue(!ClassUtils.containsUnrelatedClasses(interfaces),
            "More then one concrete class specified; cannot create proxy.");

    this.listeners = (listeners == null ? new OsgiServiceLifecycleListener[0] : listeners);
    this.interfaces = (interfaces == null ? new Class<?>[0] : interfaces);
    this.filter = (StringUtils.hasText(filter) ? filter : "");

    getUnifiedFilter(); // eager initialization of the cache to catch filter errors
}

From source file:org.eclipse.gemini.blueprint.service.importer.support.AbstractOsgiServiceImportFactoryBean.java

/**
 * Assembles the configuration properties into one unified OSGi filter. Note that this implementation creates the
 * filter on the first call and caches it afterwards.
 * /*  w w w  .  j a  v a 2  s. c o m*/
 * @return unified filter based on this factory bean configuration
 */
public Filter getUnifiedFilter() {
    if (unifiedFilter != null) {
        return unifiedFilter;
    }

    String filterWithClasses = (!ObjectUtils.isEmpty(interfaces)
            ? OsgiFilterUtils.unifyFilter(interfaces, filter)
            : filter);

    boolean trace = log.isTraceEnabled();
    if (trace)
        log.trace("Unified classes=" + ObjectUtils.nullSafeToString(interfaces) + " and filter=[" + filter
                + "]  in=[" + filterWithClasses + "]");

    // add the serviceBeanName/Blueprint component name constraint
    String nameFilter;
    if (StringUtils.hasText(serviceBeanName)) {
        StringBuilder nsFilter = new StringBuilder("(|(");
        nsFilter.append(OsgiServicePropertiesResolver.BEAN_NAME_PROPERTY_KEY);
        nsFilter.append("=");
        nsFilter.append(serviceBeanName);
        nsFilter.append(")(");
        nsFilter.append(OsgiServicePropertiesResolver.SPRING_DM_BEAN_NAME_PROPERTY_KEY);
        nsFilter.append("=");
        nsFilter.append(serviceBeanName);
        nsFilter.append(")(");
        nsFilter.append(OsgiServicePropertiesResolver.BLUEPRINT_COMP_NAME);
        nsFilter.append("=");
        nsFilter.append(serviceBeanName);
        nsFilter.append("))");
        nameFilter = nsFilter.toString();
    } else {
        nameFilter = null;
    }

    String filterWithServiceBeanName = filterWithClasses;
    if (nameFilter != null) {
        StringBuilder finalFilter = new StringBuilder();
        finalFilter.append("(&");
        finalFilter.append(filterWithClasses);
        finalFilter.append(nameFilter);
        finalFilter.append(")");
        filterWithServiceBeanName = finalFilter.toString();
    }

    if (trace)
        log.trace("Unified serviceBeanName [" + ObjectUtils.nullSafeToString(serviceBeanName) + "] and filter=["
                + filterWithClasses + "]  in=[" + filterWithServiceBeanName + "]");

    // create (which implies validation) the actual filter
    unifiedFilter = OsgiFilterUtils.createFilter(filterWithServiceBeanName);

    return unifiedFilter;
}

From source file:org.eclipse.gemini.blueprint.service.importer.support.internal.util.OsgiServiceBindingUtils.java

public static void callListenersBind(Object serviceProxy, ServiceReference reference,
        OsgiServiceLifecycleListener[] listeners) {
    if (!ObjectUtils.isEmpty(listeners)) {
        boolean debug = log.isDebugEnabled();

        // get a Dictionary implementing a Map
        Dictionary properties = OsgiServiceReferenceUtils.getServicePropertiesSnapshot(reference);
        for (int i = 0; i < listeners.length; i++) {
            if (debug)
                log.debug("Calling bind on " + listeners[i] + " w/ reference " + reference);
            try {
                listeners[i].bind(serviceProxy, (Map) properties);
            } catch (Exception ex) {
                log.warn("Bind method on listener " + listeners[i] + " threw exception ", ex);
            }// w  w  w. j  av  a 2s .  c o  m
            if (debug)
                log.debug("Called bind on " + listeners[i] + " w/ reference " + reference);
        }
    }
}

From source file:org.eclipse.gemini.blueprint.service.importer.support.internal.util.OsgiServiceBindingUtils.java

public static void callListenersUnbind(Object serviceProxy, ServiceReference reference,
        OsgiServiceLifecycleListener[] listeners) {
    if (!ObjectUtils.isEmpty(listeners)) {
        boolean debug = log.isDebugEnabled();
        // get a Dictionary implementing a Map
        Dictionary properties = (reference != null
                ? OsgiServiceReferenceUtils.getServicePropertiesSnapshot(reference)
                : null);// w  w  w  .  j  a v a 2  s  .  c  om
        for (int i = 0; i < listeners.length; i++) {
            if (debug)
                log.debug("Calling unbind on " + listeners[i] + " w/ reference " + reference);
            try {
                listeners[i].unbind(serviceProxy, (Map) properties);
            } catch (Exception ex) {
                log.warn("Unbind method on listener " + listeners[i] + " threw exception ", ex);
            }
            if (debug)
                log.debug("Called unbind on " + listeners[i] + " w/ reference " + reference);
        }
    }
}

From source file:org.eclipse.gemini.blueprint.util.ClassUtilsTest.java

@Test
public void testAutoDetectClassesForPublishingDisabled() throws Exception {
    Class<?>[] clazz = ClassUtils.getClassHierarchy(Integer.class, ClassUtils.ClassSet.INTERFACES);
    assertFalse(ObjectUtils.isEmpty(clazz));
    assertEquals(2, clazz.length);//w ww .ja v a2s  .com
}

From source file:org.eclipse.gemini.blueprint.util.DebugUtils.java

/**
 * Tries to debug the cause of the {@link Throwable}s that can appear when
 * loading classes in OSGi environments (for example when creating proxies).
 * /*from   www  .j a  v a 2 s  .c o m*/
 * <p/> This method will try to determine the class that caused the problem
 * and to search for it in the given bundle or through the classloaders of
 * the given classes.
 * 
 * It will look at the classes are visible by the given bundle on debug
 * level and do a bundle discovery process on trace level.
 * 
 * The method accepts also an array of classes which will be used for
 * loading the 'problematic' class that caused the exception on debug level.
 * 
 * @param loadingThrowable class loading {@link Throwable} (such as
 * {@link NoClassDefFoundError} or {@link ClassNotFoundException})
 * @param bundle bundle used for loading the classes
 * @param classes (optional) array of classes that will be used for loading
 * the problematic class
 */
public static void debugClassLoadingThrowable(Throwable loadingThrowable, Bundle bundle, Class<?>[] classes) {

    String className = null;
    // NoClassDefFoundError
    if (loadingThrowable instanceof NoClassDefFoundError) {
        className = loadingThrowable.getMessage();
        if (className != null)
            className = className.replace('/', '.');
    }
    // ClassNotFound
    else if (loadingThrowable instanceof ClassNotFoundException) {
        className = loadingThrowable.getMessage();

        if (className != null)
            className = className.replace('/', '.');
    }

    if (className != null) {

        debugClassLoading(bundle, className, null);

        if (!ObjectUtils.isEmpty(classes) && log.isDebugEnabled()) {
            StringBuilder message = new StringBuilder();

            // Check out all the classes.
            for (int i = 0; i < classes.length; i++) {
                ClassLoader cl = classes[i].getClassLoader();
                String cansee = "cannot";
                if (ClassUtils.isPresent(className, cl))
                    cansee = "can";
                message.append(classes[i] + " is loaded by " + cl + " which " + cansee + " see " + className);
            }
            log.debug(message);
        }
    }
}

From source file:org.eclipse.gemini.blueprint.util.OsgiServiceReferenceUtils.java

public static ServiceReference getServiceReference(ServiceReference... references) {
    if (ObjectUtils.isEmpty(references)) {
        return null;
    }//w  w w . ja  v  a 2s  . co  m

    ServiceReference winningReference = references[0];

    if (references.length > 1) {
        long winningId = getServiceId(winningReference);
        int winningRanking = getServiceRanking(winningReference);

        // start iterating in order to find the best match
        for (int i = 1; i < references.length; i++) {
            ServiceReference reference = references[i];
            int serviceRanking = getServiceRanking(reference);
            long serviceId = getServiceId(reference);

            if ((serviceRanking > winningRanking)
                    || (serviceRanking == winningRanking && winningId > serviceId)) {
                winningReference = reference;
                winningId = serviceId;
                winningRanking = serviceRanking;
            }
        }
    }
    return winningReference;
}

From source file:org.eclipse.gemini.blueprint.util.OsgiServiceReferenceUtils.java

/**
 * Returns a reference to the <em>best matching</em> service for the given classes and OSGi filter.
 * /* w ww.ja v a 2 s .c o m*/
 * @param bundleContext OSGi bundle context
 * @param classes array of fully qualified class names
 * @param filter valid OSGi filter (can be <code>null</code>)
 * @return reference to the <em>best matching</em> service
 */
public static ServiceReference getServiceReference(BundleContext bundleContext, String[] classes,
        String filter) {
    // use #getServiceReference(BundleContext, String, String) method to
    // speed the service lookup process by
    // giving one class as a hint to the OSGi implementation

    String clazz = (ObjectUtils.isEmpty(classes) ? null : classes[0]);

    return getServiceReference(bundleContext, clazz, OsgiFilterUtils.unifyFilter(classes, filter));
}