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.ecm.webengine.app.jersey;
018
019import java.lang.reflect.Type;
020import java.util.Set;
021
022import javax.ws.rs.core.Context;
023import javax.ws.rs.ext.Provider;
024
025import org.nuxeo.ecm.webengine.WebEngine;
026import org.nuxeo.ecm.webengine.app.WebEngineApplication;
027import org.nuxeo.ecm.webengine.model.WebContext;
028
029import com.sun.jersey.core.spi.component.ComponentContext;
030import com.sun.jersey.core.spi.component.ComponentScope;
031import com.sun.jersey.spi.inject.Injectable;
032import com.sun.jersey.spi.inject.InjectableProvider;
033
034/**
035 * Experimental - Can be used to inject WebContext through {@code @Context} annotation. Do not use it for now.
036 *
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039@Provider
040public class JerseyApplication extends WebEngineApplication implements InjectableProvider<Context, Type> {
041
042    @Override
043    public Set<Object> getSingletons() {
044        Set<Object> set = super.getSingletons();
045        set.add(this);
046        return set;
047    }
048
049    public ComponentScope getScope() {
050        return ComponentScope.PerRequest;
051    }
052
053    public Injectable<?> getInjectable(ComponentContext cc, Context a, Type t) {
054        if (!(t instanceof Class<?>)) {
055            return null;
056        }
057        Class<?> c = (Class<?>) t;
058        if (c == WebContext.class) {
059            return new Injectable<Object>() {
060                public Object getValue() {
061                    return WebEngine.getActiveContext();
062                }
063            };
064        }
065        return null;
066    }
067
068}