001/*
002 * (C) Copyright 2006-2011 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 *     matic
018 */
019package org.nuxeo.ecm.core.management;
020
021import org.nuxeo.ecm.core.event.EventStats;
022import org.nuxeo.ecm.core.management.api.AdministrativeStatusManager;
023import org.nuxeo.ecm.core.management.api.GlobalAdministrativeStatusManager;
024import org.nuxeo.ecm.core.management.api.ProbeManager;
025import org.nuxeo.ecm.core.management.events.EventStatsHolder;
026import org.nuxeo.ecm.core.management.events.EventStatsImpl;
027import org.nuxeo.ecm.core.management.probes.ProbeDescriptor;
028import org.nuxeo.ecm.core.management.probes.ProbeManagerImpl;
029import org.nuxeo.ecm.core.management.statuses.AdministrableServiceDescriptor;
030import org.nuxeo.ecm.core.management.statuses.AdministrativeStatusManagerImpl;
031import org.nuxeo.ecm.core.management.statuses.GlobalAdministrativeStatusManagerImpl;
032import org.nuxeo.ecm.core.management.storage.DocumentStoreConfigurationDescriptor;
033import org.nuxeo.ecm.core.management.storage.DocumentStoreHandlerDescriptor;
034import org.nuxeo.ecm.core.management.storage.DocumentStoreManager;
035import org.nuxeo.runtime.model.ComponentContext;
036import org.nuxeo.runtime.model.ComponentInstance;
037import org.nuxeo.runtime.model.ComponentName;
038import org.nuxeo.runtime.model.DefaultComponent;
039
040/**
041 * @author Stephane Lacoin (Nuxeo EP Software Engineer)
042 */
043public class CoreManagementComponent extends DefaultComponent {
044
045    public static final ComponentName NAME = new ComponentName(CoreManagementComponent.class.getCanonicalName());
046
047    public static final String PROBES_EP = "probes";
048
049    public static final String SERVICE_DEF_EP = "serviceDefinition";
050
051    public static final String STORAGE_HANDLERS_EP = "storageHandlers";
052
053    public static final String STORAGE_CONFIG_EP = "storageConfiguration";
054
055    protected static CoreManagementComponent defaultComponent;
056
057    protected final GlobalAdministrativeStatusManager globalManager = new GlobalAdministrativeStatusManagerImpl();
058
059    protected final EventStats eventStats = new EventStatsImpl();
060
061    protected final ProbeManagerImpl probeRunner = new ProbeManagerImpl();
062
063    protected final DocumentStoreManager storageManager = new DocumentStoreManager();
064
065    public CoreManagementComponent() {
066        super(); // enables breaking
067    }
068
069    public AdministrativeStatusManagerImpl getLocalManager() {
070        return (AdministrativeStatusManagerImpl) globalManager.getStatusManager(globalManager.getLocalNuxeoInstanceIdentifier());
071    }
072
073    @Override
074    public <T> T getAdapter(Class<T> adapter) {
075        if (adapter == EventStats.class) {
076            return adapter.cast(eventStats);
077        }
078        if (adapter.isAssignableFrom(GlobalAdministrativeStatusManager.class)) {
079            return adapter.cast(globalManager);
080        }
081        if (adapter.isAssignableFrom(AdministrativeStatusManager.class)) {
082            return adapter.cast(getLocalManager());
083        }
084        if (adapter.isAssignableFrom(ProbeManager.class)) {
085            return adapter.cast(probeRunner);
086        }
087        return super.getAdapter(adapter);
088    }
089
090    @Override
091    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
092        if (extensionPoint.equals(PROBES_EP)) {
093            probeRunner.registerProbe((ProbeDescriptor) contribution);
094        } else if (extensionPoint.equals(SERVICE_DEF_EP)) {
095            globalManager.registerService((AdministrableServiceDescriptor) contribution);
096        } else if (extensionPoint.equals(STORAGE_HANDLERS_EP)) {
097            storageManager.registerHandler((DocumentStoreHandlerDescriptor) contribution);
098        } else if (extensionPoint.equals(STORAGE_CONFIG_EP)) {
099            storageManager.registerConfig((DocumentStoreConfigurationDescriptor) contribution);
100        } else {
101            super.registerContribution(contribution, extensionPoint, contributor);
102        }
103    }
104
105    @Override
106    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
107        if (extensionPoint.equals("probes")) {
108            probeRunner.unregisterProbe((ProbeDescriptor) contribution);
109        }
110    }
111
112    public static CoreManagementComponent getDefault() {
113        return defaultComponent;
114    }
115
116    @Override
117    public void activate(ComponentContext context) {
118        defaultComponent = this;
119        storageManager.install();
120        EventStatsHolder.clearStats();
121    }
122
123    @Override
124    public void deactivate(ComponentContext context) {
125        defaultComponent = null;
126        storageManager.uninstall();
127        getLocalManager().onNuxeoServerShutdown();
128        EventStatsHolder.clearStats();
129    }
130
131    public void onNuxeoServerStartup() {
132        getLocalManager().onNuxeoServerStartup();
133    }
134
135}