KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > tools > PortalToolManager


1 /*
2  * Copyright 1999-2005 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.tools;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
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 JavaDoc;
52 import org.w3c.dom.Text JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54
55 /**
56  *
57  * @version CVS $Id: PortalToolManager.java 156712 2005-03-09 23:27:35Z antonio $
58  */

59 public class PortalToolManager implements ThreadSafe, Component, Parameterizable, Serviceable, Disposable {
60     
61     public static final String JavaDoc ROLE = PortalToolManager.class.getName();
62         
63     private HashMap JavaDoc tools = new HashMap JavaDoc();
64
65     private ServiceManager manager;
66     
67     private List JavaDoc i18n = new ArrayList JavaDoc();
68     
69     
70     private String JavaDoc rootDir;
71     private String JavaDoc confFile;
72     private String JavaDoc authFile;
73     private static final String JavaDoc pluginDir = "plugins/";
74     private static final String JavaDoc pluginConfFile = "tool.xml";
75     private static final String JavaDoc i18nDir = "i18n/";
76     
77     private Configuration configuration;
78     private UserRightsService userRightsService;
79
80     private ContextManager contextManager;
81
82     /** The source resolver */
83     protected SourceResolver resolver;
84     
85     /* (non-Javadoc)
86      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
87      */

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 JavaDoc e) {
106             e.printStackTrace();
107         } catch (ConfigurationException e) {
108             e.printStackTrace();
109         } catch (SAXException JavaDoc e) {
110             e.printStackTrace();
111         } finally {
112             this.resolver.release(fSource);
113         }
114     }
115     
116     
117     /**
118      * Initializes the PortalToolManager. Reads the configuration of all plugins etc.
119      * @throws ProcessingException
120      * @throws IOException
121      */

122     public void init() throws ProcessingException, IOException JavaDoc {
123         Source toolsDir = null;
124         PortalToolBuilder builder = new PortalToolBuilder();
125         try {
126             toolsDir = this.resolver.resolveURI(rootDir + pluginDir);
127             
128             final File JavaDoc 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 JavaDoc[] dirs = td.listFiles();
134             for(int i = 0; i< dirs.length; i++) {
135                 final File JavaDoc f = dirs[i];
136                 if (f.isDirectory()) {
137                     String JavaDoc path = f.getAbsolutePath().endsWith(File.separator) ? f.getAbsolutePath() : f.getAbsoluteFile() + File.separator;
138                     File JavaDoc conf = new File JavaDoc(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     /**
154      * Returns a Collection of all Tools
155      * @return
156      */

157     public Collection JavaDoc getTools() {
158         return tools.values();
159     }
160     
161     /**
162      * Returns the tool with the id.
163      * @param id Tool-Id
164      * @return
165      */

166     public PortalTool getTool(String JavaDoc id) {
167         return (PortalTool) tools.get(id);
168     }
169
170     /**
171      * Returns a Collection of tools which offers functions
172      * @return
173      */

174     public Collection JavaDoc getToolsWithFunctions() {
175         ArrayList JavaDoc tmp = new ArrayList JavaDoc();
176         for(Iterator JavaDoc 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 JavaDoc getI18n() {
185         return i18n;
186     }
187     
188     /* (non-Javadoc)
189      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
190      */

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     /**
198      * Returns the Configuration for the plugins
199      * @return
200      */

201     public Configuration getConfiguration() {
202         return configuration;
203     }
204     
205     /**
206      * Saves the configuration
207      */

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 JavaDoc e) {
218             e.printStackTrace();
219         } catch (SourceException e) {
220             e.printStackTrace();
221         } catch (IOException JavaDoc e) {
222             e.printStackTrace();
223         } catch (ConfigurationException e) {
224             e.printStackTrace();
225         } catch (SAXException JavaDoc 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     /**
237      * Returns a value from the auth context
238      * @param key Path (e.g. /foo/bar)
239      * @return
240      */

241     public String JavaDoc sGet(String JavaDoc key) {
242         SessionContext ctx;
243         try {
244             ctx = this.contextManager.getContext("authentication");
245         } catch (Exception JavaDoc e) {
246             return null;
247         }
248
249         if(!key.startsWith("/"))
250             key = "/" + key;
251         DocumentFragment JavaDoc node = null;
252         try {
253             node = ctx.getXML("/portalTools" + key);
254         } catch (Exception JavaDoc e) {
255             // TODO
256
}
257         return org.apache.cocoon.xml.dom.DOMUtil.getValueOfNode(node);
258     }
259     
260     /**
261      * Sets a value in the auth context
262      * @param key Path (e.g. /foo/bar)
263      * @param value Value
264      */

265     public void sSet(String JavaDoc key, String JavaDoc value) {
266         SessionContext ctx;
267         try {
268             ctx = this.contextManager.getContext("authentication");
269         } catch (Exception JavaDoc e) {
270             return;
271         }
272
273         if(!key.startsWith("/"))
274             key = "/" + key;
275         DocumentFragment JavaDoc frag;
276         try {
277             frag = ctx.getXML("/");
278             org.w3c.dom.Document JavaDoc doc = frag.getOwnerDocument();
279             DocumentFragment JavaDoc newFrag = doc.createDocumentFragment();
280             Text JavaDoc txt = doc.createTextNode(value);
281             newFrag.appendChild(txt);
282             ctx.setXML("/portalTools" + key, newFrag);
283         } catch (ProcessingException e) {
284         }
285     }
286     
287     /* (non-Javadoc)
288      * @see org.apache.avalon.framework.activity.Disposable#dispose()
289      */

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