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.portal.aspect.impl;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.CascadingRuntimeException;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceSelector;
026: import org.apache.cocoon.portal.aspect.AspectDataHandler;
027: import org.apache.cocoon.portal.aspect.AspectDataStore;
028: import org.apache.cocoon.portal.aspect.AspectDescription;
029: import org.apache.cocoon.portal.aspect.Aspectalizable;
030: import org.apache.cocoon.portal.aspect.AspectalizableDescription;
031:
032: /**
033: *
034: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
035: *
036: * @version CVS $Id: DefaultAspectDataHandler.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class DefaultAspectDataHandler implements AspectDataHandler {
039:
040: protected AspectalizableDescription description;
041:
042: protected ServiceSelector storeSelector;
043:
044: /**
045: * Constructor
046: */
047: public DefaultAspectDataHandler(AspectalizableDescription desc,
048: ServiceSelector storeSelector) {
049: this .description = desc;
050: this .storeSelector = storeSelector;
051: }
052:
053: /* (non-Javadoc)
054: * @see org.apache.cocoon.portal.aspect.AspectDataHandler#getAspectData(org.apache.cocoon.portal.aspect.Aspectalizable, java.lang.String)
055: */
056: public Object getAspectData(Aspectalizable owner, String aspectName) {
057: // is this aspect allowed?
058: AspectDescription aspectDesc = this .description
059: .getAspectDescription(aspectName);
060: if (aspectDesc == null)
061: return null;
062:
063: // lookup storage
064: AspectDataStore store = null;
065: Object data = null;
066: try {
067: store = (AspectDataStore) this .storeSelector
068: .select(aspectDesc.getStoreName());
069: data = store.getAspectData(owner, aspectName);
070:
071: if (data == null && aspectDesc.isAutoCreate()) {
072: data = AspectUtil.createNewInstance(aspectDesc);
073: store.setAspectData(owner, aspectName, data);
074: }
075:
076: } catch (ServiceException ce) {
077: throw new CascadingRuntimeException(
078: "Unable to lookup aspect data store "
079: + aspectDesc.getStoreName(), ce);
080: } finally {
081: this .storeSelector.release(store);
082: }
083:
084: return data;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.apache.cocoon.portal.aspect.AspectDataHandler#getAspectDatas(org.apache.cocoon.portal.aspect.Aspectalizable)
089: */
090: public Map getAspectDatas(Aspectalizable owner) {
091: AspectDatasHashMap datas = new AspectDatasHashMap(owner, this );
092: Iterator iter = this .description.getAspectDescriptions()
093: .iterator();
094: while (iter.hasNext()) {
095: AspectDescription current = (AspectDescription) iter.next();
096: Object data = this .getAspectData(owner, current.getName());
097: if (data != null) {
098: datas.put(current.getName(), data);
099: }
100: }
101: datas.initialize();
102: return datas;
103: }
104:
105: /* (non-Javadoc)
106: * @see org.apache.cocoon.portal.aspect.AspectDataHandler#getPersistentAspectDatas(org.apache.cocoon.portal.aspect.Aspectalizable)
107: */
108: public Map getPersistentAspectDatas(Aspectalizable owner) {
109: Map datas = new HashMap();
110: Iterator iter = this .description.getAspectDescriptions()
111: .iterator();
112: while (iter.hasNext()) {
113: AspectDescription current = (AspectDescription) iter.next();
114:
115: // lookup storage
116: AspectDataStore store = null;
117: Object data = null;
118: try {
119: store = (AspectDataStore) this .storeSelector
120: .select(current.getStoreName());
121: if (store.isPersistent()) {
122: data = store
123: .getAspectData(owner, current.getName());
124:
125: if (data == null && current.isAutoCreate()) {
126: data = AspectUtil.createNewInstance(current);
127: store.setAspectData(owner, current.getName(),
128: data);
129: }
130:
131: if (data != null) {
132: datas.put(current.getName(), data);
133: }
134: }
135:
136: } catch (ServiceException ce) {
137: throw new CascadingRuntimeException(
138: "Unable to lookup aspect data store "
139: + current.getStoreName(), ce);
140: } finally {
141: this .storeSelector.release(store);
142: }
143:
144: }
145: return datas;
146: }
147:
148: /* (non-Javadoc)
149: * @see org.apache.cocoon.portal.aspect.AspectDataHandler#setAspectData(org.apache.cocoon.portal.aspect.Aspectalizable, java.lang.String, java.lang.Object)
150: */
151: public void setAspectData(Aspectalizable owner, String aspectName,
152: Object data) {
153: // is this aspect allowed?
154: AspectDescription aspectDesc = this .description
155: .getAspectDescription(aspectName);
156: if (aspectDesc == null)
157: return;
158:
159: // lookup storage
160: AspectDataStore store = null;
161: try {
162: store = (AspectDataStore) this .storeSelector
163: .select(aspectDesc.getStoreName());
164: store.setAspectData(owner, aspectName, AspectUtil.convert(
165: aspectDesc, data));
166: } catch (ServiceException ce) {
167: throw new CascadingRuntimeException(
168: "Unable to lookup aspect data store "
169: + aspectDesc.getStoreName(), ce);
170: } finally {
171: this .storeSelector.release(store);
172: }
173: }
174:
175: /**
176: * Is this supported
177: */
178: public boolean isAspectSupported(String aspectName) {
179: return (this .description.getAspectDescription(aspectName) != null);
180: }
181: }
182:
183: final class AspectDatasHashMap extends HashMap {
184:
185: protected AspectDataHandler handler;
186: protected Aspectalizable owner;
187: protected boolean init = false;
188:
189: public AspectDatasHashMap(Aspectalizable owner,
190: AspectDataHandler handler) {
191: this .handler = handler;
192: this .owner = owner;
193: }
194:
195: /* (non-Javadoc)
196: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
197: */
198: public Object put(Object key, Object value) {
199: if (this .init) {
200: this .handler.setAspectData(this .owner, key.toString(),
201: value);
202: value = this .handler.getAspectData(this .owner, key
203: .toString());
204: }
205: return super .put(key, value);
206: }
207:
208: public void initialize() {
209: this .init = true;
210: }
211: }
|