001/*
002 * (C) Copyright 2018 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 *     pierre
018 */
019package org.nuxeo.ecm.core.bulk;
020
021import org.nuxeo.runtime.api.Framework;
022import org.nuxeo.runtime.model.ComponentContext;
023import org.nuxeo.runtime.model.ComponentManager;
024import org.nuxeo.runtime.model.DefaultComponent;
025
026/**
027 * The bulk component.
028 *
029 * @since 10.2
030 */
031public class BulkComponent extends DefaultComponent {
032
033    public static final String XP_ACTIONS = "actions";
034
035    protected BulkService bulkService;
036
037    protected BulkAdminService bulkAdminService;
038
039    @Override
040    @SuppressWarnings("unchecked")
041    public <T> T getAdapter(Class<T> adapter) {
042        if (adapter.isAssignableFrom(BulkService.class)) {
043            return (T) bulkService;
044        } else if (adapter.isAssignableFrom(BulkAdminService.class)) {
045            return (T) bulkAdminService;
046        }
047        return null;
048    }
049
050    @Override
051    public void start(ComponentContext context) {
052        super.start(context);
053        bulkAdminService = new BulkAdminServiceImpl(getDescriptors(XP_ACTIONS));
054        bulkService = new BulkServiceImpl();
055        new ComponentListener().install();
056    }
057
058    protected class ComponentListener implements ComponentManager.Listener {
059        @Override
060        public void afterStart(ComponentManager mgr, boolean isResume) {
061            // this is called once all components are started and ready
062            ((BulkAdminServiceImpl) bulkAdminService).afterStart();
063        }
064
065        @Override
066        public void beforeStop(ComponentManager mgr, boolean isStandby) {
067            // this is called before components are stopped
068            if (bulkAdminService != null) {
069                ((BulkAdminServiceImpl) bulkAdminService).beforeStop();
070                bulkAdminService = null;
071            }
072            bulkService = null;
073            Framework.getRuntime().getComponentManager().removeListener(this);
074        }
075    }
076}