001/*
002 * (C) Copyright 2006-2008 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.platform.management.adapters;
020
021import java.io.IOException;
022import java.util.Collection;
023import java.util.HashSet;
024import java.util.Set;
025
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.runtime.RuntimeService;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.model.ComponentName;
030import org.nuxeo.runtime.model.RegistrationInfo;
031
032/**
033 * @author Stephane Lacoin (Nuxeo EP Software Engineer)
034 */
035public class RuntimeInventoryAdapter implements RuntimeInventoryMBean {
036
037    protected final RuntimeService runtimeService = Framework.getRuntime();
038
039    protected final RuntimeInventoryFactory factory;
040
041    public RuntimeInventoryAdapter(RuntimeInventoryFactory factory) {
042        this.factory = factory;
043    }
044
045    protected Collection<RegistrationInfo> availableComponents() {
046        return runtimeService.getComponentManager().getRegistrations();
047    }
048
049    protected Collection<ComponentName> pendingComponentsName() {
050        return runtimeService.getComponentManager().getActivatingRegistrations();
051    }
052
053    public Set<String> getAvailableComponents() {
054        Set<String> names = new HashSet<String>();
055        for (RegistrationInfo info : availableComponents()) {
056            names.add(info.getName().getRawName());
057        }
058        return names;
059    }
060
061    public int getAvailableComponentsCount() {
062        return runtimeService.getComponentManager().getRegistrations().size();
063    }
064
065    public int getPendingComponentsCount() {
066        return pendingComponentsName().size();
067    }
068
069    public Set<String> getPendingComponentsName() {
070        Set<String> names = new HashSet<String>();
071        for (ComponentName componentName : pendingComponentsName()) {
072            names.add(componentName.getRawName());
073        }
074        return names;
075    }
076
077    public String getDescription() {
078        return runtimeService.getDescription();
079    }
080
081    public String getHome() {
082        try {
083            return runtimeService.getHome().getCanonicalPath();
084        } catch (IOException e) {
085            throw new NuxeoException("cannot get path", e);
086        }
087    }
088
089    public String getName() {
090        return runtimeService.getName();
091    }
092
093    public String getVersion() {
094        return runtimeService.getVersion().toString();
095    }
096
097    protected boolean isTreeBound = false;
098
099    public boolean isTreeBound() {
100        return isTreeBound;
101    }
102
103    public void bindTree() {
104        if (isTreeBound) {
105            throw new IllegalArgumentException("tree already bound");
106        }
107        isTreeBound = true;
108        factory.bindTree();
109    }
110
111    public void unbindTree() {
112        if (!isTreeBound) {
113            throw new IllegalArgumentException("tree not bound");
114        }
115        isTreeBound = false;
116        factory.unbindTree();
117    }
118
119}