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 [10/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/core/api/jobpool/JobPool.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/jobpool/JobPool.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/jobpool/JobPool.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/jobpool/JobPool.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,606 @@
+/*
+* 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.core.api.jobpool;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.job.Job;
+import lokahi.core.api.project.Project;
+import lokahi.core.api.state.State;
+import lokahi.core.api.user.User;
+import lokahi.core.common.collection.TMCCollectionImpl;
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.Datable;
+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 org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.TreeMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: JobPool.java,v 1.3 2006/03/07 22:05:21 drtobes Exp $
+ */
+public class JobPool extends TMCDao<JobPool> implements Pool, Datable {
+  static final Logger logger = Logger.getLogger(JobPool.class);
+  private static final TMCBroker<JobPool> broker = new BrokerFactory<JobPool>().getBroker();
+  private int jobPoolId;
+  private String jobPoolName;
+  private int projectId = -1;
+  private int userId;
+  private Date startTime;
+  private Date finishTime;
+  private String options;
+  private int functionId;
+  private State state;
+
+  private User user;
+  private Project project;
+  private Function function;
+  private Collection<Job> jobs;
+
+  public JobPool() {
+  }
+
+  public JobPool(int projectId, int userId, State state, String options, int functionId) {
+    this.setProjectId(projectId);
+    this.setUserId(userId);
+    this.setState(state);
+    this.setOptions(options);
+    this.setFunctionId(functionId);
+    this.setName(null);
+  }
+
+  //PREFERED constructor.
+  public JobPool(Project p, User u, State s, String options, Function f) {
+    this.setProject(p);
+    this.setUser(u);
+    this.setState(s);
+    this.setOptions(options);
+    this.setFunction(f);
+    this.setName(null);
+  }
+
+//  public JobPool(User u, int initalState, Function f) {
+//    this.setProjectId(-1);
+//    this.setUserId(u.getPk());
+//    this.setStateId(initalState);
+//    this.setFunctionId(f.getPk());
+//    this.setName(null);
+//  }
+
+  public JobPool(User u, State s, Function f) {
+    this.setProjectId(-1);
+    this.setUserId(u.getPk());
+    this.setState(s);
+    this.setFunctionId(f.getPk());
+    this.setName(null);
+  }
+
+  public JobPool(Project p, User u, String options, Function f) {
+    this.setProject(p);
+    this.setUserId(u.getPk());
+    this.setState(State.NEW);
+    this.setOptions(options);
+    this.setFunctionId(f.getPk());
+    this.setName(null);
+  }
+
+  public JobPool(int jobPoolId, String jobPoolName, int projectId, int userId, Date startTime, Date finishTime, int stateId, String options, int functionId) {
+    this.jobPoolId = jobPoolId;
+    this.jobPoolName = jobPoolName;
+    this.projectId = projectId;
+    this.userId = userId;
+    this.startTime = startTime;
+    this.finishTime = finishTime;
+    this.state = State.getState(stateId);
+    this.options = options;
+    this.functionId = functionId;
+  }
+
+  public int getPk() {
+    return jobPoolId;
+  }
+
+  public void setPk(int pk) {
+    this.jobPoolId = pk;
+  }
+
+  public String getName() {
+    return jobPoolName;
+  }
+
+  public void setName(String name) {
+    if (name != null) {
+      this.jobPoolName = name;
+    } else {
+      this.jobPoolName = generateName();
+    }
+  }
+
+  public int getProjectId() {
+    return projectId;
+  }
+
+  public void setProjectId(int projectId) {
+    this.project = null;
+    this.projectId = projectId;
+  }
+
+  public int getUserId() {
+    return userId;
+  }
+
+  public void setUserId(int userId) {
+    this.user = null;
+    this.userId = userId;
+  }
+
+  public Date getStartTime() {
+    return startTime;
+  }
+
+  public void setStartTime(Date startTime) {
+    this.startTime = startTime;
+  }
+
+  public Date getFinishTime() {
+    return finishTime;
+  }
+
+  public void setFinishTime(Date finishTime) {
+    this.finishTime = finishTime;
+  }
+
+  public String getOptions() {
+    if (options == null) options = "";
+    return options;
+  }
+
+  public void setOptions(String options) {
+    this.options = options;
+  }
+
+  public int getFunctionId() {
+    return functionId;
+  }
+
+  // Special getters and setters
+  public void setFunctionId(int functionId) {
+    this.functionId = functionId;
+    this.function = null;
+  }
+
+  public Function getFunction() {
+    if (this.function == null) {
+//      try {
+      this.function = Function.getFunction(this.functionId);
+//      } catch (SQLException e) {
+//        if (logger.isInfoEnabled()) {
+//          logger.info("Exception: " + e.getMessage());
+//        }
+//      }
+    }
+    return function;
+  }
+
+  public void setFunction(Function function) {
+    this.function = function;
+    if (function != null) {
+      this.functionId = function.getPk();
+    }
+  }
+
+  public State getState() {
+    return state;
+  }
+
+  public void setState(State state) {
+    this.state = state;
+  }
+
+  public User getUser() {
+    if (user == null) {
+      try {
+        this.user = User.getUser(this.getUserId());
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return user;
+  }
+
+  public void setUser(User user) {
+    this.user = user;
+    if (user != null) {
+      this.userId = user.getPk();
+    }
+  }
+
+  public Project getProject() {
+    if (project == null && this.projectId != -1) {
+      try {
+        this.project = Project.getProject(this.projectId);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return project;
+  }
+
+  public void setProject(Project project) {
+    if (project != null) {
+      this.projectId = project.getPk();
+    }
+    this.project = project;
+  }
+
+  public Collection<Job> getJobs() throws SQLException {
+    if (jobs == null) {
+      jobs = Job.getJobs(this.getPk());
+    }
+    return jobs;
+  }
+
+  public TreeMap<String, TreeMap<Integer, Job>> getSortedJobs() {
+    TreeMap<String, TreeMap<Integer, Job>> tm = new TreeMap<String, TreeMap<Integer, Job>>();
+    Collection<Job> c = null;
+    try {
+      c = this.getJobs();
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    if (c != null) {
+      TreeMap<Integer, Job> tm2 = new TreeMap<Integer, Job>();
+      for (final Job j : c) {
+        if (j.getHardware() != null) {
+          if (tm.keySet().contains(j.getHardware().getName())) {
+            tm2 = tm.get(j.getHardware().getName());
+          }
+          tm2.put(j.getPk(), j);
+          tm.put(j.getHardware().getName(), (TreeMap<Integer, Job>) tm2.clone());
+          tm2.clear();
+        }
+      }
+    }
+    return tm;
+  }
+
+  public void setJobs(Collection<Job> jobs) {
+    this.jobs = jobs;
+  }
+
+  public static void update(JobPool jp) throws SQLException {
+    broker.update("core.jobpool.update", jp.getPk(), jp.getName(), jp.getProjectId(), jp.getUserId(), jp.getState().getPk(), jp.getOptions(), jp.getFunctionId());
+  }
+
+  public JobPool correctStates() throws SQLException {
+    if (this.getState().getPk() <= 0 || this.getState().getPk() >= 6 || this.getState().getPk() == 3) {
+      this.setState(State.RUNNING);
+    }
+    if (this.getState().getPk() == 4 || this.getState().getPk() == 5) {
+      complete(this);
+    }
+    return this;
+  }
+
+  public static JobPool store(JobPool jp) throws SQLException {
+    jp.setPk(broker.save("core.jobpool.store", jp.getProjectId(), jp.getUserId(), jp.getState().getPk(), jp.getOptions(), jp.getFunctionId()));
+    jp.setName(jp.getName() + jp.getPk());
+    if (jp.getPk() > -1) {
+      update(jp);
+    }
+    return jp;
+  }
+
+  public static JobPool reportCompleted(JobPool jp) throws SQLException {
+    JobPool ret = jp;
+    store(jp);
+    if (jp != null) {
+      complete(jp);
+      ret = jp;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public static void complete(JobPool jp) throws SQLException {
+    broker.update("core.jobpool.finalize", jp.getPk(), jp.getState().getPk());
+  }
+
+  public JobPool get(int id) throws SQLException {
+    JobPool jp;
+    try {
+      jp = broker.getObject(JobPool.class, "core.jobpool.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return jp;
+  }
+
+  public static JobPool getJobPool(int id) throws SQLException {
+    return new JobPool().get(id);
+  }
+
+  public static Collection<JobPool> getJobPools(Project p) throws SQLException {
+    Collection<JobPool> c;
+    try {
+      c = broker.getObjects(JobPool.class, false, "core.jobpool.by.project", p.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<JobPool> getJobPools(User u) throws SQLException {
+    Collection<JobPool> c;
+    try {
+      c = broker.getObjects(JobPool.class, false, "core.jobpool.by.user", u.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static TMCCollectionImpl<JobPool> getJobPools(User u, Date startDate, Date endDate, String filter, Function... functions) throws SQLException {
+    if (endDate == null) {
+      endDate = new Date();
+    }
+    TMCCollectionImpl<JobPool> c = new TMCCollectionImpl<JobPool>();
+    try {
+      if (u != null && startDate == null && functions == null) {
+        c.addAll(broker.getObjects(JobPool.class, false, "core.jobpool.search.user", u.getPk(), filter));
+      }
+      if (u != null && startDate != null && functions == null) {
+        c.addAll(broker.getObjects(JobPool.class, false, "core.jobpool.search.user.date", u.getPk(), filter, startDate));
+      }
+      if (u != null && startDate == null && functions != null) {
+        for (final Function f : functions) {
+          c.addAll(broker.getObjects(JobPool.class, false, "core.jobpool.search.user.function", u.getPk(), filter, f.getPk()));
+        }
+      }
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<JobPool> getJobPools(State s) throws SQLException {
+    Collection<JobPool> c;
+    try {
+      c = broker.getObjects(JobPool.class, false, "core.jobpool.by.state", s.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<JobPool> getJobPoolsLimited(State s) throws SQLException {
+    Collection<JobPool> c;
+    try {
+      c = broker.getObjects(JobPool.class, false, "core.jobpool.by.state.limited", s.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public boolean setCurrentState() throws SQLException {
+    boolean ret = false;
+    State s;
+    Collection<Job> c = null;
+    try {
+      c = this.getJobs();
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    if (this.getState().getPk() <= 3) {
+      if (c == null || c.isEmpty()) {
+        s = State.COMPLETE;
+      } else {
+        int lowNum = 4000;
+        int finalState = -1;
+        for (final Job j : c) {
+          if (j.getState().getPk() > finalState && j.getState().getPk() > 3) {
+            finalState = j.getState().getPk();
+            if (logger.isDebugEnabled()) {
+              logger.debug("finalState=" + finalState);
+            }
+          } else if (j.getState().getPk() < lowNum && j.getState().getPk() <= 3) {
+            lowNum = j.getState().getPk();
+          }
+        }
+        if (finalState > -1 && lowNum > finalState) {
+          s = State.getState(finalState);
+          ret = true;
+        } else {
+          s = this.getState();
+        }
+      }
+    } else {
+      s = this.getState();
+    }
+    if (! s.equals(this.getState())) {
+      ret = true;
+      this.setState(s);
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("ret=" + ret);
+    }
+    return ret;
+  }
+
+  public static Collection<JobPool> getJobPools() throws SQLException {
+    Collection<JobPool> c;
+    try {
+      c = broker.getObjects(JobPool.class, false, "core.jobpool.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public JobPool fillObject(ResultSet r) throws SQLException {
+    return new JobPool(r.getInt("JOB_POOL_ID"),
+                       r.getString("JOB_POOL_NAME"),
+                       r.getInt("PROJECT_ID"),
+                       r.getInt("USER_ID"),
+                       r.getTimestamp("START_TIME"),
+                       r.getTimestamp("FINISH_TIME"),
+                       r.getInt("STATE_ID"),
+                       r.getString("OPTIONS"),
+                       r.getInt("FUNCTION_ID"));
+  }
+
+  private static String generateName() {
+    return "C" + new Date().getTime();
+  }
+
+  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") + "jobpool/" + this.getPk() + '/');
+    return elementBuilder("jobpool", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<jobpool>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(elementBuilder("start-time", this.getStartTime().toString()));
+    ret.append(elementBuilder("end-time", this.getFinishTime().toString()));
+    ret.append(elementBuilder("options", this.getOptions()));
+    ret.append(this.getUser().buildShortXMLRepresentation());
+    ret.append(this.getProject().buildShortXMLRepresentation());
+    ret.append(this.getFunction().buildShortXMLRepresentation());
+    ret.append(this.getState().buildShortXMLRepresentation());
+    try {
+      for (final Job j : this.getJobs()) {
+        ret.append(j.buildShortXMLRepresentation());
+      }
+    } catch (SQLException e1) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e1.getMessage());
+      }
+    }
+    ret.append("</jobpool>\n");
+    return ret;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("JobPool");
+    buf.append("{jobPoolId=").append(jobPoolId);
+    buf.append(",jobPoolName=").append(jobPoolName);
+    buf.append(",projectId=").append(projectId);
+    buf.append(",userId=").append(userId);
+    buf.append(",startTime=").append(startTime);
+    buf.append(",finishTime=").append(finishTime);
+    buf.append(",state=").append(state);
+    buf.append(",options=").append(options);
+    buf.append(",functionId=").append(functionId);
+    buf.append(",user=").append(user);
+    buf.append(",project=").append(project);
+    buf.append(",function=").append(function);
+    buf.append(",state=").append(state);
+    buf.append(",jobs=").append(jobs);
+    buf.append('}');
+    return buf.toString();
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof JobPool)) return false;
+
+    final JobPool jobPool = (JobPool) o;
+
+    if (functionId != jobPool.functionId) return false;
+    if (jobPoolId != jobPool.jobPoolId) return false;
+    if (projectId != jobPool.projectId) return false;
+    if (userId != jobPool.userId) return false;
+    if (finishTime != null ? !finishTime.equals(jobPool.finishTime) : jobPool.finishTime != null) return false;
+    if (jobPoolName != null ? !jobPoolName.equals(jobPool.jobPoolName) : jobPool.jobPoolName != null) return false;
+    if (options != null ? !options.equals(jobPool.options) : jobPool.options != null) return false;
+    if (startTime != null ? !startTime.equals(jobPool.startTime) : jobPool.startTime != null) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = jobPoolId;
+    result = 29 * result + (jobPoolName != null ? jobPoolName.hashCode() : 0);
+    result = 29 * result + projectId;
+    result = 29 * result + userId;
+    result = 29 * result + (startTime != null ? startTime.hashCode() : 0);
+    result = 29 * result + (finishTime != null ? finishTime.hashCode() : 0);
+    result = 29 * result + state.hashCode();
+    result = 29 * result + (options != null ? options.hashCode() : 0);
+    result = 29 * result + functionId;
+    return result;
+  }
+
+  public Date getSortOnDate() {
+    if (this.getStartTime() == null) {
+      this.setStartTime(new Date());
+    }
+    return this.getStartTime();
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/jobpool/JobPoolModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/jobpool/JobPoolModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/jobpool/JobPoolModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/jobpool/JobPoolModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,41 @@
+/*
+* 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.core.api.jobpool;
+
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.interfaces.LokahiModel;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: JobPoolModel.java,v 1.2 2006/03/07 19:51:56 drtobes Exp $
+ */
+public class JobPoolModel extends LokahiModel<JobPool> {
+  public Collection<JobPool> getObjects() throws SQLException {
+    return new TMCSet<JobPool>(JobPool.getJobPools());
+  }
+
+  public JobPool getObject(int id) throws SQLException {
+    return JobPool.getJobPool(id);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPool.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPool.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPool.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPool.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,494 @@
+/*
+* 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.core.api.pool;
+
+import lokahi.core.api.environment.Environment;
+import lokahi.core.api.ipaddress.Ip;
+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.pool.ApachePool;
+import lokahi.httpd.api.worker.ApacheWorker;
+import lokahi.tomcat.api.entity.TomcatContext;
+import lokahi.tomcat.api.pool.TomcatPool;
+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: HostingPool.java,v 1.4 2006/03/07 20:18:48 drtobes Exp $
+ */
+public class HostingPool extends TMCDao<HostingPool> implements Pool {
+  static final Logger logger = Logger.getLogger(HostingPool.class);
+  private static final TMCBroker<HostingPool> broker = new BrokerFactory<HostingPool>().getBroker();
+  private int id;
+  private String name;
+  private int apachePoolId = -1;
+  private int tomcatPoolId = -1;
+  private int envId;
+  private Collection<VirtualHost> virtualhosts;
+
+  private Environment env;
+  private TomcatPool tomcatPool;
+  private ApachePool apachePool;
+
+  public int getPk() {
+    return id;
+  }
+
+  public void setPk(int id) {
+    this.id = id;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public int getApachePoolId() {
+    return apachePoolId;
+  }
+
+  public void setApachePoolId(int apachePoolId) {
+    this.apachePoolId = apachePoolId;
+  }
+
+  public int getTomcatPoolId() {
+    return tomcatPoolId;
+  }
+
+  public void setTomcatPoolId(int tomcatPoolId) {
+    this.tomcatPoolId = tomcatPoolId;
+  }
+
+  public int getEnvId() {
+    return envId;
+  }
+
+  public void setEnvId(int envId) {
+    this.envId = envId;
+  }
+
+  public Environment getEnv() {
+    if (env == null) {
+      try {
+        env = new Environment().get(this.getEnvId());
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return env;
+  }
+
+  public void setEnv(Environment env) {
+    if (env != null) this.envId = env.getPk();
+    this.env = env;
+  }
+
+  public TomcatPool getTomcatPool() {
+    if (tomcatPool == null && tomcatPoolId > 0) {
+      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 (this.tomcatPool != null) {
+      this.setTomcatPoolId(this.tomcatPool.getPk());
+    }
+  }
+
+  public ApachePool getApachePool() {
+    if (this.apachePool == null && apachePoolId > 0) {
+      try {
+        this.apachePool = ApachePool.getApachePool(this.getApachePoolId());
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return apachePool;
+  }
+
+  public void setApachePool(ApachePool apachePool) {
+    this.apachePool = apachePool;
+    if (apachePool != null) {
+      this.setApachePoolId(apachePool.getPk());
+    }
+  }
+
+  public Collection<VirtualHost> getVirtualhosts() {
+    if (virtualhosts == null) {
+      try {
+        this.virtualhosts = VirtualHost.getVirtualHosts(this);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return virtualhosts;
+  }
+
+  public void setVirtualhosts(Collection<VirtualHost> virtualhosts) {
+    this.virtualhosts = virtualhosts;
+  }
+
+  public HostingPool() {
+  }
+
+  public HostingPool(String name, ApachePool apachePool, TomcatPool tomcatPool, Environment env) {
+    this.setName(name);
+    this.setApachePool(apachePool);
+    this.setTomcatPool(tomcatPool);
+    this.setEnv(env);
+  }
+
+  public HostingPool(int id, String name, int apachePoolid, int tomcatPoolId, int envId) {
+    this.setPk(id);
+    this.setName(name);
+    this.setApachePoolId(apachePoolid);
+    this.setTomcatPoolId(tomcatPoolId);
+    this.setEnvId(envId);
+  }
+
+  public HostingPool fillObject(ResultSet r) throws SQLException {
+    return new HostingPool(r.getInt("HOSTING_POOL_ID"),
+                           r.getString("HOSTING_POOL_NAME"),
+                           r.getInt("AM_POOL_ID"),
+                           r.getInt("TM_POOL_ID"),
+                           r.getInt("ENV_ID"));
+  }
+
+  /** This method returns true if the vhost name does not already exist in any of our apache workers vhost tables. */
+  public boolean isVhostAllowed(VirtualHost vh) {
+    boolean ret = false;
+    Collection<VirtualHost> vhosts = new TMCSet<VirtualHost>();
+    ApachePool ap = this.getApachePool();
+    if (ap != null) {
+      try {
+        vhosts = ap.getAllVirtualhosts();
+        ret = true;
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    Iterator i = vhosts.iterator();
+    while (i.hasNext() && ret) {
+      VirtualHost vhost = (VirtualHost) i.next();
+      if (vhost.getName().equals(vh.getName()) && vh.getPk() != vhost.getPk()) {
+        ret = false;
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input vh=" + vh + " Returning ret=" + ret);
+    }
+    return ret;
+  }
+
+  public boolean isVhostAllowed(String vhostName) {
+    boolean ret = false;
+    Collection<VirtualHost> vhosts = new TMCSet<VirtualHost>();
+    ApachePool ap = this.getApachePool();
+    if (ap != null) {
+      try {
+        vhosts = ap.getAllVirtualhosts();
+        ret = true;
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    Iterator i = vhosts.iterator();
+    while (i.hasNext() && ret) {
+      VirtualHost vhost = (VirtualHost) i.next();
+      if (vhost.getName().equals(vhostName)) {
+        ret = false;
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input name=" + vhostName + " Returning ret=" + ret);
+    }
+    return ret;
+  }
+
+  /** This method returns true if the vhost name does not already exist in any of our apache workers vhost tables. */
+  public boolean isContextAllowed(TomcatContext tc) {
+    boolean ret = false;
+    if (this.getTomcatPool() != null) {
+      Collection<TomcatContext> currentContexts = this.getTomcatPool().getAllContextsInPool();
+      Iterator i = currentContexts.iterator();
+      ret = true;
+      while (i.hasNext() && ret) {
+        TomcatContext currentTC = (TomcatContext) i.next();
+        if (tc.getName().equals(currentTC.getName()) && tc.getPk() != currentTC.getPk()) {
+          ret = false;
+        }
+      }
+    }
+    return ret;
+  }
+
+  public boolean isContextAllowed(Collection<TomcatContext> c) {
+    boolean ret = true;
+    if (c != null) {
+      for (final TomcatContext tc : c) {
+        ret = ret && this.isContextAllowed(tc);
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input c=" + c + " Returning ret=" + ret);
+    }
+    return ret;
+  }
+
+  public boolean isContextAllowed(String contextName) {
+    boolean ret = false;
+    if (this.getTomcatPool() != null) {
+      Collection<TomcatContext> currentContexts = this.getTomcatPool().getAllContextsInPool();
+      Iterator i = currentContexts.iterator();
+      ret = true;
+      while (i.hasNext() && ret) {
+        TomcatContext currentTC = (TomcatContext) i.next();
+        if (currentTC.getName().equals(contextName)) {
+          ret = false;
+        }
+      }
+    }
+    return ret;
+  }
+
+  public boolean isContextAllowed(String[] names) {
+    boolean ret = true;
+    for (final String cName : names) {
+      ret = ret && this.isContextAllowed(cName);
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input names=" + names + " Returning ret=" + ret);
+    }
+    return ret;
+  }
+
+  public Collection<Ip> getIps() {
+    ApachePool ap = this.getApachePool();
+    Collection<Ip> c = null;
+    if (ap != null) {
+      Collection<Hardware> hard = ap.getHardwares();
+      if (hard != null) {
+        c = new TMCSet<Ip>();
+        for (final Hardware h : hard) {
+          if (h != null) {
+            Collection<Ip> ips = h.getIps();
+            if (ips != null) {
+              c.addAll(ips);
+            }
+          }
+        }
+      }
+    }
+    return c;
+  }
+
+  static void update(HostingPool hp) throws SQLException {
+    broker.update("core.pool.update", hp.getPk(), hp.getName(), hp.getEnvId(), hp.getApachePoolId(), hp.getTomcatPoolId());
+  }
+
+  static HostingPool store(HostingPool hp) throws SQLException {
+    hp.setPk(broker.save("core.pool.store", hp.getName(), hp.getEnvId(), hp.getApachePoolId(), hp.getTomcatPoolId()));
+    return hp;
+  }
+
+  static boolean delete(HostingPool hp) throws SQLException {
+    return broker.delete("core.pool.delete", hp.getPk());
+  }
+
+  public static HostingPool getHostingPool(int id, boolean fromCache) throws SQLException {
+    HostingPool h = new HostingPool();
+    return h.get(id);
+  }
+
+  public static Collection<HostingPool> getHostingPools() throws SQLException {
+    Collection<HostingPool> c;
+    try {
+      c = broker.getObjects(HostingPool.class, true, "core.pool.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<HostingPool> getHostingPools(ApacheWorker aw, boolean fromCache) throws SQLException {
+    Collection<HostingPool> c;
+    try {
+      c = broker.getObjects(HostingPool.class, fromCache, "core.pool.by.apacheworker", aw.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<HostingPool> getHostingPools(String name, boolean fromCache) throws SQLException {
+    Collection<HostingPool> c;
+    try {
+      c = broker.getObjects(HostingPool.class, fromCache, "core.pool.by.name", name);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<HostingPool> getHostingPools(ApachePool ap, boolean fromCache) throws SQLException {
+    Collection<HostingPool> c;
+    try {
+      c = broker.getObjects(HostingPool.class, fromCache, "core.pool.by.apachepool", ap.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<HostingPool> getHostingPools(TomcatPool tp, boolean fromCache) throws SQLException {
+    Collection<HostingPool> c;
+    try {
+      c = broker.getObjects(HostingPool.class, fromCache, "core.pool.by.tomcatpool", tp.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") + "hostingpool/" + this.getPk() + '/');
+    return elementBuilder("hostingpool", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<hostingpool>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(this.getEnv().buildShortXMLRepresentation());
+    ret.append(this.getApachePool().buildShortXMLRepresentation());
+    ret.append(this.getTomcatPool().buildShortXMLRepresentation());
+    for (final VirtualHost vh : this.getVirtualhosts()) {
+      ret.append(vh.buildShortXMLRepresentation());
+    }
+    ret.append("</hostingpool>\n");
+    return ret;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof HostingPool)) return false;
+
+    final HostingPool hostingPool = (HostingPool) o;
+
+    if (apachePoolId != hostingPool.apachePoolId) return false;
+    if (envId != hostingPool.envId) return false;
+    if (id != hostingPool.id) return false;
+    if (tomcatPoolId != hostingPool.tomcatPoolId) return false;
+    if (name != null ? !name.equals(hostingPool.name) : hostingPool.name != null) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = id;
+    result = 29 * result + (name != null ? name.hashCode() : 0);
+    result = 29 * result + apachePoolId;
+    result = 29 * result + tomcatPoolId;
+    result = 29 * result + envId;
+    return result;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("HostingPool");
+    buf.append("{id=").append(id);
+    buf.append(",name=").append(name);
+    buf.append(",apachePoolId=").append(apachePoolId);
+    buf.append(",tomcatPoolId=").append(tomcatPoolId);
+    buf.append(",envId=").append(envId);
+    buf.append(",env=").append(env);
+    buf.append(",tomcatPool=").append(tomcatPool);
+    buf.append(",apachePool=").append(apachePool);
+    buf.append('}');
+    return buf.toString();
+  }
+
+  public HostingPool get(int id) throws SQLException {
+    HostingPool h;
+    try {
+      h = broker.getObject(HostingPool.class, "core.pool.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return h;
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPoolModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPoolModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPoolModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/pool/HostingPoolModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,300 @@
+/*
+* 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.core.api.pool;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.job.Job;
+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.TMCException;
+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.httpd.api.pool.ApachePool;
+import lokahi.httpd.api.pool.ApachePoolModel;
+import lokahi.httpd.api.worker.ApacheWorker;
+import lokahi.tomcat.api.pool.TomcatPool;
+import lokahi.tomcat.api.pool.TomcatPoolModel;
+import org.apache.log4j.Logger;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: HostingPoolModel.java,v 1.4 2006/03/07 20:18:48 drtobes Exp $
+ */
+public class HostingPoolModel extends LokahiModel<HostingPool> {
+  static final Logger logger = Logger.getLogger(HostingPoolModel.class);
+
+  public void addHostingPool(User u, HostingPool i, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, i.getName());
+    if (u.isAllowed(f) && "AddHostingPool".equals(f.getCommand())) {
+      HostingPool.store(i);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteHostingPool(User u, Collection<HostingPool> c, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u);
+    StringBuilder options = new StringBuilder();
+    if (u.isAllowed(f) && "DeleteHostingPool".equals(f.getCommand())) {
+      for (final HostingPool hp : c) {
+        HostingPool.delete(hp);
+        options.append(hp.getName());
+        options.append(',');
+      }
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setOptions(options.toString());
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteHostingPool(User u, HostingPool i, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    if (i == null || i.getPk() < 1) throw new TMCIllegalArgumentException("Project can not be null");
+    this.jpInit(f, u, i.getName());
+    if (u.isAllowed(f) && "DeleteHostingPool".equals(f.getCommand())) {
+      HostingPool.delete(i);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void updateHostingPool(User u, HostingPool i, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, i.getName());
+    if (u.isAllowed(f) && "UpdateHostingPool".equals(f.getCommand())) {
+      if (i.getPk() > 1 && i.getName() != null && !"".equals(i.getName())) {
+        HostingPool.update(i);
+      } else {
+        throw new TMCIllegalArgumentException("Hosting Pool 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 addVirtualHost(VirtualHost vh, JobPool jobPool, HostingPool hp, User u) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    vh.setHostingPool(hp);
+    Function f = Function.getFunction("DeployApacheConfig");
+    if (hp.isContextAllowed(vh.getContexts()) && hp.isVhostAllowed(vh)) {
+      VirtualHost.update(vh);
+      this.buildDeploy(vh, hp, jobPool, f, u);
+    }
+  }
+
+  private void buildDeploy(VirtualHost vh, HostingPool hp, JobPool jobPool, Function f, User u) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    String errorLog = null;
+    String customLog = null;
+    if (vh != null) {
+      errorLog = vh.getLogDirective("errorLog");
+      customLog = vh.getLogDirective("customlog");
+    }
+    if (hp != null && hp.getApachePool() != null && hp.getApachePool().getApaches() != null)
+      for (final ApacheWorker aw : hp.getApachePool().getApaches()) {
+        this.buildDeploy(aw, jobPool, errorLog, customLog);
+      }
+    if (hp != null && hp.getTomcatPool() != null) {
+      TomcatPoolModel tpm = new TomcatPoolModel();
+      tpm.deployTomcatConfig(u, null, jobPool, hp.getTomcatPool(), f);
+    }
+  }
+
+  private void buildDeploy(ApacheWorker aw, JobPool jobPool, String errorLog, String customLog) throws SQLException, IOException {
+    Job j1 = null;
+    if (errorLog != null && !"".equals(errorLog)) {
+      j1 = new Job(errorLog, aw.getHardware(), Function.getFunction("MakeDir"), jobPool);
+      j1 = Job.store(j1);
+    }
+    Job j2;
+    if (customLog != null && !"".equals(customLog)) {
+      if (j1 != null) {
+        j2 = new Job(j1, customLog, aw.getHardware(), Function.getFunction("MakeDir"), jobPool);
+      } else {
+        j2 = new Job(customLog, aw.getHardware(), Function.getFunction("MakeDir"), jobPool);
+      }
+      j2 = Job.store(j2);
+    } else {
+      j2 = j1;
+    }
+    Runnable task = new BuildDeployApacheConfig(j2, aw, jobPool, true);
+    tpe.execute(task);
+
+  }
+
+  public void checkLogAndDeploy(Project p, User u, JobPool jobPool, HostingPool hp, VirtualHost vh) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.buildDeploy(vh, hp, jobPool, Function.getFunction("DeployApacheConfig"), u);
+  }
+
+  public void deployApacheConfs(Project p, User u, JobPool jobPool, Collection<HostingPool> c) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    for (final HostingPool hp : c) {
+      this.deployApacheConfs(null, u, jp, hp);
+    }
+  }
+
+  public void deployApacheConfs(Project p, User u, JobPool jobPool, HostingPool hp) throws SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    this.buildDeploy(null, hp, jobPool, Function.getFunction("DeployApacheConfig"), u);
+  }
+
+  public void updateTomcat(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.updateTomcat(u, p, hp.getTomcatPool());
+  }
+
+  public void restartTomcat(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.restartTomcat(u, p, hp.getTomcatPool());
+  }
+
+  public void startTomcat(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.startTomcat(u, p, hp.getTomcatPool());
+  }
+
+  public void stopTomcat(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.stopTomcat(u, p, hp.getTomcatPool());
+  }
+
+  public void updateApache(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    ApachePoolModel apm = new ApachePoolModel();
+    apm.updateApache(u, p, hp.getApachePool());
+  }
+
+  public void gracefulApache(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    ApachePoolModel apm = new ApachePoolModel();
+    apm.gracefulApache(u, p, hp.getApachePool());
+  }
+
+  public void configtestApache(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    ApachePoolModel apm = new ApachePoolModel();
+    apm.configtestApache(u, p, hp.getApachePool());
+  }
+
+  public void restartApache(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    ApachePoolModel apm = new ApachePoolModel();
+    apm.restartApache(u, p, hp.getApachePool());
+  }
+
+  public void startApache(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    ApachePoolModel apm = new ApachePoolModel();
+    apm.startApache(u, p, hp.getApachePool());
+  }
+
+  public void stopApache(User u, Project p, HostingPool hp) throws NoSuchMethodException, SQLException, IOException, AuthorizationException, TMCIllegalArgumentException {
+    ApachePoolModel apm = new ApachePoolModel();
+    apm.stopApache(u, p, hp.getApachePool());
+  }
+
+  public void gracefulApache(User u, Collection<HostingPool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    ApachePoolModel apm = new ApachePoolModel();
+    Collection<ApachePool> aps = this.getApachePools(c);
+    apm.gracefulApache(u, aps, f);
+  }
+
+  public void configtestApache(User u, Collection<HostingPool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    ApachePoolModel apm = new ApachePoolModel();
+    Collection<ApachePool> aps = this.getApachePools(c);
+    apm.configtestApache(u, aps, f);
+  }
+
+  public void restartApache(User u, Collection<HostingPool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    ApachePoolModel apm = new ApachePoolModel();
+    Collection<ApachePool> aps = this.getApachePools(c);
+    apm.restartApache(u, aps, f);
+  }
+
+  public void startApache(User u, Collection<HostingPool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    ApachePoolModel apm = new ApachePoolModel();
+    Collection<ApachePool> aps = this.getApachePools(c);
+    apm.startApache(u, aps, f);
+  }
+
+  public void stopApache(User u, Collection<HostingPool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    ApachePoolModel apm = new ApachePoolModel();
+    Collection<ApachePool> aps = this.getApachePools(c);
+    apm.stopApache(u, aps, f);
+  }
+
+  public void deployApacheConfig(User u, Collection<HostingPool> c, Function f) throws NoSuchMethodException, SQLException, IOException, TMCIllegalArgumentException, AuthorizationException {
+    ApachePoolModel apm = new ApachePoolModel();
+    Collection<ApachePool> aps = this.getApachePools(c);
+    apm.deployApacheConfig(u, aps, f);
+  }
+
+  public void stopTomcat(User u, Collection<HostingPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.stopTomcat(u, this.getTomcatPools(c), f);
+  }
+
+  public void startTomcat(User u, Collection<HostingPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.startTomcat(u, this.getTomcatPools(c), f);
+  }
+
+  public void restartTomcat(User u, Collection<HostingPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.restartTomcat(u, this.getTomcatPools(c), f);
+  }
+
+  public void deployTomcatConfig(User u, Collection<HostingPool> c, Function f) throws TMCException, TMCIllegalArgumentException, SQLException, NoSuchMethodException, IOException {
+    TomcatPoolModel tpm = new TomcatPoolModel();
+    tpm.deployTomcatConfig(u, this.getTomcatPools(c), f);
+  }
+
+  private Collection<ApachePool> getApachePools(Collection<HostingPool> hps) {
+    Collection<ApachePool> aps = new TMCSet<ApachePool>();
+    for (HostingPool hp : hps) {
+      aps.add(hp.getApachePool());
+    }
+    return aps;
+  }
+
+  private Collection<TomcatPool> getTomcatPools(Collection<HostingPool> hps) {
+    Collection<TomcatPool> aps = new TMCSet<TomcatPool>();
+    for (HostingPool hp : hps) {
+      aps.add(hp.getTomcatPool());
+    }
+    return aps;
+  }
+
+  public Collection<HostingPool> getObjects() throws SQLException {
+    return new TMCSet<HostingPool>(HostingPool.getHostingPools());
+  }
+
+  public HostingPool getObject(int id) throws SQLException {
+    return HostingPool.getHostingPool(id, true);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/Project.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/Project.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/Project.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/Project.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.core.api.project;
+
+import lokahi.core.api.environment.Environment;
+import lokahi.core.api.jobpool.JobPool;
+import lokahi.core.api.user.User;
+import lokahi.core.common.authorization.Global;
+import lokahi.core.common.collection.TMCCollectionImpl;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.Dao;
+import lokahi.core.common.interfaces.FunctionableEntity;
+import lokahi.core.common.interfaces.TMCBroker;
+import lokahi.core.common.util.DateComparator;
+import lokahi.core.common.util.PropertiesFile;
+import lokahi.httpd.api.entity.VirtualHost;
+import lokahi.tomcat.api.entity.TomcatContext;
+import org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.SortedSet;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: Project.java,v 1.3 2006/03/07 22:05:23 drtobes Exp $
+ */
+public class Project extends FunctionableEntity<Project> {
+  static final Logger logger = Logger.getLogger(Project.class);
+  private static final int objectId = 2;
+  private static final TMCBroker<Project> broker = new BrokerFactory<Project>().getBroker();
+
+  private int projectId;
+  private String projectName;
+
+  private Collection<VirtualHost> virtualHosts = null;
+  private Collection<TomcatContext> tomcatContexts = null;
+
+  public int getObjectId() {
+    return objectId;
+  }
+
+  public Project() {
+  }
+
+  public Project(int projectId, String projectName) {
+    this.setPk(projectId);
+    this.setName(projectName);
+  }
+
+  public Project(String projectName) {
+    this.projectName = projectName;
+  }
+
+  public int getPk() {
+    return projectId;
+  }
+
+  public void setPk(int pk) {
+    this.projectId = pk;
+  }
+
+  public String getName() {
+    return this.projectName;
+  }
+
+  public void setName(String name) {
+    this.projectName = name;
+  }
+
+  public Project get(int id) throws SQLException {
+    Project p;
+    try {
+      p = broker.getObject(Project.class, "core.project.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 p=" + p);
+    }
+    return p;
+  }
+
+  public FunctionableEntity getParent() {
+    return new Global();
+  }
+
+  public Collection<VirtualHost> getVirtualHostsNoDB() {
+    if (this.virtualHosts == null) {
+      this.getVirtualHosts();
+    }
+    return this.virtualHosts;
+  }
+
+  public Collection<VirtualHost> getVirtualHosts() {
+    if (this.virtualHosts == null) {
+      this.virtualHosts = new TMCSet<VirtualHost>();
+      try {
+        this.virtualHosts.addAll(VirtualHost.getVirtualHosts(this.getPk()));
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return this.virtualHosts;
+  }
+
+  public boolean addVirtualHost(VirtualHost vh) {
+    Collection<VirtualHost> c = new TMCSet<VirtualHost>();
+    c.add(vh);
+    boolean ret = this.addVirtualHosts(c);
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public boolean addVirtualHosts(Collection<VirtualHost> c) {
+    Collection<VirtualHost> vhosts = this.getVirtualHosts();
+    if (vhosts == null) {
+      vhosts = new TMCSet<VirtualHost>();
+    }
+    vhosts.addAll(c);
+    this.setVirtualHosts(vhosts);
+    boolean ret = this.getVirtualHosts().containsAll(c);
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public void setVirtualHosts(Collection<VirtualHost> virtualHosts) {
+    this.virtualHosts = virtualHosts;
+  }
+
+  public Collection<User> getUsers() {
+    Collection<User> c = null;
+    try {
+      c = User.getUsers(this);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return c;
+  }
+
+  private Collection<TomcatContext> getTomcatContextsNoDB() {
+    if (this.tomcatContexts == null) {
+      this.getTomcatContexts();
+    }
+    return tomcatContexts;
+  }
+
+  public Collection<TomcatContext> getTomcatContexts() {
+    if (this.tomcatContexts == null) {
+      this.tomcatContexts = new TMCSet<TomcatContext>();
+      try {
+        this.tomcatContexts.addAll(TomcatContext.getTomcatContexts(this.getPk()));
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return tomcatContexts;
+  }
+
+  public void removeTomcatContext(TomcatContext tc) {
+    Collection<TomcatContext> tcs = this.getTomcatContexts();
+    if (tcs != null && tcs.contains(tc)) {
+      tcs.remove(tc);
+      this.setTomcatContexts(tcs);
+    }
+  }
+
+  public boolean addTomcatContexts(TomcatContext tc) {
+    Collection<TomcatContext> c = new TMCSet<TomcatContext>();
+    c.add(tc);
+    return this.addTomcatContexts(c);
+  }
+
+  public boolean addTomcatContexts(Collection<TomcatContext> c) {
+    Collection<TomcatContext> contexts = this.getTomcatContexts();
+    if (contexts == null) {
+      contexts = new TMCSet<TomcatContext>();
+    }
+    contexts.addAll(c);
+    this.setTomcatContexts(contexts);
+    boolean ret = this.getTomcatContexts().containsAll(c);
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public Collection getEnvs() {
+    Collection<Environment> c = new TMCSet<Environment>();
+    try {
+      c.addAll(Environment.getEnviroments(this));
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return c;
+  }
+
+  public void setTomcatContexts(Collection<TomcatContext> tomcatContexts) {
+    this.tomcatContexts = tomcatContexts;
+  }
+
+  public SortedSet<JobPool> getJobPools() {
+    TMCCollectionImpl<JobPool> col = null;
+    try {
+      col = new TMCCollectionImpl<JobPool>(JobPool.getJobPools(this));
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    SortedSet<JobPool> s = null;
+    if (col != null) {
+      s = col.getSortedSetByDate(new DateComparator<JobPool>(), 10);
+    }
+    return s;
+  }
+
+  /**
+   * @param id
+   *
+   * @return
+   * @throws SQLException
+   * @deprecated
+   */
+  public static Project getProject(int id) throws SQLException {
+    Project p = new Project();
+    p = p.get(id);
+    return p;
+  }
+
+  public static Collection<Project> getProjects(String name) throws SQLException {
+    TMCCollectionImpl<Project> c = new TMCCollectionImpl<Project>();
+    try {
+      c.addAll(broker.getObjects(Project.class, false, "core.project.search", name));
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input " + name + " Returning c=" + c);
+    }
+    return c.orderByName().values();
+  }
+
+  public static Collection<Project> getProjects(User u) throws SQLException {
+    Collection<Project> c;
+    try {
+      c = broker.getObjects(Project.class,
+                            false, "core.project.by.user",
+                            u.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input " + u + " Returning c=" + c);
+    }
+    return c;
+  }
+
+  public static Collection<Project> getProjects(TomcatContext tc) throws SQLException {
+    Collection<Project> c;
+    try {
+      c = broker.getObjects(Project.class,
+                            false, "core.project.by.context",
+                            tc.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input " + tc + " Returning c=" + c);
+    }
+    return c;
+  }
+
+  public static Collection<Project> getProjects(VirtualHost vh) throws SQLException {
+    Collection<Project> c;
+    try {
+      c = broker.getObjects(Project.class,
+                            false, "core.project.by.vhost",
+                            vh.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input " + vh + " Returning c=" + c);
+    }
+    return c;
+  }
+
+  public static Collection<Project> getProjects() throws SQLException {
+    Collection<Project> c;
+    try {
+      c = broker.getObjects(Project.class,
+                            false, "core.project.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<Project> getRecentProjects(User u) throws SQLException {
+    Collection<Project> c;
+    try {
+      c = broker.getObjects(Project.class,
+                            false, "core.project.recent",
+                            u.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("c=" + c);
+    }
+    return c;
+
+  }
+
+  public static boolean update(Project p) throws SQLException {
+    if (logger.isDebugEnabled()) {
+      logger.debug("p=" + p);
+    }
+    updateGroups(p);
+    broker.update("core.project.update", p.getPk(), p.getName());
+    return true;
+  }
+
+  private static void updateGroups(Project p) throws SQLException {
+    Collection<TomcatContext> newContexts = p.getTomcatContextsNoDB();
+    Collection<VirtualHost> newVhosts = p.getVirtualHostsNoDB();
+    p.setTomcatContexts(null);
+    p.setVirtualHosts(null);
+    Collection<TomcatContext> oldContexts = p.getTomcatContexts();
+    Collection<VirtualHost> oldVhosts = p.getVirtualHosts();
+    diffCollections(p, oldContexts, newContexts, "context");
+    diffCollections(p, oldVhosts, newVhosts, "vhost");
+
+  }
+
+  private static <T extends FunctionableEntity> void diffCollections(Project p, Collection<T> oldObjects, Collection<T> newObjects, String type) throws SQLException {
+    Collection<T> toRemove = new TMCSet<T>();
+    Collection<T> toAdd = new TMCSet<T>();
+    toRemove.addAll(oldObjects);
+    toRemove.removeAll(newObjects);
+    toAdd.addAll(newObjects);
+    toAdd.removeAll(oldObjects);
+    modifyRel(p, toRemove, "core.project.rel." + type + ".delete");
+    modifyRel(p, toAdd, "core.project.rel." + type + ".store");
+  }
+
+  private static <T extends FunctionableEntity> void modifyRel(Project p, Collection<T> c, String statement) throws SQLException {
+    for (final T t : c) {
+      broker.modifyRelationship(statement, p.getPk(), t.getPk());
+    }
+  }
+
+  public boolean containsObject(Dao object) {
+    return this.getTomcatContexts().contains(object) || this.getVirtualHosts().contains(object);
+  }
+
+  public static Project store(Project p) throws SQLException {
+    updateGroups(p);
+    p.setPk(broker.save("core.project.store", p.getName()));
+    return p;
+  }
+
+  static boolean delete(Collection<Project> c) throws SQLException {
+    boolean ret = true;
+    if (c != null) {
+      for (final Project p : c) {
+        ret = ret && delete(p);
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  static boolean delete(Project p) throws SQLException {
+    return broker.delete("core.project.delete", p.getPk());
+  }
+
+  public Project fillObject(ResultSet r) throws SQLException {
+    return new Project(r.getInt("PROJECT_ID"),
+                       r.getString("PROJECT_NAME"));
+  }
+
+  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") + "project/" + this.getPk() + '/');
+    return elementBuilder("project", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<project>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    if (this.getVirtualHosts() != null)
+      for (final VirtualHost vh : this.getVirtualHosts()) {
+        ret.append(vh.buildShortXMLRepresentation());
+      }
+    if (this.getTomcatContexts() != null)
+      for (final TomcatContext tc : this.getTomcatContexts()) {
+        ret.append(tc.buildShortXMLRepresentation());
+      }
+    if (this.getUsers() != null)
+      for (final User u : this.getUsers()) {
+        ret.append(u.buildShortXMLRepresentation());
+      }
+    ret.append("</project>\n");
+    return ret;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof Project)) return false;
+    final Project project = (Project) o;
+    if (projectId != project.projectId) return false;
+    if (projectName != null ? !projectName.equals(project.projectName) : project.projectName != null) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = projectId;
+    result = 29 * result + (projectName != null ? projectName.hashCode() : 0);
+    return result;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("Project");
+    buf.append("{projectId=").append(projectId);
+    buf.append(",projectName=").append(projectName);
+    buf.append(",virtualHosts=").append(virtualHosts);
+    buf.append(",tomcatContexts=").append(tomcatContexts);
+    buf.append('}');
+    return buf.toString();
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/ProjectModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/ProjectModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/ProjectModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/project/ProjectModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,115 @@
+/*
+* 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.core.api.project;
+
+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.collection.TMCSet;
+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: ProjectModel.java,v 1.3 2006/03/07 20:18:48 drtobes Exp $
+ */
+public class ProjectModel extends LokahiModel<Project> {
+
+  public void addProject(User u, Project i, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, i.getName());
+    if (u.isAllowed(f) && "AddProject".equals(f.getCommand())) {
+      i = Project.store(i);
+      if (i != null) {
+        jp.setProject(i);
+      }
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteProject(User u, Collection<Project> c, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    this.jpInit(f, u);
+    StringBuffer options = new StringBuffer();
+    if (u.isAllowed(f)) {
+      for (Project p : c) {
+        options.append(p.getName());
+        options.append(',');
+        Project.delete(p);
+      }
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    jp.setOptions(options.toString());
+    JobPool.update(jp);
+  }
+
+  public void deleteProject(User u, Project i, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    if (i == null || i.getPk() < 1) throw new TMCIllegalArgumentException("Project can not be null");
+    this.jpInit(f, u, i.getName());
+    if (u.isAllowed(f) && "DeleteProject".equals(f.getCommand())) {
+      Project.delete(i);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void updateProject(User u, Project i, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, i.getName());
+    if (u.isAllowed(f) && "UpdateProject".equals(f.getCommand())) {
+      if (i.getPk() > 1 && i.getName() != null && !"".equals(i.getName())) {
+        Project.update(i);
+      } else {
+        throw new TMCIllegalArgumentException("Project can not be null");
+      }
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public Project viewProject(int id) throws SQLException {
+    return Project.getProject(id);
+  }
+
+  public Collection<Project> viewProjects() throws SQLException {
+    return Project.getProjects();
+  }
+
+  public Collection<Project> getObjects() throws SQLException {
+    return new TMCSet<Project>(this.viewProjects());
+  }
+
+  public Project getObject(int id) throws SQLException {
+    return this.viewProject(id);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/server/Hardware.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/server/Hardware.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/server/Hardware.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/server/Hardware.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,566 @@
+/*
+* 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.core.api.server;
+
+import lokahi.core.api.environment.Environment;
+import lokahi.core.api.instance.Instance;
+import lokahi.core.api.ipaddress.Ip;
+import lokahi.core.api.state.State;
+import lokahi.core.common.collection.TMCCollectionImpl;
+import lokahi.core.common.collection.TMCSet;
+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 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: Hardware.java,v 1.4 2006/03/07 20:18:49 drtobes Exp $
+ */
+public class Hardware extends TMCDao<Hardware> implements Server {
+  static final Logger logger = Logger.getLogger(Hardware.class);
+  private static final TMCBroker<Hardware> broker = new BrokerFactory<Hardware>().getBroker();
+  private String name;
+  private String physicalLocation;
+  private String comments;
+  private int id;
+  private int instanceId;
+  private int envId;
+  private int statusId;
+  private int defaultIpId;
+
+  private Collection<Ip> ipAddresses;
+
+  private Instance instance;
+  private Ip defaultIp;
+  private State status;
+  private Environment env;
+
+  public int getPk() {
+    return this.id;
+  }
+
+  public void setPk(int pk) {
+    this.id = pk;
+  }
+
+  public String getServerName() {
+    return this.name;
+  }
+
+  public String getName() {
+    return this.getInstance().getName() + ':' + this.name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public int getInstanceId() {
+    return instanceId;
+  }
+
+  public void setInstanceId(int instanceId) {
+    this.instanceId = instanceId;
+  }
+
+  public int getEnvId() {
+    return envId;
+  }
+
+  public void setEnvId(int envId) {
+    this.envId = envId;
+  }
+
+  public Collection<Ip> getIpAddresses() {
+    if (ipAddresses == null) {
+      try {
+        ipAddresses = Ip.getIps(this.getPk(), true);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return ipAddresses;
+  }
+
+  private Collection<Ip> getIpAddressesNoDb() {
+    return ipAddresses;
+  }
+
+  public Collection<Ip> getIps() {
+    Collection<Ip> c;
+    if (this.getIpAddresses() != null) {
+      c = this.getIpAddresses();
+    } else {
+      c = new TMCSet<Ip>();
+    }
+    c.add(this.getDefaultIp());
+    return c;
+  }
+
+  public TMCCollectionImpl<Ip> getIpCollection() {
+    TMCCollectionImpl<Ip> c = new TMCCollectionImpl<Ip>();
+    if (this.getIpAddresses() != null) {
+      c.addAll(this.getIpAddresses());
+    }
+    c.add(this.getDefaultIp());
+    return c;
+  }
+
+  public boolean isActive() {
+    return this.status != State.INACTIVE;
+  }
+
+  public void setIpAddresses(Collection<Ip> ipAddresses) {
+    this.ipAddresses = ipAddresses;
+  }
+
+  public String getPhysicalLocation() {
+    return physicalLocation;
+  }
+
+  public void setPhysicalLocation(String physicalLocation) {
+    this.physicalLocation = physicalLocation;
+  }
+
+  public int getStatusId() {
+    return statusId;
+  }
+
+  public void setStatusId(int statusId) {
+    this.statusId = statusId;
+  }
+
+  public int getDefaultIpId() {
+    return defaultIpId;
+  }
+
+  public void setDefaultIpId(int defaultIpId) {
+    this.defaultIpId = defaultIpId;
+  }
+
+  public String getComments() {
+    return comments;
+  }
+
+  public void setComments(String comments) {
+    this.comments = comments;
+  }
+
+  public State getStatus() {
+    if (status == null) {
+      this.status = State.getState(this.getStatusId());
+    }
+    return status;
+  }
+
+  public void setStatus(State status) {
+    this.status = status;
+    if (status != null) {
+      this.setStatusId(status.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 Instance getInstance() {
+    if (this.instance == null) {
+      try {
+        this.instance = Instance.getInstance(this.getInstanceId(), true);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return instance;
+  }
+
+  public void setInstance(Instance instance) {
+    this.instance = instance;
+    if (instance != null) {
+      this.setInstanceId(instance.getPk());
+    }
+  }
+
+  public Ip getDefaultIp() {
+    if (this.defaultIp == null) {
+      try {
+        this.defaultIp = Ip.getIp(this.getDefaultIpId(), true);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return defaultIp;
+  }
+
+  public void setDefaultIp(Ip defaultIp) {
+    this.defaultIp = defaultIp;
+    if (defaultIp != null) {
+      this.setDefaultIpId(defaultIp.getPk());
+    }
+  }
+
+  public Hardware() {
+  }
+
+  public Hardware(String name, String physicalLocation, String comments, int instanceId, int envId,
+                  int statusId, int defaultIpId) {
+    this.setName(name);
+    this.setInstanceId(instanceId);
+    this.setPhysicalLocation(physicalLocation);
+    this.setEnvId(envId);
+    this.setComments(comments);
+    this.setStatusId(statusId);
+    this.setDefaultIpId(defaultIpId);
+  }
+
+  public Hardware(String name, String physicalLocation, String comments, Instance instance, Environment env,
+                  State status, Ip defaultIp) {
+    this.setName(name);
+    this.setInstance(instance);
+    this.setInstanceId(this.getInstance().getPk());
+    this.setPhysicalLocation(physicalLocation);
+    this.setEnv(env);
+    this.setEnvId(this.getEnv().getPk());
+    this.setComments(comments);
+    this.setStatus(status);
+    this.setStatusId(this.getStatus().getPk());
+    this.setDefaultIp(defaultIp);
+    this.setDefaultIpId(this.getDefaultIp().getPk());
+  }
+
+  public Hardware(int id, String name, String physicalLocation, String comments, int instanceId, int envId,
+                  int statusId, int defaultIpId) {
+    this.setPk(id);
+    this.setName(name);
+    this.setInstanceId(instanceId);
+    this.setPhysicalLocation(physicalLocation);
+    this.setEnvId(envId);
+    this.setComments(comments);
+    this.setStatusId(statusId);
+    this.setDefaultIpId(defaultIpId);
+  }
+
+  private boolean storeIps() throws SQLException {
+    return this.storeIps(this.getIpAddressesNoDb());
+  }
+
+  private boolean storeIps(Collection<Ip> c) throws SQLException {
+    boolean ret = false;
+    if (c == null) {
+      c = new TMCSet<Ip>();
+    }
+    if (!c.contains(this.getDefaultIp())) {
+      c.add(this.getDefaultIp());
+    }
+    for (final Ip ip : c) {
+      broker.modifyRelationship("core.server.rel.ip.add", this.getPk(), ip.getPk());
+    }
+    this.setIpAddresses(null);
+    if (this.getIpAddresses().containsAll(c)) {
+      ret = true;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public boolean hasIp(Ip ip) {
+    boolean ret = false;
+    if (this.getIpAddresses() != null) {
+      if (this.getIpAddresses().contains(ip)) {
+        ret = true;
+      }
+    }
+    return ret;
+  }
+
+  private boolean deleteIps() throws SQLException {
+    Collection<Ip> c = this.getIpAddressesNoDb();
+    this.setIpAddresses(null);
+    Collection<Ip> old = this.getIpAddresses();
+    if (c != null)
+      old.removeAll(c);
+    return this.deleteIps(old);
+  }
+
+  private boolean deleteIps(Collection<Ip> c) throws SQLException {
+    boolean ret = false;
+    if (logger.isDebugEnabled()) {
+      logger.debug("Input c=" + c);
+    }
+    if (c != null) {
+      for (final Ip ip : c) {
+        broker.modifyRelationship("core.server.rel.ip.delete", this.getPk(), ip.getPk());
+      }
+      this.setIpAddresses(null);
+      if (logger.isDebugEnabled()) {
+        logger.debug("this.getIpAddresses() = " + this.getIpAddresses());
+        logger.debug("this.getIpAddresses().containsAll(c) = " + this.getIpAddresses().containsAll(c));
+      }
+      if (!this.getIpAddresses().containsAll(c) || c.isEmpty()) {
+        ret = true;
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  private boolean updateIps() throws SQLException {
+    Collection<Ip> newIps = this.getIpAddressesNoDb();
+    if (newIps == null) {
+      newIps = new TMCSet<Ip>();
+    }
+    this.setIpAddresses(null);
+    Collection<Ip> oldIps = this.getIpAddresses();
+    if (oldIps == null) {
+      oldIps = new TMCSet<Ip>();
+    }
+    Collection<Ip> keepers = oldIps;
+    keepers.retainAll(newIps);
+    newIps.removeAll(keepers);
+    oldIps.removeAll(keepers);
+    if (logger.isDebugEnabled()) {
+      logger.debug("keepers to " + keepers);
+      logger.debug("oldIps = " + oldIps);
+      logger.debug("newIps = " + newIps);
+    }
+    boolean ret = this.storeIps(newIps) && this.deleteIps(oldIps);
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public Hardware get(int id) throws SQLException {
+    Hardware h;
+    try {
+      h = broker.getObject(Hardware.class, "core.server.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return h;
+  }
+
+  public static Hardware getHardware(int id, boolean fromCache) throws SQLException {
+    return new Hardware().get(id);
+  }
+
+  public static Hardware getHardware(String name, Instance i, boolean fromCache) throws SQLException {
+    Hardware h;
+    try {
+      h = broker.getObject(Hardware.class, "core.server.by.name.instance", fromCache, name, i.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return h;
+  }
+
+  public static Collection<Hardware> getHardware(Environment env, boolean fromCache) throws SQLException {
+    Collection<Hardware> c;
+    try {
+      c = broker.getObjects(Hardware.class, fromCache, "core.server.by.environment", env.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<Hardware> getHardwares(String name, boolean fromCache) throws SQLException {
+    Collection<Hardware> c;
+    try {
+      c = broker.getObjects(Hardware.class, fromCache, "core.server.by.name", name);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<Hardware> getHardwares() throws SQLException {
+    Collection<Hardware> c;
+    try {
+      c = broker.getObjects(Hardware.class, true, "core.server.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  static Hardware store(Hardware h) throws SQLException {
+    h.setPk(broker.save("core.server.store", h.getServerName(), h.getPhysicalLocation(), h.getComments(),
+                        h.getInstanceId(), h.getEnvId(), h.getStatusId(), h.getDefaultIpId()));
+    Hardware ret = null;
+    if (h.storeIps()) {
+      ret = h;
+    }
+    return ret;
+  }
+
+  static void update(Hardware h) throws SQLException {
+    broker.update("core.server.update", h.getPk(), h.getServerName(), h.getPhysicalLocation(), h.getComments(),
+                  h.getInstanceId(), h.getEnvId(), h.getStatusId(), h.getDefaultIpId());
+    h.updateIps();
+  }
+
+  static boolean delete(Hardware h) throws SQLException {
+    boolean ret = broker.delete("core.server.delete", h.getPk());
+    if (ret && h.deleteIps()) {
+      ret = true;
+    }
+    return ret;
+  }
+
+  public Hardware fillObject(ResultSet r) throws SQLException {
+    return 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("STATUS_ID"),
+                        r.getInt("DEFAULT_IP_ID"));
+  }
+
+  public StringBuilder buildShortXMLRepresentation() {
+    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(4, 1);
+    map.put("id", Integer.toString(this.getPk()));
+    map.put("name", this.getName());
+    map.put("instance", Integer.toString(this.getInstanceId()));
+    map.put("href", PropertiesFile.getConstantValue("rest.servlet.url") + "hardware/" + this.getPk() + '/');
+    return elementBuilder("hardware", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<hardware>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(elementBuilder("comments", this.getComments()));
+    ret.append(elementBuilder("physicalLocation", this.getPhysicalLocation()));
+    ret.append(this.getInstance().buildShortXMLRepresentation());
+    ret.append(this.getStatus().buildShortXMLRepresentation());
+    ret.append(this.getEnv().buildShortXMLRepresentation());
+    ret.append(elementBuilder("defaultIp", this.getDefaultIp().getName()));
+    for (final Ip ip : this.getIpAddresses()) {
+      ret.append(ip.buildXMLRepresention());
+    }
+    ret.append("</hardware>\n");
+    return ret;
+  }
+
+  public String toString() {
+    return "Hardware{" +
+        "name='" + name + '\'' +
+        ", physicalLocation='" + physicalLocation + '\'' +
+        ", comments='" + comments + '\'' +
+        ", id=" + id +
+        ", instanceId=" + instanceId +
+        ", envId=" + envId +
+        ", statusId=" + statusId +
+        ", defaultIpId=" + defaultIpId +
+        ", ipAddresses=" + (ipAddresses == null ? null : "(size:" + ipAddresses.size() + ')' + ipAddresses) +
+        ", instance=" + instance +
+        ", defaultIp=" + defaultIp +
+        ", status=" + status +
+        ", env=" + env +
+        '}';
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof Hardware)) return false;
+
+    final Hardware hardware = (Hardware) o;
+
+    if (defaultIpId != hardware.defaultIpId) return false;
+    if (envId != hardware.envId) return false;
+    if (id != hardware.id) return false;
+    if (instanceId != hardware.instanceId) return false;
+    if (statusId != hardware.statusId) return false;
+    if (comments != null ? !comments.equals(hardware.comments) : hardware.comments != null) return false;
+    if (name != null ? !name.equals(hardware.name) : hardware.name != null) return false;
+    if (physicalLocation != null ? !physicalLocation.equals(hardware.physicalLocation) : hardware.physicalLocation != null)
+      return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = name != null ? name.hashCode() : 0;
+    result = 29 * result + (physicalLocation != null ? physicalLocation.hashCode() : 0);
+    result = 29 * result + (comments != null ? comments.hashCode() : 0);
+    result = 29 * result + id;
+    result = 29 * result + instanceId;
+    result = 29 * result + envId;
+    result = 29 * result + statusId;
+    result = 29 * result + defaultIpId;
+    return result;
+  }
+}
+
+
+
+
+



Mime
View raw message