001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.connect.update;
018
019import java.io.IOException;
020
021import org.nuxeo.connect.update.PackageUpdateService;
022import org.nuxeo.connect.update.live.UpdateServiceImpl;
023import org.nuxeo.runtime.model.ComponentContext;
024import org.nuxeo.runtime.model.DefaultComponent;
025import org.nuxeo.runtime.model.RuntimeContext;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class PackageUpdateComponent extends DefaultComponent {
031
032    protected UpdateServiceImpl svc;
033
034    static RuntimeContext ctx;
035
036    public static RuntimeContext getContext() {
037        return ctx;
038    }
039
040    @Override
041    public void activate(ComponentContext context) {
042        ctx = context.getRuntimeContext();
043        try {
044            svc = new UpdateServiceImpl();
045        } catch (IOException e) {
046            throw new RuntimeException(e);
047        }
048        try {
049            svc.initialize();
050        } catch (PackageException e) {
051            throw new RuntimeException(e);
052        }
053    }
054
055    @Override
056    public void deactivate(ComponentContext context) {
057        try {
058            svc.shutdown();
059        } catch (PackageException e) {
060            throw new RuntimeException(e);
061        }
062        svc = null;
063    }
064
065    @SuppressWarnings("unchecked")
066    @Override
067    public <T> T getAdapter(Class<T> adapter) {
068        return adapter == PackageUpdateService.class ? (T) svc : null;
069    }
070
071}