001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.runtime.model.persistence;
013
014import java.io.IOException;
015import java.util.List;
016
017import org.nuxeo.runtime.RuntimeServiceException;
018import org.nuxeo.runtime.model.ComponentContext;
019import org.nuxeo.runtime.model.ComponentInstance;
020import org.nuxeo.runtime.model.DefaultComponent;
021import org.nuxeo.runtime.model.RegistrationInfo;
022import org.nuxeo.runtime.model.RuntimeContext;
023import org.nuxeo.runtime.model.persistence.fs.FileSystemStorage;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public class ContributionPersistenceComponent extends DefaultComponent implements ContributionPersistenceManager {
029
030    public static final String STORAGE_XP = "storage";
031
032    protected ContributionStorage storage;
033
034    protected RuntimeContext ctx;
035
036    public static String getComponentName(String contribName) {
037        return "config:" + contribName;
038    }
039
040    @Override
041    public void activate(ComponentContext context) {
042        super.activate(context);
043        this.ctx = context.getRuntimeContext();
044        storage = new FileSystemStorage();
045    }
046
047    @Override
048    public void deactivate(ComponentContext context) {
049        super.deactivate(context);
050        ctx = null;
051        storage = null;
052    }
053
054    @Override
055    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
056        // This extension point is a singleton. It supports only one
057        // contribution!
058        // I am not using a runtime property to specify the implementation class
059        // because
060        // of possible problems caused by class loaders in real OSGI frameworks.
061        ContributionStorageDescriptor c = (ContributionStorageDescriptor) contribution;
062        try {
063            storage = (ContributionStorage) c.clazz.newInstance();
064        } catch (ReflectiveOperationException e) {
065            throw new RuntimeServiceException(e);
066        }
067    }
068
069    @Override
070    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
071        storage = null;
072    }
073
074    @Override
075    public List<Contribution> getContributions() {
076        return storage.getContributions();
077    }
078
079    @Override
080    public Contribution getContribution(String name) {
081        return storage.getContribution(name);
082    }
083
084    @Override
085    public Contribution addContribution(Contribution contrib) {
086        return storage.addContribution(contrib);
087    }
088
089    @Override
090    public boolean removeContribution(Contribution contrib) {
091        return storage.removeContribution(contrib);
092    }
093
094    @Override
095    public boolean isInstalled(Contribution contrib) {
096        return ctx.isDeployed(contrib);
097    }
098
099    @Override
100    public synchronized boolean installContribution(Contribution contrib) {
101        RegistrationInfo ri;
102        try {
103            ri = ctx.deploy(contrib);
104        } catch (IOException e) {
105            throw new RuntimeServiceException(e);
106        }
107        if (ri == null) {
108            return false;
109        }
110        ri.setPersistent(true);
111        return true;
112    }
113
114    @Override
115    public boolean uninstallContribution(Contribution contrib) {
116        boolean ret = isInstalled(contrib);
117        try {
118            ctx.undeploy(contrib);
119        } catch (IOException e) {
120            throw new RuntimeServiceException(e);
121        }
122        return ret;
123    }
124
125    @Override
126    public Contribution updateContribution(Contribution contribution) {
127        return storage.updateContribution(contribution);
128    }
129
130    @Override
131    public boolean isPersisted(Contribution contrib) {
132        return storage.getContribution(contrib.getName()) != null;
133    }
134
135    @Override
136    public void start() {
137        for (Contribution c : storage.getContributions()) {
138            if (!c.isDisabled()) {
139                installContribution(c);
140            }
141        }
142    }
143
144    @Override
145    public void stop() {
146        for (Contribution c : storage.getContributions()) {
147            if (!c.isDisabled()) {
148                uninstallContribution(c);
149            }
150        }
151    }
152
153    @Override
154    public void applicationStarted(ComponentContext context) {
155        if (storage == null) {
156            storage = new FileSystemStorage();
157            start();
158        }
159    }
160}