DefaultServer.java

/***************************************************************************
   Copyright 2008 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.simpletemplate;


import net.metanotion.scripting.ObjectServer;
import net.metanotion.util.Dispatcher;
import net.metanotion.web.Default;
import net.metanotion.web.concrete.BasicRequestObject;

/** An implementation of the Default interface that is backed by a ResourceFactory.
If you wish to use a ResourceFactory, particularly one that doesn't rely on anything other than the
URL of the resource({@link net.metanotion.simpletemplate.StaticResources} can function without
a real request - although if GIVEN A real request, it will respond properly to cache control headers.).
*/
public final class DefaultServer implements Default {
	/** Since ResourceFactory's expect an ObjectServer this our instance that simply
		returns null for every object. If this is used with an ObjectServer that
		needs a specific object to exist, null pointer exceptions will enuse.
		Note: ResourceFactory's should be "nullary correct" anyway. ObjectServer's
		are the set of scriptable objects an application DYNAMICALLY provides, so lookup
		failure is a possibilty any consumer should be prepared to deal with. */
	private static final ObjectServer os = new ObjectServer() {
			@Override public Object get(final String name) { return null; }
			@Override public Dispatcher dispatcher(final String name) { return null; }
		};

	/** Since ResourceFactory's expect a RequestObject make an empty one to pass to it.
		Obviously, many things will return null, so if this is used with a ResourceFactory that
		really needs a value to be set, there will be trouble.
		Note: ResourceFactory's should be "nullary correct" anyway. RequestObject's are
		potentially malicious user input. */
	private static final BasicRequestObject NULL_REQUEST = new BasicRequestObject();

	/** The ResourceFactory our Default instance uses to lookup requests. */
	private final ResourceFactory factory;

		/** Create an instance of the Default service backed by a ResourceFactory instance.
		@param factory The ResourceFactory to perform resource lookup on.
	*/
	public DefaultServer(final ResourceFactory factory) { this.factory = factory; }

	@Override public Object get(final String url) {
		return this.factory.get(url).skin(os,  DefaultServer.NULL_REQUEST);
	}
}