Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tolerant locator #156

Open
wants to merge 3 commits into
base: gst-foundation-12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,21 @@ protected Factory getFactory(ICS ics) {
* <p>
* This implementation uses the constructor configured in the {@link #CONFIG_FILE}. It also
* uses ICS to locate the ServletContext, and then gets the servletContext-backed factory
* which it will use as a delegate for the ics-backed factory.
* which it will use as a delegate for the ics-backed factory, if possible.
*
* @param ics the ics context
* @return the factory
*/
protected Factory createFactory(ICS ics) {
ServletContext servletContext = ics.getIServlet().getServlet().getServletContext();
Factory delegate = getFactory(servletContext);

Factory delegate;
try {
delegate = getFactory(ics.getIServlet().getServlet().getServletContext());
LOG.debug("Creating ICS-backed factory that delegates to a servletContext-backed factory");
} catch (RuntimeException e) {
delegate = null;
LOG.debug("Creating ICS-backed factory that has no delegate because no servletContext could be located");
}

Constructor<Factory> con = factoryConstructors.get(ICS.class);
try {
Expand Down
31 changes: 28 additions & 3 deletions gsf-core/src/main/java/tools/gsf/config/FactoryLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
package tools.gsf.config;

import COM.FutureTense.Interfaces.ICS;
import COM.FutureTense.Interfaces.IServlet;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

/**
* Utility class for working with the factory producer and factories.
Expand Down Expand Up @@ -49,12 +52,34 @@ public static FactoryProducer locateFactoryProducer(ServletContext servletContex
* Convenience method for locating the factory producer that resides in the servlet context.
* Never returns null.
*
* @param ics the ics object (which of course holds a pointer to the servlet context)
* @param ics the ics object (which, should of course, hold a pointer to the servlet context). If the
* specified ICS object does NOT hold a pointer to the servlet context, this locator method
* will fall back to the <code>DefaultFactoryProducer</code> class, as there is no defined
* configuration capability for creating custom factory producers without a servlet context.
* @return the factory producer, never null
*/
public static FactoryProducer locateFactoryProducer(ICS ics) {
ServletContext servletContext = ics.getIServlet().getServlet().getServletContext();
return locateFactoryProducer(servletContext);
if (ics == null) throw new IllegalArgumentException("No ICS found - cannot locate factory without a scope");

ServletContext servletContext;
try {
servletContext = ics.getIServlet().getServlet().getServletContext();
} catch (RuntimeException e) {
servletContext = null;
}

if (servletContext == null) {
Object o = ics.GetObj(ServletContextLoader.GSF_FACTORY_PRODUCER);
if (o == null) {
// There is no defined configuration capability for a factory producer that does not involve
// the servlet context. Rather than fail, return the default factory producer instead.
o = new DefaultFactoryProducer();
ics.SetObj(ServletContextLoader.GSF_FACTORY_PRODUCER, o);
}
return (FactoryProducer)o;
} else {
return locateFactoryProducer(servletContext);
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions gsf-core/src/main/java/tools/gsf/config/IcsBackedFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import tools.gsf.facade.assetapi.asset.TemplateAssetAccess;
import tools.gsf.mapping.IcsMappingService;
import tools.gsf.mapping.MappingService;
import tools.gsf.time.LoggerStopwatch;
import tools.gsf.time.Stopwatch;
import tools.gsf.properties.AssetApiPropertyDao;
import tools.gsf.properties.PropertyDao;
Expand All @@ -58,6 +59,11 @@ public IcsBackedFactory(ICS ics, Factory delegate) {
this.ics = ics;
}

@ServiceProducer
public Stopwatch newStopwatch() {
return LoggerStopwatch.getInstance();
}

@ServiceProducer(cache = true, name="bindInjector")
public Injector createBindInjector() {
return new BindInjector(ics);
Expand Down