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    @Override
054    public Set<String> getAvailableComponents() {
055        Set<String> names = new HashSet<>();
056        for (RegistrationInfo info : availableComponents()) {
057            names.add(info.getName().getRawName());
058        }
059        return names;
060    }
061
062    @Override
063    public int getAvailableComponentsCount() {
064        return runtimeService.getComponentManager().getRegistrations().size();
065    }
066
067    @Override
068    public int getPendingComponentsCount() {
069        return pendingComponentsName().size();
070    }
071
072    @Override
073    public Set<String> getPendingComponentsName() {
074        Set<String> names = new HashSet<>();
075        for (ComponentName componentName : pendingComponentsName()) {
076            names.add(componentName.getRawName());
077        }
078        return names;
079    }
080
081    @Override
082    public String getDescription() {
083        return runtimeService.getDescription();
084    }
085
086    @Override
087    public String getHome() {
088        try {
089            return runtimeService.getHome().getCanonicalPath();
090        } catch (IOException e) {
091            throw new NuxeoException("cannot get path", e);
092        }
093    }
094
095    @Override
096    public String getName() {
097        return runtimeService.getName();
098    }
099
100    @Override
101    public String getVersion() {
102        return runtimeService.getVersion().toString();
103    }
104
105    protected boolean isTreeBound = false;
106
107    @Override
108    public boolean isTreeBound() {
109        return isTreeBound;
110    }
111
112    @Override
113    public void bindTree() {
114        if (isTreeBound) {
115            throw new IllegalArgumentException("tree already bound");
116        }
117        isTreeBound = true;
118        factory.bindTree();
119    }
120
121    @Override
122    public void unbindTree() {
123        if (!isTreeBound) {
124            throw new IllegalArgumentException("tree not bound");
125        }
126        isTreeBound = false;
127        factory.unbindTree();
128    }
129
130}