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.server.AutomationServer; 032import org.nuxeo.ecm.webengine.app.WebEngineModule; 033import org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * @author <a href="mailto:[email protected]">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(MultiPartFormRequestReader.class); 050 return result; 051 } 052 053 protected static Set<Object> setupSingletons() { 054 055 Set<Object> result = new HashSet<Object>(); 056 057 AutomationServer as = Framework.getService(AutomationServer.class); 058 059 for (Class<? extends MessageBodyReader<?>> readerKlass : as.getReaders()) { 060 try { 061 result.add(readerKlass.newInstance()); 062 } catch (InstantiationException | IllegalAccessException e) { 063 log.error("Unable to instanciate MessageBodyReader : " + readerKlass, e); 064 } 065 } 066 067 for (Class<? extends MessageBodyWriter<?>> writerKlass : as.getWriters()) { 068 try { 069 result.add(writerKlass.newInstance()); 070 } catch (InstantiationException | IllegalAccessException e) { 071 log.error("Unable to instanciate MessageBodyWriter : " + writerKlass, e); 072 } 073 } 074 075 result.add(new AutomationServiceProvider()); 076 result.add(new AutomationServerProvider()); 077 result.add(new JsonFactoryProvider()); 078 result.add(new CoreSessionProvider()); 079 // nuxeo-core-io MarshallerRegistry service reading and writing 080 result.add(new JsonCoreIODelegate()); 081 return result; 082 } 083 084 @Override 085 public Set<Object> getSingletons() { 086 return setupSingletons(); 087 } 088 089}