001/*
002 * (C) Copyright 2013 Nuxeo SA (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.operations.test;
020
021import java.net.URL;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.drive.adapter.FileSystemItem;
026import org.nuxeo.drive.service.FileSystemItemAdapterService;
027import org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Activates / deactivates the {@link FileSystemItem} factories of the given profile.
036 *
037 * @author Antoine Taillefer
038 */
039@Operation(id = NuxeoDriveSetActiveFactories.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Activate or deactivate file system item factories")
040public class NuxeoDriveSetActiveFactories {
041
042    public static final String ID = "NuxeoDrive.SetActiveFactories";
043
044    private static final Log log = LogFactory.getLog(NuxeoDriveSetActiveFactories.class);
045
046    @Param(name = "profile")
047    protected String profile;
048
049    @Param(name = "enable", required = false)
050    protected boolean enable = true;
051
052    @OperationMethod
053    public boolean run() throws Exception {
054        NuxeoDriveIntegrationTestsHelper.checkOperationAllowed();
055        String contrib = null;
056        if ("userworkspace".equals(profile)) {
057            contrib = "/OSGI-INF/nuxeodrive-hierarchy-userworkspace-contrib.xml";
058        } else if ("permission".equals(profile)) {
059            contrib = "/OSGI-INF/nuxeodrive-hierarchy-permission-contrib.xml";
060        } else {
061            log.warn(String.format("No active file system item factory contribution for profile '%s'.", profile));
062            return false;
063        }
064        URL url = NuxeoDriveSetActiveFactories.class.getResource(contrib);
065        if (enable) {
066            Framework.getRuntime().getContext().deploy(url);
067        } else {
068            Framework.getRuntime().getContext().undeploy(url);
069        }
070        FileSystemItemAdapterServiceImpl fileSystemItemAdapterService = (FileSystemItemAdapterServiceImpl) Framework.getService(FileSystemItemAdapterService.class);
071        fileSystemItemAdapterService.setActiveFactories();
072        return true;
073    }
074
075}