1 16 package org.apache.cocoon.portal.tools; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.avalon.framework.activity.Disposable; 28 import org.apache.avalon.framework.component.Component; 29 import org.apache.avalon.framework.configuration.Configuration; 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 32 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer; 33 import org.apache.avalon.framework.parameters.ParameterException; 34 import org.apache.avalon.framework.parameters.Parameterizable; 35 import org.apache.avalon.framework.parameters.Parameters; 36 import org.apache.avalon.framework.service.ServiceException; 37 import org.apache.avalon.framework.service.ServiceManager; 38 import org.apache.avalon.framework.service.Serviceable; 39 import org.apache.avalon.framework.thread.ThreadSafe; 40 import org.apache.cocoon.ProcessingException; 41 import org.apache.cocoon.portal.PortalService; 42 import org.apache.cocoon.portal.tools.helper.PortalObjects; 43 import org.apache.cocoon.portal.tools.service.UserRightsService; 44 import org.apache.cocoon.webapps.session.ContextManager; 45 import org.apache.cocoon.webapps.session.context.SessionContext; 46 import org.apache.excalibur.source.ModifiableSource; 47 import org.apache.excalibur.source.Source; 48 import org.apache.excalibur.source.SourceException; 49 import org.apache.excalibur.source.SourceResolver; 50 import org.apache.excalibur.source.SourceUtil; 51 import org.w3c.dom.DocumentFragment ; 52 import org.w3c.dom.Text ; 53 import org.xml.sax.SAXException ; 54 55 59 public class PortalToolManager implements ThreadSafe, Component, Parameterizable, Serviceable, Disposable { 60 61 public static final String ROLE = PortalToolManager.class.getName(); 62 63 private HashMap tools = new HashMap (); 64 65 private ServiceManager manager; 66 67 private List i18n = new ArrayList (); 68 69 70 private String rootDir; 71 private String confFile; 72 private String authFile; 73 private static final String pluginDir = "plugins/"; 74 private static final String pluginConfFile = "tool.xml"; 75 private static final String i18nDir = "i18n/"; 76 77 private Configuration configuration; 78 private UserRightsService userRightsService; 79 80 private ContextManager contextManager; 81 82 83 protected SourceResolver resolver; 84 85 88 public void parameterize(Parameters para) throws ParameterException { 89 this.rootDir = para.getParameter("root", "/"); 90 this.confFile = para.getParameter("conf", "conf.xml"); 91 this.authFile = para.getParameter("auth", "auth.xml"); 92 93 Source fSource = null; 94 try { 95 fSource = this.resolver.resolveURI(rootDir + confFile); 96 DefaultConfigurationBuilder confBuilder = new DefaultConfigurationBuilder(); 97 this.configuration = confBuilder.build(fSource.getInputStream()); 98 fSource = this.resolver.resolveURI(rootDir + authFile); 99 this.userRightsService = new UserRightsService(); 100 this.userRightsService.setLocation(fSource); 101 this.userRightsService.initialize(); 102 this.init(); 103 } catch (ProcessingException e) { 104 e.printStackTrace(); 105 } catch (IOException e) { 106 e.printStackTrace(); 107 } catch (ConfigurationException e) { 108 e.printStackTrace(); 109 } catch (SAXException e) { 110 e.printStackTrace(); 111 } finally { 112 this.resolver.release(fSource); 113 } 114 } 115 116 117 122 public void init() throws ProcessingException, IOException { 123 Source toolsDir = null; 124 PortalToolBuilder builder = new PortalToolBuilder(); 125 try { 126 toolsDir = this.resolver.resolveURI(rootDir + pluginDir); 127 128 final File td = SourceUtil.getFile(toolsDir); 129 130 if( td == null || !td.isDirectory() ) { 131 throw new ProcessingException("PortalToolManager: tool-dir must be a directory: " + toolsDir.getURI()); 132 } 133 final File [] dirs = td.listFiles(); 134 for(int i = 0; i< dirs.length; i++) { 135 final File f = dirs[i]; 136 if (f.isDirectory()) { 137 String path = f.getAbsolutePath().endsWith(File.separator) ? f.getAbsolutePath() : f.getAbsoluteFile() + File.separator; 138 File conf = new File (path + pluginConfFile); 139 if (conf.exists()) { 140 PortalTool pTool = builder.buildTool(conf, this.rootDir, pluginDir, i18nDir); 141 if(pTool != null) { 142 tools.put(pTool.getId(), pTool); 143 i18n.addAll(pTool.getI18n()); 144 } 145 } 146 } 147 } 148 } finally { 149 this.resolver.release(toolsDir); 150 } 151 } 152 153 157 public Collection getTools() { 158 return tools.values(); 159 } 160 161 166 public PortalTool getTool(String id) { 167 return (PortalTool) tools.get(id); 168 } 169 170 174 public Collection getToolsWithFunctions() { 175 ArrayList tmp = new ArrayList (); 176 for(Iterator it = tools.values().iterator(); it.hasNext();) { 177 PortalTool pt; 178 if(((pt = (PortalTool) it.next())).getPublicFunctions().size() > 0) 179 tmp.add(pt); 180 } 181 return tmp; 182 } 183 184 public List getI18n() { 185 return i18n; 186 } 187 188 191 public void service(ServiceManager manager) throws ServiceException { 192 this.manager = manager; 193 this.resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE); 194 this.contextManager = (ContextManager)this.manager.lookup(ContextManager.ROLE); 195 } 196 197 201 public Configuration getConfiguration() { 202 return configuration; 203 } 204 205 208 public synchronized void saveConfiguration() { 209 DefaultConfigurationSerializer confSer = new DefaultConfigurationSerializer(); 210 Source confSource = null; 211 try { 212 213 confSource = this.resolver.resolveURI(rootDir + confFile); 214 if (confSource instanceof ModifiableSource) { 215 confSer.serialize(((ModifiableSource) confSource).getOutputStream(), configuration); 216 } 217 } catch (MalformedURLException e) { 218 e.printStackTrace(); 219 } catch (SourceException e) { 220 e.printStackTrace(); 221 } catch (IOException e) { 222 e.printStackTrace(); 223 } catch (ConfigurationException e) { 224 e.printStackTrace(); 225 } catch (SAXException e) { 226 e.printStackTrace(); 227 } finally { 228 this.resolver.release(confSource); 229 } 230 } 231 232 public UserRightsService getUserRightsService() { 233 return this.userRightsService; 234 } 235 236 241 public String sGet(String key) { 242 SessionContext ctx; 243 try { 244 ctx = this.contextManager.getContext("authentication"); 245 } catch (Exception e) { 246 return null; 247 } 248 249 if(!key.startsWith("/")) 250 key = "/" + key; 251 DocumentFragment node = null; 252 try { 253 node = ctx.getXML("/portalTools" + key); 254 } catch (Exception e) { 255 } 257 return org.apache.cocoon.xml.dom.DOMUtil.getValueOfNode(node); 258 } 259 260 265 public void sSet(String key, String value) { 266 SessionContext ctx; 267 try { 268 ctx = this.contextManager.getContext("authentication"); 269 } catch (Exception e) { 270 return; 271 } 272 273 if(!key.startsWith("/")) 274 key = "/" + key; 275 DocumentFragment frag; 276 try { 277 frag = ctx.getXML("/"); 278 org.w3c.dom.Document doc = frag.getOwnerDocument(); 279 DocumentFragment newFrag = doc.createDocumentFragment(); 280 Text txt = doc.createTextNode(value); 281 newFrag.appendChild(txt); 282 ctx.setXML("/portalTools" + key, newFrag); 283 } catch (ProcessingException e) { 284 } 285 } 286 287 290 public void dispose() { 291 if ( this.manager != null ) { 292 this.manager.release(this.resolver); 293 this.manager.release(this.contextManager); 294 this.resolver = null; 295 this.contextManager = null; 296 this.manager = null; 297 } 298 } 299 300 public PortalObjects getPortalObjects() { 301 try { 302 return new PortalObjects((PortalService) this.manager.lookup(org.apache.cocoon.portal.PortalService.ROLE)); 303 } catch (ServiceException e) { 304 return null; 305 } 306 307 } 308 309 public void releasePortalObjects(PortalObjects pObj) { 310 this.manager.release(pObj.getPortalService()); 311 } 312 313 } 314 | Popular Tags |