1 16 package org.apache.cocoon.portal.profile.impl; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.io.UnsupportedEncodingException ; 22 import java.util.Iterator ; 23 import java.util.Map ; 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 ; 44 45 52 public class MapProfileLS 53 extends AbstractLogEnabled 54 implements Component, Serviceable, ProfileLS, ThreadSafe, Disposable { 55 56 57 protected ServiceManager manager; 58 59 60 protected XPathProcessor xpathProcessor; 61 62 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 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 getURI(Map keyMap) 82 throws Exception { 83 final StringBuffer buffer = new StringBuffer (); 84 Iterator iter = keyMap.entrySet().iterator(); 85 boolean pars = false; 86 boolean first = true; 87 while ( iter.hasNext() ) { 88 final Map.Entry entry = (Entry) iter.next(); 89 final String 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 getSaveURI(Map keyMap) 121 throws Exception { 122 final StringBuffer buffer = new StringBuffer (this.getURI(keyMap)); 123 return buffer; 124 } 125 126 129 public Object loadProfile(Object key, Map parameters) 130 throws Exception { 131 final Map keyMap = (Map ) key; 132 133 final String 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 155 public void saveProfile(Object key, Map parameters, Object profile) throws Exception { 156 final Map keyMap = (Map ) key; 157 158 final String uri = this.getURI( keyMap ); 159 160 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 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 writer = new ByteArrayOutputStream (); 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 uee) { 199 } 201 202 source = resolver.resolveURI(buffer.toString()); 203 204 parser = (SAXParser)this.manager.lookup(SAXParser.ROLE); 205 Element element = DOMUtil.getDocumentFragment(parser, new InputStreamReader (source.getInputStream())).getOwnerDocument().getDocumentElement(); 206 if (!DOMUtil.getValueOf(element, "descendant::sourceResult/execution", this.xpathProcessor).trim().equals("success")) { 207 throw new IOException ("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 223 public SourceValidity getValidity(Object key, Map parameters) { 224 SourceResolver resolver = null; 225 Source source = null; 226 try { 227 final Map keyMap = (Map ) key; 228 229 final String 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 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 |