KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > profile > impl > MapProfileLS


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.profile.impl;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Map.Entry;
25
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.component.Component;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.avalon.framework.service.Serviceable;
32 import org.apache.avalon.framework.thread.ThreadSafe;
33 import org.apache.cocoon.components.persistence.CastorSourceConverter;
34 import org.apache.cocoon.portal.profile.ProfileLS;
35 import org.apache.cocoon.util.NetUtils;
36 import org.apache.cocoon.xml.dom.DOMUtil;
37 import org.apache.excalibur.source.ModifiableSource;
38 import org.apache.excalibur.source.Source;
39 import org.apache.excalibur.source.SourceResolver;
40 import org.apache.excalibur.source.SourceValidity;
41 import org.apache.excalibur.xml.sax.SAXParser;
42 import org.apache.excalibur.xml.xpath.XPathProcessor;
43 import org.w3c.dom.Element JavaDoc;
44
45 /**
46  * This implementation uses Castor to load/save the profiles.
47  *
48  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
49  *
50  * @version CVS $Id: MapProfileLS.java 156634 2005-03-09 11:00:37Z cziegeler $
51  */

52 public class MapProfileLS
53     extends AbstractLogEnabled
54     implements Component, Serviceable, ProfileLS, ThreadSafe, Disposable {
55
56     /** The component manager */
57     protected ServiceManager manager;
58
59     /** The XPath Processor */
60     protected XPathProcessor xpathProcessor;
61     
62     /* (non-Javadoc)
63      * @see org.apache.avalon.framework.activity.Disposable#dispose()
64      */

65     public void dispose() {
66         if ( this.manager != null ) {
67             this.manager.release( this.xpathProcessor );
68             this.xpathProcessor = null;
69             this.manager = null;
70         }
71     }
72
73     /* (non-Javadoc)
74      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
75      */

76     public void service(ServiceManager manager) throws ServiceException {
77         this.manager = manager;
78         this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
79     }
80
81     protected String JavaDoc getURI(Map JavaDoc keyMap)
82     throws Exception JavaDoc {
83         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84         Iterator JavaDoc iter = keyMap.entrySet().iterator();
85         boolean pars = false;
86         boolean first = true;
87         while ( iter.hasNext() ) {
88             final Map.Entry JavaDoc entry = (Entry) iter.next();
89             final String JavaDoc append = entry.getValue().toString();
90             if ( pars ) {
91                 if ( first ) {
92                     first = false;
93                     if ( buffer.toString().indexOf('?') == -1 ) {
94                         buffer.append('?');
95                     } else {
96                         buffer.append('&');
97                     }
98                 } else {
99                     buffer.append('&');
100                 }
101                 buffer.append(entry.getKey().toString());
102                 buffer.append('=');
103             } else {
104                 if ( !first && !"?".equals(append) ) {
105                     buffer.append('/');
106                 }
107                 first = false;
108             }
109             if ( "?".equals(append) ) {
110                 first = true;
111                 pars = true;
112             } else {
113                 buffer.append(append);
114             }
115         }
116         
117         return buffer.toString();
118     }
119     
120     protected StringBuffer JavaDoc getSaveURI(Map JavaDoc keyMap)
121     throws Exception JavaDoc {
122         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(this.getURI(keyMap));
123         return buffer;
124     }
125     
126     /* (non-Javadoc)
127      * @see org.apache.cocoon.portal.profile.ProfileLS#loadProfile(java.lang.Object)
128      */

129     public Object JavaDoc loadProfile(Object JavaDoc key, Map JavaDoc parameters)
130     throws Exception JavaDoc {
131         final Map JavaDoc keyMap = (Map JavaDoc) key;
132         
133         final String JavaDoc uri = this.getURI( keyMap );
134         
135         Source source = null;
136         CastorSourceConverter converter = null;
137         SourceResolver resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
138         try {
139             source = resolver.resolveURI(uri);
140             converter = (CastorSourceConverter) this.manager.lookup(CastorSourceConverter.ROLE);
141
142             return converter.getObject(source.getInputStream(), parameters);
143         } finally {
144             if ( resolver != null ) {
145                 resolver.release(source);
146             }
147             manager.release(converter);
148             manager.release(resolver);
149         }
150     }
151
152     /* (non-Javadoc)
153      * @see org.apache.cocoon.portal.profile.ProfileLS#saveProfile(java.lang.Object, java.lang.Object)
154      */

155     public void saveProfile(Object JavaDoc key, Map JavaDoc parameters, Object JavaDoc profile) throws Exception JavaDoc {
156         final Map JavaDoc keyMap = (Map JavaDoc) key;
157         
158         final String JavaDoc uri = this.getURI( keyMap );
159
160         // first test: modifiable source?
161
SourceResolver resolver = null;
162         CastorSourceConverter converter = null;
163         Source source = null;
164         try {
165             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
166             source = resolver.resolveURI(uri);
167             if ( source instanceof ModifiableSource ) {
168                 converter = (CastorSourceConverter) this.manager.lookup(CastorSourceConverter.ROLE);
169                 converter.storeObject( ((ModifiableSource)source).getOutputStream(), parameters, profile);
170                 return;
171             }
172
173         } finally {
174             if ( resolver != null ) {
175                 resolver.release(source);
176             }
177             manager.release(converter);
178             manager.release(resolver);
179             source = null;
180             converter = null;
181             resolver = null;
182         }
183         
184         final StringBuffer JavaDoc buffer = this.getSaveURI( keyMap );
185
186         SAXParser parser = null;
187         try {
188             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
189             converter = (CastorSourceConverter) this.manager.lookup(CastorSourceConverter.ROLE);
190
191             ByteArrayOutputStream JavaDoc writer = new ByteArrayOutputStream JavaDoc();
192         
193             converter.storeObject(writer, parameters, profile);
194
195             buffer.append("&content=");
196             try {
197                 buffer.append(NetUtils.encode(writer.toString(), "utf-8"));
198             } catch (UnsupportedEncodingException JavaDoc uee) {
199                 // ignore this as utf-8 is always supported
200
}
201
202             source = resolver.resolveURI(buffer.toString());
203
204             parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
205             Element JavaDoc element = DOMUtil.getDocumentFragment(parser, new InputStreamReader JavaDoc(source.getInputStream())).getOwnerDocument().getDocumentElement();
206             if (!DOMUtil.getValueOf(element, "descendant::sourceResult/execution", this.xpathProcessor).trim().equals("success")) {
207                 throw new IOException JavaDoc("Could not save profile: "+DOMUtil.getValueOf(element, "descendant::sourceResult/message", this.xpathProcessor));
208             }
209
210         } finally {
211             if ( resolver != null ) {
212                 resolver.release(source);
213             }
214             manager.release(parser);
215             manager.release(converter);
216             manager.release(resolver);
217         }
218     }
219
220     /* (non-Javadoc)
221      * @see org.apache.cocoon.portal.profile.ProfileLS#getValidity(java.lang.Object)
222      */

223     public SourceValidity getValidity(Object JavaDoc key, Map JavaDoc parameters) {
224         SourceResolver resolver = null;
225         Source source = null;
226         try {
227             final Map JavaDoc keyMap = (Map JavaDoc) key;
228         
229             final String JavaDoc uri = this.getURI( keyMap );
230
231             resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
232             source = resolver.resolveURI(uri);
233             return source.getValidity();
234         } catch (Exception JavaDoc e) {
235             getLogger().warn(e.getMessage(), e);
236             return null;
237         } finally {
238             if (resolver != null) {
239                 resolver.release(source);
240             }
241             manager.release(resolver);
242         }
243     }
244
245 }
246
Popular Tags