dims 01/10/02 12:38:57 Modified: src/org/apache/cocoon/components/classloader RepositoryClassLoader.java src/org/apache/cocoon/servlet CocoonServlet.java src/org/apache/cocoon/util StringUtils.java Log: Add the same functionality in C2.1 too. Revision Changes Path 1.3 +19 -1 xml-cocoon2/src/org/apache/cocoon/components/classloader/RepositoryClassLoader.java Index: RepositoryClassLoader.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/classloader/RepositoryClassLoader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- RepositoryClassLoader.java 2001/08/20 13:55:10 1.2 +++ RepositoryClassLoader.java 2001/10/02 19:38:57 1.3 @@ -27,7 +27,7 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a> * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> - * @version CVS $Revision: 1.2 $ $Date: 2001/08/20 13:55:10 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/10/02 19:38:57 $ */ public class RepositoryClassLoader extends URLClassLoader implements Loggable { @@ -92,6 +92,24 @@ public void addDirectory(File repository) throws IOException { try { this.addURL(repository.getCanonicalFile().toURL()); + } catch (MalformedURLException mue) { + log.error("The repository had a bad URL", mue); + throw new IOException("Could not add repository"); + } + } + + /** + * Add a directory to the list of searchable repositories. + * This methods ensures that no directory is specified more than once. + * + * @param directoryName The path directory + * @exception IOException Non-existent, non-readable or non-directory + * repository + */ + public void addDirectory(String repository) throws IOException { + try { + File file = new File(repository); + this.addURL(file.getCanonicalFile().toURL()); } catch (MalformedURLException mue) { log.error("The repository had a bad URL", mue); throw new IOException("Could not add repository"); 1.39 +66 -10 xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java Index: CocoonServlet.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java,v retrieving revision 1.38 retrieving revision 1.39 diff -u -r1.38 -r1.39 --- CocoonServlet.java 2001/10/01 14:56:35 1.38 +++ CocoonServlet.java 2001/10/02 19:38:57 1.39 @@ -30,6 +30,7 @@ import org.apache.cocoon.util.IOUtils; import org.apache.cocoon.util.log.CocoonLogFormatter; import org.apache.cocoon.util.log.XMLCocoonLogFormatter; +import org.apache.cocoon.util.StringUtils; import org.apache.log.ContextMap; import org.apache.log.Hierarchy; import org.apache.log.LogTarget; @@ -67,7 +68,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a> - * @version CVS $Revision: 1.38 $ $Date: 2001/10/01 14:56:35 $ + * @version CVS $Revision: 1.39 $ $Date: 2001/10/02 19:38:57 $ */ public class CocoonServlet extends HttpServlet { @@ -110,6 +111,7 @@ private String parentComponentManagerClass; protected String forceLoadParameter; + protected String forceSystemProperty; private boolean addClassDirs; /** @@ -140,6 +142,8 @@ log.debug("load-class was not set - defaulting to false?"); } + this.forceSystemProperty = conf.getInitParameter("force-property"); + value = conf.getInitParameter("init-classloader"); this.addClassDirs = "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value); if(value == null) { @@ -315,20 +319,36 @@ (s.charAt(1) == ':')) { log.debug ("extraClassPath is absolute: " + s); sb.append(s); + + if (this.addClassDirs) { + try { + classLoader.addDirectory(s.toString()); + } catch (Exception e) { + log.debug("Could not add " + s.toString()); + } + } } else { if(s.indexOf("${")!=-1) { - int startToken = s.indexOf("${"); - int endToken = s.indexOf("}",startToken); - String token = s.substring(startToken+2,endToken); - StringBuffer value = new StringBuffer(); - value.append(s.substring(0,startToken)); - value.append(System.getProperty(token)); - value.append(s.substring(endToken+1)); - sb.append(value.toString()); - log.debug ("extraClassPath is not absolute replacing using token: [" + token + "] : " + value.toString()); + String path = StringUtils.replaceToken(s); + sb.append(path); + log.debug ("extraClassPath is not absolute replacing using token: [" + s + "] : " + path); + if (this.addClassDirs) { + try { + classLoader.addDirectory(path); + } catch (Exception e) { + log.debug("Could not add " + path); + } + } } else { log.debug ("extraClassPath is not absolute pre-pending context path: " + this.servletContext.getRealPath("/") + s); sb.append(this.servletContext.getRealPath("/") + s); + if (this.addClassDirs) { + try { + classLoader.addDirectory(this.servletContext.getRealPath("/") + s); + } catch (Exception e) { + log.debug("Could not add " + this.servletContext.getRealPath("/") + s); + } + } } } } @@ -463,6 +483,41 @@ } /** + * Handle the "force-property" parameter. + * + * If you need to force more than one property to load, then + * separate each entry with whitespace, a comma, or a semi-colon. + * Cocoon will strip any whitespace from the entry. + * + * @throws ServletException + */ + private void forceProperty() { + if (this.forceSystemProperty != null) { + StringTokenizer tokenizer = new StringTokenizer(forceSystemProperty, " \t\r\n\f;,", false); + + java.util.Properties systemProps = System.getProperties(); + while (tokenizer.hasMoreTokens()) { + final String property = tokenizer.nextToken().trim(); + if(property.indexOf('=')==-1) + continue; + try { + + String key = property.substring(0,property.indexOf('=')); + String value = property.substring(property.indexOf('=')+1); + if(value.indexOf("${")!=-1) + value = StringUtils.replaceToken(value); + log.debug("setting " + key + "=" + value); + systemProps.setProperty(key,value); + } catch (Exception e) { + log.warn("Could not set property: " + property, e); + // Do not throw an exception, because it is not a fatal error. + } + } + System.setProperties(systemProps); + } + } + + /** * Process the specified <code>HttpServletRequest</code> producing output * on the specified <code>HttpServletResponse</code>. */ @@ -722,6 +777,7 @@ this.appContext.put(Constants.CONTEXT_CLASSPATH, this.getClassPath()); this.forceLoad(); + this.forceProperty(); try { URL configFile = (URL) this.appContext.get(Constants.CONTEXT_CONFIG_URL); 1.3 +15 -1 xml-cocoon2/src/org/apache/cocoon/util/StringUtils.java Index: StringUtils.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/util/StringUtils.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- StringUtils.java 2001/08/20 13:55:18 1.2 +++ StringUtils.java 2001/10/02 19:38:57 1.3 @@ -14,7 +14,7 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a> * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> - * @version CVS $Revision: 1.2 $ $Date: 2001/08/20 13:55:18 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/10/02 19:38:57 $ */ public class StringUtils { @@ -92,5 +92,19 @@ if (ca[i] != cb[i]) break; } return i; + } + + /** + * Replaces tokens in input with Value present in System.getProperty + */ + public static String replaceToken(String s) { + int startToken = s.indexOf("${"); + int endToken = s.indexOf("}",startToken); + String token = s.substring(startToken+2,endToken); + StringBuffer value = new StringBuffer(); + value.append(s.substring(0,startToken)); + value.append(System.getProperty(token)); + value.append(s.substring(endToken+1)); + return value.toString(); } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]