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.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public class AutomationModule extends WebEngineModule {
041
042    protected static final Log log = LogFactory.getLog(AutomationModule.class);
043
044    @Override
045    public Set<Class<?>> getClasses() {
046
047        Set<Class<?>> result = super.getClasses();
048        // need to be stateless since it needs the request member to be
049        // injected
050        result.add(MultiPartRequestReader.class);
051        result.add(MultiPartFormRequestReader.class);
052        return result;
053    }
054
055    protected static Set<Object> setupSingletons() {
056
057        Set<Object> result = new HashSet<Object>();
058
059        AutomationServer as = Framework.getLocalService(AutomationServer.class);
060
061        for (Class<? extends MessageBodyReader<?>> readerKlass : as.getReaders()) {
062            try {
063                result.add(readerKlass.newInstance());
064            } catch (InstantiationException | IllegalAccessException e) {
065                log.error("Unable to instanciate MessageBodyReader : " + readerKlass, e);
066            }
067        }
068
069        for (Class<? extends MessageBodyWriter<?>> writerKlass : as.getWriters()) {
070            try {
071                result.add(writerKlass.newInstance());
072            } catch (InstantiationException | IllegalAccessException e) {
073                log.error("Unable to instanciate MessageBodyWriter : " + writerKlass, e);
074            }
075        }
076
077        result.add(new JsonFactoryProvider());
078        // nuxeo-core-io MarshallerRegistry service reading and writing
079        result.add(new JsonCoreIODelegate());
080        return result;
081    }
082
083    @Override
084    public Set<Object> getSingletons() {
085        return setupSingletons();
086    }
087
088}