001: /*
002: * Created on 21 May 2007
003: */
004: package uk.org.ponder.rsf.viewstate.support;
005:
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: import uk.org.ponder.reflect.DeepBeanCloner;
010: import uk.org.ponder.reflect.ReflectiveCache;
011: import uk.org.ponder.rsf.viewstate.SimpleViewParameters;
012: import uk.org.ponder.rsf.viewstate.ViewParameters;
013: import uk.org.ponder.rsf.viewstate.ViewParamsReceiver;
014: import uk.org.ponder.rsf.viewstate.ViewParamsRegistry;
015: import uk.org.ponder.rsf.viewstate.ViewParamsReporter;
016: import uk.org.ponder.util.Logger;
017:
018: /** Receives and coordinates view parameters information from the ViewProducers,
019: * as well as contributing the default view marked there.
020: * @author Antranig Basman (antranig@caret.cam.ac.uk)
021: *
022: */
023:
024: public class ViewParamsRegistryImpl implements ViewParamsReceiver,
025: ViewParamsReporter, ViewParamsRegistry {
026: // a concurrent map to be used when live
027: private Map exemplarmap;
028:
029: // A single-threaded hashmap to be used during startup.
030: private Map pendingmap = new HashMap();
031:
032: private String defaultview;
033:
034: // Called from ConcreteViewResolver.init(), i.e. "somewhat late"
035: public void setDefaultView(String viewid) {
036: defaultview = viewid;
037: }
038:
039: private DeepBeanCloner beancloner;
040:
041: public void setDeepBeanCloner(DeepBeanCloner beancloner) {
042: this .beancloner = beancloner;
043: }
044:
045: public void init() {
046: exemplarmap.putAll(pendingmap);
047: pendingmap = null;
048: }
049:
050: public void setReflectiveCache(ReflectiveCache reflectivecache) {
051: exemplarmap = reflectivecache.getConcurrentMap(1);
052: }
053:
054: /**
055: * DO NOT call this method in application scope. Default view cannot be
056: * guaranteed to be called until all views are read.
057: *
058: * @return The default view to be redirected to in case of a Level-1
059: * exception.
060: */
061: public String getDefaultView() {
062: if (defaultview == null) {
063: Logger.log
064: .warn("Warning: no view has been marked as default during initialisation -"
065: + "\nDid you remember to define ViewProducers?");
066: }
067: return "/" + defaultview;
068: }
069:
070: private ViewParameters defaultexemplar = new SimpleViewParameters();
071:
072: public void setDefaultExemplar(ViewParameters defaultexemplar) {
073: this .defaultexemplar = defaultexemplar;
074: }
075:
076: /**
077: * AT MOST ONE view parameters map may be set as "pending" during startup
078: * (presumably through reading of a static file). It will be actioned on
079: * initialisation of the bean, all further will be actioned immediately.
080: */
081: public void setViewParametersMap(Map exemplarmap) {
082: (this .exemplarmap == null ? pendingmap : this .exemplarmap)
083: .putAll(exemplarmap);
084: }
085:
086: public ViewParameters getViewParamsExemplar(String viewID) {
087: ViewParameters togo = (ViewParameters) exemplarmap.get(viewID);
088: if (togo == null)
089: togo = defaultexemplar;
090: togo = togo.copyBase(beancloner);
091: togo.viewID = viewID;
092: return togo;
093: }
094:
095: public void setViewParamsExemplar(String viewid,
096: ViewParameters vpexemplar) {
097: if (vpexemplar != null) {
098: (exemplarmap == null ? pendingmap : exemplarmap).put(
099: viewid, vpexemplar);
100: }
101: }
102:
103: public ViewParameters getViewParameters() {
104: if (defaultview == null) {
105: Logger.log
106: .warn("Warning: no view has been marked as default during initialisation -"
107: + "\nDid you remember to define ViewProducers?");
108: return null;
109: } else {
110: ViewParameters togo = getViewParamsExemplar(defaultview)
111: .copyBase();
112: togo.viewID = defaultview;
113: return togo;
114: }
115:
116: }
117:
118: }
|