001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.persistence;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.component.Component;
023: import org.apache.avalon.framework.context.Context;
024: import org.apache.avalon.framework.context.ContextException;
025: import org.apache.avalon.framework.context.Contextualizable;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028: import org.apache.cocoon.components.ContextHelper;
029: import org.apache.cocoon.environment.ObjectModelHelper;
030:
031: /**
032: * The default implementation
033: *
034: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
035: *
036: * @version CVS $Id: RequestDataStoreImpl.java 433543 2006-08-22 06:22:54Z crossley $
037: * @since 2.1.1
038: */
039: public class RequestDataStoreImpl extends AbstractLogEnabled implements
040: Component, ThreadSafe, RequestDataStore, Contextualizable {
041:
042: protected Context context;
043:
044: protected final String requestDataKey = this .getClass().getName()
045: + "/RD";
046:
047: protected final String globalRequestDataKey = this .getClass()
048: .getName()
049: + "/GRD";
050:
051: /* (non-Javadoc)
052: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
053: */
054: public void contextualize(Context context) throws ContextException {
055: this .context = context;
056: }
057:
058: /* (non-Javadoc)
059: * @see org.apache.cocoon.components.persistance.RequestDataStore#getGlobalRequestData(java.lang.String)
060: */
061: public Object getGlobalRequestData(String key) {
062: Object value = null;
063: final Map objectModel = ContextHelper
064: .getObjectModel(this .context);
065: Map m = (Map) objectModel.get(this .globalRequestDataKey);
066: if (m != null) {
067: value = m.get(key);
068: }
069: return value;
070: }
071:
072: /* (non-Javadoc)
073: * @see org.apache.cocoon.components.persistance.RequestDataStore#getRequestData(java.lang.String)
074: */
075: public Object getRequestData(String key) {
076: Object value = null;
077: final Map objectModel = ContextHelper
078: .getObjectModel(this .context);
079: Map m = (Map) objectModel.get(this .requestDataKey
080: + ObjectModelHelper.getRequest(objectModel).hashCode());
081: if (m != null) {
082: value = m.get(key);
083: }
084: return value;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.apache.cocoon.components.persistance.RequestDataStore#removeGlobalRequestData(java.lang.String)
089: */
090: public void removeGlobalRequestData(String key) {
091: final Map objectModel = ContextHelper
092: .getObjectModel(this .context);
093: Map m = (Map) objectModel.get(this .globalRequestDataKey);
094: if (m != null) {
095: objectModel.remove(key);
096: }
097: }
098:
099: /* (non-Javadoc)
100: * @see org.apache.cocoon.components.persistance.RequestDataStore#removeRequestData(java.lang.String)
101: */
102: public void removeRequestData(String key) {
103: final Map objectModel = ContextHelper
104: .getObjectModel(this .context);
105: Map m = (Map) objectModel.get(this .requestDataKey
106: + ObjectModelHelper.getRequest(objectModel).hashCode());
107: if (m != null) {
108: objectModel.remove(key);
109: }
110: }
111:
112: /* (non-Javadoc)
113: * @see org.apache.cocoon.components.persistance.RequestDataStore#setGlobalRequestData(java.lang.String, java.lang.Object)
114: */
115: public void setGlobalRequestData(String key, Object value) {
116: final Map objectModel = ContextHelper
117: .getObjectModel(this .context);
118: Map m = (Map) objectModel.get(this .globalRequestDataKey);
119: if (m == null) {
120: m = new HashMap();
121: objectModel.put(this .globalRequestDataKey, m);
122: }
123: m.put(key, value);
124: }
125:
126: /* (non-Javadoc)
127: * @see org.apache.cocoon.components.persistance.RequestDataStore#setRequestData(java.lang.String, java.lang.Object)
128: */
129: public void setRequestData(String key, Object value) {
130: final Map objectModel = ContextHelper
131: .getObjectModel(this .context);
132: Map m = (Map) objectModel.get(this .requestDataKey
133: + ObjectModelHelper.getRequest(objectModel).hashCode());
134: if (m == null) {
135: m = new HashMap();
136: objectModel.put(this.requestDataKey
137: + ObjectModelHelper.getRequest(objectModel)
138: .hashCode(), m);
139: }
140: m.put(key, value);
141: }
142:
143: }
|