Author: sijskes Date: Thu Jan 13 16:12:01 2011 New Revision: 1058642 URL: http://svn.apache.org/viewvc?rev=1058642&view=rev Log: added velocity
Added: incubator/river/jtsk/trunk/src/org/apache/river/config/builder/VelocityConfigurationBuilder.java Added: incubator/river/jtsk/trunk/src/org/apache/river/config/builder/VelocityConfigurationBuilder.java URL: http://svn.apache.org/viewvc/incubator/river/jtsk/trunk/src/org/apache/river/config/builder/VelocityConfigurationBuilder.java?rev=1058642&view=auto ============================================================================== --- incubator/river/jtsk/trunk/src/org/apache/river/config/builder/VelocityConfigurationBuilder.java (added) +++ incubator/river/jtsk/trunk/src/org/apache/river/config/builder/VelocityConfigurationBuilder.java Thu Jan 13 16:12:01 2011 @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.river.config.builder; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Properties; +import net.jini.config.Configuration; +import net.jini.config.ConfigurationException; +import net.jini.config.ConfigurationFile; +import org.apache.river.config.ConfigurationFactory; +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; + +/** + * + * @author sim + */ +public class VelocityConfigurationBuilder + implements ConfigurationFactory +{ + private String serviceHost = null ; + + private int servicePort = 0 ; + + private String registryHost = null ; + + private int registryPort = 0 ; + + private String group = "org.apache.river.demo" ; + + private String codebase = "" ; + + private boolean disableMulticast = false ; + + public VelocityConfigurationBuilder() + { + } + + public int getServicePort() + { + return servicePort; + } + + public void setServicePort(int servicePort) + { + this.servicePort = servicePort; + } + + public String getServiceHost() + { + return serviceHost; + } + + public void setServiceHost(String serviceHost) + { + this.serviceHost = serviceHost; + } + + public String getRegistryHost() + { + return registryHost; + } + + public void setRegistryHost(String registryHost) + { + this.registryHost = registryHost; + } + + public int getRegistryPort() + { + return registryPort; + } + + public void setRegistryPort(int registryPort) + { + this.registryPort = registryPort; + } + + public String getGroup() + { + return group; + } + + public void setGroup(String group) + { + this.group = group; + } + + public String getCodebase() + { + return codebase; + } + + public void setCodebase(String codebase) + { + this.codebase = codebase; + } + + public boolean isDisableMulticast() + { + return disableMulticast; + } + + public void setDisableMulticast(boolean disableMulticast) + { + this.disableMulticast = disableMulticast; + } + + public String getConfigurationText() throws IOException + { + Properties p = new Properties(); + p.load( getClass().getResourceAsStream("velocity.properties") ); + + VelocityEngine ve = new VelocityEngine(p); + + VelocityContext context = new VelocityContext(); + + if( disableMulticast ) { + final String mcstr = "multicastInterfaces = new java.net.NetworkInterface[] { } ;" ; + context.put("multicastInterfaces", mcstr ); + } + + context.put("registryEndpoint", String.format("TcpServerEndpoint.getInstance(%s,%d)",registryHost,registryPort) ); + context.put("serverEndpoint", String.format("TcpServerEndpoint.getInstance(%s,%d)",serviceHost,servicePort) ); + context.put("groups", String.format("\"%s\"",group) ); + + Template template = ve.getTemplate( getClass().getResource("template.vm").toExternalForm() ); + + StringWriter sw = new StringWriter(); + + template.merge(context, sw); + + return sw.toString(); + } + + public class ConfigurationFile2 + extends ConfigurationFile + { + + public ConfigurationFile2(Reader reader, String[] options, ClassLoader cl) throws ConfigurationException + { + super(reader, options, cl); + } + + public ConfigurationFile2(Reader reader, String[] options) throws ConfigurationException + { + super(reader, options); + } + + @Override + protected Object getSpecialEntry(String name) throws ConfigurationException + { + if( "$configuration".equals(name) ) { + return this ; + } + return super.getSpecialEntry(name); + } + + @Override + protected Class getSpecialEntryType(String name) throws ConfigurationException + { + if( "$configuration".equals(name) ) { + return this.getClass(); + } + return super.getSpecialEntryType(name); + } + + } + + @Override + public Configuration createConfiguration() throws ConfigurationException + { + try { + StringReader sr = new StringReader( getConfigurationText() ); + + final ConfigurationFile cf = new ConfigurationFile2(sr,null); + + return cf ; + } catch( ConfigurationException c ) { + throw c ; + } catch( Exception e ) { + throw new ConfigurationException("",e); + } + } + + public void print( File file ) throws IOException + { + FileWriter fw = new FileWriter(file); + print(fw); + fw.close(); + } + + public void print( Writer wr ) throws IOException + { + wr.append( getConfigurationText() ); + } +}