incubator-lokahi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tob...@apache.org
Subject svn commit: r392982 [16/25] - in /incubator/lokahi/lokahi/trunk: ./ conf/ database/ docs/ lib/ src/ src/java/ src/java/lokahi/ src/java/lokahi/core/ src/java/lokahi/core/agent/ src/java/lokahi/core/agent/callable/ src/java/lokahi/core/agent/callable/co...
Date Mon, 10 Apr 2006 16:20:11 GMT
Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePool.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePool.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePool.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePool.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,422 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.api.pool;
+
+import lokahi.core.api.environment.Environment;
+import lokahi.core.api.instance.Instance;
+import lokahi.core.api.server.Hardware;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.Pool;
+import lokahi.core.common.interfaces.TMCBroker;
+import lokahi.core.common.interfaces.TMCDao;
+import lokahi.core.common.util.PropertiesFile;
+import lokahi.httpd.api.entity.VirtualHost;
+import lokahi.httpd.api.worker.ApacheWorker;
+import org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApachePool.java,v 1.3 2006/03/07 20:18:53 drtobes Exp $
+ */
+public class ApachePool extends TMCDao<ApachePool> implements Pool {
+  static final Logger logger = Logger.getLogger(ApachePool.class);
+  private static final TMCBroker<ApachePool> broker = new BrokerFactory<ApachePool>().getBroker();
+  private int id;
+  private String name;
+  private int envId;
+
+  private Environment env;
+
+  private Collection<ApacheWorker> apaches;
+
+  public int getPk() {
+    return id;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public void setPk(int pk) {
+    this.id = pk;
+  }
+
+  public Environment getEnv() {
+    if (env == null) {
+      try {
+        env = Environment.getEnviroment(this.getEnvId(), true);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return env;
+  }
+
+  public void setEnv(Environment env) {
+    this.env = env;
+  }
+
+  public int getEnvId() {
+    return envId;
+  }
+
+  public void setEnvId(int envId) {
+    this.envId = envId;
+  }
+
+  private Collection<ApacheWorker> getApachesNoDB() {
+    return apaches;
+  }
+
+  public Collection<ApacheWorker> getApaches() {
+    if (apaches == null) {
+      try {
+        this.apaches = ApacheWorker.getApacheWorkers(this.getPk(), false);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return apaches;
+  }
+
+  public Collection<ApacheWorker> getApaches(Instance i) {
+    TMCSet<ApacheWorker> ret = new TMCSet<ApacheWorker>();
+    if (i != null) {
+      if (apaches == null) {
+        try {
+          this.apaches = ApacheWorker.getApacheWorkers(this.getPk(), false);
+        } catch (SQLException e) {
+          if (logger.isInfoEnabled()) {
+            logger.info("Exception: " + e.getMessage());
+          }
+        }
+      }
+      for (ApacheWorker aw : apaches) {
+        if (aw.getHardware() != null && i.equals(aw.getHardware().getInstance())) {
+          ret.add(aw);
+        }
+      }
+    }
+    return ret;
+  }
+
+  public Collection<Hardware> getHardwares() {
+    Collection<Hardware> c = null;
+    Collection<ApacheWorker> aws = this.getApaches();
+    if (aws != null && !aws.isEmpty()) {
+      c = new TMCSet<Hardware>();
+      for (final ApacheWorker aw : aws) {
+        if (!c.contains(aw.getHardware())) {
+          c.add(aw.getHardware());
+        }
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + c);
+    }
+    return c;
+  }
+
+  public void setApaches(Collection<ApacheWorker> apaches) {
+    this.apaches = apaches;
+  }
+
+  public ApachePool() {
+  }
+
+  public ApachePool(Environment env, String name) {
+    this.setEnv(env);
+    this.name = name;
+  }
+
+  public ApachePool(int id, String name, int envId) {
+    this.setPk(id);
+    this.setName(name);
+    this.setEnvId(envId);
+  }
+
+  public static ApachePool store(ApachePool ap) throws SQLException {
+    ap.setPk(broker.save("apache.pool.store", ap.getName(), ap.getEnvId()));
+    Collection<ApacheWorker> c = ap.getApachesNoDB();
+    ap.setApaches(null);
+    if (c != null) {
+      ap.addWorkers(c);
+    }
+    return ap;
+  }
+
+  public static boolean update(ApachePool ap) throws SQLException {
+    boolean ret = false;
+    broker.update("apache.pool.update", ap.getPk(), ap.getName(), ap.getEnvId());
+    Collection<ApacheWorker> newWorkers = ap.getApachesNoDB();
+    ap.setApaches(null);
+    Collection<ApacheWorker> oldWorkers = ap.getApaches();
+    Collection<ApacheWorker> keepers = oldWorkers;
+    keepers.retainAll(newWorkers);
+    newWorkers.removeAll(keepers);
+    oldWorkers.removeAll(keepers);
+    if (ap.addWorkers(newWorkers) && ap.removeWorkers(oldWorkers)) {
+      ret = true;
+    }
+    return ret;
+  }
+
+  public static boolean delete(ApachePool ap) throws SQLException {
+    return broker.delete("apache.pool.delete", ap.getPk());
+  }
+
+  public ApachePool get(int id) throws SQLException {
+    ApachePool ap;
+    try {
+      ap = broker.getObject(ApachePool.class, "apache.pool.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input id=" + id + " Returning " + ap);
+    }
+    return ap;
+  }
+
+  public static ApachePool getApachePool(int id) throws SQLException {
+    return new ApachePool().get(id);
+  }
+
+  public static Collection<ApachePool> getApachePools(String name) throws SQLException {
+    Collection<ApachePool> ret;
+    try {
+      ret = broker.getObjects(ApachePool.class, false, "apache.pool.by.name", name);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return ret;
+  }
+
+  public static ApachePool getApachePool(VirtualHost vh) throws SQLException {  //this should only return one...
+    ApachePool ap;
+    try {
+      ap = broker.getObject(ApachePool.class,
+                            "apache.pool.by.vhost", false,
+                            vh.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input vh=" + vh + "Out: ap=" + ap);
+    }
+    return ap;
+  }
+
+  public static Collection<ApachePool> getApachePools() throws SQLException {
+    Collection<ApachePool> c;
+    try {
+      c = broker.getObjects(ApachePool.class, false, "apache.pool.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<ApachePool> getApachePools(ApacheWorker aw) throws SQLException {
+    Collection<ApachePool> c;
+    try {
+      c = broker.getObjects(ApachePool.class, false, "apache.pool.by.worker", aw.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public ApachePool fillObject(ResultSet r) throws SQLException {
+    return new ApachePool(r.getInt("POOL_ID"), r.getString("POOL_NAME"), r.getInt("ENV_ID"));
+  }
+
+  public boolean addWorker(ApacheWorker aw) throws SQLException {
+    boolean ret = false;
+    if (!this.getApaches().contains(aw)) {
+      AMPoolWorker.save(new AMPoolWorker(this, aw));
+      ret = true;
+    }
+    return ret;
+
+  }
+
+  public boolean removeWorker(ApacheWorker aw) throws SQLException {
+    boolean ret = false;
+    if (this.getApaches().contains(aw)) {
+      AMPoolWorker.delete(new AMPoolWorker(this, aw));
+      apaches.remove(aw);
+      ret = true;
+    }
+    return ret;
+  }
+
+  public boolean addWorkers(Collection<ApacheWorker> c) throws SQLException {
+    boolean ret = false;
+    Collection<ApacheWorker> col = this.getApaches();
+    if (logger.isDebugEnabled()) {
+      logger.debug("col=" + col);
+      logger.debug("c=" + c);
+    }
+    if (col == null || col.isEmpty()) {
+      col = new TMCSet<ApacheWorker>();
+    }
+    if (c != null && !c.isEmpty()) {
+      for (final ApacheWorker aw : c) {
+        if (col.contains(aw)) {
+          if (logger.isDebugEnabled()) {
+            logger.debug("THE SP IS NOT BEING CALLED BECAUSE !col.contains(aw)");
+          }
+        } else {
+          this.addWorker(aw);
+        }
+      }
+      if (col.containsAll(c)) {
+        ret = true;
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public Collection<VirtualHost> getAllVirtualhosts() throws SQLException {
+    Collection<VirtualHost> ret = new TMCSet<VirtualHost>();
+    if (this.getApaches() != null) {
+      for (final ApacheWorker aw : apaches) {
+        Collection<VirtualHost> temp = VirtualHost.getVirtualHosts(aw);
+        if (temp != null) {
+          ret.addAll(temp);
+        }
+      }
+    }
+    return ret;
+  }
+
+  public boolean removeWorkers(Collection<ApacheWorker> c) throws SQLException {
+    boolean ret = false;
+    Collection<ApacheWorker> col = this.getApaches();
+    if (col != null && !col.isEmpty()) {
+      if (c != null && !c.isEmpty()) {
+        for (final ApacheWorker aw : c) {
+          this.removeWorker(aw);
+          col.remove(aw);
+        }
+        this.setApaches(col);
+        if (!col.containsAll(c)) {
+          ret = true;
+        }
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public StringBuilder buildShortXMLRepresentation() {
+    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(3, 1);
+    map.put("id", Integer.toString(this.getPk()));
+    map.put("name", this.getName());
+    map.put("href", PropertiesFile.getConstantValue("rest.servlet.url") + "apachepool/" + this.getPk() + '/');
+    return elementBuilder("apachepool", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<apachepool>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(this.getEnv().buildShortXMLRepresentation());
+    for (final ApacheWorker aw : this.getApaches()) {
+      ret.append(aw.buildShortXMLRepresentation());
+    }
+    ret.append("</apachepool>\n");
+    return ret;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("ApachePool");
+    buf.append("{id=").append(id);
+    buf.append(",name=").append(name);
+    buf.append(",envId=").append(envId);
+    buf.append(",env=").append(env);
+    buf.append(",apaches=").append(apaches);
+    buf.append('}');
+    return buf.toString();
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof ApachePool)) return false;
+
+    final ApachePool apachePool = (ApachePool) o;
+
+    if (envId != apachePool.envId) return false;
+    if (id != apachePool.id) return false;
+    if (apaches != null ? !apaches.equals(apachePool.apaches) : apachePool.apaches != null) return false;
+    if (env != null ? !env.equals(apachePool.env) : apachePool.env != null) return false;
+    if (name != null ? !name.equals(apachePool.name) : apachePool.name != null) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = id;
+    result = 29 * result + (name != null ? name.hashCode() : 0);
+    result = 29 * result + envId;
+    result = 29 * result + (env != null ? env.hashCode() : 0);
+    result = 29 * result + (apaches != null ? apaches.hashCode() : 0);
+    return result;
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePoolModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePoolModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePoolModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/pool/ApachePoolModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,257 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.api.pool;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.jobpool.JobPool;
+import lokahi.core.api.project.Project;
+import lokahi.core.api.state.State;
+import lokahi.core.api.user.User;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCIllegalArgumentException;
+import lokahi.core.common.interfaces.LokahiModel;
+import lokahi.httpd.api.worker.ApacheWorker;
+import lokahi.httpd.api.worker.ApacheWorkerModel;
+import org.apache.log4j.Logger;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApachePoolModel.java,v 1.3 2006/03/07 20:18:53 drtobes Exp $
+ */
+public class ApachePoolModel extends LokahiModel<ApachePool> {
+  static final Logger logger = Logger.getLogger(ApachePoolModel.class);
+
+  public void updateApache(User u, Project p, JobPool jobPool, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "UpdateApache", jobPool, ap);
+  }
+
+  public void gracefulApache(User u, Project p, JobPool jobPool, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "GracefulApache", jobPool, ap);
+  }
+
+  public void configtestApache(User u, Project p, JobPool jobPool, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "ConfigtestApache", jobPool, ap);
+  }
+
+  public void restartApache(User u, Project p, JobPool jobPool, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "RestartApache", jp, ap);
+  }
+
+  public void startApache(User u, Project p, JobPool jobPool, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "StartApache", jobPool, ap);
+  }
+
+  public void stopApache(User u, Project p, JobPool jobPool, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "StopApache", jobPool, ap);
+  }
+
+  public void deployApacheConfig(User u, Project p, JobPool jobPool, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "DeployApacheConfig", jobPool, ap);
+  }
+
+  public void updateApache(User u, Project p, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "UpdateApache", null, ap);
+  }
+
+  public void gracefulApache(User u, Project p, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "GracefulApache", null, ap);
+  }
+
+  public void configtestApache(User u, Project p, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "ConfigtestApache", null, ap);
+  }
+
+  public void restartApache(User u, Project p, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "RestartApache", null, ap);
+  }
+
+  public void startApache(User u, Project p, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "StartApache", null, ap);
+  }
+
+  public void stopApache(User u, Project p, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "StopApache", null, ap);
+  }
+
+  public void deployApacheConfig(User u, Project p, ApachePool ap) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, p, "DeployApacheConfig", null, ap);
+  }
+
+  public void gracefulApache(User u, Collection<ApachePool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, c, null, f);
+  }
+
+  public void configtestApache(User u, Collection<ApachePool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, c, null, f);
+  }
+
+  public void restartApache(User u, Collection<ApachePool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, c, null, f);
+  }
+
+  public void startApache(User u, Collection<ApachePool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, c, null, f);
+  }
+
+  public void stopApache(User u, Collection<ApachePool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, c, null, f);
+  }
+
+  public void deployApacheConfig(User u, Collection<ApachePool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.function(u, c, null, f);
+  }
+
+  private void function(User u, Collection<ApachePool> c, JobPool jobPool, Function f) throws SQLException, TMCIllegalArgumentException, IOException, AuthorizationException, NoSuchMethodException {
+    if (!u.isAllowed(f)) throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    if (jobPool == null) {
+      this.jpInit(f, u);
+      jobPool = this.jp;
+    }
+    StringBuffer sb = new StringBuffer();
+    for (final ApachePool ap : c) {
+      this.function(f, jobPool, ap);
+      sb.append(ap.getName());
+      sb.append(',');
+    }
+    this.jp.setOptions(sb.toString());
+    this.jp.setState(State.NEW);
+    JobPool.update(this.jp);
+  }
+
+  private void function(User u, Project p, String command, JobPool jobPool, ApachePool ap) throws SQLException, TMCIllegalArgumentException, IOException, AuthorizationException, NoSuchMethodException {
+    Function f = Function.getFunction(command);
+    if (!u.isAllowed(f, p)) throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    this.function(f, jobPool, ap);
+  }
+
+  private void function(Function f, JobPool jobPool, ApachePool ap) throws SQLException, TMCIllegalArgumentException, IOException, AuthorizationException, NoSuchMethodException {
+    String command = f.getCommand();
+    command = command.substring(0, 1).toLowerCase() + command.substring(1);
+    Method method;
+    try {
+      method = ApacheWorkerModel.class.getMethod(command, f.getClass(), JobPool.class, ApacheWorker.class);
+    } catch (NoSuchMethodException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("NoSuchMethodException: " + e.getMessage());
+      }
+      throw e;
+    }
+    ApacheWorkerModel awm = new ApacheWorkerModel();
+    for (final ApacheWorker aw : ap.getApaches()) {
+      try {
+        method.invoke(awm, f, jobPool, aw);
+      } catch (IllegalAccessException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("IllegalAccessException: " + e.getMessage());
+        }
+      } catch (InvocationTargetException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("InvocationTargetException: " + e.getMessage());
+        }
+        Throwable ex = e.getCause();
+        if (ex != null) {
+          logger.info("TMCMessage: " + ex.getMessage());
+          logger.info("Class: " + ex.getClass());
+          if (ex instanceof TMCIllegalArgumentException) {
+            throw new IllegalArgumentException(ex.getMessage());
+          }
+          if (ex instanceof AuthorizationException) {
+            throw new AuthorizationException(ex.getMessage());
+          }
+          if (ex instanceof SQLException) {
+            throw new SQLException(ex.getMessage());
+          }
+        }
+
+      }
+    }
+  }
+
+  public void addApachePool(User u, ApachePool tp, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u, tp.getName());
+    if (u.isAllowed(f) && "AddApachePool".equals(f.getCommand())) {
+      ApachePool.store(tp);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteApachePool(User u, ApachePool tp, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    if (tp == null || tp.getPk() < 1) throw new TMCIllegalArgumentException("ApachePool can not be null");
+    this.jpInit(f, u, tp.getName());
+    this.deletePool(u, tp, f);
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  private void deletePool(User u, ApachePool tp, Function f) throws AuthorizationException, SQLException {
+    if (u.isAllowed(f) && "DeleteApachePool".equals(f.getCommand())) {
+      ApachePool.delete(tp);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+  }
+
+  public void updateApachePool(User u, ApachePool tp, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u, tp.getName());
+    if (u.isAllowed(f) && "UpdateApachePool".equals(f.getCommand())) {
+      if (tp.getPk() >= 1 && tp.getName() != null && !"".equals(tp.getName())) {
+        ApachePool.update(tp);
+      } else {
+        throw new TMCIllegalArgumentException("ApachePool can not be null");
+      }
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteApachePool(User u, Collection<ApachePool> c, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u);
+    StringBuffer names = new StringBuffer();
+    for (final ApachePool ap : c) {
+      names.append(ap.getName());
+      names.append(',');
+      this.deleteApachePool(u, ap, f);
+    }
+    jp.setOptions(names.toString());
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public Collection<ApachePool> getObjects() throws SQLException {
+    return new TMCSet<ApachePool>(ApachePool.getApachePools());
+  }
+
+  public ApachePool getObject(int id) throws SQLException {
+    return ApachePool.getApachePool(id);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/Apache.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/Apache.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/Apache.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/Apache.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,387 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.api.server;
+
+import lokahi.core.api.file.File;
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.Application;
+import lokahi.core.common.interfaces.TMCBroker;
+import lokahi.core.common.util.PropertiesFile;
+import org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: Apache.java,v 1.4 2006/03/07 22:05:25 drtobes Exp $
+ */
+public class Apache extends Application<Apache> {
+  static final Logger logger = Logger.getLogger(Apache.class);
+  private static final TMCBroker<Apache> broker = new BrokerFactory<Apache>().getBroker();
+  private int pk;
+  private String name;
+  private String serverRoot;
+  private String logLocation;
+  private String sysUser;
+  private String sysGroup;
+  private String startCommand;
+  private String stopCommand;
+  private String restartCommand;
+  private int forkWait;
+  private String envVaribles;
+  private String pidFileLocation;
+  private int mainId;
+
+  private File main;
+
+  public int getPk() {
+    return pk;
+  }
+
+  public void setPk(int pk) {
+    this.pk = pk;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public String getServerRoot() {
+    return serverRoot;
+  }
+
+  public void setServerRoot(String serverRoot) {
+    this.serverRoot = serverRoot;
+  }
+
+  public String getLogLocation() {
+    return logLocation;
+  }
+
+  public void setLogLocation(String logLocation) {
+    this.logLocation = logLocation;
+  }
+
+  public String getSysUser() {
+    return sysUser;
+  }
+
+  public void setSysUser(String sysUser) {
+    this.sysUser = sysUser;
+  }
+
+  public String getSysGroup() {
+    return sysGroup;
+  }
+
+  public void setSysGroup(String sysGroup) {
+    this.sysGroup = sysGroup;
+  }
+
+  public String getStartCommand() {
+    return startCommand;
+  }
+
+  public void setStartCommand(String startCommand) {
+    this.startCommand = startCommand;
+  }
+
+  public String getStopCommand() {
+    return stopCommand;
+  }
+
+  public void setStopCommand(String stopCommand) {
+    this.stopCommand = stopCommand;
+  }
+
+  public String getRestartCommand() {
+    return restartCommand;
+  }
+
+  public void setRestartCommand(String restartCommand) {
+    this.restartCommand = restartCommand;
+  }
+
+  public int getForkWait() {
+    return forkWait;
+  }
+
+  public void setForkWait(int forkWait) {
+    this.forkWait = forkWait;
+  }
+
+  public String getEnvVaribles() {
+    return envVaribles;
+  }
+
+  public void setEnvVaribles(String envVaribles) {
+    this.envVaribles = envVaribles;
+  }
+
+  public String getPidFileLocation() {
+    return pidFileLocation;
+  }
+
+  public void setPidFileLocation(String pidFileLocation) {
+    this.pidFileLocation = pidFileLocation;
+  }
+
+  public int getMainId() {
+    return mainId;
+  }
+
+  public void setMainId(int mainId) {
+    this.mainId = mainId;
+  }
+
+  public File getMain() {
+    if (main == null) {
+      try {
+        main = File.getFile(this.getMainId());
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return main;
+  }
+
+  public void setMain(File main) {
+    this.main = main;
+    if (main != null) {
+      this.setMainId(main.getPk());
+    }
+  }
+
+  public Apache() {
+  }
+
+  public Apache(int container_id, String containerName, String logLocation, String envVaribles, int forkWait,
+                String pidfileLocation, String restartCommand, String startCommand, String stopCommand, String sysGroup,
+                String sysUser, String serverRoot, int mainId) {
+    this.setPk(container_id);
+    this.setName(containerName);
+    this.setLogLocation(logLocation);
+    this.setEnvVaribles(envVaribles);
+    this.setForkWait(forkWait);
+    this.setStartCommand(startCommand);
+    this.setStopCommand(stopCommand);
+    this.setRestartCommand(restartCommand);
+    this.setPidFileLocation(pidfileLocation);
+    this.setSysGroup(sysGroup);
+    this.setSysUser(sysUser);
+    this.setServerRoot(serverRoot);
+    this.setMainId(mainId);
+  }
+
+  public Apache fillObject(ResultSet r) throws SQLException {
+    return new Apache(r.getInt("CONTAINER_ID"),
+                      r.getString("CONTAINER_NAME"),
+                      r.getString("LOG_LOCATION"),
+                      r.getString("ENV_VARIBLES"),
+                      r.getInt("FORK_WAIT"),
+                      r.getString("PIDFILE_LOCATION"),
+                      r.getString("RESTART_COMMAND"),
+                      r.getString("START_COMMAND"),
+                      r.getString("STOP_COMMAND"),
+                      r.getString("SYS_GROUP"),
+                      r.getString("SYS_USER"),
+                      r.getString("SERVER_ROOT"),
+                      r.getInt("MAIN_ID"));
+
+  }
+
+  public static Apache getApache(int id, boolean fromCache) throws SQLException {
+    Apache ap = new Apache();
+    ap.get(id);
+    return ap;
+  }
+
+  public static Collection<Apache> getApaches() throws SQLException {
+    Collection<Apache> c;
+    try {
+      c = broker.getObjects(Apache.class, true, "apache.server.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<Apache> getApaches(String name, boolean fromCache) throws SQLException {
+    Collection<Apache> c;
+    try {
+      c = broker.getObjects(Apache.class, fromCache, "apache.server.by.name", name);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Apache store(Apache ap) throws SQLException {
+    ap.setPk(broker.save("apache.server.store",
+                         ap.getName(),
+                         ap.getSysUser(),
+                         ap.getSysGroup(),
+                         ap.getStartCommand(),
+                         ap.getStopCommand(),
+                         ap.getRestartCommand(),
+                         ap.getForkWait(),
+                         ap.getEnvVaribles(),
+                         ap.getPidFileLocation(),
+                         ap.getServerRoot(),
+                         ap.getLogLocation(),
+                         ap.getMainId()));
+    return ap;
+  }
+
+  public static void update(Apache ap) throws SQLException {
+    broker.update("apache.server.update",
+                  ap.getPk(),
+                  ap.getName(),
+                  ap.getSysUser(),
+                  ap.getSysGroup(),
+                  ap.getStartCommand(),
+                  ap.getStopCommand(),
+                  ap.getRestartCommand(),
+                  ap.getForkWait(),
+                  ap.getEnvVaribles(),
+                  ap.getPidFileLocation(),
+                  ap.getServerRoot(),
+                  ap.getLogLocation(),
+                  ap.getMainId());
+  }
+
+  public static boolean delete(Apache ap) throws SQLException {
+    return broker.delete("apache.server.delete", ap.getPk());
+  }
+
+  public StringBuilder buildShortXMLRepresentation() {
+    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(3, 1);
+    map.put("id", Integer.toString(this.getPk()));
+    map.put("name", this.getName());
+    map.put("href", PropertiesFile.getConstantValue("rest.servlet.url") + "apache/" + this.getPk() + '/');
+    return elementBuilder("apache", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<apache>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(elementBuilder("serverRoot", this.getServerRoot()));
+    ret.append(elementBuilder("logLocation", this.getLogLocation()));
+    ret.append(elementBuilder("sysUser", this.getSysUser()));
+    ret.append(elementBuilder("sysGroup", this.getSysGroup()));
+    ret.append(elementBuilder("startCommand", this.getStartCommand()));
+    ret.append(elementBuilder("stopCommand", this.getStopCommand()));
+    ret.append(elementBuilder("restartCommand", this.getRestartCommand()));
+    ret.append(elementBuilder("forkWait", Integer.toString(this.getForkWait())));
+    ret.append(this.getMain().buildShortXMLRepresentation());
+    ret.append("</apache>\n");
+    return ret;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("Apache");
+    buf.append("{pk=").append(pk);
+    buf.append(",name=").append(name);
+    buf.append(",serverRoot=").append(serverRoot);
+    buf.append(",logLocation=").append(logLocation);
+    buf.append(",sysUser=").append(sysUser);
+    buf.append(",sysGroup=").append(sysGroup);
+    buf.append(",startCommand=").append(startCommand);
+    buf.append(",stopCommand=").append(stopCommand);
+    buf.append(",restartCommand=").append(restartCommand);
+    buf.append(",forkWait=").append(forkWait);
+    buf.append(",envVaribles=").append(envVaribles);
+    buf.append(",pidFileLocation=").append(pidFileLocation);
+    buf.append(",mainId=").append(mainId);
+    buf.append(",main=").append(main);
+    buf.append('}');
+    return buf.toString();
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof Apache)) return false;
+    final Apache apache = (Apache) o;
+    if (forkWait != apache.forkWait) return false;
+    if (mainId != apache.mainId) return false;
+    if (pk != apache.pk) return false;
+    if (envVaribles != null ? !envVaribles.equals(apache.envVaribles) : apache.envVaribles != null) return false;
+    if (!logLocation.equals(apache.logLocation)) return false;
+    if (!name.equals(apache.name)) return false;
+    if (!pidFileLocation.equals(apache.pidFileLocation)) return false;
+    if (!restartCommand.equals(apache.restartCommand)) return false;
+    if (!serverRoot.equals(apache.serverRoot)) return false;
+    if (!startCommand.equals(apache.startCommand)) return false;
+    if (!stopCommand.equals(apache.stopCommand)) return false;
+    if (!sysGroup.equals(apache.sysGroup)) return false;
+    if (!sysUser.equals(apache.sysUser)) return false;
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = pk;
+    result = 29 * result + name.hashCode();
+    result = 29 * result + serverRoot.hashCode();
+    result = 29 * result + logLocation.hashCode();
+    result = 29 * result + sysUser.hashCode();
+    result = 29 * result + sysGroup.hashCode();
+    result = 29 * result + startCommand.hashCode();
+    result = 29 * result + stopCommand.hashCode();
+    result = 29 * result + restartCommand.hashCode();
+    result = 29 * result + forkWait;
+    result = 29 * result + (envVaribles != null ? envVaribles.hashCode() : 0);
+    result = 29 * result + (envVaribles != null ? pidFileLocation.hashCode() : 0);
+    result = 29 * result + mainId;
+    return result;
+  }
+
+  public Apache get(int id) throws SQLException {
+    Apache ap;
+    try {
+      ap = broker.getObject(Apache.class, "apache.server.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return ap;
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/ApacheModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/ApacheModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/ApacheModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/server/ApacheModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,94 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.api.server;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.jobpool.JobPool;
+import lokahi.core.api.state.State;
+import lokahi.core.api.user.User;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCIllegalArgumentException;
+import lokahi.core.common.interfaces.LokahiModel;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApacheModel.java,v 1.3 2006/03/07 20:18:53 drtobes Exp $
+ */
+public class ApacheModel extends LokahiModel<Apache> {
+
+  public void addApache(User u, Apache a, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, a.getName());
+    if (u.isAllowed(f) && "AddApache".equals(f.getCommand())) {
+      Apache.store(a);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteApache(User u, Apache a, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    if (a == null || a.getPk() < 1) throw new TMCIllegalArgumentException("Apache can not be null");
+    this.jpInit(f, u, a.getName());
+    if (u.isAllowed(f) && "DeleteApache".equals(f.getCommand())) {
+      Apache.delete(a);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void updateApache(User u, Apache a, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, a.getName());
+    if (u.isAllowed(f) && "UpdateApache".equals(f.getCommand())) {
+      if (a.getPk() > 1 && a.getName() != null && !"".equals(a.getName())) {
+        Apache.update(a);
+      } else {
+        throw new TMCIllegalArgumentException("Apache can not be null");
+      }
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public Apache viewApache(int id) throws SQLException {
+    return Apache.getApache(id, true);
+  }
+
+  public Collection<Apache> viewApaches() throws SQLException {
+    return Apache.getApaches();
+  }
+
+  public Collection<Apache> getObjects() throws SQLException {
+    return this.viewApaches();
+  }
+
+  public Apache getObject(int id) throws SQLException {
+    return this.viewApache(id);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorker.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorker.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorker.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorker.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,438 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.api.worker;
+
+import lokahi.core.api.instance.Instance;
+import lokahi.core.api.server.Hardware;
+import lokahi.core.api.state.State;
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.Server;
+import lokahi.core.common.interfaces.TMCBroker;
+import lokahi.core.common.interfaces.TMCDao;
+import lokahi.core.common.util.PropertiesFile;
+import lokahi.httpd.api.pool.ApachePool;
+import lokahi.httpd.api.server.Apache;
+import org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApacheWorker.java,v 1.3 2006/03/07 20:18:54 drtobes Exp $
+ */
+public class ApacheWorker extends TMCDao<ApacheWorker> implements Server {
+  static final Logger logger = Logger.getLogger(ApacheWorker.class);
+  private static final TMCBroker<ApacheWorker> broker = new BrokerFactory<ApacheWorker>().getBroker();
+  private int pk;
+  private int apacheId;
+  private int stateId;
+  private int hardwareId;
+  private int poolId;
+
+  private State state;
+  private Hardware hardware;
+  private Apache apache;
+
+  public ApacheWorker() {
+  }
+
+  public ApacheWorker(State state, Hardware hardware, Apache apache, ApachePool aPool) {
+    this.setState(state);
+    this.setHardware(hardware);
+    this.setApache(apache);
+    this.poolId = aPool.getPk();
+  }
+
+  public ApacheWorker(int apacheId, int hardwareId) {
+    this.apacheId = apacheId;
+    this.stateId = 8;
+    this.hardwareId = hardwareId;
+  }
+
+  public ApacheWorker(int pk, int apacheId, int stateId, int hardwareId) {
+    this.pk = pk;
+    this.apacheId = apacheId;
+    this.stateId = stateId;
+    this.hardwareId = hardwareId;
+  }
+
+  public ApacheWorker(int pk, int stateId, Apache apache, Hardware h) {
+    this.pk = pk;
+    this.setApache(apache);
+    this.setHardware(h);
+    this.stateId = stateId;
+  }
+
+  public int getPk() {
+    return pk;
+  }
+
+  public void setPk(int pk) {
+    this.pk = pk;
+  }
+
+  public String getName() {
+    String name = "Invalid Name";
+    if (this.getHardware() != null) {
+      name = this.getHardware().getName();
+    }
+    if (this.getApache() != null) {
+      name = name + ':' + this.getApache().getName();
+    }
+    return name;
+  }
+
+  public void setName(String name) {
+    //NULL!
+  }
+
+  public int getApacheId() {
+    return apacheId;
+  }
+
+  public void setApacheId(int apacheId) {
+    this.apacheId = apacheId;
+  }
+
+  public int getStateId() {
+    return stateId;
+  }
+
+  public void setStateId(int stateId) {
+    this.stateId = stateId;
+  }
+
+  public int getHardwareId() {
+    return hardwareId;
+  }
+
+  public void setHardwareId(int hardwareId) {
+    this.hardwareId = hardwareId;
+  }
+
+  public State getState() {
+    if (state == null) {
+      this.state = State.getState(this.getStateId());
+    }
+    return state;
+  }
+
+  public void setState(State state) {
+    this.state = state;
+    if (state != null) {
+      this.setStateId(state.getPk());
+    }
+  }
+
+  public Hardware getHardware() {
+    if (hardware == null) {
+      try {
+        this.hardware = Hardware.getHardware(this.getHardwareId(), true);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return hardware;
+  }
+
+  public void setHardware(Hardware hardware) {
+    this.hardware = hardware;
+    if (hardware != null) {
+      this.setHardwareId(hardware.getPk());
+    }
+  }
+
+  public Apache getApache() {
+    if (apache == null) {
+      try {
+        this.apache = Apache.getApache(this.getApacheId(), true);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return apache;
+  }
+
+  public void setApache(Apache apache) {
+    this.apache = apache;
+    if (apache != null) {
+      this.setApacheId(apache.getPk());
+    }
+  }
+
+  public int getPoolId() {
+    return poolId;
+  }
+
+  public void setPoolId(int poolId) {
+    this.poolId = poolId;
+  }
+
+  public boolean isActive() {
+    return this.getHardware().isActive();
+  }
+
+  public ApacheWorker get(int id) throws SQLException {
+    ApacheWorker aw;
+    try {
+      aw = broker.getObject(ApacheWorker.class, "apache.worker.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input " + id + " Returning aw " + aw);
+    }
+    return aw;
+  }
+
+  public static ApacheWorker getApacheWorker(int id, boolean cache) throws SQLException {
+    return new ApacheWorker().get(id);
+  }
+
+  public static ApacheWorker getApacheWorker(int apacheId, int hardwareId, boolean cache) throws SQLException {
+    ApacheWorker aw;
+    try {
+      aw = broker.getObject(ApacheWorker.class, "apache.worker.by.server.hardware", cache, apacheId, hardwareId);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input apacheId=" + apacheId + ", hardwareId=" + hardwareId + " Returning aw " + aw);
+    }
+    return aw;
+  }
+
+  public static Collection<ApacheWorker> getApacheWorkers(boolean cache) throws SQLException {
+    Collection<ApacheWorker> c;
+    try {
+      c = broker.getObjects(ApacheWorker.class, cache, "apache.worker.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<ApacheWorker> getApacheWorkers(int poolId, boolean cache) throws SQLException {
+    Collection<ApacheWorker> c;
+    try {
+      c = broker.getObjects(ApacheWorker.class, cache, "apache.worker.by.pool", poolId);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<ApacheWorker> getApacheWorkers(String name, boolean cache) throws SQLException {
+    Collection<ApacheWorker> c;
+    try {
+      c = broker.getObjects(ApacheWorker.class, cache, "apache.worker.by.name", name);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<ApacheWorker> getApacheWorkers(String name, Instance i, boolean cache) throws SQLException {
+    Collection<ApacheWorker> c;
+    try {
+      c = broker.getObjects(ApacheWorker.class, cache, "apache.worker.by.name.instance", name, i.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<ApacheWorker> getApacheWorkers(Hardware h, boolean cache) throws SQLException {
+    Collection<ApacheWorker> c;
+    try {
+      c = broker.getObjects(ApacheWorker.class, cache, "apache.worker.by.hardware", h.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<ApacheWorker> getApacheWorkers(Apache a, boolean cache) throws SQLException {
+    Collection<ApacheWorker> c;
+    try {
+      c = broker.getObjects(ApacheWorker.class, cache, "apache.worker.by.server", a.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public ApacheWorker fillObject(ResultSet r) throws SQLException {
+    return new ApacheWorker(r.getInt("WORKER_ID"),
+                            r.getInt("STATE_ID"),
+                            new Apache(r.getInt("CONTAINER_ID"),
+                                       r.getString("CONTAINER_NAME"),
+                                       r.getString("LOG_LOCATION"),
+                                       r.getString("ENV_VARIBLES"),
+                                       r.getInt("FORK_WAIT"),
+                                       r.getString("PIDFILE_LOCATION"),
+                                       r.getString("RESTART_COMMAND"),
+                                       r.getString("START_COMMAND"),
+                                       r.getString("STOP_COMMAND"),
+                                       r.getString("SYS_GROUP"),
+                                       r.getString("SYS_USER"),
+                                       r.getString("SERVER_ROOT"),
+                                       r.getInt("MAIN_ID")),
+                            new Hardware(r.getInt("HARDWARE_ID"),
+                                         r.getString("HARDWARE_NAME"),
+                                         r.getString("PHYSICAL_LOCATION"),
+                                         r.getString("COMMENTS"),
+                                         r.getInt("INSTANCE_ID"),
+                                         r.getInt("ENV_ID"),
+                                         r.getInt("hardware_state_id"),
+                                         r.getInt("DEFAULT_IP_ID")));
+  }
+
+  public static ApacheWorker store(ApacheWorker aw) throws SQLException {
+    ApacheWorker ret = getApacheWorker(aw.getApacheId(), aw.getHardwareId(), false);
+    if (ret == null) {
+      aw.setPk(broker.save("apache.worker.store", aw.getApacheId(), aw.getHardwareId(), aw.getStateId()));
+      ret = aw;
+    }
+    return ret;
+  }
+
+  public static void update(ApacheWorker aw) throws SQLException {
+    broker.update("apache.worker.update", aw.getPk(), aw.getApacheId(), aw.getHardwareId(), aw.getStateId());
+  }
+
+  public static boolean delete(ApacheWorker aw) throws SQLException {
+    return broker.delete("sp_am_drop_worker", aw.getPk());
+  }
+
+  public String buildConfForWeb() {
+    String ret = "A Database error has occurred.";
+    ApacheWorkerModel awm = new ApacheWorkerModel();
+    try {
+      ret = awm.buildConf(this);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return ret;
+  }
+
+  public String buildWorkersProperties() throws SQLException {
+    ApacheWorkerModel awm = new ApacheWorkerModel();
+    return awm.buildWorkerProperties(this);
+  }
+
+  public StringBuilder buildShortXMLRepresentation() {
+    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(3, 1);
+    map.put("id", Integer.toString(this.getPk()));
+    map.put("name", this.getName());
+    map.put("href", PropertiesFile.getConstantValue("rest.servlet.url") + "apacheworker/" + this.getPk() + '/');
+    return elementBuilder("apacheworker", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<apacheworker>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(this.getState().buildShortXMLRepresentation());
+    ret.append(this.getApache().buildShortXMLRepresentation());
+    ret.append(this.getHardware().buildShortXMLRepresentation());
+    ret.append("</apacheworker>\n");
+    return ret;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("ApacheWorker");
+    buf.append("{pk=").append(pk);
+    buf.append(",apacheId=").append(apacheId);
+    buf.append(",stateId=").append(stateId);
+    buf.append(",hardwareId=").append(hardwareId);
+    buf.append(",poolId=").append(poolId);
+    buf.append(",state=").append(state);
+    buf.append(",hardware=").append(hardware);
+    buf.append(",apache=").append(apache);
+    buf.append('}');
+    return buf.toString();
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof ApacheWorker)) return false;
+
+    final ApacheWorker apacheWorker = (ApacheWorker) o;
+
+    if (apacheId != apacheWorker.apacheId) return false;
+    if (hardwareId != apacheWorker.hardwareId) return false;
+    if (pk != apacheWorker.pk) return false;
+    if (poolId != apacheWorker.poolId) return false;
+    if (stateId != apacheWorker.stateId) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = pk;
+    result = 29 * result + apacheId;
+    result = 29 * result + stateId;
+    result = 29 * result + hardwareId;
+    result = 29 * result + poolId;
+    result = 29 * result + (state != null ? state.hashCode() : 0);
+    result = 29 * result + (hardware != null ? hardware.hashCode() : 0);
+    result = 29 * result + (apache != null ? apache.hashCode() : 0);
+    return result;
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorkerModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorkerModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorkerModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/api/worker/ApacheWorkerModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,361 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.api.worker;
+
+import lokahi.core.api.file.File;
+import lokahi.core.api.function.Function;
+import lokahi.core.api.ipaddress.Ip;
+import lokahi.core.api.job.Job;
+import lokahi.core.api.jobpool.JobPool;
+import lokahi.core.api.project.Project;
+import lokahi.core.api.server.Hardware;
+import lokahi.core.api.state.State;
+import lokahi.core.api.user.User;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCIllegalArgumentException;
+import lokahi.core.common.interfaces.LokahiModel;
+import lokahi.core.controller.task.BuildDeployApacheConfig;
+import lokahi.httpd.api.entity.VirtualHost;
+import lokahi.modjk.WorkersPropertiesFile;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApacheWorkerModel.java,v 1.3 2006/03/07 20:18:54 drtobes Exp $
+ */
+public class ApacheWorkerModel extends LokahiModel<ApacheWorker> {
+
+//  public void updateApache(User u, Project p, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+//    String options;
+//    Apache ap = aw.getApache();
+//    if (ap != null) {
+//      String delim = PropertiesFile.getConstantValue("agent.delim.options");
+//      options = ap.getName() + "ap" + delim +
+//              ap.getSysUser() + delim +
+//              ap.getSysGroup() + delim +
+//              ap.getStartCommand() + delim +
+//              ap.getStopCommand() + delim +
+//              ap.getRestartCommand() + delim +
+//              ap.getForkWait() + delim +
+//              1 + delim +
+//              ap.getEnvVaribles();
+//      this.function(jobPool, options, p, u, "UpdateApache", aw);
+//    }
+//  }
+
+  public void gracefulApache(User u, Project p, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.gracefulApache(u, p, null, aw);
+  }
+
+  public void gracefulApache(Function f, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException {
+    String options = aw.getApache().getServerRoot();
+    this.function(f, options, jobPool, aw);
+  }
+
+  public void gracefulApache(User u, Project p, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    String options = aw.getApache().getServerRoot();
+    this.function(jobPool, options, p, u, "GracefulApache", aw);
+  }
+
+  public void gracefulApache(User u, Collection<ApacheWorker> c, Function f) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.functions(u, f, c);
+  }
+
+  public void configtestApache(User u, Project p, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    String options = aw.getApache().getServerRoot();
+    this.function(jobPool, options, p, u, "ConfigtestApache", aw);
+  }
+
+  public void configtestApache(User u, Project p, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.configtestApache(u, p, null, aw);
+  }
+
+  public void configtestApache(User u, Collection<ApacheWorker> c, Function f) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.functions(u, f, c);
+  }
+
+  public void configtestApache(Function f, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException {
+    String options = aw.getApache().getServerRoot();
+    this.function(f, options, jobPool, aw);
+  }
+
+  public void restartApache(User u, Project p, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    String options = aw.getApache().getServerRoot();
+    this.function(jobPool, options, p, u, "RestartApache", aw);
+  }
+
+  public void restartApache(User u, Project p, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.restartApache(u, p, null, aw);
+  }
+
+  public void restartApache(User u, Collection<ApacheWorker> c, Function f) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.functions(u, f, c);
+  }
+
+  public void restartApache(Function f, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException {
+    String options = aw.getApache().getServerRoot();
+    this.function(f, options, jobPool, aw);
+  }
+
+  public void startApache(User u, Project p, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.startApache(u, p, null, aw);
+  }
+
+  public void startApache(User u, Project p, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    String options = aw.getApache().getServerRoot();
+    this.function(jobPool, options, p, u, "StartApache", aw);
+  }
+
+  public void startApache(User u, Collection<ApacheWorker> c, Function f) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.functions(u, f, c);
+  }
+
+  public void startApache(Function f, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException {
+    String options = aw.getApache().getServerRoot();
+    this.function(f, options, jobPool, aw);
+  }
+
+  public void stopApache(User u, Project p, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.stopApache(u, p, null, aw);
+  }
+
+  public void stopApache(User u, Project p, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    String options = aw.getApache().getServerRoot();
+    this.function(jobPool, options, p, u, "StopApache", aw);
+  }
+
+  public void stopApache(User u, Collection<ApacheWorker> c, Function f) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.functions(u, f, c);
+  }
+
+  public void stopApache(Function f, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException {
+    String options = aw.getApache().getServerRoot();
+    this.function(f, options, jobPool, aw);
+  }
+
+  public void deployApacheConfig(User u, Project p, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.deployApacheConfig(u, p, null, aw);
+  }
+
+  public void deployApacheConfig(User u, Project p, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    String options = aw.getApache().getServerRoot();
+    this.function(jobPool, options, p, u, "DeployApacheConfig", aw);
+  }
+
+  public void deployApacheConfig(User u, Collection<ApacheWorker> c, Function f) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    JobPool jobPool = new JobPool(u, State.NEW, f);
+    JobPool.store(jobPool);
+    for (final ApacheWorker aw : c) {
+      this.deployApacheConfig(u, null, jobPool, aw, f);
+    }
+  }
+
+  public void deployApacheConfig(Function f, JobPool jobPool, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.deployApacheConfig(jobPool, aw);
+  }
+
+  public void deployApacheConfig(User u, Project p, JobPool jobPool, ApacheWorker aw, Function f) throws SQLException, AuthorizationException, IOException, TMCIllegalArgumentException {
+    if (!u.isAllowed(f, p)) throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    if (jobPool == null) {
+      jobPool = new JobPool(p, u, State.NEW, aw.getName(), f);
+      jobPool = JobPool.store(jobPool);
+    }
+    this.deployApacheConfig(jobPool, aw);
+  }
+
+  private void deployApacheConfig(JobPool jobPool, ApacheWorker aw) throws TMCIllegalArgumentException {
+    if (jobPool != null && aw.getApache() != null) {
+      Runnable task = new BuildDeployApacheConfig(aw, jobPool, false);
+      tpe.execute(task);
+    } else {
+      throw new TMCIllegalArgumentException("Error with input");
+    }
+  }
+
+  private void functions(User u, Function f, Collection<ApacheWorker> c) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    JobPool jobPool = new JobPool(u, State.NEW, f);
+    JobPool.store(jobPool);
+    for (final ApacheWorker aw : c) {
+      String options = aw.getApache().getServerRoot();
+      this.function(jobPool, options, null, u, f.getCommand(), aw);
+    }
+  }
+
+  private void function(JobPool jobPool, String options, Project p, User u, String functionCommand, ApacheWorker aw) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    Function f = Function.getFunction(functionCommand);
+    this.function(u, p, f, options, jobPool, aw);
+  }
+
+  private void function(User u, Project p, Function f, String options, JobPool jobPool, ApacheWorker aw) throws SQLException, TMCIllegalArgumentException, IOException, AuthorizationException {
+    if (!u.isAllowed(f, p)) throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    if (jobPool == null) {
+      jobPool = new JobPool(p, u, State.NEW, options, f);
+      jobPool = JobPool.store(jobPool);
+    }
+    this.function(f, options, jobPool, aw);
+  }
+
+  private void function(Function f, String options, JobPool jobPool, ApacheWorker aw) throws SQLException, TMCIllegalArgumentException, IOException {
+    if (jobPool != null && options != null && !"".equals(options) && f != null) {
+      Job j = new Job(-1, State.NEW, options, aw.getHardware(), f, jobPool);
+      Job.store(j);
+    } else {
+      throw new TMCIllegalArgumentException("Error with input");
+    }
+  }
+
+  public String buildWorkerProperties(ApacheWorker aw) throws SQLException {
+    WorkersPropertiesFile wpf = new WorkersPropertiesFile(aw);
+    return wpf.build();
+  }
+
+//  private String buildWorkers(Collection<TomcatWorker> workers) {
+//    StringBuffer poolWorkers = new StringBuffer();
+//    if (workers != null) {
+//      for (final TomcatWorker tw : workers) {
+//        poolWorkers.append(this.buildWorkerEntry(tw));
+//      }
+//    }
+//    return poolWorkers.toString();
+//  }
+
+//  private StringBuffer buildWorkerEntry(TomcatWorker tw) {
+//    StringBuffer poolWorkers = new StringBuffer();
+//         poolWorkers.append("worker.");
+//         poolWorkers.append(tw.getWorkerName());
+//         poolWorkers.append(".port=");
+//         poolWorkers.append(tw.getTomcat().getHttpPort() - 1);
+//         poolWorkers.append("\nworker.");
+//         poolWorkers.append(tw.getWorkerName());
+//         poolWorkers.append(".host=");
+//         poolWorkers.append(tw.getHardware().getName());
+//         poolWorkers.append("\nworker.");
+//         poolWorkers.append(tw.getWorkerName());
+//         poolWorkers.append(".type=ajp13\nworker.");
+//         poolWorkers.append(tw.getWorkerName());
+//         poolWorkers.append(".lbfactor=1\n");
+//     return poolWorkers;
+//  }
+
+  private StringBuffer appendVhosts(Collection<VirtualHost> vhosts, Hardware h) {
+    StringBuffer ret = new StringBuffer();
+    if (vhosts != null && h != null) {
+      for (final VirtualHost vh : vhosts) {
+        if (logger.isDebugEnabled()) {
+          logger.debug("Adding vh=" + vh);
+        }
+        if (vh.getEntry() != null) {
+          ret.append(vh.buildVhostEntry(h));
+        }
+      }
+    }
+    ret.append('\n');
+    return ret;
+  }
+
+  public String buildConf(ApacheWorker aw) throws SQLException {
+    File f = aw.getApache().getMain();
+    StringBuffer content = new StringBuffer();
+    String temp = null;
+    Hardware h = aw.getHardware();
+    if (f != null) {
+      temp = f.getContent();
+    }
+    if (temp != null && h != null) {
+      if (!temp.endsWith("\n")) temp += "\n";
+      content.append(temp);
+      content.append("<VirtualHost ");
+      content.append(h.getDefaultIp().getName());
+      content.append(":80>\n");
+      content.append("ServerName ");
+      content.append(h.getServerName());
+      content.append('\n');
+      content.append("DocumentRoot ").append(aw.getApache().getServerRoot()).append("/htdocs\n");
+      content.append("ScriptAlias /htbin ").append(aw.getApache().getServerRoot()).append("/htbin\n");
+      content.append("</VirtualHost>\n");
+      Collection<VirtualHost> c = VirtualHost.getVirtualHosts(aw);
+      Collection<VirtualHost> catchalls = new TMCSet<VirtualHost>();
+      Collection<Ip> Ips = new TMCSet<Ip>();
+      Collection<Ip> nameBased = new TMCSet<Ip>();
+      VirtualHost defaultCatchAll = null;
+      for (final VirtualHost vh : c) {
+        if (vh.isCatchAll()) {
+          if (vh.isDefault()) {
+            defaultCatchAll = vh;
+          } else {
+            catchalls.add(vh);
+          }
+        }
+        if (!Ips.contains(vh.getIp(h)) && !vh.isDefault()) {
+          Ips.add(vh.getIp(h));
+        }
+        if (vh.isNameBased() && !nameBased.contains(vh.getIp(h)) && !vh.isDefault()) {
+          nameBased.add(vh.getIp(h));
+        }
+      }
+      if (logger.isDebugEnabled()) {
+        logger.debug("defaultCatchAll=" + defaultCatchAll);
+        logger.debug("Ips=" + Ips);
+        logger.debug("nameBased=" + nameBased);
+        logger.debug("c=" + c);
+      }
+      if (!Ips.contains(h.getDefaultIp())) Ips.add(h.getDefaultIp());
+      content.append('\n');
+      content.append(this.appendIps(Ips, "Listen ", ":80\n"));
+      content.append(this.appendIps(nameBased, "NameVirtualHost ", ":80\n"));
+      if (defaultCatchAll != null) {
+        c.remove(defaultCatchAll);
+        if (defaultCatchAll.getEntry() != null && defaultCatchAll.getIp(h) != null)
+          content.append(defaultCatchAll.buildVhostEntry(h));
+      }
+      if (!catchalls.isEmpty()) {
+        c.removeAll(catchalls);
+        content.append(this.appendVhosts(catchalls, h));
+      }
+      content.append(this.appendVhosts(c, h));
+    }
+    return content.toString();
+  }
+
+  private StringBuffer appendIps(Collection<Ip> ips, String preface, String endOfLine) {
+    StringBuffer ret = new StringBuffer();
+    if (ips != null) {
+      for (final Ip ip : ips) {
+        if (ip != null) {
+          ret.append(preface);
+          ret.append(ip.getName());
+          ret.append(endOfLine);
+        }
+      }
+    }
+    ret.append('\n');
+    return ret;
+  }
+
+  public Collection<ApacheWorker> getObjects() throws SQLException {
+    return new TMCSet<ApacheWorker>(ApacheWorker.getApacheWorkers(false));
+  }
+
+  public ApacheWorker getObject(int id) throws SQLException {
+    return ApacheWorker.getApacheWorker(id, false);
+  }
+}
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheAction.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheAction.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheAction.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheAction.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,137 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.gui.www;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.user.User;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCException;
+import lokahi.core.common.exception.TMCIllegalArgumentException;
+import lokahi.core.gui.www.TMCAction;
+import lokahi.httpd.api.server.Apache;
+import lokahi.httpd.api.server.ApacheModel;
+import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionMessage;
+import org.apache.struts.action.ActionMessages;
+import org.apache.struts.validator.DynaValidatorForm;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApacheAction.java,v 1.1 2006/03/07 22:05:25 drtobes Exp $
+ */
+public class ApacheAction extends TMCAction<Apache> {
+  static final Logger logger = Logger.getLogger(ApacheAction.class);
+
+  public ActionMessages executeFunctions(DynaValidatorForm form, Function f, User user) {
+    ActionMessages messages = new ActionMessages();
+    try {
+      ApacheModel am = new ApacheModel();
+      if ("AddApache".equalsIgnoreCase(f.getCommand()) || "UpdateApache".equalsIgnoreCase(f.getCommand())) {
+        Apache a = getTemplate(form);
+        am.perform(f, new Class[]{user.getClass(), a.getClass(), f.getClass()}, new Object[]{user, a, f});
+      } else {
+        Collection<Apache> c = getMultiTemplate(form, Apache.class);
+        am.perform(f, new Class[]{user.getClass(), Collection.class, f.getClass()}, new Object[]{user, c, f});
+      }
+    } catch (AuthorizationException e) {
+      messages = this.addMessage(messages, new ActionMessage("error.notAuthorized"));
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.db"));
+    } catch (TMCIllegalArgumentException e) {
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    } catch (NoSuchMethodException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.invalidFunction", f.getCommand()));
+    } catch (TMCException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+    } catch (IllegalAccessException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+    } catch (InstantiationException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+    }
+    return messages;
+  }
+
+  private Apache getTemplate(DynaValidatorForm dynaForm) throws SQLException {
+    Apache a = new Apache();
+    String Id = null;
+    if (dynaForm.getMap().containsKey("id")) {
+      Id = (String) dynaForm.get("id");
+    }
+    String name = (String) dynaForm.get("name");
+    String base = (String) dynaForm.get("serverRoot");
+    String logLoc = (String) dynaForm.get("logLocation");
+    String sysUser = (String) dynaForm.get("sysUser");
+    String group = (String) dynaForm.get("sysGroup");
+    String start = (String) dynaForm.get("startCommand");
+    String stop = (String) dynaForm.get("stopCommand");
+    String restart = (String) dynaForm.get("restartCommand");
+    String forkWait = (String) dynaForm.get("forkWait");
+    String envVars = (String) dynaForm.get("envVaribles");
+    String pidLoc = (String) dynaForm.get("pidFileLocation");
+    String mainId = (String) dynaForm.get("mainId");
+    if (Id != null && !"".equals(Id)) {
+      a.setPk(Integer.parseInt(Id));
+    }
+    if (mainId != null && !"".equals(mainId)) {
+      a.setMainId(Integer.parseInt(mainId));
+    }
+    a.setName(name);
+    a.setServerRoot(base);
+    a.setLogLocation(logLoc);
+    a.setSysUser(sysUser);
+    a.setSysGroup(group);
+    a.setStartCommand(start);
+    a.setStopCommand(stop);
+    a.setRestartCommand(restart);
+    if (forkWait != null && !"".equals(forkWait)) {
+      a.setForkWait(Integer.parseInt(forkWait));
+    }
+    a.setEnvVaribles(envVars);
+    a.setPidFileLocation(pidLoc);
+    return a;
+  }
+
+}
+
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApachePoolAction.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApachePoolAction.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApachePoolAction.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApachePoolAction.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,138 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.gui.www;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.user.User;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCException;
+import lokahi.core.gui.www.TMCAction;
+import lokahi.httpd.api.pool.ApachePool;
+import lokahi.httpd.api.pool.ApachePoolModel;
+import lokahi.httpd.api.worker.ApacheWorker;
+import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionMessage;
+import org.apache.struts.action.ActionMessages;
+import org.apache.struts.validator.DynaValidatorForm;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApachePoolAction.java,v 1.1 2006/03/07 22:05:25 drtobes Exp $
+ */
+public class ApachePoolAction extends TMCAction {
+  static final Logger logger = Logger.getLogger(ApachePoolAction.class);
+
+  public ActionMessages executeFunctions(DynaValidatorForm form, Function f, User user) {
+    ActionMessages messages = new ActionMessages();
+    try {
+      ApachePoolModel tpm = new ApachePoolModel();
+      if ("AddApachePool".equalsIgnoreCase(f.getCommand()) || "UpdateApachePool".equalsIgnoreCase(f.getCommand())) {
+        ApachePool tp = getTemplate(form);
+        tpm.perform(f, new Class[]{user.getClass(), tp.getClass(), f.getClass()}, new Object[]{user, tp, f});
+      } else {
+        Collection<ApachePool> c = getMultiTemplate(form);
+        tpm.perform(f, new Class[]{user.getClass(), Collection.class, f.getClass()}, new Object[]{user, c, f});
+      }
+      messages = this.addMessage(messages, SUCCESS_MESSAGE, new ActionMessage("function.success." + f.getCommand()));
+    } catch (AuthorizationException e) {
+      messages = this.addMessage(messages, new ActionMessage("error.notAuthorized"));
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.db"));
+    } catch (lokahi.core.common.exception.TMCIllegalArgumentException e) {
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    } catch (NoSuchMethodException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.invalidFunction", f.getCommand()));
+    } catch (TMCException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+    }
+    return messages;
+  }
+
+  private Collection<ApachePool> getMultiTemplate(DynaValidatorForm dynaForm) throws SQLException {
+    Collection<ApachePool> c = new TMCSet<ApachePool>();
+    String[] ids = (String[]) dynaForm.get("id");
+    if (ids != null && ids.length > 0) {
+      for (int i = 0; i < ids.length; i++) {
+        if (!"".equals(ids[i])) {
+          ApachePool ap = ApachePool.getApachePool(Integer.parseInt(ids[i]));
+          if (ap != null) {
+            c.add(ap);
+          }
+        }
+      }
+    }
+    return c;
+  }
+
+  private ApachePool getTemplate(DynaValidatorForm dynaForm) throws SQLException {
+    ApachePool ap = new ApachePool();
+    String Id = null;
+    if (dynaForm.getMap().containsKey("poolId")) {
+      Id = (String) dynaForm.get("poolId");
+    }
+    String name = (String) dynaForm.get("poolName");
+    String envId = (String) dynaForm.get("envId");
+    String[] apaches = (String[]) dynaForm.get("apacheId");
+    String[] hardware = (String[]) dynaForm.get("hardwareId");
+    if (Id != null && !"".equals(Id)) {
+      ap.setPk(Integer.parseInt(Id));
+    }
+    if (envId != null && !"".equals(envId)) {
+      ap.setEnvId(Integer.parseInt(envId));
+    }
+    if (name != null && !"".equals(name)) {
+      ap.setName(name);
+    }
+    if (apaches != null && apaches.length > 0) {
+      Collection<ApacheWorker> c = new TMCSet<ApacheWorker>();
+      for (int i = 0; i < apaches.length; i++) {
+        ApacheWorker aw = ApacheWorker.getApacheWorker(Integer.parseInt(apaches[i]), Integer.parseInt(hardware[i]), false);
+        if (aw != null) {
+          c.add(aw);
+        } else {
+          c.add(ApacheWorker.store(new ApacheWorker(Integer.parseInt(apaches[i]), Integer.parseInt(hardware[i]))));
+        }
+      }
+      ap.setApaches(c);
+    }
+    return ap;
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheWorkerAction.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheWorkerAction.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheWorkerAction.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/ApacheWorkerAction.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,121 @@
+/*
+* Copyright 2006  The Apache Software Foundation
+*
+* Licensed 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 lokahi.httpd.gui.www;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.user.User;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCException;
+import lokahi.core.gui.www.TMCAction;
+import lokahi.httpd.api.worker.ApacheWorker;
+import lokahi.httpd.api.worker.ApacheWorkerModel;
+import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionMessage;
+import org.apache.struts.action.ActionMessages;
+import org.apache.struts.validator.DynaValidatorForm;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: ApacheWorkerAction.java,v 1.1 2006/03/07 22:05:25 drtobes Exp $
+ */
+public class ApacheWorkerAction extends TMCAction {
+  static final Logger logger = Logger.getLogger(ApacheWorkerAction.class);
+
+  public ActionMessages executeFunctions(DynaValidatorForm form, Function f, User user) {
+    ActionMessages messages = new ActionMessages();
+    try {
+      Collection<ApacheWorker> c = getMultiTemplate(form);
+      ApacheWorkerModel awm = new ApacheWorkerModel();
+      awm.perform(f, new Class[]{user.getClass(), Collection.class, f.getClass()}, new Object[]{user, c, f});
+      messages = this.addMessage(messages, SUCCESS_MESSAGE, new ActionMessage("function.success." + f.getCommand()));
+    } catch (AuthorizationException e) {
+      messages = this.addMessage(messages, new ActionMessage("error.notAuthorized"));
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.db"));
+    } catch (lokahi.core.common.exception.TMCIllegalArgumentException e) {
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    } catch (NoSuchMethodException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.invalidFunction", f.getCommand()));
+    } catch (TMCException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      messages = this.addMessage(messages, new ActionMessage("error.badData"));
+    }
+    return messages;
+  }
+
+  private Collection<ApacheWorker> getMultiTemplate(DynaValidatorForm dynaForm) throws SQLException {
+    Collection<ApacheWorker> c = new TMCSet<ApacheWorker>();
+    String[] ids = (String[]) dynaForm.get("id");
+    if (ids != null && ids.length > 0) {
+      for (int i = 0; i < ids.length; i++) {
+        if (!"".equals(ids[i])) {
+          ApacheWorker aw = ApacheWorker.getApacheWorker(Integer.parseInt(ids[i]), false);
+          if (aw != null) {
+            c.add(aw);
+          }
+        }
+      }
+    }
+    return c;
+  }
+
+  private ApacheWorker getTemplate(DynaValidatorForm dynaForm) {
+    ApacheWorker aw = new ApacheWorker();
+    String Id = null;
+    if (dynaForm.getMap().containsKey("id")) {
+      Id = (String) dynaForm.get("id");
+    }
+    String apacheId = (String) dynaForm.get("apacheId");
+    String stateId = (String) dynaForm.get("stateId");
+    String hardwareId = (String) dynaForm.get("hardwareId");
+    if (Id != null && !"".equals(Id)) {
+      aw.setPk(Integer.parseInt(Id));
+    }
+    if (hardwareId != null && !"".equals(hardwareId)) {
+      aw.setHardwareId(Integer.parseInt(hardwareId));
+    }
+    if (stateId != null && !"".equals(stateId)) {
+      aw.setStateId(Integer.parseInt(stateId));
+    }
+    if (apacheId != null && !"".equals(apacheId)) {
+      aw.setApacheId(Integer.parseInt(apacheId));
+    }
+    return aw;
+  }
+}
+
+
+
+
+



Mime
View raw message