001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.tools;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.avalon.framework.activity.Disposable;
029: import org.apache.avalon.framework.component.Component;
030: import org.apache.avalon.framework.configuration.Configuration;
031: import org.apache.avalon.framework.configuration.ConfigurationException;
032: import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
033: import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
034: import org.apache.avalon.framework.parameters.ParameterException;
035: import org.apache.avalon.framework.parameters.Parameterizable;
036: import org.apache.avalon.framework.parameters.Parameters;
037: import org.apache.avalon.framework.service.ServiceException;
038: import org.apache.avalon.framework.service.ServiceManager;
039: import org.apache.avalon.framework.service.Serviceable;
040: import org.apache.avalon.framework.thread.ThreadSafe;
041: import org.apache.cocoon.ProcessingException;
042: import org.apache.cocoon.portal.PortalService;
043: import org.apache.cocoon.portal.tools.helper.PortalObjects;
044: import org.apache.cocoon.portal.tools.service.UserRightsService;
045: import org.apache.cocoon.webapps.session.ContextManager;
046: import org.apache.cocoon.webapps.session.context.SessionContext;
047: import org.apache.excalibur.source.ModifiableSource;
048: import org.apache.excalibur.source.Source;
049: import org.apache.excalibur.source.SourceException;
050: import org.apache.excalibur.source.SourceResolver;
051: import org.apache.excalibur.source.SourceUtil;
052: import org.w3c.dom.DocumentFragment;
053: import org.w3c.dom.Text;
054: import org.xml.sax.SAXException;
055:
056: /**
057: *
058: * @version CVS $Id: PortalToolManager.java 433543 2006-08-22 06:22:54Z crossley $
059: */
060: public class PortalToolManager implements ThreadSafe, Component,
061: Parameterizable, Serviceable, Disposable {
062:
063: public static final String ROLE = PortalToolManager.class.getName();
064:
065: private HashMap tools = new HashMap();
066:
067: private ServiceManager manager;
068:
069: private List i18n = new ArrayList();
070:
071: private String rootDir;
072: private String confFile;
073: private String authFile;
074: private static final String pluginDir = "plugins/";
075: private static final String pluginConfFile = "tool.xml";
076: private static final String i18nDir = "i18n/";
077:
078: private Configuration configuration;
079: private UserRightsService userRightsService;
080:
081: private ContextManager contextManager;
082:
083: /** The source resolver */
084: protected SourceResolver resolver;
085:
086: /* (non-Javadoc)
087: * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
088: */
089: public void parameterize(Parameters para) throws ParameterException {
090: this .rootDir = para.getParameter("root", "/");
091: this .confFile = para.getParameter("conf", "conf.xml");
092: this .authFile = para.getParameter("auth", "auth.xml");
093:
094: Source fSource = null;
095: try {
096: fSource = this .resolver.resolveURI(rootDir + confFile);
097: DefaultConfigurationBuilder confBuilder = new DefaultConfigurationBuilder();
098: this .configuration = confBuilder.build(fSource
099: .getInputStream());
100: fSource = this .resolver.resolveURI(rootDir + authFile);
101: this .userRightsService = new UserRightsService();
102: this .userRightsService.setLocation(fSource);
103: this .userRightsService.initialize();
104: this .init();
105: } catch (ProcessingException e) {
106: e.printStackTrace();
107: } catch (IOException e) {
108: e.printStackTrace();
109: } catch (ConfigurationException e) {
110: e.printStackTrace();
111: } catch (SAXException e) {
112: e.printStackTrace();
113: } finally {
114: this .resolver.release(fSource);
115: }
116: }
117:
118: /**
119: * Initializes the PortalToolManager. Reads the configuration of all plugins etc.
120: * @throws ProcessingException
121: * @throws IOException
122: */
123: public void init() throws ProcessingException, IOException {
124: Source toolsDir = null;
125: PortalToolBuilder builder = new PortalToolBuilder();
126: try {
127: toolsDir = this .resolver.resolveURI(rootDir + pluginDir);
128:
129: final File td = SourceUtil.getFile(toolsDir);
130:
131: if (td == null || !td.isDirectory()) {
132: throw new ProcessingException(
133: "PortalToolManager: tool-dir must be a directory: "
134: + toolsDir.getURI());
135: }
136: final File[] dirs = td.listFiles();
137: for (int i = 0; i < dirs.length; i++) {
138: final File f = dirs[i];
139: if (f.isDirectory()) {
140: String path = f.getAbsolutePath().endsWith(
141: File.separator) ? f.getAbsolutePath() : f
142: .getAbsoluteFile()
143: + File.separator;
144: File conf = new File(path + pluginConfFile);
145: if (conf.exists()) {
146: PortalTool pTool = builder.buildTool(conf,
147: this .rootDir, pluginDir, i18nDir);
148: if (pTool != null) {
149: tools.put(pTool.getId(), pTool);
150: i18n.addAll(pTool.getI18n());
151: }
152: }
153: }
154: }
155: } finally {
156: this .resolver.release(toolsDir);
157: }
158: }
159:
160: /**
161: * Returns a Collection of all Tools
162: */
163: public Collection getTools() {
164: return tools.values();
165: }
166:
167: /**
168: * Returns the tool with the id.
169: * @param id Tool-Id
170: */
171: public PortalTool getTool(String id) {
172: return (PortalTool) tools.get(id);
173: }
174:
175: /**
176: * Returns a Collection of tools which offers functions
177: */
178: public Collection getToolsWithFunctions() {
179: ArrayList tmp = new ArrayList();
180: for (Iterator it = tools.values().iterator(); it.hasNext();) {
181: PortalTool pt;
182: if (((pt = (PortalTool) it.next())).getPublicFunctions()
183: .size() > 0)
184: tmp.add(pt);
185: }
186: return tmp;
187: }
188:
189: public List getI18n() {
190: return i18n;
191: }
192:
193: /* (non-Javadoc)
194: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
195: */
196: public void service(ServiceManager manager) throws ServiceException {
197: this .manager = manager;
198: this .resolver = (SourceResolver) this .manager
199: .lookup(SourceResolver.ROLE);
200: this .contextManager = (ContextManager) this .manager
201: .lookup(ContextManager.ROLE);
202: }
203:
204: /**
205: * Returns the Configuration for the plugins
206: */
207: public Configuration getConfiguration() {
208: return configuration;
209: }
210:
211: /**
212: * Saves the configuration
213: */
214: public synchronized void saveConfiguration() {
215: DefaultConfigurationSerializer confSer = new DefaultConfigurationSerializer();
216: Source confSource = null;
217: try {
218:
219: confSource = this .resolver.resolveURI(rootDir + confFile);
220: if (confSource instanceof ModifiableSource) {
221: confSer.serialize(((ModifiableSource) confSource)
222: .getOutputStream(), configuration);
223: }
224: } catch (MalformedURLException e) {
225: e.printStackTrace();
226: } catch (SourceException e) {
227: e.printStackTrace();
228: } catch (IOException e) {
229: e.printStackTrace();
230: } catch (ConfigurationException e) {
231: e.printStackTrace();
232: } catch (SAXException e) {
233: e.printStackTrace();
234: } finally {
235: this .resolver.release(confSource);
236: }
237: }
238:
239: public UserRightsService getUserRightsService() {
240: return this .userRightsService;
241: }
242:
243: /**
244: * Returns a value from the auth context
245: * @param key Path (e.g. /foo/bar)
246: */
247: public String sGet(String key) {
248: SessionContext ctx;
249: try {
250: ctx = this .contextManager.getContext("authentication");
251: } catch (Exception e) {
252: return null;
253: }
254:
255: if (!key.startsWith("/"))
256: key = "/" + key;
257: DocumentFragment node = null;
258: try {
259: node = ctx.getXML("/portalTools" + key);
260: } catch (Exception e) {
261: // TODO
262: }
263: return org.apache.cocoon.xml.dom.DOMUtil.getValueOfNode(node);
264: }
265:
266: /**
267: * Sets a value in the auth context
268: * @param key Path (e.g. /foo/bar)
269: * @param value Value
270: */
271: public void sSet(String key, String value) {
272: SessionContext ctx;
273: try {
274: ctx = this .contextManager.getContext("authentication");
275: } catch (Exception e) {
276: return;
277: }
278:
279: if (!key.startsWith("/"))
280: key = "/" + key;
281: DocumentFragment frag;
282: try {
283: frag = ctx.getXML("/");
284: org.w3c.dom.Document doc = frag.getOwnerDocument();
285: DocumentFragment newFrag = doc.createDocumentFragment();
286: Text txt = doc.createTextNode(value);
287: newFrag.appendChild(txt);
288: ctx.setXML("/portalTools" + key, newFrag);
289: } catch (ProcessingException e) {
290: }
291: }
292:
293: /* (non-Javadoc)
294: * @see org.apache.avalon.framework.activity.Disposable#dispose()
295: */
296: public void dispose() {
297: if (this .manager != null) {
298: this .manager.release(this .resolver);
299: this .manager.release(this .contextManager);
300: this .resolver = null;
301: this .contextManager = null;
302: this .manager = null;
303: }
304: }
305:
306: public PortalObjects getPortalObjects() {
307: try {
308: return new PortalObjects(
309: (PortalService) this .manager
310: .lookup(org.apache.cocoon.portal.PortalService.ROLE));
311: } catch (ServiceException e) {
312: return null;
313: }
314:
315: }
316:
317: public void releasePortalObjects(PortalObjects pObj) {
318: this.manager.release(pObj.getPortalService());
319: }
320:
321: }
|