/** * Copyright 2005-2014 Restlet * * The contents of this file are subject to the terms of one of the following * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can * select the license that you prefer but you may not use this file except in * compliance with one of these Licenses. * * You can obtain a copy of the Apache 2.0 license at * http://www.opensource.org/licenses/apache-2.0 * * You can obtain a copy of the EPL 1.0 license at * http://www.opensource.org/licenses/eclipse-1.0 * * See the Licenses for the specific language governing permissions and * limitations under the Licenses. * * Alternatively, you can obtain a royalty free commercial license with less * limitations, transferable or non-transferable, directly at * http://restlet.com/products/restlet-framework * * Restlet is a registered trademark of Restlet S.A.S. */ package org.restlet.ext.spring; import java.util.ArrayList; import java.util.List; import org.restlet.Client; import org.restlet.Restlet; import org.restlet.Server; import org.restlet.data.Protocol; /** * Component that is easily configurable from Spring. Here is a usage example: * * <pre> * <bean id="component" * class="org.restlet.ext.spring.SpringComponent"> * <property name="clientsList"> * <list> * <value>file</value> * </list> * </property> * <property name="server" ref="server" /> * <property name="defaultTarget" ref="application" /> * <property name="hosts"> * <list> * <ref bean="virtualHost" /> * </list> * </property> * </bean> * * <bean id="component.context" * class="org.springframework.beans.factory.config.PropertyPathFactoryBean" /> * * <bean id="server" class="org.restlet.ext.spring.SpringServer"> * <constructor-arg value="http" /> * <constructor-arg value="8111" /> * <property name="parameters"> * <props> * <prop key="key1">value1</prop> * <prop key="key2">value2</prop> * </props> * </property> * </bean> * </pre> * * Concurrency note: instances of this class or its subclasses can be invoked by * several threads at the same time and therefore must be thread-safe. You * should be especially careful when storing state in member variables. * * @see <a href="http://www.springframework.org/">Spring home page</a> * @author Jerome Louvel */ public class SpringComponent extends org.restlet.Component { /** * Adds a client to the list of connectors. The value can be either a * protocol name, a Protocol instance or a Client instance. * * @param clientInfo * The client info. */ public void setClient(Object clientInfo) { final List<Object> clients = new ArrayList<Object>(); clients.add(clientInfo); setClientsList(clients); } /** * Sets the list of clients, either as protocol names, Protocol instances or * Client instances. * * @param clients * The list of clients. */ public synchronized void setClientsList(List<Object> clients) { for (final Object client : clients) { if (client instanceof String) { getClients().add(Protocol.valueOf((String) client)); } else if (client instanceof Protocol) { getClients().add((Protocol) client); } else if (client instanceof Client) { getClients().add((Client) client); } else { getLogger() .warning( "Unknown object found in the clients list. Only instances of String, org.restlet.data.Protocol and org.restlet.Client are allowed."); } } } /** * Attaches a target Restlet to the default host. * * @param target * The target Restlet. */ public void setDefaultTarget(Restlet target) { getDefaultHost().attach(target); } /** * Adds a server to the list of connectors. The value can be either a * protocol name, a Protocol instance or a Server instance. * * @param serverInfo * The server info. */ public void setServer(Object serverInfo) { final List<Object> servers = new ArrayList<Object>(); servers.add(serverInfo); setServersList(servers); } /** * Sets the list of servers, either as protocol names, Protocol instances or * Server instances. * * @param serversInfo * The list of servers. */ public void setServersList(List<Object> serversInfo) { for (final Object serverInfo : serversInfo) { if (serverInfo instanceof String) { getServers().add(Protocol.valueOf((String) serverInfo)); } else if (serverInfo instanceof Protocol) { getServers().add((Protocol) serverInfo); } else if (serverInfo instanceof Server) { getServers().add((Server) serverInfo); } else { getLogger() .warning( "Unknown object found in the servers list. Only instances of String, org.restlet.data.Protocol and org.restlet.Server are allowed."); } } } }