1 16 package org.apache.cocoon.portal.impl; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.apache.cocoon.environment.ObjectModelHelper; 27 import org.apache.cocoon.environment.Session; 28 import org.apache.cocoon.portal.Constants; 29 import org.apache.cocoon.portal.PortalComponentManager; 30 31 38 public class PortalServiceInfo { 39 40 private Map portalComponentManagers; 41 42 private Map objectModel; 43 44 protected Map temporaryAttributes = new HashMap (); 45 46 protected String portalName; 47 48 protected String attributePrefix; 49 50 protected PortalComponentManager portalComponentManager; 51 52 public void setup(Map objectModel, Map managers) { 53 this.objectModel = objectModel; 54 this.portalComponentManagers = managers; 55 Map context = (Map )objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 56 if (context != null) { 57 String pm = (String )context.get(Constants.PORTAL_NAME_KEY); 58 if (pm != null) { 59 this.setPortalName(pm); 60 } 61 } 62 if ( this.portalName == null && this.portalComponentManagers.size() == 1 ) { 63 String pm = this.portalComponentManagers.keySet().iterator().next().toString(); 65 this.setPortalName(pm); 66 } 67 } 68 69 public String getPortalName() { 70 return this.portalName; 71 } 72 73 public void setPortalName(String value) { 74 this.portalName = value; 75 this.attributePrefix = this.getClass().getName() + '/' + this.portalName + '/'; 76 this.portalComponentManager = (PortalComponentManager) this.portalComponentManagers.get(this.portalName); 77 if ( this.portalComponentManager == null ) { 78 throw new RuntimeException ("Portal '"+this.portalName+"' is not configured."); 79 } 80 } 81 82 public Object getAttribute(String key) { 83 final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession(false); 84 if (session == null) { 85 return null; 86 } 87 return session.getAttribute( this.attributePrefix + key); 88 } 89 90 public void setAttribute(String key, Object value) { 91 final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession(); 92 session.setAttribute( this.attributePrefix + key, value); 93 } 94 95 public void removeAttribute(String key) { 96 final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession(false); 97 if ( session != null ) { 98 session.removeAttribute( this.attributePrefix + key); 99 } 100 } 101 102 public Iterator getAttributeNames() { 103 final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession(false); 104 if ( session != null ) { 105 List names = new ArrayList (); 106 Enumeration e = session.getAttributeNames(); 107 final int pos = this.attributePrefix.length() + 1; 108 if ( e != null ) { 109 while ( e.hasMoreElements() ) { 110 final String name = (String )e.nextElement(); 111 if ( name.startsWith( this.attributePrefix )) { 112 names.add( name.substring( pos ) ); 113 } 114 } 115 } 116 return names.iterator(); 117 } 118 return Collections.EMPTY_MAP.keySet().iterator(); 119 } 120 121 public Object getTemporaryAttribute(String key) { 122 return this.temporaryAttributes.get( key ); 123 } 124 125 public void setTemporaryAttribute(String key, Object value) { 126 this.temporaryAttributes.put( key, value ); 127 } 128 129 public void removeTemporaryAttribute(String key) { 130 this.temporaryAttributes.remove( key ); 131 } 132 133 public Iterator getTemporaryAttributeNames() { 134 return this.temporaryAttributes.keySet().iterator(); 135 } 136 137 140 public PortalComponentManager getComponentManager() { 141 return this.portalComponentManager; 142 } 143 144 } 145 | Popular Tags |