001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018: package org.restlet.ext.spring;
019:
020: import java.util.logging.Level;
021:
022: import org.restlet.Context;
023: import org.restlet.Finder;
024: import org.restlet.data.Request;
025: import org.restlet.data.Response;
026: import org.restlet.resource.Resource;
027:
028: /**
029: * Finder that is specialized for easier usage by Spring wiring services. The idea
030: * is to create a singleton Spring bean based on that SpringFinder and configure
031: * it using Spring's "lookup-method" element to return instances of a
032: * "prototype" bean for {@link #createResource()}. Finally, attach the
033: * SpringFinder to your Router. When the createResource() method is invoked, a
034: * new instance of your prototype bean will be created and returned.
035: * A sample xml for "lookup-method":
036: *
037: * <pre>
038: * <bean id="myFinder" class="org.restlet.ext.spring.SpringFinder">
039: * <lookup-method name="createResource" bean="myResource"/>
040: * </bean>
041: *
042: * <bean id="myResource" class="com.mycompany.rest.resource.MyResource" scope="prototype">
043: * <property name="aProperty" value="anotherOne"/>
044: * <property name="oneMore" value="true"/>
045: * </bean>
046: * </pre>
047: *
048: * Note that the <a href="http://cglib.sourceforge.net/">Code Generation Library</a>
049: * (cglib) will be required in order to use the Spring's lookup method
050: * mechanism.
051: *
052: * @author Jerome Louvel (contact@noelios.com)
053: */
054: public class SpringFinder extends Finder {
055:
056: /**
057: * Constructor.
058: */
059: public SpringFinder() {
060: super ();
061: }
062:
063: /**
064: * Constructor.
065: *
066: * @param context
067: * The context.
068: */
069: public SpringFinder(Context context) {
070: super (context);
071: }
072:
073: /**
074: * Constructor.
075: *
076: * @param context
077: * The context.
078: * @param targetClass
079: * The target resource class.
080: */
081: public SpringFinder(Context context,
082: Class<? extends Resource> targetClass) {
083: super (context, targetClass);
084: }
085:
086: @Override
087: public Resource createResource(Request request, Response response) {
088: Resource result = createResource();
089:
090: if (result != null) {
091: result.init(getContext(), request, response);
092: }
093:
094: return result;
095: }
096:
097: /**
098: * Creates a new instance of the resource class designated by the
099: * "targetClass" property. For easier Spring configuration, the default
100: * target resource's constructor is invoked. The created instance is
101: * initialized by the calling {@link #createResource(Request, Response)}
102: * method, by invoking the {@link Resource#init(Context, Request, Response)}
103: * method on the resource.
104: *
105: * @return The created resource or null.
106: */
107: public Resource createResource() {
108: Resource result = null;
109:
110: if (getTargetClass() != null) {
111: try {
112: // Invoke the default constructor
113: result = (Resource) getTargetClass().newInstance();
114: } catch (Exception e) {
115: getLogger()
116: .log(
117: Level.WARNING,
118: "Exception while instantiating the target resource.",
119: e);
120: }
121: }
122:
123: return result;
124: }
125:
126: }
|