Index: java/org/apache/lokahi/core/api/pool/HostingPoolModel.java =================================================================== --- java/org/apache/lokahi/core/api/pool/HostingPoolModel.java (revision 632358) +++ java/org/apache/lokahi/core/api/pool/HostingPoolModel.java (working copy) @@ -6,9 +6,9 @@ * 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 @@ -30,6 +30,7 @@ import org.apache.lokahi.core.common.exception.TMCException; import org.apache.lokahi.core.common.exception.TMCIllegalArgumentException; import org.apache.lokahi.core.common.interfaces.LokahiModel; +import org.apache.lokahi.core.common.templating.TemplateDeployer; import org.apache.lokahi.core.controller.task.BuildDeployApacheConfig; import org.apache.lokahi.httpd.api.entity.VirtualHost; import org.apache.lokahi.httpd.api.pool.ApachePool; @@ -147,9 +148,9 @@ } else { j2 = j1; } - Runnable task = new BuildDeployApacheConfig(j2, aw, jobPool, true); + + Runnable task = new TemplateDeployer(j2, aw, jobPool, true); tpe.execute(task); - } public void checkLogAndDeploy(Project p, User u, JobPool jobPool, HostingPool hp, VirtualHost vh) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException { @@ -325,3 +326,4 @@ + Index: java/org/apache/lokahi/core/common/interfaces/TemplateEngine.java =================================================================== --- java/org/apache/lokahi/core/common/interfaces/TemplateEngine.java (revision 632358) +++ java/org/apache/lokahi/core/common/interfaces/TemplateEngine.java (working copy) @@ -6,9 +6,9 @@ * 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 @@ -23,4 +23,5 @@ public interface TemplateEngine { Collection buildConfs(Object param); String buildConf(Object... params); + String buildString(Object... params); } Index: java/org/apache/lokahi/core/common/templating/TemplateBuilder.java =================================================================== --- java/org/apache/lokahi/core/common/templating/TemplateBuilder.java (revision 632358) +++ java/org/apache/lokahi/core/common/templating/TemplateBuilder.java (working copy) @@ -78,4 +78,11 @@ public String buildConf(Object... params) { return masterEngine.buildConf(params); } -} \ No newline at end of file + + /** + * Builds a String a template + */ + public String buildString(Object... params) { + return masterEngine.buildString(params); + } +} Index: java/org/apache/lokahi/core/common/templating/TemplateDeployer.java =================================================================== --- java/org/apache/lokahi/core/common/templating/TemplateDeployer.java (revision 632358) +++ java/org/apache/lokahi/core/common/templating/TemplateDeployer.java (working copy) @@ -1,86 +1,87 @@ -+/* -+* 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.lokahi.core.common.templating; -+ -+import java.io.IOException; -+import java.sql.SQLException; -+ -+import org.apache.log4j.Logger; -+import org.apache.lokahi.core.api.function.Function; -+import org.apache.lokahi.core.api.job.Job; -+import org.apache.lokahi.core.api.jobpool.JobPool; -+import org.apache.lokahi.core.api.template.Template; -+import org.apache.lokahi.core.common.interfaces.TemplateEngine; -+import org.apache.lokahi.core.common.interfaces.Worker; -+ -+/** -+ * TemplateDeployer -+ * Deploys configuration files based on templates -+ * @author The Apache Incubated Lokahi project - http://incubator.apache.org/lokahi/ -+ * @version $Id$ -+ */ -+public class TemplateDeployer implements Runnable { -+ public static final Logger logger = Logger.getLogger(TemplateDeployer.class); -+ protected static final TemplateEngine templateEngine = new TemplateFactory().getEngine(); -+ -+ private final Worker worker; -+ private final JobPool jp; -+ private final boolean graceful; -+ private final Job prereq; -+ -+ public TemplateDeployer(Worker worker, JobPool jobPool, boolean shouldGraceful) { -+ this.worker = worker; -+ this.graceful = shouldGraceful; -+ this.jp = jobPool; -+ this.prereq = null; -+ } -+ -+ public TemplateDeployer(Job j, Worker worker, JobPool jobPool, boolean shouldGraceful) { -+ this.worker = worker; -+ this.graceful = shouldGraceful; -+ this.jp = jobPool; -+ this.prereq = j; -+ } -+ -+ /** -+ * Gets all of the template files from the worker and deploys them -+ * The path and file name are read from the template -+ */ -+ public void run() { -+ try { -+ Function f = Function.getFunction("PutFile"); -+ Job j = new Job(); -+ for (Object temp : this.worker.getApplication().getTemplates()) { -+ Template template = (Template) temp; -+ j = new Job(this.prereq, template.getPath() + template.getDescriptor(), this.worker.getHardware(), f, this.jp); -+ String conf = templateEngine.buildConf(this.worker, template.getPk()); -+ j.setResult(conf); -+ j = Job.store(j); -+ } -+ if (j != null && this.graceful) { -+ j = new Job(j, this.worker.getApplication().getServerLocation(), this.worker.getHardware(), Function.getFunction("GracefulApache"), this.jp); -+ Job.store(j); -+ } -+ } catch (SQLException e) { -+ logger.error("SQLException: " + e.getMessage()); -+ } catch (IOException e) { -+ logger.error("SQLException: " + e.getMessage()); -+ } -+ } -+} \ No newline at end of file +/* +* 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.lokahi.core.common.templating; + +import java.io.IOException; +import java.sql.SQLException; + +import org.apache.log4j.Logger; +import org.apache.lokahi.core.api.function.Function; +import org.apache.lokahi.core.api.job.Job; +import org.apache.lokahi.core.api.jobpool.JobPool; +import org.apache.lokahi.core.api.template.Template; +import org.apache.lokahi.core.common.interfaces.TemplateEngine; +import org.apache.lokahi.core.common.interfaces.Worker; + +/** + * TemplateDeployer + * Deploys configuration files based on templates + * @author The Apache Incubated Lokahi project - http://incubator.apache.org/lokahi/ + * @version $Id$ + */ +public class TemplateDeployer implements Runnable { + public static final Logger logger = Logger.getLogger(TemplateDeployer.class); + protected static final TemplateEngine templateEngine = new TemplateFactory().getEngine(); + + private final Worker worker; + private final JobPool jp; + private final boolean graceful; + private final Job prereq; + + public TemplateDeployer(Worker worker, JobPool jobPool, boolean shouldGraceful) { + this.worker = worker; + this.graceful = shouldGraceful; + this.jp = jobPool; + this.prereq = null; + } + + public TemplateDeployer(Job j, Worker worker, JobPool jobPool, boolean shouldGraceful) { + this.worker = worker; + this.graceful = shouldGraceful; + this.jp = jobPool; + this.prereq = j; + } + + /** + * Gets all of the template files from the worker and deploys them + * The path and file name are read from the template + */ + public void run() { + try { + Function f = Function.getFunction("PutFile"); + Job j = new Job(); + for (Object temp : this.worker.getApplication().getTemplates()) { + Template template = (Template) temp; + String path = templateEngine.buildString(this.worker, template.getPath()); + j = new Job(this.prereq, path + template.getDescriptor(), this.worker.getHardware(), f, this.jp); + String conf = templateEngine.buildConf(this.worker, template.getPk()); + j.setResult(conf); + j = Job.store(j); + } + if (j != null && this.graceful) { + j = new Job(j, this.worker.getApplication().getServerLocation(), this.worker.getHardware(), Function.getFunction("GracefulApache"), this.jp); + Job.store(j); + } + } catch (SQLException e) { + logger.error("SQLException: " + e.getMessage()); + } catch (IOException e) { + logger.error("SQLException: " + e.getMessage()); + } + } +} Index: java/org/apache/lokahi/core/common/templating/TemplateFactory.java =================================================================== --- java/org/apache/lokahi/core/common/templating/TemplateFactory.java (revision 632358) +++ java/org/apache/lokahi/core/common/templating/TemplateFactory.java (working copy) @@ -38,4 +38,4 @@ } -} \ No newline at end of file +} Index: java/org/apache/lokahi/core/common/templating/VelocityTemplateBuilder.java =================================================================== --- java/org/apache/lokahi/core/common/templating/VelocityTemplateBuilder.java (revision 632358) +++ java/org/apache/lokahi/core/common/templating/VelocityTemplateBuilder.java (working copy) @@ -32,6 +32,8 @@ import org.apache.lokahi.tomcat.api.entity.TomcatContext; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; +import org.apache.velocity.runtime.RuntimeConstants; +import org.apache.velocity.tools.generic.MathTool; /** * VelocityTemplateBuilder @@ -55,19 +57,18 @@ for (Template confTemplate : confTemplates) { String confFile = confTemplate.getContent(); + // initialize the logging to use our logger + Velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, + "org.apache.velocity.runtime.log.SimpleLog4JLogSystem" ); + Velocity.setProperty("runtime.log.logsystem.log4j.category", "org.apache.lokahi"); Velocity.init(); VelocityContext context = new VelocityContext(); - context.put("worker", worker); // replace the $worker with the actual worker - context.put("hpTool", new HostingPool()); - context.put("vhTool", new VirtualHost()); - context.put("contextTool", new TomcatContext()); - context.put("view", new TMCVelocityViewTool()); - context.put("n", '\n'); + context = this.addTools(context, (Worker) worker); Velocity.evaluate(context, w, confTemplate.getName(), confFile); confFiles.add(w.toString()); } } catch (Exception e) { - logger.error("Exception building the cofig file: " + e.getMessage()); + logger.error("Exception building the config file: " + e.getMessage()); } return confFiles; } @@ -88,23 +89,65 @@ if (confTemplate.getPk() == id) { String confFile = confTemplate.getContent(); + // initialize the logging to use our logger + Velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, + "org.apache.velocity.runtime.log.SimpleLog4JLogSystem" ); + Velocity.setProperty("runtime.log.logsystem.log4j.category", "org.apache.lokahi"); Velocity.init(); VelocityContext context = new VelocityContext(); - context.put("worker", worker); // replace the $worker with the actual worker - context.put("hpTool", new HostingPool()); - context.put("vhTool", new VirtualHost()); - context.put("contextTool", new TomcatContext()); - context.put("view", new TMCVelocityViewTool()); - context.put("n", '\n'); + context = this.addTools(context, worker); Velocity.evaluate(context, w, confTemplate.getName(), confFile); builtConfFile = w.toString(); break; } } } catch (Exception e) { - logger.error("Exception building the cofig file: " + e.getMessage()); + logger.error("Exception building the config file: " + e.getMessage()); } return builtConfFile; } + /** + * Runs the velocity engine on a String + * @param params - An array of parameters: The worker and the String to convert + * @return A String that has been processed + */ + public String buildString(Object[] params) { + Worker worker = (Worker) params[0]; + String str = (String)params[1]; + String builtConfFile = ""; + StringWriter w = new StringWriter(); + try { + // initialize the logging to use our logger + Velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, + "org.apache.velocity.runtime.log.SimpleLog4JLogSystem" ); + Velocity.setProperty("runtime.log.logsystem.log4j.category", "org.apache.lokahi"); + Velocity.init(); + VelocityContext context = new VelocityContext(); + context = this.addTools(context, worker); + Velocity.evaluate(context, w, worker.getApplication().getName(), str); + builtConfFile = w.toString(); + } catch (Exception e) { + logger.error("Exception building the config file: " + e.getMessage()); + } + return builtConfFile; + } + + /** + * Adds all of the tools that the velocity engine will need + * @param context + * @param worker + * @return A VelocityContext will all of the tools added + */ + private VelocityContext addTools(VelocityContext context, Worker worker) { + context.put("worker", worker); // replace the $worker with the actual worker + context.put("hpTool", new HostingPool()); + context.put("vhTool", new VirtualHost()); + context.put("contextTool", new TomcatContext()); + context.put("view", new TMCVelocityViewTool()); + context.put("math", new MathTool()); + context.put("n", '\n'); + return context; + } + }