SimpleAppFactory.java

/***************************************************************************
   Copyright 2015 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.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.sql.DataSource;

import net.metanotion.sql.DbUtil;
import net.metanotion.sql.SchemaGenerator;
import net.metanotion.util.Dispatcher;
import net.metanotion.util.Unknown;
import net.metanotion.web.AppFactory;
import net.metanotion.web.Instance;
import net.metanotion.web.InstanceFactory;
import net.metanotion.web.RequestObject;

/** This interface models a specifc type of web application initializaiton pattern. Relatively straightforward database
backed web applications can often be described by a dispatcher, a database schema, and an application "instance" that
can produe sessions. */
public final class SimpleAppFactory implements AppFactory {
	private final Dispatcher<? extends Object, RequestObject> disp;
	private final InstanceFactory iFactory;
	private final SchemaGenerator schema;

	private static final SchemaGenerator EMPTY_SCHEMA = new SchemaGenerator() {
		@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(); }
			};
		}
	};

	public SimpleAppFactory(final Dispatcher<? extends Object, RequestObject> disp, final InstanceFactory iFactory) {
		this(disp, iFactory, EMPTY_SCHEMA);
	}

	public SimpleAppFactory(final Dispatcher<? extends Object, RequestObject> disp,
			final InstanceFactory iFactory,
			final Class klazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		this(disp, iFactory, DbUtil.reflectSchemaGenerator(klazz));
	}

	public SimpleAppFactory(final Dispatcher<? extends Object, RequestObject> disp,
			final InstanceFactory iFactory,
			final SchemaGenerator schema) {
		this.disp = disp;
		this.iFactory = iFactory;
		this.schema = schema;
	}

	@Override public Dispatcher<? extends Object,RequestObject> dispatcher() { return disp; }
	@Override public Instance newInstance(final DataSource ds, final String prefix, final Unknown container) {
		return iFactory.newInstance(ds, prefix, container);
	}
	@Override public Iterator<String> openSchema() { return schema.openSchema(); }
}