KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > aspect > impl > DefaultAspectDataHandler


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.aspect.impl;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.CascadingRuntimeException;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceSelector;
25 import org.apache.cocoon.portal.aspect.AspectDataHandler;
26 import org.apache.cocoon.portal.aspect.AspectDataStore;
27 import org.apache.cocoon.portal.aspect.AspectDescription;
28 import org.apache.cocoon.portal.aspect.Aspectalizable;
29 import org.apache.cocoon.portal.aspect.AspectalizableDescription;
30
31 /**
32  *
33  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
34  *
35  * @version CVS $Id: DefaultAspectDataHandler.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public class DefaultAspectDataHandler
38     implements AspectDataHandler {
39
40     protected AspectalizableDescription description;
41     
42     protected ServiceSelector storeSelector;
43     
44     /**
45      * Constructor
46      */

47     public DefaultAspectDataHandler(AspectalizableDescription desc,
48                                     ServiceSelector storeSelector) {
49         this.description = desc;
50         this.storeSelector = storeSelector;
51     }
52
53     /* (non-Javadoc)
54      * @see org.apache.cocoon.portal.aspect.AspectDataHandler#getAspectData(org.apache.cocoon.portal.aspect.Aspectalizable, java.lang.String)
55      */

56     public Object JavaDoc getAspectData(Aspectalizable owner, String JavaDoc aspectName) {
57         // is this aspect allowed?
58
AspectDescription aspectDesc = this.description.getAspectDescription( aspectName );
59         if ( aspectDesc == null ) return null;
60         
61         // lookup storage
62
AspectDataStore store = null;
63         Object JavaDoc data = null;
64         try {
65             store = (AspectDataStore)this.storeSelector.select(aspectDesc.getStoreName());
66             data = store.getAspectData(owner, aspectName);
67
68             if ( data == null && aspectDesc.isAutoCreate() ) {
69                 data = AspectUtil.createNewInstance(aspectDesc);
70                 store.setAspectData( owner, aspectName, data );
71             }
72
73         } catch (ServiceException ce) {
74             throw new CascadingRuntimeException("Unable to lookup aspect data store " + aspectDesc.getStoreName(), ce);
75         } finally {
76             this.storeSelector.release( store );
77         }
78
79         return data;
80     }
81
82     /* (non-Javadoc)
83      * @see org.apache.cocoon.portal.aspect.AspectDataHandler#getAspectDatas(org.apache.cocoon.portal.aspect.Aspectalizable)
84      */

85     public Map JavaDoc getAspectDatas(Aspectalizable owner) {
86         AspectDatasHashMap datas = new AspectDatasHashMap(owner, this);
87         Iterator JavaDoc iter = this.description.getAspectDescriptions().iterator();
88         while ( iter.hasNext() ) {
89             AspectDescription current = (AspectDescription)iter.next();
90             Object JavaDoc data = this.getAspectData(owner, current.getName());
91             if ( data != null ) {
92                 datas.put( current.getName(), data );
93             }
94         }
95         datas.initialize();
96         return datas;
97     }
98
99     /* (non-Javadoc)
100      * @see org.apache.cocoon.portal.aspect.AspectDataHandler#getPersistentAspectDatas(org.apache.cocoon.portal.aspect.Aspectalizable)
101      */

102     public Map JavaDoc getPersistentAspectDatas(Aspectalizable owner) {
103         Map JavaDoc datas = new HashMap JavaDoc();
104         Iterator JavaDoc iter = this.description.getAspectDescriptions().iterator();
105         while ( iter.hasNext() ) {
106             AspectDescription current = (AspectDescription)iter.next();
107
108             // lookup storage
109
AspectDataStore store = null;
110             Object JavaDoc data = null;
111             try {
112                 store = (AspectDataStore)this.storeSelector.select(current.getStoreName());
113                 if ( store.isPersistent() ) {
114                     data = store.getAspectData(owner, current.getName());
115
116                     if ( data == null && current.isAutoCreate() ) {
117                         data = AspectUtil.createNewInstance(current);
118                         store.setAspectData( owner, current.getName(), data );
119                     }
120
121                     if ( data != null ) {
122                         datas.put( current.getName(), data );
123                     }
124                 }
125
126             } catch (ServiceException ce) {
127                 throw new CascadingRuntimeException("Unable to lookup aspect data store " + current.getStoreName(), ce);
128             } finally {
129                 this.storeSelector.release( store );
130             }
131
132         }
133         return datas;
134     }
135
136     /* (non-Javadoc)
137      * @see org.apache.cocoon.portal.aspect.AspectDataHandler#setAspectData(org.apache.cocoon.portal.aspect.Aspectalizable, java.lang.String, java.lang.Object)
138      */

139     public void setAspectData(Aspectalizable owner,
140                                String JavaDoc aspectName,
141                                Object JavaDoc data) {
142         // is this aspect allowed?
143
AspectDescription aspectDesc = this.description.getAspectDescription( aspectName );
144         if ( aspectDesc == null ) return;
145
146         // lookup storage
147
AspectDataStore store = null;
148         try {
149             store = (AspectDataStore)this.storeSelector.select(aspectDesc.getStoreName());
150             store.setAspectData(owner, aspectName, AspectUtil.convert(aspectDesc, data));
151         } catch (ServiceException ce) {
152             throw new CascadingRuntimeException("Unable to lookup aspect data store " + aspectDesc.getStoreName(), ce);
153         } finally {
154             this.storeSelector.release( store );
155         }
156     }
157
158     /**
159      * Is this supported
160      */

161     public boolean isAspectSupported(String JavaDoc aspectName) {
162         return (this.description.getAspectDescription(aspectName) != null);
163     }
164 }
165
166 final class AspectDatasHashMap extends HashMap JavaDoc {
167     
168     protected AspectDataHandler handler;
169     protected Aspectalizable owner;
170     protected boolean init = false;
171     
172     public AspectDatasHashMap(Aspectalizable owner, AspectDataHandler handler) {
173         this.handler = handler;
174         this.owner = owner;
175     }
176     
177     /* (non-Javadoc)
178      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
179      */

180     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
181         if ( this.init ) {
182             this.handler.setAspectData(this.owner, key.toString(), value);
183             value = this.handler.getAspectData(this.owner, key.toString());
184         }
185         return super.put(key, value);
186     }
187
188     public void initialize() {
189         this.init = true;
190     }
191 }
192
Popular Tags