001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.server.jaxrs;
020
021import java.util.HashSet;
022import java.util.Set;
023
024import javax.ws.rs.ext.MessageBodyReader;
025import javax.ws.rs.ext.MessageBodyWriter;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.automation.jaxrs.JsonFactoryProvider;
030import org.nuxeo.ecm.automation.jaxrs.io.operations.MultiPartFormRequestReader;
031import org.nuxeo.ecm.automation.jaxrs.io.operations.MultiPartRequestReader;
032import org.nuxeo.ecm.automation.server.AutomationServer;
033import org.nuxeo.ecm.webengine.app.WebEngineModule;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class AutomationModule extends WebEngineModule {
040
041    protected static final Log log = LogFactory.getLog(AutomationModule.class);
042
043    @Override
044    public Set<Class<?>> getClasses() {
045
046        Set<Class<?>> result = super.getClasses();
047        // need to be stateless since it needs the request member to be
048        // injected
049        result.add(MultiPartRequestReader.class);
050        result.add(MultiPartFormRequestReader.class);
051        return result;
052    }
053
054    protected static Set<Object> setupSingletons() {
055
056        Set<Object> result = new HashSet<Object>();
057
058        AutomationServer as = Framework.getLocalService(AutomationServer.class);
059
060        for (Class<? extends MessageBodyReader<?>> readerKlass : as.getReaders()) {
061            try {
062                result.add(readerKlass.newInstance());
063            } catch (InstantiationException | IllegalAccessException e) {
064                log.error("Unable to instanciate MessageBodyReader : " + readerKlass, e);
065            }
066        }
067
068        for (Class<? extends MessageBodyWriter<?>> writerKlass : as.getWriters()) {
069            try {
070                result.add(writerKlass.newInstance());
071            } catch (InstantiationException | IllegalAccessException e) {
072                log.error("Unable to instanciate MessageBodyWriter : " + writerKlass, e);
073            }
074        }
075
076        result.add(new JsonFactoryProvider());
077        return result;
078    }
079
080    @Override
081    public Set<Object> getSingletons() {
082        return setupSingletons();
083    }
084
085}