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 [17/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/gui/www/VirtualHostAction.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/VirtualHostAction.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/VirtualHostAction.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/httpd/gui/www/VirtualHostAction.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,125 @@
+/*
+* 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.entity.VirtualHost;
+import lokahi.httpd.api.entity.VirtualHostModel;
+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: VirtualHostAction.java,v 1.1 2006/03/07 22:05:25 drtobes Exp $
+ */
+public class VirtualHostAction extends TMCAction {
+  static final Logger logger = Logger.getLogger(VirtualHostAction.class);
+
+  public ActionMessages executeFunctions(DynaValidatorForm form, Function f, User user) {
+    ActionMessages messages = new ActionMessages();
+    try {
+      VirtualHostModel tm = new VirtualHostModel();
+      if ("AddVirtualHost".equalsIgnoreCase(f.getCommand()) || "UpdateVirtualHost".equalsIgnoreCase(f.getCommand())) {
+        VirtualHost t = getTemplate(form);
+        tm.perform(f, new Class[]{user.getClass(), t.getClass(), f.getClass()}, new Object[]{user, t, f});
+      } else {
+        Collection<VirtualHost> c = getMultiTemplate(form);
+        tm.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 (IllegalArgumentException 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<VirtualHost> getMultiTemplate(DynaValidatorForm dynaForm) throws SQLException {
+    Collection<VirtualHost> c = new TMCSet<VirtualHost>();
+    String[] ids = (String[]) dynaForm.get("id");
+    if (ids != null && ids.length > 0) {
+      for (int i = 0; i < ids.length; i++) {
+        if (!"".equals(ids[i])) {
+          VirtualHost vh = VirtualHost.getVirtualHost(Integer.parseInt(ids[i]));
+          if (vh != null) {
+            c.add(vh);
+          }
+        }
+      }
+    }
+    return c;
+  }
+
+  private VirtualHost getTemplate(DynaValidatorForm dynaForm) {
+    VirtualHost vh = new VirtualHost();
+    String Id = null;
+    if (dynaForm.getMap().containsKey("id")) {
+      Id = (String) dynaForm.get("id");
+    }
+    String name = (String) dynaForm.get("name");
+    String entry = (String) dynaForm.get("entry");
+    String envId = (String) dynaForm.get("envId");
+    String stateId = (String) dynaForm.get("stateId");
+    if (Id != null && !"".equals(Id)) {
+      vh.setPk(Integer.parseInt(Id));
+    }
+    vh.setName(name);
+    if (envId != null && !"".equals(envId)) {
+      vh.setEnvId(Integer.parseInt(envId));
+    }
+    if (stateId != null && !"".equals(stateId)) {
+      vh.setStateId(Integer.parseInt(stateId));
+    }
+    vh.setEntry(entry);
+    return vh;
+  }
+
+}
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/modjk/WorkersPropertiesFile.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/modjk/WorkersPropertiesFile.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/modjk/WorkersPropertiesFile.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/modjk/WorkersPropertiesFile.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,175 @@
+/*
+* 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.modjk;
+
+import lokahi.core.api.instance.Instance;
+import lokahi.core.api.pool.HostingPool;
+import lokahi.core.common.collection.TMCCollectionImpl;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.httpd.api.pool.ApachePool;
+import lokahi.httpd.api.worker.ApacheWorker;
+import lokahi.tomcat.api.pool.TomcatPool;
+import lokahi.tomcat.api.worker.TomcatWorker;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: WorkersPropertiesFile.java,v 1.4 2006/03/07 19:52:06 drtobes Exp $
+ */
+public class WorkersPropertiesFile {
+
+  private ApacheWorker aw;
+  private Collection<TomcatWorker> usedWorkers = new TMCSet<TomcatWorker>();
+  private StringBuffer workerList = new StringBuffer();
+
+  public WorkersPropertiesFile(ApacheWorker aw) {
+    this.aw = aw;
+  }
+
+  public String build() throws SQLException {
+    String ret = null;
+    StringBuffer props = new StringBuffer();
+    props.append("### define your current Tomcat Home\nworkers.tomcat_home=/usr/local/covalent/tomcat4.0.4\n\n### should match current JDK being used\nworkers.java_home=/usr/local/java/IBMjava1.4.0_00\n\n### Set to \"/\" for linux\nps=/\n");
+    StringBuffer workers = new StringBuffer();
+    Instance i = aw.getHardware().getInstance();
+    Collection<HostingPool> c = HostingPool.getHostingPools(aw, true);
+    TMCSet<TomcatPool> tps = new TMCSet<TomcatPool>();
+    if (c != null) {
+      for (final HostingPool hp : c) {
+        TomcatPool tp = hp.getTomcatPool();
+        ApachePool ap = hp.getApachePool();
+        if (ap != null) {
+          int awPosition = determinePosition(ap, i);
+          if (!tps.contains(tp) && tp != null && tp.isActive()) {
+            tps.add(tp);
+            workers.append(buildTomcatPoolWorkersDefinition(tp, awPosition, i));
+          }
+        }
+      }
+      props.append(workerList);
+      props.append('\n');
+      props.append(workers.append('\n'));
+      props.insert(0, "#This file automatically generated by TMC DO NOT MODIFY\n");
+      ret = props.toString();
+    }
+    return ret;
+  }
+
+  private StringBuffer buildTomcatPoolWorkersDefinition(TomcatPool tp, int awPosition, Instance i) {
+    StringBuffer ret = new StringBuffer();
+    for (TomcatWorker tw : tp.getTomcatWorkers()) {
+      if (!usedWorkers.contains(tw) && tw.getHardware() != null && i.equals(tw.getHardware().getInstance()) && tw.isActive())
+      {
+        ret.append(buildWorkerEntry(tw));
+      }
+    }
+    if (tp.getTomcatWorkers(i).size() > 0) {
+      ret.append("worker.");
+      ret.append(tp.getWorkerName());
+      ret.append(".type=lb\nworker.");
+      ret.append(tp.getWorkerName());
+      ret.append(".balanced_workers=");
+      ret.append(this.getWorkerNames(tp, awPosition, i));
+      ret.append("\n\n");
+      if (workerList.length() < 1) {
+        workerList.append("worker.list=");
+        workerList.append(tp.getWorkerName());
+      } else {
+        workerList.append(',');
+        workerList.append(tp.getWorkerName());
+      }
+    }
+    return ret;
+  }
+
+//  private String buildWorkers(Collection<TomcatWorker> workers, Instance i) {
+//    StringBuffer poolWorkers = new StringBuffer();
+//    if (workers != null) {
+//      for (final TomcatWorker tw : workers) {
+//        if (i.equals(tw.getHardware().getInstance()))
+//        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().getServerName());
+    poolWorkers.append("\nworker.");
+    poolWorkers.append(tw.getWorkerName());
+    poolWorkers.append(".type=ajp13\nworker.");
+    poolWorkers.append(tw.getWorkerName());
+    poolWorkers.append(".lbfactor=1\n\n");
+    usedWorkers.add(tw);
+    return poolWorkers;
+  }
+
+  private int determinePosition(ApachePool ap, Instance i) {
+    int ret = 0;
+    TMCCollectionImpl<ApacheWorker> col = new TMCCollectionImpl<ApacheWorker>(ap.getApaches(i));
+    ApacheWorker[] workers = col.orderedList().values().toArray(new ApacheWorker[]{});
+    for (int index = 0; index < workers.length; index++) {
+      if (aw.equals(workers[index])) {
+        ret = index;
+      }
+    }
+    ret++;
+    return ret;
+  }
+
+  public String getWorkerNames(TomcatPool tp, int awPosition, Instance instance) {
+    tp.getTomcatWorkers();
+    StringBuffer front = new StringBuffer();
+    StringBuffer end = new StringBuffer();
+    if (awPosition > tp.getTomcatWorkers(instance).size()) {
+      awPosition %= tp.getTomcatWorkers(instance).size();
+    }
+    awPosition--;
+    if (awPosition < 0) awPosition += tp.getTomcatWorkers(instance).size();
+    TMCCollectionImpl<TomcatWorker> col = new TMCCollectionImpl<TomcatWorker>(tp.getTomcatWorkers(instance));
+    TomcatWorker[] workers = col.orderedList().values().toArray(new TomcatWorker[]{});
+    for (int i = 0; i < workers.length; i++) {
+      if (workers[i].isActive())
+        if (i < awPosition) {
+          end.append(workers[i].getWorkerName());
+          end.append(',');
+        } else {
+          front.append(workers[i].getWorkerName());
+          front.append(',');
+        }
+    }
+    front.append(end);
+    String ret = "";
+    if (front.length() > 1) {
+      ret = front.toString();
+      ret = ret.substring(0, ret.lastIndexOf(','));
+    }
+    return ret;
+  }
+
+}
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContext.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContext.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContext.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContext.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,541 @@
+/*
+* 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.tomcat.api.entity;
+
+import lokahi.core.api.environment.Environment;
+import lokahi.core.api.state.State;
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.FunctionableEntity;
+import lokahi.core.common.interfaces.TMCBroker;
+import lokahi.core.common.util.PropertiesFile;
+import lokahi.httpd.api.entity.VirtualHost;
+import lokahi.tomcat.api.pool.TomcatPool;
+import lokahi.tomcat.api.worker.TomcatWorker;
+import org.apache.log4j.Logger;
+import org.jdom.Attribute;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.jdom.output.Format;
+import org.jdom.output.XMLOutputter;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: TomcatContext.java,v 1.4 2006/03/07 22:05:25 drtobes Exp $
+ */
+public class TomcatContext extends FunctionableEntity<TomcatContext> {
+  static final Logger logger = Logger.getLogger(TomcatContext.class);
+  private static final TMCBroker<TomcatContext> broker = new BrokerFactory<TomcatContext>().getBroker();
+  private static final int objectId = 4;
+  private int contextId;
+  private String name;
+  private String docBase;
+  private String applicationXml;
+  private int stateId;
+  private int envId;
+  private int tomcatPoolId;
+  private int externalProjectId;
+
+  private TomcatPool tomcatPool;
+  private State state;
+  private Environment env;
+
+  public int getObjectId() {
+    return objectId;
+  }
+
+  public int getPk() {
+    return this.contextId;
+  }
+
+  public void setPk(int pk) {
+    this.contextId = pk;
+  }
+
+  public String getName() {
+    if (name != null && name.length() > 0) {
+      if (name.indexOf("/") != 0) {
+        name = '/' + name;
+      }
+      if (name.endsWith("/")) {
+        name = name.substring(0, name.length() - 1);
+      }
+    } else if (name != null && name.length() == 0) {
+      name = "/";
+    }
+    return this.name;
+  }
+
+  public void setName(String name) {
+    name = cleanName(name);
+    this.name = name;
+  }
+
+  public String getDocBase() {
+    return docBase;
+  }
+
+  public void setDocBase(String docBase) {
+    this.docBase = docBase;
+  }
+
+  public int getStateId() {
+    return stateId;
+  }
+
+  public void setStateId(int stateId) {
+    this.stateId = stateId;
+  }
+
+  public int getEnvId() {
+    return envId;
+  }
+
+  public void setEnvId(int envId) {
+    this.envId = envId;
+  }
+
+  public int getExternalProjectId() {
+    return externalProjectId;
+  }
+
+  public void setExternalProjectId(int externalProjectId) {
+    this.externalProjectId = externalProjectId;
+  }
+
+  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 Environment getEnv() {
+    if (env == null) {
+      try {
+        this.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;
+    if (env != null) {
+      this.setEnvId(env.getPk());
+    }
+  }
+
+  public int getTomcatPoolId() {
+    return tomcatPoolId;
+  }
+
+  public void setTomcatPoolId(int tomcatPoolId) {
+    this.tomcatPoolId = tomcatPoolId;
+    if (this.tomcatPool != null && this.tomcatPool.getPk() != tomcatPoolId) {
+      this.tomcatPool = null;
+    }
+  }
+
+  public TomcatPool getTomcatPool() {
+    if (tomcatPool == null) {
+      try {
+        tomcatPool = TomcatPool.getTomcatPool(this.getTomcatPoolId());
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return tomcatPool;
+  }
+
+  public void setTomcatPool(TomcatPool tomcatPool) {
+    this.tomcatPool = tomcatPool;
+    if (logger.isDebugEnabled()) {
+      logger.debug("tomcatPool=" + tomcatPool);
+    }
+    if (tomcatPool != null) {
+      this.setTomcatPoolId(tomcatPool.getPk());
+    }
+  }
+
+  public String getWorkerName() {
+    String ret = "";
+    if (this.getTomcatPool() != null) {
+      ret = this.getTomcatPool().getWorkerName();
+    }
+    return ret;
+  }
+
+  public String getApplicationXml() {
+    if (applicationXml == null || "".equals(applicationXml)) {
+      applicationXml = this.buildApplicationXML();
+    }
+    return applicationXml;
+  }
+
+  public void setApplicationXml(String applicationXml) {
+    this.applicationXml = applicationXml;
+  }
+
+  public TomcatContext() {
+  }
+
+  public TomcatContext(int contextId, String name, String docBase, int stateId, int envId, String xml, int exPid, TomcatPool tp) {
+    this.setPk(contextId);
+    this.setName(name);
+    this.setDocBase(docBase);
+    this.setStateId(stateId);
+    this.setEnvId(envId);
+    this.setExternalProjectId(exPid);
+    this.setTomcatPool(tp);
+    this.setApplicationXml(xml);
+  }
+
+  public TomcatContext(String docBase, String name, int stateId, int envId, int tomcatPoolId) {
+    this.setName(name);
+    this.setDocBase(docBase);
+    this.setStateId(stateId);
+    this.setEnvId(envId);
+    this.setTomcatPoolId(tomcatPoolId);
+  }
+
+  public TomcatContext(String name, String docBase, State state, Environment env, TomcatPool tp) {
+    this.setName(name);
+    this.setDocBase(docBase);
+    this.setState(state);
+    this.setEnv(env);
+    this.setTomcatPool(tp);
+  }
+
+  public String getContextName() {
+    Element e = this.buildServerXML();
+    Attribute a = e.getAttribute("path");
+    return a.getValue();
+  }
+
+  public Element buildServerXML() {
+    Element e = null;
+    try {
+      if (!"".equals(this.getApplicationXml()) && this.getApplicationXml() != null) {
+        SAXBuilder builder = new SAXBuilder();
+        Document doc = builder.build(new StringReader(this.getApplicationXml()));
+        e = doc.getRootElement();
+        doc.removeContent(e);
+      }
+    } catch (JDOMException e1) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e1.getMessage());
+      }
+    } catch (IOException e1) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e1.getMessage());
+      }
+    }
+    if (e == null) {
+      e = new Element("Context");
+      e.setAttribute("path", this.getName());
+      e.setAttribute("docBase", this.getDocBase());
+      e.setAttribute("debug", "0");
+      e.setAttribute("privileged", "false");
+    }
+    return e;
+  }
+
+  public String buildApplicationXML() {
+    return this.buildApplicationXML(this.getName(), this.getDocBase());
+  }
+
+  public String buildApplicationXML(String contextName, String dBase) {
+    Element e = new Element("Context");
+    e.setAttribute("path", contextName);
+    e.setAttribute("docBase", dBase);
+    e.setAttribute("debug", "0");
+    e.setAttribute("privileged", "false");
+    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
+    return out.outputString(e);
+  }
+
+  public TomcatContext get(int id) throws SQLException {
+    return getTomcatContext(id);
+  }
+
+  public FunctionableEntity getParent() {
+    return null;
+  }
+
+  public static Collection<TomcatContext> getTomcatContexts(int id) throws SQLException {
+    Collection<TomcatContext> c;
+    try {
+      c = broker.getObjects(TomcatContext.class,
+                            false, "tomcat.entity.by.projectId", id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<TomcatContext> getTomcatContexts() throws SQLException {
+    Collection<TomcatContext> c;
+    try {
+      c = broker.getObjects(TomcatContext.class, false, "tomcat.entity.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<TomcatContext> getTomcatContexts(String name) throws SQLException {
+    Collection<TomcatContext> c;
+    try {
+      c = broker.getObjects(TomcatContext.class, false, "tomcat.entity.by.name", name);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<TomcatContext> getTomcatContexts(VirtualHost vh) throws SQLException {
+    Collection<TomcatContext> c;
+    try {
+      c = broker.getObjects(TomcatContext.class,
+                            false, "tomcat.entity.by.vhost",
+                            vh.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input vh=" + vh + " Returning " + c);
+    }
+    return c;
+  }
+
+  public static Collection<TomcatContext> getTomcatContexts(TomcatPool tp) throws SQLException {
+    Collection<TomcatContext> c;
+    try {
+      c = broker.getObjects(TomcatContext.class,
+                            false, "tomcat.entity.by.pool",
+                            tp.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static TomcatContext getTomcatContext(int tpid, String name) throws SQLException {
+    TomcatContext ret = null;
+    Collection<TomcatContext> c;
+    try {
+      c = broker.getObjects(TomcatContext.class,
+                            false, "tomcat.entity.by.pool",
+                            tpid);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    for (final TomcatContext tc : c) {
+      if (tc.getName().toLowerCase().indexOf(name.toLowerCase()) > -1) {
+        ret = tc;
+      }
+    }
+    return ret;
+  }
+
+  public static Collection<TomcatContext> getTomcatContexts(TomcatWorker tw) throws SQLException {
+    Collection<TomcatContext> c;
+    try {
+      c = broker.getObjects(TomcatContext.class,
+                            false, "tomcat.entity.by.worker",
+                            tw.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static TomcatContext getTomcatContext(TomcatContext tc) throws SQLException {
+    return getTomcatContext(tc.getPk());
+  }
+
+  public static TomcatContext getTomcatContext(int id) throws SQLException {
+    TomcatContext tc;
+    try {
+      tc = broker.getObject(TomcatContext.class, "tomcat.entity.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return tc;
+  }
+
+  private static String cleanName(String name) {
+    String ret = "";
+    if (name != null) {
+      name = name.trim();
+      if (name.startsWith("/")) {
+        name = name.substring(1);
+      }
+      if (name.endsWith("/")) {
+        name = name.substring(0, name.length());
+      }
+      ret = name;
+    }
+    return ret;
+  }
+
+  public TomcatContext fillObject(ResultSet r) throws SQLException {
+    return new TomcatContext(r.getInt("ENTITY_ID"),
+                             r.getString("ENTITY_NAME"),
+                             r.getString("DOC_BASE"),
+                             r.getInt("ENTITY_STATE_ID"),
+                             r.getInt("ENV_ID"),
+                             r.getString("entity_defin"),
+                             r.getInt("ex_project_id"),
+                             new TomcatPool(r.getInt("TOMCAT_POOL_ID"),
+                                            r.getString("POOL_NAME"),
+                                            r.getInt("POOL_ENV_ID")));
+  }
+
+  public static void update(TomcatContext tc) throws SQLException {
+    broker.update("tomcat.entity.update", tc.getPk(), tc.getName(), tc.getDocBase(), tc.getEnvId(), tc.getStateId(), tc.getTomcatPoolId(), tc.getApplicationXml(), tc.getExternalProjectId());
+  }
+
+  public static TomcatContext store(TomcatContext tc) throws SQLException {
+    tc.setPk(broker.save("tomcat.entity.store", tc.getName(), tc.getDocBase(), tc.getEnvId(), tc.getStateId(), tc.getTomcatPoolId(), tc.getApplicationXml(), tc.getExternalProjectId()));
+    return tc;
+  }
+
+  public static boolean delete(TomcatContext tc) throws SQLException {
+    return broker.delete("tomcat.entity.delete", tc.getPk());
+  }
+
+  public boolean hasTomcatPool() {
+    boolean ret = false;
+    if (this.getTomcatPool() != null) {
+      ret = true;
+    }
+    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") + "tomcatcontext/" + this.getPk() + '/');
+    return elementBuilder("tomcatcontext", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<tomcatcontext>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(elementBuilder("docBase", this.getDocBase()));
+    ret.append(this.getApplicationXml());
+    ret.append(this.getTomcatPool().buildShortXMLRepresentation());
+    ret.append(this.getState().buildShortXMLRepresentation());
+    ret.append(this.getEnv().buildShortXMLRepresentation());
+    ret.append("</tomcatcontext>\n");
+    return ret;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof TomcatContext)) return false;
+
+    final TomcatContext tomcatContext = (TomcatContext) o;
+
+    if (contextId != tomcatContext.contextId) return false;
+    if (envId != tomcatContext.envId) return false;
+    if (stateId != tomcatContext.stateId) return false;
+    if (docBase != null ? !docBase.equals(tomcatContext.docBase) : tomcatContext.docBase != null) return false;
+    if (this.getName() != null ? !this.getName().equals(tomcatContext.getName()) : tomcatContext.getName() != null)
+      return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = contextId;
+    result = 29 * result + (this.getName() != null ? this.getName().hashCode() : 0);
+    result = 29 * result + (docBase != null ? docBase.hashCode() : 0);
+    result = 29 * result + stateId;
+    result = 29 * result + envId;
+    return result;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("TomcatContext");
+    buf.append("{contextId=").append(contextId);
+    buf.append(",name=").append(name);
+    buf.append(",docBase=").append(docBase);
+    buf.append(",stateId=").append(stateId);
+    buf.append(",envId=").append(envId);
+    buf.append(",state=").append(state);
+    buf.append(",env=").append(env);
+    buf.append('}');
+    return buf.toString();
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContextModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContextModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContextModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/entity/TomcatContextModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,496 @@
+/*
+* 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.tomcat.api.entity;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.job.Job;
+import lokahi.core.api.jobpool.JobPool;
+import lokahi.core.api.pool.HostingPool;
+import lokahi.core.api.project.Project;
+import lokahi.core.api.state.State;
+import lokahi.core.api.user.User;
+import lokahi.core.common.exception.AlreadyExistException;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCIllegalArgumentException;
+import lokahi.core.common.interfaces.LokahiModel;
+import lokahi.core.common.util.PropertiesFile;
+import lokahi.httpd.api.entity.VirtualHost;
+import lokahi.tomcat.api.pool.TomcatPoolModel;
+import lokahi.tomcat.api.worker.TomcatWorker;
+import org.apache.log4j.Logger;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: TomcatContextModel.java,v 1.3 2006/03/07 20:18:55 drtobes Exp $
+ */
+public class TomcatContextModel extends LokahiModel<TomcatContext> {
+  protected static final Logger logger = Logger.getLogger(TomcatContextModel.class);
+
+  public void reloadContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("ReloadContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to ReloadContext");
+    this.reloadContext(null, tw, p, u, tc);
+  }
+
+  public void reloadContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("ReloadContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to ReloadContext");
+    this.jpInit(Function.getFunction("ReloadContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.reloadContext(jp, tw, p, u, tc);
+  }
+
+  void reloadContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("ReloadContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.reloadContext(jobPool, tw, p, u, tc);
+  }
+
+  void reloadContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "ReloadContext", tc);
+  }
+
+  public void reloadContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.reloadContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void statusOfContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("StatusOfContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to StatusOfContext");
+    this.statusOfContext(null, tw, p, u, tc);
+  }
+
+  public void statusOfContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("StatusOfContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to StatusOfContext");
+    this.jpInit(Function.getFunction("StatusOfContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.statusOfContext(jp, tw, p, u, tc);
+  }
+
+  void statusOfContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("StatusOfContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.statusOfContext(jobPool, tw, p, u, tc);
+  }
+
+  void statusOfContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "StatusOfContext", tc);
+  }
+
+  public void statusOfContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.statusOfContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void redeployContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("RedeployContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to RedeployContext");
+    this.redeployContext(null, tw, p, u, tc);
+  }
+
+  public void redeployContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("RedeployContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to RedeployContext");
+    this.jpInit(Function.getFunction("RedeployContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.redeployContext(jp, tw, p, u, tc);
+  }
+
+  void redeployContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("RedeployContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.redeployContext(jobPool, tw, p, u, tc);
+  }
+
+  void redeployContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "RedeployContext", tc);
+  }
+
+  public void redeployContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.redeployContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void listContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("ListContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to ListContext");
+    this.listContext(null, tw, p, u, tc);
+  }
+
+  public void listContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("ListContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to ListContext");
+    this.jpInit(Function.getFunction("ListContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.listContext(jp, tw, p, u, tc);
+  }
+
+  void listContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("ListContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.listContext(jobPool, tw, p, u, tc);
+  }
+
+  void listContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "ListContext", tc);
+  }
+
+  public void listContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.listContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void installContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("InstallContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to InstallContext");
+    this.installContext(null, tw, p, u, tc);
+  }
+
+  public void installContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("InstallContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to InstallContext");
+    this.jpInit(Function.getFunction("InstallContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.installContext(jp, tw, p, u, tc);
+  }
+
+  void installContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("InstallContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.installContext(jobPool, tw, p, u, tc);
+  }
+
+  void installContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName()
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getDocBase();
+    this.function(tw, jobPool, options, p, u, "InstallContext", tc);
+  }
+
+  public void installContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.installContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void undeployContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("UndeployContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to UndeployContext");
+    this.undeployContext(null, tw, p, u, tc);
+  }
+
+  public void undeployContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("UndeployContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to UndeployContext");
+    this.jpInit(Function.getFunction("UndeployContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.undeployContext(jp, tw, p, u, tc);
+  }
+
+  void undeployContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("UndeployContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.undeployContext(jobPool, tw, p, u, tc);
+  }
+
+  void undeployContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "UndeployContext", tc);
+  }
+
+  public void undeployContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.undeployContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void sessionsOfContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("SessionsOfContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to SessionsOfContext");
+    this.sessionsOfContext(null, tw, p, u, tc);
+  }
+
+  public void sessionsOfContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("SessionsOfContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to SessionsOfContext");
+    this.jpInit(Function.getFunction("SessionsOfContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.sessionsOfContext(jp, tw, p, u, tc);
+  }
+
+  void sessionsOfContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("SessionsOfContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.sessionsOfContext(jobPool, tw, p, u, tc);
+  }
+
+  void sessionsOfContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "SessionsOfContext", tc);
+  }
+
+  public void sessionsOfContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.sessionsOfContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void startContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("StartContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to StartContext");
+    this.startContext(null, tw, p, u, tc);
+  }
+
+  public void startContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("StartContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to StartContext");
+    this.jpInit(Function.getFunction("StartContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.startContext(jp, tw, p, u, tc);
+  }
+
+  void startContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("StartContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.startContext(jobPool, tw, p, u, tc);
+  }
+
+  void startContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "StartContext", tc);
+  }
+
+  public void startContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.startContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  public void stopContext(TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("StopContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to StopContext");
+    this.stopContext(null, tw, p, u, tc);
+  }
+
+  public void stopContext(Collection<TomcatWorker> c, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (!u.isAllowed("StopContext", p, tc))
+      throw new AuthorizationException(u.getName() + "Not allowed to StopContext");
+    this.jpInit(Function.getFunction("StopContext"), u, p, "");
+    for (TomcatWorker tw : c)
+      this.stopContext(jp, tw, p, u, tc);
+  }
+
+  void stopContext(JobPool jobPool, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (jobPool == null) {
+      this.jpInit(Function.getFunction("StopContext"), u, p, "");
+      jobPool = jp;
+    }
+    for (TomcatWorker tw : tc.getTomcatPool().getTomcatWorkers())
+      this.stopContext(jobPool, tw, p, u, tc);
+  }
+
+  void stopContext(JobPool jobPool, TomcatWorker tw, Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    String options = Integer.toString(tw.getTomcat().getHttpPort())
+        + PropertiesFile.getConstantValue("agent.delim.options")
+        + tc.getContextName();
+    this.function(tw, jobPool, options, p, u, "StopContext", tc);
+  }
+
+  public void stopContext(Project p, User u, TomcatContext tc) throws SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    if (tc.getTomcatPool() == null || tc.getTomcatPool().getTomcatWorkers() == null)
+      throw new TMCIllegalArgumentException("Error with Context tomcat pool.");
+    this.stopContext(tc.getTomcatPool().getTomcatWorkers(), p, u, tc);
+  }
+
+  private void function(Collection<TomcatWorker> tomcatWorkers, String origOptions, Project p, User u, String functionCommand, JobPool jobPool, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if ("".equals(origOptions)) {
+      origOptions = tc.getContextName();
+    } else {
+      origOptions = tc.getContextName() + PropertiesFile.getConstantValue("agent.delim.options") + origOptions;
+    }
+    Function f = Function.getFunction(functionCommand);
+    if (!u.isAllowed(f, p, tc))
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    this.function(tomcatWorkers, origOptions, p, u, f, jobPool, tc);
+  }
+
+  private void function(Collection<TomcatWorker> tomcatWorkers, String origOptions, Project p, User u, Function f, JobPool jobPool, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    if (tomcatWorkers != null && f != null) {
+      if (jobPool == null || jobPool.getPk() <= 0) {
+        jobPool = new JobPool(p, u, tc.getContextName(), f);
+        jobPool = JobPool.store(jobPool);
+      }
+      for (final TomcatWorker tw : tomcatWorkers) {
+        StringBuffer options = new StringBuffer();
+        if (logger.isDebugEnabled()) {
+          logger.debug("tw=" + tw);
+        }
+        options.append(Integer.toString(tw.getTomcat().getHttpPort()));
+        options.append(PropertiesFile.getConstantValue("agent.delim.options"));
+        options.append(origOptions);
+        this.function(tw, jobPool, options.toString(), p, u, f);
+      }
+    }
+  }
+
+  private void function(TomcatWorker tw, JobPool jobPool, String options, Project p, User u, String functionCommand, TomcatContext tc) throws SQLException, IOException, AuthorizationException {
+    Function f = Function.getFunction(functionCommand);
+    this.function(tw, jobPool, options, p, u, f);
+  }
+
+  private void function(TomcatWorker tw, JobPool jobPool, String options, Project p, User u, Function f) throws SQLException, IOException {
+    State s = State.getState(0);
+    if (jobPool == null) {
+      jobPool = new JobPool(p, u, s, options, f);
+      jobPool = JobPool.store(jobPool);
+    }
+    if (jobPool != null && tw != null && options != null && !"".equals(options) && f != null) {
+      if (tw.getType() == 6) f = Function.getFunction(f.getCommand() + "6");
+      Job j = new Job(-1, s, options, tw.getHardware(), f, jobPool);
+      if (Job.store(j) != null) {
+      }
+    }
+  }
+
+  public void addContext(TomcatContext tc, VirtualHost vh, HostingPool hp, Project p, User u, Function f) throws TMCIllegalArgumentException, AuthorizationException, AlreadyExistException, SQLException, IOException {
+    this.jpInit(f, u, p, tc.getContextName());
+    if (p == null || hp == null || vh == null) throw new TMCIllegalArgumentException("Error with input");
+    if (!u.isAllowed(f, p, tc))
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    if (!hp.isContextAllowed(tc)) throw new AlreadyExistException("Name not allowed, already exists.");
+    tc.setTomcatPool(hp.getTomcatPool());
+    tc = TomcatContext.store(tc);
+    p.addTomcatContexts(tc);
+    vh.addContext(tc);
+    Project.update(p);
+    this.installContext(jp, p, u, tc);
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.deployTomcatConfig(u, p, jp, hp.getTomcatPool(), f);
+    jp.setState(State.NEW);
+    JobPool.update(jp);
+  }
+
+  public void updateContext(TomcatContext tc, User u, Function f, Project p) throws TMCIllegalArgumentException, AuthorizationException, AlreadyExistException, SQLException, IOException {
+    this.jpInit(f, u, p, tc.getContextName());
+    if (!u.isAllowed(f)) throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    if (tc.getPk() < 1) throw new TMCIllegalArgumentException("Error with input");
+    TomcatContext.update(tc);
+    this.installContext(jp, null, u, tc);
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.deployTomcatConfig(u, null, jp, tc.getTomcatPool(), f);
+    jp.setState(State.NEW);
+    JobPool.update(jp);
+  }
+
+  public void deleteContext(Collection<TomcatContext> c, User u, Function f) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.jpInit(f, u);
+    if (!u.isAllowed(f)) throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    StringBuilder options = new StringBuilder();
+    for (final TomcatContext tc : c) {
+      if (tc.getPk() < 1) throw new TMCIllegalArgumentException("Error with input");
+      this.undeployContext(jp, null, u, tc);
+      options.append(tc.getName());
+      options.append(',');
+      TomcatContext.delete(tc);
+      tpm.deployTomcatConfig(u, null, jp, tc.getTomcatPool(), f);
+    }
+    jp.setOptions(options.toString());
+    jp.setState(State.NEW);
+    JobPool.update(jp);
+  }
+
+  public void removeContextFromVhost(TomcatContext tc, VirtualHost vh, Project p, User u, Function f, boolean removeFromProject) throws AuthorizationException, SQLException, TMCIllegalArgumentException {
+    if (f == null || tc == null || vh == null || p == null || u == null)
+      throw new TMCIllegalArgumentException("Error with input");
+    if (!u.isAllowed(f)) throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    this.jpInit(f, u, p, tc.getName() + ' ' + vh.getName());
+    vh.removeContext(tc);
+    if (removeFromProject) {
+      p.removeTomcatContext(tc);
+      Project.update(p);
+    }
+    jp.setState(State.NEW);
+    JobPool.update(jp);
+  }
+
+  public Collection<TomcatContext> getObjects() throws SQLException {
+    return TomcatContext.getTomcatContexts();
+  }
+
+  public TomcatContext getObject(int id) throws SQLException {
+    return TomcatContext.getTomcatContext(id);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TMPoolWorker.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TMPoolWorker.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TMPoolWorker.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TMPoolWorker.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.tomcat.api.pool;
+
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.Cacheable;
+import lokahi.core.common.interfaces.TMCBroker;
+import lokahi.tomcat.api.worker.TomcatWorker;
+import org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: TMPoolWorker.java,v 1.4 2006/03/07 20:18:52 drtobes Exp $
+ */
+public class TMPoolWorker implements Cacheable {
+  static final Logger logger = Logger.getLogger(TMPoolWorker.class);
+  private static final TMCBroker<TMPoolWorker> broker = new BrokerFactory<TMPoolWorker>().getBroker();
+
+  private int poolId;
+  private int workerId;
+
+  public TMPoolWorker() {
+  }
+
+  public TMPoolWorker(int poolId, int workerId) {
+    this.poolId = poolId;
+    this.workerId = workerId;
+  }
+
+  public TMPoolWorker(TomcatPool tp, TomcatWorker tw) {
+    if (tp != null && tw != null) {
+      this.poolId = tp.getPk();
+      this.workerId = tw.getPk();
+    }
+  }
+
+  public int getPoolId() {
+    return poolId;
+  }
+
+  public void setPoolId(int poolId) {
+    this.poolId = poolId;
+  }
+
+  public int getWorkerId() {
+    return workerId;
+  }
+
+  public void setWorkerId(int workerId) {
+    this.workerId = workerId;
+  }
+
+  public TMPoolWorker fillObject(ResultSet r) throws SQLException {
+    return new TMPoolWorker(r.getInt("POOL_ID"), r.getInt("WORKER_ID"));
+  }
+
+  public TomcatWorker getWorker() throws SQLException {
+    return TomcatWorker.getTomcatWorker(workerId, false);
+  }
+
+  public TomcatPool getPool() throws SQLException {
+    return TomcatPool.getTomcatPool(poolId);
+  }
+
+  public static void save(TMPoolWorker rel) throws SQLException {
+    broker.modifyRelationship("tomcat.pool.rel.worker.add", rel.getPoolId(), rel.getWorkerId());
+  }
+
+  public static void delete(TMPoolWorker rel) throws SQLException {
+    broker.modifyRelationship("tomcat.pool.rel.worker.delete", rel.getPoolId(), rel.getWorkerId());
+  }
+
+  public Collection<TMPoolWorker> getAll() throws SQLException {
+    return broker.getObjects(TMPoolWorker.class, false, "tomcat.pool.rel.worker.all");
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof TMPoolWorker)) return false;
+
+    final TMPoolWorker tmPoolWorker = (TMPoolWorker) o;
+
+    if (poolId != tmPoolWorker.poolId) return false;
+    if (workerId != tmPoolWorker.workerId) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = poolId;
+    result = 29 * result + workerId;
+    return result;
+  }
+
+  public String toString() {
+    return "TMPoolWorker{" +
+        "poolId=" + poolId +
+        ", workerId=" + workerId +
+        '}';
+  }
+}
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPool.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPool.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPool.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPool.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,433 @@
+/*
+* 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.tomcat.api.pool;
+
+import lokahi.core.api.environment.Environment;
+import lokahi.core.api.instance.Instance;
+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.tomcat.api.entity.TomcatContext;
+import lokahi.tomcat.api.worker.TomcatWorker;
+import org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: TomcatPool.java,v 1.3 2006/03/07 20:18:55 drtobes Exp $
+ */
+public class TomcatPool extends TMCDao<TomcatPool> implements Pool {
+  static final Logger logger = Logger.getLogger(TomcatPool.class);
+  private static final TMCBroker<TomcatPool> broker = new BrokerFactory<TomcatPool>().getBroker();
+  private int id;
+  private String name;
+  private int envId;
+
+  private Environment env;
+  private Collection<TomcatWorker> tomcatWorkers;
+
+  public int getPk() {
+    return id;
+  }
+
+  public void setPk(int pk) {
+    this.id = pk;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public String getWorkerName() {
+    return "nss" + this.getPk();
+  }
+
+  public int getEnvId() {
+    return envId;
+  }
+
+  public void setEnvId(int envId) {
+    this.envId = envId;
+  }
+
+  public Environment getEnv() {
+    if (env == null) {
+      try {
+        this.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;
+    if (env != null) {
+      this.envId = env.getPk();
+    }
+  }
+
+  private Collection<TomcatWorker> getTomcatWorkersNoDB() {
+    return tomcatWorkers;
+  }
+
+  public Collection<TomcatWorker> getTomcatWorkers() {
+    if (tomcatWorkers == null) {
+      try {
+        this.tomcatWorkers = TomcatWorker.getTomcatWorkers(this, false);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return tomcatWorkers;
+  }
+
+  public Collection<TomcatWorker> getTomcatWorkers(Instance i) {
+    TMCSet<TomcatWorker> ret = new TMCSet<TomcatWorker>();
+    if (i != null) {
+      if (tomcatWorkers == null) {
+        try {
+          this.tomcatWorkers = TomcatWorker.getTomcatWorkers(this, false);
+        } catch (SQLException e) {
+          if (logger.isInfoEnabled()) {
+            logger.info("Exception: " + e.getMessage());
+          }
+        }
+      }
+      for (TomcatWorker tw : tomcatWorkers) {
+        if (tw.getHardware() != null && i.equals(tw.getHardware().getInstance())) {
+          ret.add(tw);
+        }
+      }
+    }
+    return ret;
+  }
+
+  public void setTomcatWorkers(Collection<TomcatWorker> tc4servers) {
+    this.tomcatWorkers = tc4servers;
+  }
+
+  public boolean addWorker(TomcatWorker t) throws SQLException {
+    Collection<TomcatWorker> c = new TMCSet<TomcatWorker>();
+    c.add(t);
+    return this.addWorkers(c);
+  }
+
+  public boolean removeWorker(TomcatWorker t) throws SQLException {
+    Collection<TomcatWorker> c = new TMCSet<TomcatWorker>();
+    c.add(t);
+    return this.removeWorkers(c);
+  }
+
+  public String getWorkerNames() {
+    this.getTomcatWorkers();
+    StringBuffer temp = new StringBuffer();
+    for (final TomcatWorker tw : this.getTomcatWorkers()) {
+      temp.append(tw.getWorkerName());
+      temp.append(',');
+    }
+    String ret = "";
+    if (temp.length() > 1) {
+      ret = temp.toString();
+      ret = ret.substring(0, ret.lastIndexOf(','));
+    }
+    return ret;
+  }
+
+//  public int getHTTPPort() {
+//    Collection<TomcatWorker> c = this.getTomcatWorkers();
+//    int ret = 0;
+//    for (TomcatWorker t : c) {
+//      ret += t.getTomcat().getHttpPort();
+//    }
+//    if (c.size() != 0)
+//      ret /= c.size();
+//    return ret;
+//  }
+
+  public boolean isActive() {
+    boolean ret = false;
+    Iterator<TomcatWorker> i = this.getTomcatWorkers().iterator();
+    while (!ret && i.hasNext()) {
+      ret = i.next().isActive();
+    }
+    return ret;
+  }
+
+  public boolean addWorkers(Collection<TomcatWorker> c) throws SQLException {
+    boolean ret = false;
+    Collection<TomcatWorker> col = this.getTomcatWorkers();
+    if (col == null || col.isEmpty())
+      col = new TMCSet<TomcatWorker>();
+    if (c == null || c.isEmpty())
+      c = new TMCSet<TomcatWorker>();
+    for (final TomcatWorker tw : c) {
+      if (!col.contains(tw)) {
+        this.storeWorkerRelationship(tw);
+        col.add(tw);
+      }
+    }
+    this.setTomcatWorkers(col);
+    if (col.containsAll(c)) {
+      ret = true;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  private void storeWorkerRelationship(TomcatWorker tw) throws SQLException {
+    TMPoolWorker.save(new TMPoolWorker(this, tw));
+  }
+
+  public boolean removeWorkers(Collection<TomcatWorker> c) throws SQLException {  //abusive to the connection pool!
+    boolean ret = false;
+    Collection<TomcatWorker> col = this.getTomcatWorkers();
+    if (col != null) {
+      if (c != null && !c.isEmpty()) {
+        for (final TomcatWorker tw : c) {
+          if (col.contains(tw)) {
+            this.removeWorkerRelationship(tw);
+            col.remove(tw);
+          }
+        }
+        this.setTomcatWorkers(col);
+        if (!col.containsAll(c)) {
+          ret = true;
+        }
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  private void removeWorkerRelationship(TomcatWorker tw) throws SQLException {
+    TMPoolWorker.delete(new TMPoolWorker(this, tw));
+  }
+
+  public TomcatPool() {
+  }
+
+  public TomcatPool(String name, Environment env, Collection<TomcatWorker> TomcatWorkers) {
+    this.setName(name);
+    this.setEnv(env);
+    this.setTomcatWorkers(TomcatWorkers);
+  }
+
+  public TomcatPool(int id, String name, int envId) {
+    this.setPk(id);
+    this.setName(name);
+    this.setEnvId(envId);
+  }
+
+  public Collection<TomcatContext> getAllContextsInPool() {
+    Collection<TomcatContext> c = new TMCSet<TomcatContext>();
+    if (this.getTomcatWorkers() != null) {
+      for (final TomcatWorker tw : this.getTomcatWorkers()) {
+        c.addAll(tw.getAllContexts());
+      }
+    }
+    return c;
+  }
+
+  public TomcatPool fillObject(ResultSet r) throws SQLException {
+    return new TomcatPool(r.getInt("POOL_ID"),
+                          r.getString("POOL_NAME"),
+                          r.getInt("ENV_ID"));
+  }
+
+  public static void update(TomcatPool tp) throws SQLException {
+    broker.update("tomcat.pool.update", tp.getPk(), tp.getName(), tp.getEnvId());
+    Collection<TomcatWorker> newWorkers = tp.getTomcatWorkersNoDB();
+    tp.setTomcatWorkers(null);
+    Collection<TomcatWorker> oldWorkers = tp.getTomcatWorkers();
+    Collection<TomcatWorker> keepers = oldWorkers;
+    keepers.retainAll(newWorkers);
+    newWorkers.removeAll(keepers);
+    oldWorkers.removeAll(keepers);
+    tp.addWorkers(newWorkers);
+    tp.removeWorkers(oldWorkers);
+  }
+
+  public static boolean delete(TomcatPool tp) throws SQLException {
+    return broker.delete("tomcat.pool.delete", tp.getPk());
+  }
+
+  public static TomcatPool store(TomcatPool tp) throws SQLException {
+    tp.setPk(broker.save("tomcat.pool.store", tp.getName(), tp.getEnvId()));
+    Collection<TomcatWorker> c = tp.getTomcatWorkers();
+    tp.setTomcatWorkers(null);
+    if (c != null) {
+      tp.addWorkers(c);
+    }
+    return tp;
+  }
+
+  public static TomcatPool getTomcatPool(int id) throws SQLException {
+    return new TomcatPool().get(id);
+  }
+
+  public TomcatPool get(int id) throws SQLException {
+    TomcatPool tp;
+    try {
+      tp = broker.getObject(TomcatPool.class, "tomcat.pool.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return tp;
+  }
+
+  public static TomcatPool getTomcatPool(TomcatContext tc) throws SQLException {
+    TomcatPool tp;
+    try {
+      tp = broker.getObject(TomcatPool.class,
+                            "tomcat.pool.by.context", false,
+                            tc.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input tc=" + tc + "Returning " + tp);
+    }
+    return tp;
+  }
+
+  public static Collection<TomcatPool> getTomcatPools() throws SQLException {
+    Collection<TomcatPool> c;
+    try {
+      c = broker.getObjects(TomcatPool.class, false, "tomcat.pool.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<TomcatPool> getTomcatPools(String name) throws SQLException {
+    Collection<TomcatPool> c;
+    try {
+      c = broker.getObjects(TomcatPool.class, false, "tomcat.pool.by.name", name);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<TomcatPool> getTomcatPools(TomcatWorker tw) throws SQLException {
+    Collection<TomcatPool> c;
+    try {
+      c = broker.getObjects(TomcatPool.class, false, "tomcat.pool.by.worker", tw.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  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") + "tomcatpool/" + this.getPk() + '/');
+    return elementBuilder("tomcatpool", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<tomcatpool>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(elementBuilder("workerName", this.getWorkerName()));
+    ret.append(this.getEnv().buildShortXMLRepresentation());
+    for (final TomcatWorker tw : this.getTomcatWorkers()) {
+      ret.append(tw.buildShortXMLRepresentation());
+    }
+    ret.append(this.getEnv().buildShortXMLRepresentation());
+    ret.append("</tomcatpool>\n");
+    return ret;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof TomcatPool)) return false;
+
+    final TomcatPool tomcatPool = (TomcatPool) o;
+
+    if (envId != tomcatPool.envId) return false;
+    if (id != tomcatPool.id) return false;
+    if (name != null ? !name.equals(tomcatPool.name) : tomcatPool.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;
+    return result;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("TomcatPool");
+    buf.append("{id=").append(id);
+    buf.append(",name=").append(name);
+    buf.append(",envId=").append(envId);
+    buf.append(",env=").append(env);
+    buf.append(",tomcatWorkers=").append(tomcatWorkers);
+    buf.append('}');
+    return buf.toString();
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPoolModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPoolModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPoolModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/tomcat/api/pool/TomcatPoolModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,290 @@
+/*
+* 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.tomcat.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.AlreadyExistException;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCException;
+import lokahi.core.common.exception.TMCIllegalArgumentException;
+import lokahi.core.common.exception.UnsupportedParameterException;
+import lokahi.core.common.interfaces.LokahiModel;
+import lokahi.tomcat.api.worker.TomcatWorker;
+import lokahi.tomcat.api.worker.TomcatWorkerModel;
+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: TomcatPoolModel.java,v 1.3 2006/03/07 20:18:55 drtobes Exp $
+ */
+public class TomcatPoolModel extends LokahiModel<TomcatPool> {
+  static final Logger logger = Logger.getLogger(TomcatPoolModel.class);
+
+  public void addTomcatPool(User u, TomcatPool tp, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u, tp.getName());
+    if (u.isAllowed(f) && "AddTomcatPool".equals(f.getCommand())) {
+      TomcatPool.store(tp);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteTomcatPool(User u, TomcatPool tp, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    if (tp == null || tp.getPk() < 1) throw new TMCIllegalArgumentException("TomcatPool 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, TomcatPool tp, Function f) throws AuthorizationException, SQLException {
+    if (u.isAllowed(f) && "DeleteTomcatPool".equals(f.getCommand())) {
+      TomcatPool.delete(tp);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+  }
+
+  public void updateTomcatPool(User u, TomcatPool tp, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u, tp.getName());
+    if (u.isAllowed(f) && "UpdateTomcatPool".equals(f.getCommand())) {
+      if (tp.getPk() >= 1 && tp.getName() != null && !"".equals(tp.getName())) {
+        TomcatPool.update(tp);
+      } else {
+        throw new TMCIllegalArgumentException("TomcatPool 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 deleteTomcatPool(User u, Collection<TomcatPool> c, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u);
+    StringBuffer names = new StringBuffer();
+    for (final TomcatPool tp : c) {
+      names.append(tp.getName());
+      names.append(',');
+      this.deleteTomcatPool(u, tp, f);
+    }
+    jp.setOptions(names.toString());
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void stopTomcat(User u, Collection<TomcatPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    Method m = TomcatPoolModel.class.getMethod("stopTomcat", User.class, Project.class, JobPool.class, TomcatPool.class);
+    tomcatFunction(m, u, c, f);
+  }
+
+  public void startTomcat(User u, Collection<TomcatPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    Method m = TomcatPoolModel.class.getMethod("startTomcat", User.class, Project.class, JobPool.class, TomcatPool.class);
+    tomcatFunction(m, u, c, f);
+  }
+
+  public void restartTomcat(User u, Collection<TomcatPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    Method m = TomcatPoolModel.class.getMethod("restartTomcat", User.class, Project.class, JobPool.class, TomcatPool.class);
+    tomcatFunction(m, u, c, f);
+  }
+
+  public void deployTomcatConfig(User u, Collection<TomcatPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    Method m = TomcatPoolModel.class.getMethod("deployTomcatConfig", User.class, Project.class, JobPool.class, TomcatPool.class);
+    tomcatFunction(m, u, c, f);
+  }
+
+  private void tomcatFunction(Method m, User u, Collection<TomcatPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException, UnsupportedParameterException, AlreadyExistException {
+    this.jpInit(f, u);
+    StringBuffer names = new StringBuffer();
+    for (final TomcatPool tp : c) {
+      names.append(tp.getName());
+      names.append(',');
+      try {
+        m.invoke(this, u, null, jp, tp);
+      } catch (IllegalAccessException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + 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 UnsupportedParameterException) {
+            throw new UnsupportedParameterException(ex.getMessage());
+          }
+          if (ex instanceof TMCIllegalArgumentException) {
+            throw new TMCIllegalArgumentException(ex.getMessage());
+          }
+          if (ex instanceof AuthorizationException) {
+            throw new AuthorizationException(ex.getMessage());
+          }
+          if (ex instanceof AlreadyExistException) {
+            throw new AlreadyExistException(ex.getMessage());
+          }
+          if (ex instanceof TMCException) {
+            throw new TMCException(ex.getMessage());
+          }
+          if (ex instanceof SQLException) {
+            throw new SQLException(ex.getMessage());
+          }
+        }
+      }
+    }
+    jp.setOptions(names.toString());
+    jp.setState(State.NEW);
+    JobPool.update(jp);
+  }
+
+//  public void statusOfTomcat
+
+  public void updateTomcat(User u, Project p, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "UpdateTomcat", null, tp);
+  }
+
+  public void restartTomcat(User u, Project p, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "RestartTomcat", null, tp);
+  }
+
+  public void startTomcat(User u, Project p, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "StartTomcat", null, tp);
+  }
+
+  public void stopTomcat(User u, Project p, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "StopTomcat", null, tp);
+  }
+
+  public void deployTomcatConfig(User u, Project p, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "DeployTomcatConfig", null, tp);
+  }
+
+  public void updateTomcat(User u, Project p, JobPool jobPool, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "UpdateTomcat", jobPool, tp);
+  }
+
+  public void restartTomcat(User u, Project p, JobPool jobPool, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "RestartTomcat", jobPool, tp);
+  }
+
+  public void startTomcat(User u, Project p, JobPool jobPool, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "StartTomcat", jobPool, tp);
+  }
+
+  public void stopTomcat(User u, Project p, JobPool jobPool, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "StopTomcat", jobPool, tp);
+  }
+
+  public void deployTomcatConfig(User u, Project p, JobPool jobPool, TomcatPool tp) throws SQLException, NoSuchMethodException, IOException, AuthorizationException {
+    this.function(u, p, "DeployTomcatConfig", jobPool, tp);
+  }
+
+  private void function(User u, Project p, String command, JobPool jobPool, TomcatPool tp) throws NoSuchMethodException, SQLException, AuthorizationException, IOException {
+    Function f = Function.getFunction(command);
+    command = command.substring(0, 1).toLowerCase() + command.substring(1);
+    Method method;
+    if (jobPool == null) {
+      jpInit(f, u, tp.getName());
+      jobPool = this.jp;
+    }
+    try {
+      method = TomcatWorkerModel.class.getMethod(command, u.getClass(), Project.class, JobPool.class, TomcatWorker.class);
+    } catch (NoSuchMethodException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("method.getName()=" + method.getName() + "Should be equal to: command=" + command);
+    }
+    TomcatWorkerModel tm = new TomcatWorkerModel();
+    for (final TomcatWorker tw : tp.getTomcatWorkers()) {
+      try {
+        method.invoke(tm, u, p, jobPool, tw);
+      } catch (IllegalAccessException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("IllegalAccessException: " + e.getMessage());
+        }
+      } catch (InvocationTargetException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("InvocationTargetException: " + e.getMessage());
+        }
+      }
+    }
+  }
+
+  public TomcatPool viewTomcatPool(int id) throws SQLException {
+    return TomcatPool.getTomcatPool(id);
+  }
+
+  public Collection<TomcatPool> viewTomcatPools() throws SQLException {
+    return TomcatPool.getTomcatPools();
+  }
+
+  public void deployTomcatConfig(User u, Project p, JobPool jobPool, TomcatPool tp, Function f) throws SQLException, AuthorizationException, IOException, TMCIllegalArgumentException {
+    Collection<TomcatWorker> c = tp.getTomcatWorkers();
+    TomcatWorkerModel twm = new TomcatWorkerModel();
+    for (final TomcatWorker tw : c) {
+      twm.deployTomcatConfig(u, p, jobPool, tw, f);
+    }
+  }
+
+//  public boolean deployTomcatConfig(User u, Project p, JobPool jp, TomcatPool tp) throws SQLException, IOException, AuthorizationException {
+//    boolean ret = false;
+//    if (jp != null) {
+//      Collection c = tp.getTomcatWorkers();
+//      TomcatWorkerModel twm = new TomcatWorkerModel();
+//      for (Iterator i = c.iterator(); i.hasNext();) {
+//        TomcatWorker tw = (TomcatWorker) i.next();
+//        if (tw.getTomcat() != null && tw.getHardware() != null) {
+//          twm.deployTomcatConfig(u, p, jp, tw);
+//        }
+//      }
+//    }
+//    return ret;
+//  }
+
+  public Collection<TomcatPool> getObjects() throws SQLException {
+    Collection<TomcatPool> c = new TMCSet<TomcatPool>();
+    c.addAll(this.viewTomcatPools());
+    return c;
+  }
+
+  public TomcatPool getObject(int id) throws SQLException {
+    return this.viewTomcatPool(id);
+  }
+}
+
+
+
+
+



Mime
View raw message