001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.module.framework.test;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.module.framework.ModuleFrameworkUtilAdapter;
021    import com.liferay.registry.Registry;
022    import com.liferay.registry.RegistryUtil;
023    import com.liferay.registry.ServiceReference;
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    
028    /**
029     * @author Adolfo P??rez
030     */
031    public class ModuleFrameworkTestUtil {
032    
033            public static <T> Collection<Long> getBundleIds(
034                            Class<T> clazz, String filter)
035                    throws Exception {
036    
037                    Registry registry = RegistryUtil.getRegistry();
038    
039                    Collection<Long> bundleIds = new ArrayList<>();
040    
041                    Collection<ServiceReference<T>>
042                            serviceReferences = registry.getServiceReferences(clazz, filter);
043    
044                    for (ServiceReference<T> serviceReference : serviceReferences) {
045                            Long bundleId = (Long)serviceReference.getProperty(
046                                    "service.bundleid");
047    
048                            bundleIds.add(bundleId);
049                    }
050    
051                    return bundleIds;
052            }
053    
054            public static void startBundles(Iterable<Long> bundleIds) {
055                    for (long bundleId : bundleIds) {
056                            try {
057                                    ModuleFrameworkUtilAdapter.startBundle(bundleId);
058                            }
059                            catch (Exception e) {
060                                    _log.error("Unable to start bundle " + bundleId, e);
061                            }
062                    }
063            }
064    
065            public static void stopBundles(Iterable<Long> bundleIds)
066                    throws PortalException {
067    
068                    for (long bundleId : bundleIds) {
069                            ModuleFrameworkUtilAdapter.stopBundle(bundleId);
070                    }
071            }
072    
073            private static final Log _log = LogFactoryUtil.getLog(
074                    ModuleFrameworkTestUtil.class);
075    
076    }