StaticWebApp.java

/***************************************************************************
   Copyright 2013 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.web.concrete;


import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.sql.DataSource;

import net.metanotion.scripting.ObjectServer;
import net.metanotion.simpletemplate.ResourceDispatcher;
import net.metanotion.simpletemplate.ResourceFactory;
import net.metanotion.util.Service;
import net.metanotion.util.Dispatcher;
import net.metanotion.util.Unknown;
import net.metanotion.util.UnknownMagic;
import net.metanotion.web.AppFactory;
import net.metanotion.web.Instance;
import net.metanotion.web.RequestObject;

/** This is an incredibly simple web app to serve assets out of a resource factory. */
public final class StaticWebApp implements Instance, AppFactory {
	private static final UnknownMagic u = new UnknownMagic(StaticWebApp.class);
	private static final ResourceDispatcher dispatcher = new ResourceDispatcher();

	@Service public final ResourceFactory resources;
	@Service public final ObjectServer os;

	/** Create a basic static web application.
		@param resources The resource factory to serve for requests directed at the instances.
	*/
	public StaticWebApp(final ResourceFactory resources) { this(resources, null); }

		/** Create a basic static web application.
		@param resources The resource factory to serve for requests directed at the instances.
		@param os The object server instance to use for any scriptable templates.
	*/
	public StaticWebApp(final ResourceFactory resources, final ObjectServer os) {
		this.resources = resources;
		this.os = os;
	}

	// AppFactory
	@Override public Dispatcher<? extends Object,RequestObject> dispatcher() { return dispatcher; }
	@Override public Instance newInstance(final DataSource ds, final String prefix, final Unknown container) { return this; }
	@Override public Iterator<String> openSchema() {
		return new Iterator<String>() {
			@Override public boolean hasNext() { return false; }
			@Override public String next() { throw new NoSuchElementException(); }
			@Override public void remove() { throw new UnsupportedOperationException(); }
		};
	}

	// Instance
	@Override public Unknown newSession() { return this; }
	@Override public <I> I lookupInterface(final Class<I> theInterface) { return u.lookupInterface(theInterface, this); }
}