From do-not-reply at jboss.org Tue Jan 26 10:01:36 2010 Content-Type: multipart/mixed; boundary="===============8912114026365941223==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: exo-jcr-commits at lists.jboss.org Subject: [exo-jcr-commits] exo-jcr SVN: r1580 - in jcr/trunk/exo.jcr.component.core/src: test/java/org/exoplatform/services/jcr and 1 other directories. Date: Tue, 26 Jan 2010 10:01:36 -0500 Message-ID: <201001261501.o0QF1aI9009326@svn01.web.mwc.hst.phx2.redhat.com> --===============8912114026365941223== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: nzamosenchuk Date: 2010-01-26 10:01:35 -0500 (Tue, 26 Jan 2010) New Revision: 1580 Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/= jcr/config/TemplateConfigurationHelper.java jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/= jcr/config/ jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services/= jcr/config/TestTemplateConfigurationHelper.java Log: EXOJCR-439: Helper for building configuration files from templates and it's= test added. Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/servi= ces/jcr/config/TemplateConfigurationHelper.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services= /jcr/config/TemplateConfigurationHelper.java (rev 0) +++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services= /jcr/config/TemplateConfigurationHelper.java 2010-01-26 15:01:35 UTC (rev 1= 580) @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2010 eXo Platform SAS. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.exoplatform.services.jcr.config; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Pattern; + +/** + * Builds configuration from template using map of template-variables <-->= value. + * Class provides extra functionality for filtering parameters by pattern,= excluding = + * unnecessary parameters. = + * = + * @author Nikolay Zamosenchuk + * @version $Id: TemplateConfigurationHelper.java 34360 2009-07-22 23:58:5= 9Z nzamosenchuk $ + * + */ +public class TemplateConfigurationHelper +{ + // list with include-patterns + private List includes =3D new ArrayList(); + + // list with exclude-patterns + private List excludes =3D new ArrayList(); + + /** + * Creates instance of template configuration helper with given lists o= f filtering = + * patterns. Parameter will be included only if it matches any include-= pattern and + * doesn't match any exclude-pattern. I.e. You can include "extended-*"= and exclude + * "extended-type". Please refer to Java regexp documentation. Filterin= g for this + * example, should be defined as following: + * include: "^extended-.*" + * exclude: "^extended-type" + * = + * @param includes Array with string representation of include reg-exp = patterns + * @param excludes Array with string representation of exclude reg-exp = patterns + */ + public TemplateConfigurationHelper(String[] includes, String[] excludes) + { + super(); + // compile include patterns + for (String regex : includes) + { + this.includes.add(Pattern.compile(regex)); + } + // compile exclude patterns + for (String regex : excludes) + { + this.excludes.add(Pattern.compile(regex)); + } + } + + /** + * Creates instance of TemplateConfigurationHelper pre-configured for J= BossCache parameters,
+ * including: "jbosscache-*" and "jgroups-configuration", and excluding= "jbosscache-configuration" + * = + * @return + */ + public static TemplateConfigurationHelper createJBossCacheHelper() + { + return new TemplateConfigurationHelper(new String[]{"^jbosscache-.*"= , "^jgroups-configuration"}, + new String[]{"^jbosscache-configuration"}); + } + + /** + * Reads configuration file from a stream and replaces all the occurren= ces of template-variables = + * (like : "${parameter.name}") with values provided in the map. = + * = + * @param inputStream + * @param parameters + * @return + * @throws IOException + */ + public InputStream fillTemplate(InputStream inputStream, Map parameters) throws IOException + { + // parameters filtering + Map preparedParams =3D prepareParameters(parameters); + // read stream + String configuration =3D readStream(inputStream); + for (Entry entry : preparedParams.entrySet()) + { + configuration =3D configuration.replace(entry.getKey(), entry.get= Value()); + } + // create new stream = + InputStream configurationStream =3D new ByteArrayInputStream(configu= ration.getBytes()); + return configurationStream; + } + + /** + * Checks if String mathes to any pattern from the list + * = + * @param patterns + * @param parameter + * @return + */ + private boolean matches(List patterns, String parameter) + { + for (Pattern pattern : patterns) + { + if (pattern.matcher(parameter).matches()) + { + // string matched + return true; + } + } + return false; + } + + /** + * Filters the map of parameters, leaving only those than matches filte= ring regular expressions. + * Also adds "${}" to the parameter key:
+ * I.e. such map provided on input: + * = + * "jbosscache-cache.loader":"org.exoplatform" + * "jbosscache-configuration":"/conf/test.xml" + * "max-volatile-size":"100Kb" + * = + * the output will be like: + * = + * "${jbosscache-cache.loader}":"org.exoplatform" + * = + * Other will be ignored (depending on includes/excludes lists provided= in constructor). + * = + * @param parameters + * @return + */ + protected Map prepareParameters(Map par= ameters) + { + Map map =3D new HashMap(); + for (Entry entry : parameters.entrySet()) + { + if (matches(includes, entry.getKey()) && !matches(excludes, entry= .getKey())) + { + map.put("${" + entry.getKey() + "}", entry.getValue()); + } + } + return map; + } + + /** + * Reads bytes from input stream and builds a string from them + * = + * @param inputStream + * @return + * @throws IOException + */ + protected String readStream(InputStream inputStream) throws IOException + { + StringBuffer out =3D new StringBuffer(); + byte[] b =3D new byte[4096]; + for (int n; (n =3D inputStream.read(b)) !=3D -1;) + { + out.append(new String(b, 0, n)); + } + return out.toString(); + } +} Property changes on: jcr/trunk/exo.jcr.component.core/src/main/java/org/exo= platform/services/jcr/config/TemplateConfigurationHelper.java ___________________________________________________________________ Name: svn:mime-type + text/plain Added: jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/servi= ces/jcr/config/TestTemplateConfigurationHelper.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services= /jcr/config/TestTemplateConfigurationHelper.java (r= ev 0) +++ jcr/trunk/exo.jcr.component.core/src/test/java/org/exoplatform/services= /jcr/config/TestTemplateConfigurationHelper.java 2010-01-26 15:01:35 UTC (r= ev 1580) @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2010 eXo Platform SAS. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.exoplatform.services.jcr.config; + +import junit.framework.TestCase; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * @author
Nikolay Zamosenchuk + * @version $Id: TestTemplateHelper.java 34360 2009-07-22 23:58:59Z nzamos= enchuk $ + * + */ +public class TestTemplateConfigurationHelper extends TestCase +{ + + public void testFilters() + { + // create helper with predefined include and exclude patterns + TemplateConfigurationHelper helper =3D TemplateConfigurationHelper.c= reateJBossCacheHelper(); + Map parameters =3D new HashMap(); + parameters.put("jbosscache-configuration", ""); + parameters.put("jbosscache-cache.loader", ""); + parameters.put("jbosscache-clustername", ""); + parameters.put("max-volatile-size", ""); + Map preparedParameters =3D helper.prepareParameters(= parameters); + assertEquals(2, preparedParameters.size()); + // "jbosscache-configuration" and "max-volatile-size" should be excl= uded + assertFalse(preparedParameters.containsKey("${jbosscache-configurati= on}")); + assertFalse(preparedParameters.containsKey("${max-volatile-size}")); + assertTrue(preparedParameters.containsKey("${jbosscache-cache.loader= }")); + assertTrue(preparedParameters.containsKey("${jbosscache-clustername}= ")); + } + + public void testTemplating() throws IOException + { + TemplateConfigurationHelper helper =3D TemplateConfigurationHelper.c= reateJBossCacheHelper(); + String template =3D "configuration in any format, containing ${jboss= cache-template-variable} and many others"; + String expectedConfig =3D "configuration in any format, containing p= retty good parameter and many others"; + + InputStream templateStream =3D new ByteArrayInputStream(template.get= Bytes()); + + Map parameters =3D new HashMap(); + parameters.put("jbosscache-template-variable", "pretty good paramete= r"); + + InputStream configStream =3D helper.fillTemplate(templateStream, par= ameters); + String config =3D readStream(configStream); + assertTrue(expectedConfig.equals(config)); + } + + private String readStream(InputStream inputStream) throws IOException + { + StringBuffer out =3D new StringBuffer(); + byte[] b =3D new byte[4096]; + for (int n; (n =3D inputStream.read(b)) !=3D -1;) + { + out.append(new String(b, 0, n)); + } + return out.toString(); + } +} Property changes on: jcr/trunk/exo.jcr.component.core/src/test/java/org/exo= platform/services/jcr/config/TestTemplateConfigurationHelper.java ___________________________________________________________________ Name: svn:mime-type + text/plain --===============8912114026365941223==--