| 1 |
|
package org.trails.page; |
| 2 |
|
|
| 3 |
|
import org.apache.commons.logging.Log; |
| 4 |
|
import org.apache.commons.logging.LogFactory; |
| 5 |
|
import org.apache.hivemind.impl.MessageFormatter; |
| 6 |
|
import org.apache.hivemind.util.Defense; |
| 7 |
|
import org.apache.tapestry.IPage; |
| 8 |
|
import org.apache.tapestry.IRequestCycle; |
| 9 |
|
import org.apache.tapestry.PageNotFoundException; |
| 10 |
|
|
| 11 |
|
import java.util.HashMap; |
| 12 |
|
import java.util.Map; |
| 13 |
|
|
| 14 |
4 |
public class DefaultPageResolver implements PageResolver |
| 15 |
|
{ |
| 16 |
4 |
private static final Log LOG = LogFactory.getLog(DefaultPageResolver.class); |
| 17 |
|
|
| 18 |
4 |
private String defaultPrefix = "Default"; |
| 19 |
|
|
| 20 |
4 |
private boolean cacheDisabled = System.getProperty("org.apache.tapestry.disable-caching") != null; |
| 21 |
|
|
| 22 |
|
private Map<PageType, String> postFixMap; |
| 23 |
|
|
| 24 |
|
public String getPostFix(PageType pageType) |
| 25 |
|
{ |
| 26 |
20 |
return getPostFixMap().get(pageType); |
| 27 |
|
} |
| 28 |
|
|
| 29 |
|
public IPage resolvePage(IRequestCycle cycle, Class type, PageType pageType) |
| 30 |
|
{ |
| 31 |
12 |
Defense.notNull(type, "type"); |
| 32 |
12 |
Defense.notNull(pageType, "pageType"); |
| 33 |
|
|
| 34 |
12 |
String pageName = type.getSimpleName() + getPostFix(pageType); |
| 35 |
12 |
IPage page = null; |
| 36 |
|
try |
| 37 |
|
{ |
| 38 |
12 |
page = cycle.getPage(pageName); |
| 39 |
|
|
| 40 |
8 |
} catch (PageNotFoundException ae) |
| 41 |
|
{ |
| 42 |
8 |
page = cycle.getPage(getDefaultPrefix() + getPostFix(pageType)); |
| 43 |
|
|
| 44 |
8 |
if (!cacheDisabled) |
| 45 |
|
{ |
| 46 |
0 |
if (LOG.isDebugEnabled()) |
| 47 |
0 |
LOG.debug(_formatter.format("installing-page", pageName, page.getNamespace(), page.getSpecification())); |
| 48 |
0 |
page.getNamespace().installPageSpecification(pageName, page.getSpecification()); |
| 49 |
0 |
page = cycle.getPage(pageName); |
| 50 |
|
} |
| 51 |
|
} |
| 52 |
12 |
return page; |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
public Map<PageType, String> getPostFixMap() |
| 56 |
|
{ |
| 57 |
20 |
return postFixMap; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
public void setPostFixMap(Map<PageType, String> postFixMap) |
| 61 |
|
{ |
| 62 |
0 |
this.postFixMap = postFixMap; |
| 63 |
0 |
} |
| 64 |
|
|
| 65 |
4 |
public DefaultPageResolver() |
| 66 |
|
{ |
| 67 |
4 |
postFixMap = new HashMap<PageType, String>(); |
| 68 |
32 |
for (PageType pageType : PageType.values()) |
| 69 |
|
{ |
| 70 |
24 |
postFixMap.put(pageType, pageType.toString()); |
| 71 |
|
} |
| 72 |
4 |
} |
| 73 |
|
|
| 74 |
|
public String getDefaultPrefix() |
| 75 |
|
{ |
| 76 |
8 |
return defaultPrefix; |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
public void setDefaultPrefix(String defaultPostfix) |
| 80 |
|
{ |
| 81 |
0 |
this.defaultPrefix = defaultPostfix; |
| 82 |
0 |
} |
| 83 |
|
|
| 84 |
|
public void setCacheDisabled(boolean cacheDisabled) |
| 85 |
|
{ |
| 86 |
4 |
this.cacheDisabled = cacheDisabled; |
| 87 |
4 |
} |
| 88 |
|
|
| 89 |
4 |
private static final MessageFormatter _formatter = new MessageFormatter(LOG, "org.apache.tapestry.resolver.ResolverStrings"); |
| 90 |
|
} |