001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Kevin Leturc <kleturc@nuxeo.com>
018 *
019 */
020package org.nuxeo.runtime.reload;
021
022import java.io.File;
023import java.util.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026
027import org.nuxeo.runtime.RuntimeServiceException;
028import org.nuxeo.runtime.api.Framework;
029import org.osgi.framework.Bundle;
030import org.osgi.framework.BundleException;
031
032/**
033 * This class is used from DevFrameworkBootstrap class by reflection. It is instantiated by reflection too, so keep the
034 * default constructor.
035 * <p />
036 * We need to keep common type for arguments and returned value.
037 *
038 * @since 9.3
039 */
040public class DevReloadBridge {
041
042    /**
043     * Keep this default constructor. The class is instantiated from ReloadServiceInvoker by reflection.
044     */
045    public DevReloadBridge() {
046        // nothing to do
047    }
048
049    /**
050     * @return the deployed bundles as a map associating the sbundle symbolic name to its location.
051     */
052    public Map<String, String> reloadBundles(List<String> bundleNamesToUndeploy, List<File> bundlesToDeploy) {
053        ReloadService reloadService = Framework.getService(ReloadService.class);
054
055        ReloadContext context = new ReloadContext("dev-bundles").undeploy(bundleNamesToUndeploy)
056                                                                .deploy(bundlesToDeploy);
057
058        try {
059            // reload server
060            ReloadResult result = reloadService.reloadBundles(context);
061            // convert result to something usual
062            // furthermore, we just need the deployed bundles list in DevFrameworkBootstrap
063            return result.deployedBundles()
064                         .stream()
065                         .collect(Collectors.toMap(Bundle::getSymbolicName, Bundle::getLocation));
066        } catch (BundleException e) {
067            throw new RuntimeServiceException("Unable to reload server", e);
068        }
069    }
070
071}