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 [11/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/server/HardwareModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/server/HardwareModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/server/HardwareModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/server/HardwareModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,167 @@
+/*
+* 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.function.Function;
+import lokahi.core.api.job.Job;
+import lokahi.core.api.jobpool.JobPool;
+import lokahi.core.api.state.State;
+import lokahi.core.api.user.User;
+import lokahi.core.common.exception.AuthorizationException;
+import lokahi.core.common.exception.TMCIllegalArgumentException;
+import lokahi.core.common.interfaces.LokahiModel;
+import lokahi.tomcat.api.server.Tomcat;
+import lokahi.tomcat.api.worker.TomcatWorker;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: HardwareModel.java,v 1.5 2006/03/10 22:46:07 drtobes Exp $
+ */
+public class HardwareModel extends LokahiModel<Hardware> {
+
+  public void addHardware(User u, Hardware h, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, h.getName());
+    if (u.isAllowed(f) && "AddHardware".equals(f.getCommand())) {
+      Hardware.store(h);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void deleteHardware(User u, Hardware h, Function f) throws SQLException, AuthorizationException, TMCIllegalArgumentException {
+    if (h == null || h.getPk() < 1) throw new TMCIllegalArgumentException("Hardware can not be null");
+    this.jpInit(f, u, h.getName());
+    if (u.isAllowed(f) && "DeleteHardware".equals(f.getCommand())) {
+      Hardware.delete(h);
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void updateHardware(User u, Hardware h, Function f) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    this.jpInit(f, u, h.getName());
+    if (u.isAllowed(f) && "UpdateHardware".equals(f.getCommand())) {
+      if (h.getPk() > 1 && h.getServerName() != null && !"".equals(h.getServerName())) {
+        Hardware.update(h);
+      } else {
+        throw new TMCIllegalArgumentException("Hardware 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 deployJvmConf(User u, Collection<Hardware> c, JobPool jobPool) throws SQLException, IOException {
+    for (final Hardware h : c) {
+      this.deployJvmConf(u, h, jobPool);
+    }
+  }
+
+  public void deployJvmConf(User u, Hardware h, JobPool jobPool) throws SQLException, IOException {
+    Function f = Function.getFunction("PutFile");
+    Job j = new Job("/www/tmc/agent/jvm.conf", h, f, jobPool);
+    j.setResult(this.buildJvmConf(h));
+    Job.store(j);
+  }
+
+  public String buildJvmConf(Hardware h) throws SQLException {
+    Collection<TomcatWorker> c = TomcatWorker.getTomcatWorkers(h, false);
+    StringBuffer ret = new StringBuffer();
+    for (final TomcatWorker tw : c) {
+      Tomcat t = tw.getTomcat();
+      if (t != null)
+        ret.append(t.buildJvmconfEntry());
+    }
+    return ret.toString();
+  }
+
+  public void addHardware(User u, String hardwareName, String physicalLocation, String comments, int instanceId, int envId, int statusId, String defaultIp, String... ips) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    Hardware h = new Hardware(); //todo finish this
+    this.addHardware(u, h);
+  }
+
+  public void addHardware(User u, Hardware h) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+    Function f = Function.getFunction("AddHardware");
+    this.jpInit(f, u, h.getName());
+    if (!u.isAllowed(f)) throw new AuthorizationException(""); //todo finish this
+    this.addHardware(jp, h);
+  }
+
+  public void addHardware(JobPool jp, Hardware h) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+
+  }
+
+  public void updateHardware(User u, int id, String hardwareName, String physicalLocation, String comments, int instanceId, int envId, int statusId, String defaultIp, String... ips) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+
+  }
+
+  public void updateHardware(User u, Hardware h) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+
+  }
+
+  public void updateHardware(JobPool jp, Hardware h) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+
+  }
+
+  public void deleteHardware(User u, int[] ids) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+
+  }
+
+  public void deleteHardware(User u, Collection<Hardware> c) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+
+  }
+
+  public void deleteHardware(JobPool jp, Hardware h) throws AuthorizationException, TMCIllegalArgumentException, SQLException, IOException {
+
+  }
+
+  /** @deprecated  */
+  public Hardware viewHardware(int id) throws SQLException {
+    return Hardware.getHardware(id, true);
+  }
+
+  /**
+   * @return
+   * @throws SQLException
+   * @deprecated
+   */
+  public Collection<Hardware> viewHardwares() throws SQLException {
+    return Hardware.getHardwares();
+  }
+
+  public Collection<Hardware> getObjects() throws SQLException {
+    return Hardware.getHardwares();
+  }
+
+  public Hardware getObject(int id) throws SQLException {
+    return Hardware.getHardware(id, true);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/State.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/State.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/State.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/State.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,151 @@
+/*
+* 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.state;
+
+import lokahi.core.common.interfaces.Collectable;
+import lokahi.core.common.interfaces.Restable;
+
+import java.util.Collection;
+import java.util.EnumSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: State.java,v 1.3 2006/03/07 20:18:49 drtobes Exp $
+ */
+
+public enum State implements Collectable, Restable {
+  NEW(0, "New", "blue"),
+  RUNNING(1, "Running", "blue"),
+  CANCEL(2, "Canceled", "red"),
+  COMPLETE(4, "Complete", "green"),
+  FAILED(5, "Failed", "red"),
+  ACTIVE(8, "Active", "green"),
+  INACTIVE(9, "Inactive", "red");
+
+  private final int pk;
+  private final String name;
+  private final String color;
+
+  public int getPk() {
+    return this.pk;
+  }
+
+  public String getName() {
+    return this.name;
+  }
+
+  public String getColor() {
+    return this.color;
+  }
+
+  State(int pk, String name, String color) {
+    this.name = name;
+    this.pk = pk;
+    this.color = color;
+  }
+
+  public static State getState(int id) {
+    State s;
+    switch (id) {
+      case 1:
+        s = RUNNING;
+        break;
+      case 2:
+        s = CANCEL;
+        break;
+      case 4:
+        s = COMPLETE;
+        break;
+      case 5:
+        s = FAILED;
+        break;
+      case 8:
+        s = ACTIVE;
+        break;
+      case 9:
+        s = INACTIVE;
+        break;
+      default:
+        s = NEW;
+    }
+    return s;
+  }
+
+  public static Collection<State> getStates() {
+    return EnumSet.allOf(State.class);
+  }
+
+  public boolean isFinal() {
+    boolean ret = false;
+    if (this.getPk() == 4 || this.getPk() == 5) {
+      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("color", this.getColor());
+    return elementBuilder("state", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<state>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(elementBuilder("color", this.getColor()));
+    ret.append("</state>\n");
+    return ret;
+  }
+
+  protected static StringBuilder elementBuilder(String name, Map<String, String> a) {
+    StringBuilder ret = new StringBuilder();
+    ret.append('<');
+    ret.append(name);
+    ret.append(' ');
+    for (final String s : a.keySet()) {
+      ret.append(s);
+      ret.append("=\"");
+      ret.append(a.get(s));
+      ret.append("\" ");
+    }
+    ret.append("/>\n");
+    return ret;
+  }
+
+  protected static StringBuilder elementBuilder(String name, String value) {
+    StringBuilder ret = new StringBuilder();
+    ret.append('<');
+    ret.append(name);
+    ret.append('>');
+    ret.append(value);
+    ret.append("</");
+    ret.append(name);
+    ret.append(">\n");
+    return ret;
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/StateModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/StateModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/StateModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/state/StateModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,57 @@
+/*
+* 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.state;
+
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.interfaces.LokahiModel;
+import org.apache.log4j.Logger;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: StateModel.java,v 1.1 2006/03/06 22:31:48 drtobes Exp $
+ */
+public class StateModel extends LokahiModel<State> {
+  static final Logger logger = Logger.getLogger(StateModel.class);
+
+  public StateModel() {
+  }
+
+  public State viewState(int id) throws SQLException {
+    return State.getState(id);
+  }
+
+  public Collection<State> viewStates() throws SQLException {
+    return State.getStates();
+  }
+
+  public Collection<State> getObjects() throws SQLException {
+    Collection<State> c = new TMCSet<State>();
+    c.addAll(this.viewStates());
+    return c;
+  }
+
+  public State getObject(int id) throws SQLException {
+    return this.viewState(id);
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/User.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/User.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/User.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/User.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,852 @@
+/*
+* 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.user;
+
+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.common.authorization.AuthorizationManager;
+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.FunctionableEntity;
+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.tomcat.api.entity.TomcatContext;
+import org.apache.log4j.Logger;
+
+import java.io.Serializable;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.TreeMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: User.java,v 1.3 2006/03/07 20:18:50 drtobes Exp $
+ */
+public class User extends TMCDao<User> implements Serializable {
+  static final Logger logger = Logger.getLogger(User.class);
+  private static final TMCBroker<User> broker = new BrokerFactory<User>().getBroker();
+  private int userId;
+  private String userName;
+  private String firstName = "";
+  private String lastName = "";
+  private String jobTitle = "";
+  private String comments = "";
+  private String email = "";
+  private String manager;
+  private String empIndicator;
+  private State state = State.ACTIVE;
+
+  private AuthorizationManager am;
+
+  private Collection<Project> projects;
+
+  public User() {
+  }
+
+  public User(String userName) {
+    this.setName(userName);
+  }
+
+  public User(String userName, String firstName, String lastName, String jobTitle, String comments, String email) {
+    this(userName);
+    this.firstName = firstName;
+    this.lastName = lastName;
+    this.jobTitle = jobTitle;
+    this.comments = comments;
+    this.email = email;
+  }
+
+  public User(int userId, String userName, String firstName, String lastName, String jobTitle, String comments, String email) {
+    this(userName, firstName, lastName, jobTitle, comments, email);
+    this.userId = userId;
+  }
+
+  public User(int userId, String userName, String firstName, String lastName, String jobTitle, String comments, String email, int stateId) {
+    this(userId, userName, firstName, lastName, jobTitle, comments, email);
+    this.state = State.getState(stateId);
+  }
+
+  public int getPk() {
+    return userId;
+  }
+
+  public void setPk(int userId) {
+    this.userId = userId;
+  }
+
+  public String getName() {
+    return this.userName;
+  }
+
+  public void setName(String name) {
+    this.userName = name.trim();
+  }
+
+  public String getUserName() {
+    return userName;
+  }
+
+  public void setUserName(String userName) {
+    this.userName = userName;
+  }
+
+  public String getFirstName() {
+    return firstName;
+  }
+
+  public void setFirstName(String firstName) {
+    this.firstName = firstName;
+  }
+
+  public String getLastName() {
+    return lastName;
+  }
+
+  public void setLastName(String lastName) {
+    this.lastName = lastName;
+  }
+
+  public String getJobTitle() {
+    return jobTitle;
+  }
+
+  public void setJobTitle(String jobTitle) {
+    this.jobTitle = jobTitle;
+  }
+
+  public String getComments() {
+    return comments;
+  }
+
+  public void setComments(String comments) {
+    this.comments = comments;
+  }
+
+  public String getFullName() {
+    String ret = this.getName();
+    if (!"".equals(this.getFirstName()) && !"".equals(this.getLastName())) {
+      ret = this.getFirstName() + ' ' + this.getLastName();
+    }
+    return ret;
+  }
+
+  public String getEmail() {
+    return email;
+  }
+
+  public void setEmail(String email) {
+    this.email = email;
+  }
+
+  public String getManager() {
+    return manager;
+  }
+
+  public void setManager(String manager) {
+    this.manager = manager;
+  }
+
+  public String getEmpIndicator() {
+    return empIndicator;
+  }
+
+  public void setEmpIndicator(String empIndicator) {
+    this.empIndicator = empIndicator;
+  }
+
+  public boolean isActive() {
+    return this.state != State.INACTIVE;
+  }
+
+  public State getState() {
+    return state;
+  }
+
+  public AuthorizationManager getAuthManager() {
+    if (am == null) {
+      if (logger.isDebugEnabled()) {
+        logger.debug("LOADING AUTH MANAGER USER =" + this.userName);
+      }
+      am = AuthorizationManager.getManager(this.getPk());
+      if (logger.isDebugEnabled()) {
+        logger.debug("END LOADING AUTH MANAGER USER =" + this.userName);
+      }
+    }
+    return am;
+  }
+
+  public Collection<Project> getAddableProjects() {
+    TMCCollectionImpl<Project> visibleProjects = new TMCCollectionImpl<Project>();
+    if (this.getProjects() != null) {
+      Function f;
+      try {
+        f = Function.getFunction("ViewProject");
+        Iterator<Project> i = this.getProjects().iterator();
+        while (i.hasNext()) {
+          Project p = i.next();
+          if (logger.isDebugEnabled()) {
+            logger.debug("Checking project ..." + p.getPk());
+          }
+          visibleProjects.add(p);
+        }
+        AuthorizationManager authManager = this.getAuthManager();
+        if (authManager.hasPermission(this, f)) {
+          if (logger.isDebugEnabled()) {
+            logger.debug("View ALL PROJ sub called");
+          }
+          visibleProjects.addAll(Project.getProjects());
+        }
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    }
+    return visibleProjects;
+  }
+
+  public Collection<Project> getVisibleProjects() {
+    Collection<Project> visibleProjects = this.getAddableProjects();
+    visibleProjects.remove("0");
+    return visibleProjects;
+  }
+
+  public Collection<Project> getProjects() {
+    try {
+      projects = Project.getProjects(this);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return projects;
+  }
+
+  public void setProjects(Collection<Project> c) {
+    this.projects = c;
+  }
+
+  private Collection<Project> getProjectsNoDatabase() {
+    return projects;
+  }
+
+  public Project getProject(String pId) {
+    return this.getProject(Integer.parseInt(pId));
+  }
+
+  public Project getProject(int projectId) {
+    Project ret = null;
+    if (this.getProjects() != null) {
+      Iterator i = this.getProjects().iterator();
+      while (ret == null && i.hasNext()) {
+        Project p = (Project) i.next();
+        if (p.getPk() == projectId) {
+          ret = p;
+        }
+      }
+      if (ret == null && this.isAllowed("ViewProject", projectId)) {
+        try {
+          ret = Project.getProject(projectId);
+        } catch (SQLException e) {
+          if (logger.isInfoEnabled()) {
+            logger.info("Exception: " + e.getMessage());
+          }
+        }
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public Collection<Function> getAllMyFunctions() {
+    Collection<Function> c = this.getAuthManager().getFunctions();
+    return c;
+  }
+
+  public boolean addProject(Project p) throws SQLException {
+    Collection<Project> c = new TMCSet<Project>();
+    c.add(p);
+    return this.addProject(c);
+  }
+
+  public boolean addProject(Collection<Project> c) throws SQLException {
+    boolean ret = false;
+    if (projects == null) {
+      projects = Project.getProjects(this);
+    }
+    if (projects == null) {
+      projects = new TMCSet<Project>();
+    }
+    for (final Project p : c) {
+      if (!projects.contains(p)) {
+        broker.modifyRelationship("core.user.rel.project.add", this.getPk(), p.getPk());
+        projects.add(p);
+      }
+    }
+    if (projects.containsAll(c) || c.isEmpty()) {
+      ret = true;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public boolean removeProject(Project p) throws SQLException {
+    Collection<Project> c = new TMCSet<Project>();
+    c.add(p);
+    return this.removeProject(c);
+  }
+
+  public boolean removeProject(Collection<Project> c) throws SQLException {
+    if (logger.isDebugEnabled()) {
+      logger.debug("c=" + c);
+    }
+    boolean ret = false;
+    if (projects == null) {
+      projects = Project.getProjects(this);
+    }
+    if (projects != null) {
+      for (final Project p : c) {
+        if (projects.contains(p)) {
+          broker.modifyRelationship("core.user.rel.project.delete", this.getPk(), p.getPk());
+          projects.remove(p);
+        }
+      }
+      if (!projects.containsAll(c) || c.isEmpty()) {
+        ret = true;
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public static User getUser(String uName) throws SQLException {
+    User u;
+    try {
+      u = broker.getObject(User.class, "core.user.by.username", false, uName);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return u;
+  }
+
+  public static User addUser(User u) throws SQLException {
+    if (u != null) {
+      if (getUser(u.getName()) == null) {
+        store(u);
+      }
+    }
+    return u;
+  }
+
+  public User get(int id) throws SQLException {
+    User u;
+    try {
+      u = broker.getObject(User.class, "core.user.by.id", false, id);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return u;
+  }
+
+  public static User getUser(int id) throws SQLException {
+    return new User().get(id);
+  }
+
+  public static Collection<User> getAllUsers() throws SQLException {
+    Collection<User> c;
+    try {
+      c = broker.getObjects(User.class, false, "core.user.all");
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public static Collection<User> getUsers(Project p) throws SQLException {
+    Collection<User> c;
+    try {
+      c = broker.getObjects(User.class, false, "core.user.by.project", p.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+      throw e;
+    }
+    return c;
+  }
+
+  public TreeMap<Integer, JobPool> getRecentJobPools(String number) {
+    return this.getRecentJobPools(Integer.parseInt(number));
+  }
+
+  public TreeMap<Integer, JobPool> getRecentJobPools(int number) {
+    TMCCollectionImpl<JobPool> col = new TMCCollectionImpl<JobPool>();
+    try {
+      col.addAll(JobPool.getJobPools(this));
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    TreeMap<Integer, JobPool> tm = col.last(number);
+    return tm;
+  }
+
+  public Collection<Project> getRecentProjects() {
+    Collection<Project> c = null;
+    try {
+      c = Project.getRecentProjects(this);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return c;
+  }
+
+  public User fillObject(ResultSet r) throws SQLException {
+    return new User(r.getInt("USER_ID"),
+                    r.getString("USER_NAME"),
+                    r.getString("FIRST_NAME"),
+                    r.getString("LAST_NAME"),
+                    r.getString("JOB_TITLE"),
+                    r.getString("COMMENTS"),
+                    r.getString("EMAIL"),
+                    r.getInt("STATE_ID"));
+  }
+
+  public static void update(User u) throws SQLException {
+    broker.update("core.user.update", u.getPk(), u.getName(), u.getFirstName(), u.getLastName(), u.getEmail(), u.getComments(), u.getJobTitle(), u.getState().getPk());
+    Collection<Project> newProjects = u.getProjectsNoDatabase();
+    u.setProjects(null);
+    Collection<Project> oldProjects = u.getProjects();
+    Collection<Project> keepers = new TMCSet<Project>();
+    keepers.addAll(oldProjects);
+    keepers.retainAll(newProjects);
+    newProjects.removeAll(keepers);
+    oldProjects.removeAll(keepers);
+    u.removeProject(oldProjects);
+    u.addProject(newProjects);
+  }
+
+  public static User store(User u) throws SQLException {
+    u.setPk(broker.save("core.user.store", u.getName(), u.getFirstName(), u.getLastName(), u.getEmail(), u.getComments(), u.getJobTitle(), u.getState().getPk()));
+    if (u.getProjects() != null && !u.getProjects().isEmpty()) {
+      u.addProject(u.getProjects());
+    }
+    return u;
+  }
+
+  public static boolean delete(User u) throws SQLException {
+    boolean ret = broker.delete("core.user.delete", u.getPk());
+    if (ret) ret = u.removeProject(u.getProjects());
+    return ret;
+  }
+
+/*
+ Start stuff to move
+*/
+
+  /** @deprecated to be removed */
+  public Collection<Function> getFunctions(String projectId) {
+    Collection<Function> c = null;
+    Project p = null;
+    try {
+      p = Project.getProject(Integer.parseInt(projectId));
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    Collection<Project> projs = this.getProjects();
+    if (projs != null) {
+      c = new TMCSet<Function>();
+      if ((projectId == null || "".equals(projectId)) && this.isAllowed("ViewFunction")) {
+        try {
+          c.addAll(Function.getFunctions());
+        } catch (SQLException e) {
+          if (logger.isInfoEnabled()) {
+            logger.info("Exception: " + e.getMessage());
+          }
+        }
+      } else if (projs.contains(p)) {
+        AuthorizationManager authManager = this.getAuthManager();
+        c = authManager.getAvailFunctions(p);
+      }
+    }
+    return c;
+  }
+
+  public TreeMap getProjects(String str) {
+    Collection<Project> c = this.getVisibleProjects();
+    char a = str.toCharArray()[0];
+    TreeMap<String, Project> ret = new TreeMap<String, Project>();
+    for (final Project proj : c) {
+      if (proj.getName().startsWith(Character.toString(Character.toLowerCase(a)))
+          || proj.getName().startsWith(Character.toString(Character.toUpperCase(a)))) {
+        ret.put(proj.getName(), proj);
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public boolean isAllowed(String functionCommand) {
+    return this.isAllowed(functionCommand, -1);
+  }
+
+  public boolean isAllowed(Function f) {
+    return this.isAllowed(f, -1);
+  }
+
+  public boolean isAllowed(String functionCommand, String projectId) {
+    int pId = -1;
+    if (projectId != null && !"".equals(projectId)) {
+      pId = Integer.parseInt(projectId);
+    }
+    return this.isAllowed(functionCommand, pId);
+  }
+
+  public boolean isAllowed(String functionCommand, int projectId) {
+    boolean ret = false;
+    Function f;
+    f = Function.getFunction(functionCommand);
+    ret = this.isAllowed(f, projectId);
+    return ret;
+  }
+
+  public boolean isAllowed(Function f, Project p) {
+    int pid = -1;
+    if (p != null) pid = p.getPk();
+    return this.isAllowed(f, pid);
+  }
+
+  public boolean isAllowed(Function f, String projectId) {
+    int pId = -1;
+    if (projectId != null && !"".equals(projectId)) {
+      pId = Integer.parseInt(projectId);
+    }
+    return this.isAllowed(f, pId);
+  }
+
+  public Collection<Function> getAllowedFunctions() {
+    return this.getAllowedFunctions(new Global());
+  }
+
+  public Collection<Function> getAllowedFunctions(Project p) {
+    return this.getAllowedFunctions((FunctionableEntity) p);
+  }
+
+  private Collection<Function> getAllowedFunctions(FunctionableEntity fe) {
+    AuthorizationManager authManager = this.getAuthManager();
+    return authManager.getAvailFunctions(fe);
+  }
+
+  public Collection<Function> getAllowedFunctions(FunctionableEntity fe, Project p) {
+    AuthorizationManager authManager = this.getAuthManager();
+    return authManager.getAvailFunctions(fe, p);
+  }
+
+  public boolean isAllowed(Function f, int projectId) {
+    boolean ret = false;
+    if (f != null) {
+      AuthorizationManager authManager = this.getAuthManager();
+      if (projectId == -1) {
+        ret = authManager.hasPermission(this, f);
+      } else {
+        try {
+          ret = authManager.hasPermission(this, f, Project.getProject(projectId));
+        } catch (SQLException e) {
+          if (logger.isInfoEnabled()) {
+            logger.info("Exception: " + e.getMessage());
+          }
+        }
+      }
+    }
+    return ret;
+  }
+
+  public boolean isAllowed(String func, Project p, FunctionableEntity object) {
+    boolean ret = false;
+    Function f = null;
+    f = Function.getFunction(func);
+    if (f != null) ret = this.isAllowed(f, p, object);
+    return ret;
+  }
+
+  public boolean isAllowed(Function f, Project p, FunctionableEntity object) {
+    boolean ret = false;
+    AuthorizationManager authManager = this.getAuthManager();
+    if (p != null) {
+      ret = authManager.hasPermission(this, f, object, p);
+    } else {
+      ret = authManager.hasPermission(this, f, object);
+      if (logger.isDebugEnabled()) {
+        logger.debug("PROJECT IS NULL with f=" + f + " object=" + object);
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("ret=" + ret);
+    }
+    return ret;
+  }
+
+  public boolean isAllowed(String func, String projectId, VirtualHost object) {
+    return this.isAllowed(func, Integer.parseInt(projectId), object);
+  }
+
+  public boolean isAllowed(String func, String projectId, TomcatContext object) {
+    return this.isAllowed(func, Integer.parseInt(projectId), object);
+  }
+
+  public boolean isAllowed(String func, int projectId, VirtualHost object) {
+    return this.isAllowed(func, projectId, (FunctionableEntity) object);
+  }
+
+  public boolean isAllowed(String func, int projectId, TomcatContext object) {
+    return this.isAllowed(func, projectId, (FunctionableEntity) object);
+  }
+
+  public boolean isAllowed(String func, int projectId, FunctionableEntity object) {
+    Function f = null;
+    Project p = null;
+    try {
+      f = Function.getFunction(func);
+      p = Project.getProject(projectId);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return this.isAllowed(f, p, object);
+  }
+
+  private boolean isAllowed(Function f, int projectId, FunctionableEntity object) {
+    boolean ret = false;
+    Project p;
+    try {
+      p = Project.getProject(projectId);
+      ret = isAllowed(f, p, object);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return ret;
+  }
+
+  public Collection<Function> getContextFunctions(String projectId, TomcatContext tc) {
+    int pId = -1;
+    if (projectId != null && !"".equals(projectId)) {
+      pId = Integer.parseInt(projectId);
+    }
+    return this.getContextFunctions(pId, tc);
+  }
+
+  public Collection<Function> getContextFunctions(Project project, TomcatContext tc) {
+    return this.getContextFunctions(project.getPk(), tc);
+  }
+
+  public Collection<Function> getContextFunctions(TomcatContext tc) {
+    return this.getContextFunctions(-1, tc);
+  }
+
+  public Collection<Function> getContextFunctions(int projectId, TomcatContext tc) {
+    Collection<Function> ret;
+    String[] commands = new String[]{
+        "ReloadContext",
+        "StopContext",
+        "StartContext",
+        "ViewContext",
+        "UpdateContext",
+        "DeleteContext",
+        "UndeployContext",
+        "RedeployContext",
+        "InstallContext",
+        "ListContext",
+        "SessionsOfContext",
+        "StatusOfContext"};
+    ret = getAllowedFunctions(commands, projectId, tc);
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public Collection<Function> getVhostFunctions(String projectId, VirtualHost vh) {
+    int pId = -1;
+    if (projectId != null && !"".equals(projectId)) {
+      pId = Integer.parseInt(projectId);
+    }
+    return this.getVhostFunctions(pId, vh);
+  }
+
+  public Collection<Function> getVhostFunctions(int projectId, VirtualHost vh) {
+    Collection<Function> ret;
+    String[] commands = new String[]{
+        "ViewVirtualHost",
+        "UpdateVirtualHost"};
+    ret = getAllowedFunctions(commands, projectId, vh);
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  private Collection<Function> getAllowedFunctions(String[] listOfCommands, int projectId, FunctionableEntity object) {
+    Collection<Function> ret = new TMCSet<Function>();
+    Function f;
+    for (final String newVar : listOfCommands) {
+      f = Function.getFunction(newVar);
+      if (logger.isDebugEnabled()) {
+        logger.debug("Checking function f=" + f);
+        logger.debug("Checking function projectId=" + projectId);
+        logger.debug("Checking function object=" + object);
+      }
+      if (this.isAllowed(f, projectId, object)) {
+        ret.add(f);
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("ret=" + ret);
+    }
+    return ret;
+  }
+
+  public Collection<Project> getAdministratableProjects() {
+    Collection<Project> c;
+    if (this.isAllowed("AddUserToProject")) {
+      c = this.getVisibleProjects();
+    } else {
+      c = new TMCSet<Project>();
+      Collection<Project> col = this.getProjects();
+      Function f = null;
+      f = Function.getFunction("AddUserToProject");
+      if (col != null && f != null) {
+        for (Project p : col) {
+          if (this.isAllowed(f, p)) {
+            c.add(p);
+          }
+        }
+      }
+    }
+    return c;
+  }
+
+/*
+ end stuff to move
+*/
+
+  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") + "user/" + this.getPk() + '/');
+    return elementBuilder("user", map);
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    StringBuilder ret = new StringBuilder();
+    ret.append("<user>\n");
+    ret.append(elementBuilder("id", Integer.toString(this.getPk())));
+    ret.append(elementBuilder("name", this.getName()));
+    ret.append(elementBuilder("firstName", this.getFirstName()));
+    ret.append(elementBuilder("lastName", this.getLastName()));
+    ret.append(elementBuilder("jobTitle", this.getJobTitle()));
+    ret.append(elementBuilder("email", this.getEmail()));
+    for (final Project p : this.getProjects()) {
+      ret.append(p.buildShortXMLRepresentation());
+    }
+    ret.append("</user>\n");
+    return ret;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof User)) return false;
+
+    final User user = (User) o;
+
+    if (userId != user.userId) return false;
+    if (comments != null ? !comments.equals(user.comments) : user.comments != null) return false;
+    if (email != null ? !email.equals(user.email) : user.email != null) return false;
+    if (firstName != null ? !firstName.equals(user.firstName) : user.firstName != null) return false;
+    if (jobTitle != null ? !jobTitle.equals(user.jobTitle) : user.jobTitle != null) return false;
+    if (lastName != null ? !lastName.equals(user.lastName) : user.lastName != null) return false;
+    if (!userName.equals(user.userName)) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = userId;
+    result = 29 * result + userName.hashCode();
+    result = 29 * result + (firstName != null ? firstName.hashCode() : 0);
+    result = 29 * result + (lastName != null ? lastName.hashCode() : 0);
+    result = 29 * result + (jobTitle != null ? jobTitle.hashCode() : 0);
+    result = 29 * result + (comments != null ? comments.hashCode() : 0);
+    result = 29 * result + (email != null ? email.hashCode() : 0);
+    return result;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("User");
+    buf.append("{userId=").append(userId);
+    buf.append(",userName=").append(userName);
+    buf.append(",firstName=").append(firstName);
+    buf.append(",lastName=").append(lastName);
+    buf.append(",jobTitle=").append(jobTitle);
+    buf.append(",comments=").append(comments);
+    buf.append(",email=").append(email);
+    buf.append(",projects=").append(projects);
+    buf.append('}');
+    return buf.toString();
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/UserModel.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/UserModel.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/UserModel.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/api/user/UserModel.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,212 @@
+/*
+* 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.user;
+
+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.common.authorization.AuthorizationManager;
+import lokahi.core.common.authorization.Global;
+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.interfaces.FunctionableEntity;
+import lokahi.core.common.interfaces.LokahiModel;
+import org.apache.log4j.Logger;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: UserModel.java,v 1.3 2006/03/07 20:18:50 drtobes Exp $
+ */
+public class UserModel extends LokahiModel<User> {
+  static final Logger logger = Logger.getLogger(UserModel.class);
+
+  public UserModel() {
+  }
+
+  public void addUser(User u, User s, Function f) throws SQLException, TMCException {
+    jpInit(f, u, s.getName());
+    if (u.isAllowed(f) && "AddUser".equals(f.getCommand())) {
+      if (User.getUser(s.getName()) == null) {
+        s = User.addUser(s);
+        jp.setState(State.COMPLETE);
+        JobPool.update(jp);
+        if (s == null) {
+          throw new TMCIllegalArgumentException("User appears to be be null");
+        }
+      } else
+        throw new AlreadyExistException("User " + s.getName() + "already exists");
+    } else
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+  }
+
+  public void deleteUser(User u, User s, Function f) throws SQLException,
+      TMCException {
+    if (s == null || s.getPk() <= 1) throw new TMCIllegalArgumentException("User can not be null");
+    jpInit(f, u, s.getName());
+    if (u.isAllowed(f) && "AddUser".equals(f.getCommand())) {
+      User.delete(s);
+      jp.setState(State.COMPLETE);
+      JobPool.update(jp);
+    } else
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+  }
+
+  public void deleteUser(User u, Collection c, Function f) throws SQLException, TMCException {
+    StringBuffer names = new StringBuffer();
+    for (final Object newVar : c) {
+      User formUser = (User) newVar;
+      names.append(formUser.getName());
+      names.append(',');
+      deleteUser(u, formUser, f);
+    }
+    jp.setOptions(names.toString());
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void updateUser(User u, User s, Function f) throws SQLException,
+      TMCException {
+    if (s == null) {
+      throw new TMCIllegalArgumentException("User appears to be be null");
+    }
+    jpInit(f, u, s.getName());
+    if (u.isAllowed(f) && "UpdateUser".equals(f.getCommand())) {
+      if (s.getPk() >= 1 && s.getName() != null && !"".equals(s.getName())) {
+        User.update(s);
+        jp.setState(State.COMPLETE);
+        JobPool.update(jp);
+      } else
+        throw new TMCIllegalArgumentException("User must be an existing, valid user");
+    } else {
+      throw new AuthorizationException("User " + u.getName() + " Not allowed to " + f.getName());
+    }
+  }
+
+  public User viewUser(int id) throws SQLException {
+    return User.getUser(id);
+  }
+
+  public Collection listUsers(User u) throws SQLException {
+    return User.getAllUsers();
+  }
+
+  public void addUserToProject(User user, Function f, String newUserName, String[] functions) throws AuthorizationException, SQLException {
+    this.jpInit(f, user, newUserName);
+    if (user.isAllowed(f)) {
+      boolean isNewUser = false;
+      User newUser = User.getUser(newUserName);
+      if (newUser == null) {
+        newUser = new User(newUserName);
+        User.store(newUser);
+        isNewUser = true;
+      } else {
+        AuthorizationManager.removeAbilities(newUser, 0);
+      }
+      Global g = new Global();
+      for (final String newVar : functions) {
+        Function func = Function.getFunction(Integer.parseInt(newVar));
+        if (isNewUser || !newUser.isAllowed(func)) {
+          AuthorizationManager.addAbility(newUser, func, g, 0);
+        }
+      }
+    } else {
+      throw new AuthorizationException("User " + user.getName() + " not allowed to Add User to All projects");
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  public void addUserToProject(User user, Function f, String newUserName, String[] contextFunctions, String[] vhostFunctions, int projectId) throws AuthorizationException, SQLException, TMCIllegalArgumentException {
+    if (projectId <= 0) {
+      throw new TMCIllegalArgumentException("Project Id cannot be equal to -1");
+    }
+    Project p = Project.getProject(projectId);
+    if (p == null) throw new TMCIllegalArgumentException("Project not found");
+    this.jpInit(f, user, p, newUserName);
+    if (user.isAllowed(f, p)) {
+      try {
+        User newUser = User.getUser(newUserName);
+        if (newUser == null) {
+          newUser = new User(newUserName);
+          User.store(newUser);
+        } else {
+          AuthorizationManager.removeAbilities(newUser, p.getPk());
+        }
+        proccesAbilityMap(newUser, contextFunctions, 4, p);
+        proccesAbilityMap(newUser, vhostFunctions, 3, p);
+        newUser.addProject(p);
+        jp.setState(State.COMPLETE);
+      } catch (SQLException e) {
+        if (logger.isInfoEnabled()) {
+          logger.info("Exception: " + e.getMessage());
+        }
+      }
+    } else {
+      throw new AuthorizationException("User " + user.getName() + " not allowed to Add User to Project: " + p);
+    }
+    jp.setState(State.COMPLETE);
+    JobPool.update(jp);
+  }
+
+  private void proccesAbilityMap(User u, String[] map, int type, Project p) throws SQLException {
+    for (final String newVar : map) {
+      String[] entityFunction = newVar.split(",");
+      if (logger.isDebugEnabled()) {
+        logger.debug("newVar=" + newVar);
+      }
+      if (!"".equals(entityFunction[0]) && !"".equals(entityFunction[1])) {
+        FunctionableEntity fe = getFunctionableEntity(entityFunction[0], type);
+        Function f = Function.getFunction(Integer.parseInt(entityFunction[1]));
+        if (f != null && fe != null) {
+          if (logger.isDebugEnabled()) {
+            logger.debug("f=" + f);
+            logger.debug("u=" + u);
+            logger.debug("fe=" + fe);
+            logger.debug("p=" + p);
+          }
+          AuthorizationManager.addAbility(u, f, fe, p);
+        }
+      } else {
+        if (logger.isDebugEnabled()) {
+          logger.debug("SPLIT IS NOT WORKING");
+        }
+      }
+    }
+  }
+
+  private FunctionableEntity getFunctionableEntity(String id, int type) {
+    return AuthorizationManager.getEntity(type, Integer.parseInt(id));
+  }
+
+  public Collection<User> getObjects() throws SQLException {
+    return new TMCSet<User>(User.getAllUsers());
+  }
+
+  public User getObject(int id) throws SQLException {
+    return User.getUser(id);
+  }
+}
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/Authentication.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/Authentication.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/Authentication.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/Authentication.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,31 @@
+/*
+* 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.common.authentication;
+
+import lokahi.core.common.exception.AuthenticationException;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: Authentication.java,v 1.1 2006/03/07 20:18:50 drtobes Exp $
+ */
+public interface Authentication {
+  boolean authenticate(String userID, String password) throws AuthenticationException;
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/AuthenticationFactory.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/AuthenticationFactory.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/AuthenticationFactory.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/AuthenticationFactory.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,91 @@
+/*
+* 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.common.authentication;
+
+import lokahi.core.common.exception.AuthenticationException;
+import lokahi.core.common.util.PropertiesFile;
+import org.apache.log4j.Logger;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: AuthenticationFactory.java,v 1.1 2006/03/07 20:18:50 drtobes Exp $
+ */
+public class AuthenticationFactory {
+  static final Logger logger = Logger.getLogger(AuthenticationFactory.class);
+  private static final String authenticationModule = PropertiesFile.getConstantValue("AuthenticateModule");
+  private static final Authentication authentication = getAuthentication();
+
+  private AuthenticationFactory() {
+  }
+
+  public static boolean authenticate(String user, String password) throws AuthenticationException {
+    boolean ret = false;
+    if (authentication != null) {
+      ret = authentication.authenticate(user, password);
+    }
+    return ret;
+  }
+
+  private static Authentication getAuthentication() {
+    Object obj;
+    Authentication ret = null;
+    try {
+      if (logger.isDebugEnabled()) {
+        logger.debug("authenticationModule=" + authenticationModule);
+      }
+      Class c = Class.forName(authenticationModule);
+      Constructor constructor = c.getConstructor(new Class[]{});
+      obj = constructor.newInstance(new Object[]{});
+      if (obj instanceof Authentication) {
+        ret = (Authentication) obj;
+      }
+    } catch (ClassNotFoundException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("ClassNotFoundException: " + e.getMessage());
+      }
+    } catch (NoSuchMethodException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("NoSuchMethodException: " + e.getMessage());
+      }
+    } catch (SecurityException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("SecurityException: " + e.getMessage());
+      }
+    } catch (InstantiationException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("InstantiationException: " + e.getMessage());
+      }
+    } catch (IllegalAccessException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("IllegalAccessException: " + e.getMessage());
+      }
+    } catch (InvocationTargetException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("InvocationTargetException: " + e.getMessage());
+      }
+    }
+    return ret;
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/DatabaseAuthentication.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/DatabaseAuthentication.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/DatabaseAuthentication.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/DatabaseAuthentication.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,59 @@
+/*
+* 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.common.authentication;
+
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.exception.AuthenticationException;
+import lokahi.core.common.interfaces.Dao;
+import lokahi.core.common.interfaces.TMCBroker;
+import org.apache.log4j.Logger;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: DatabaseAuthentication.java,v 1.1 2006/03/07 20:18:50 drtobes Exp $
+ */
+public class DatabaseAuthentication implements Authentication, Dao {
+  static final Logger logger = Logger.getLogger(DatabaseAuthentication.class);
+  private static final TMCBroker<DatabaseAuthentication> broker = new BrokerFactory<DatabaseAuthentication>().getBroker();
+  private String userName;
+  private String password;
+
+  public DatabaseAuthentication() {
+  }
+
+  public DatabaseAuthentication(String userName, String password) {
+    this.userName = userName;
+    this.password = password;
+  }
+
+  public boolean authenticate(String userID, String passWord) throws AuthenticationException { //TODO finish this.
+    return false;
+  }
+
+  private String getPassword(String userName) throws SQLException {
+    return "";
+  }
+
+  public DatabaseAuthentication fillObject(ResultSet r) throws SQLException { //TODO finish this.
+    return null;
+  }
+
+}
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/NoAuthentication.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/NoAuthentication.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/NoAuthentication.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authentication/NoAuthentication.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,36 @@
+/*
+* 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.common.authentication;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: NoAuthentication.java,v 1.1 2006/03/07 20:18:50 drtobes Exp $
+ */
+public class NoAuthentication implements Authentication {
+
+  public NoAuthentication() {
+  }
+
+  public boolean authenticate(String userID, String password) {
+    return true;
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityDAO.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityDAO.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityDAO.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityDAO.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,57 @@
+/*
+* 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.common.authorization;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.common.interfaces.Dao;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: AbilityDAO.java,v 1.1 2006/03/07 20:18:50 drtobes Exp $
+ */
+public class AbilityDAO implements Dao {
+  private AbilityObject ao;
+  private int functionId;
+
+  public AbilityObject getAbilityObject() {
+    return ao;
+  }
+
+  public Function getFunction() {
+    return Function.getFunction(functionId);
+  }
+
+  public AbilityDAO() {
+  }
+
+  public AbilityDAO(int f, AbilityObject ao) {
+    this.ao = ao;
+    this.functionId = f;
+  }
+
+  public AbilityDAO fillObject(ResultSet r) throws SQLException {
+    return new AbilityDAO(r.getInt("FUNCTION_ID"),
+                          new AbilityObject(r.getInt("OBJECT_TYPE"), r.getInt("PROJECT_ID"), r.getInt("OBJECT_ID")));
+  }
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityObject.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityObject.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityObject.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AbilityObject.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,116 @@
+/*
+* 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.common.authorization;
+
+import lokahi.core.api.project.Project;
+import lokahi.core.common.interfaces.FunctionableEntity;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: AbilityObject.java,v 1.1 2006/03/07 20:18:50 drtobes Exp $
+ */
+public class AbilityObject {
+  private int objectId;
+  private int projectId;
+  private int functionableEntityId;
+
+  AbilityObject(int type, int projectId, int pk) {
+    this.objectId = type;
+    this.projectId = projectId;
+    this.functionableEntityId = pk;
+  }
+
+  AbilityObject(FunctionableEntity fe, int projectId) {
+    this.objectId = fe.getObjectId();
+    this.projectId = projectId;
+    this.functionableEntityId = fe.getPk();
+  }
+
+  AbilityObject(FunctionableEntity fe, Project p) {
+    this.objectId = fe.getObjectId();
+    if (p != null) {
+      this.projectId = p.getPk();
+    } else {
+      this.projectId = 0;
+    }
+    this.functionableEntityId = fe.getPk();
+  }
+
+  int getPk() {
+    return functionableEntityId;
+  }
+
+  int getObjectId() {
+    return objectId;
+  }
+
+  int getProjectId() {
+    return projectId;
+  }
+
+  boolean hasParent() {
+    boolean ret = false;
+    if (this.objectId >= AuthorizationManager.cutOffId) {
+      ret = true;
+    }
+    return ret;
+  }
+
+  AbilityObject getParent() {
+    AbilityObject ao = null;
+    if (this.hasParent()) {
+      if (this.objectId == AuthorizationManager.cutOffId) {
+        ao = new AbilityObject(AuthorizationManager.topObject, 0);
+      } else {
+        ao = new AbilityObject(AuthorizationManager.cutOffId, this.getProjectId(), this.getProjectId());
+      }
+
+    }
+    return ao;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof AbilityObject)) return false;
+
+    final AbilityObject abilityObject = (AbilityObject) o;
+
+    if (functionableEntityId != abilityObject.functionableEntityId) return false;
+    if (objectId != abilityObject.objectId) return false;
+    if (projectId != abilityObject.projectId) return false;
+
+    return true;
+  }
+
+  public int hashCode() {
+    int result;
+    result = objectId;
+    result = 29 * result + projectId;
+    result = 29 * result + functionableEntityId;
+    return result;
+  }
+
+  public String toString() {
+    return "AbilityObject{" +
+        "objectId=" + objectId +
+        ", projectId=" + projectId +
+        ", functionableEntityId=" + functionableEntityId +
+        '}';
+  }
+}
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AuthorizationManager.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AuthorizationManager.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AuthorizationManager.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/AuthorizationManager.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,287 @@
+/*
+* 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.common.authorization;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.api.project.Project;
+import lokahi.core.api.user.User;
+import lokahi.core.common.collection.TMCSet;
+import lokahi.core.common.database.BrokerFactory;
+import lokahi.core.common.interfaces.FunctionableEntity;
+import lokahi.core.common.interfaces.TMCBroker;
+import lokahi.httpd.api.entity.VirtualHost;
+import lokahi.tomcat.api.entity.TomcatContext;
+import org.apache.log4j.Logger;
+
+import java.io.Serializable;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.HashMap;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: AuthorizationManager.java,v 1.2 2006/03/07 22:05:23 drtobes Exp $
+ */
+public class AuthorizationManager implements Serializable {
+  static final Logger logger = Logger.getLogger(AuthorizationManager.class);
+  private static final TMCBroker<AbilityDAO> broker = new BrokerFactory<AbilityDAO>().getBroker();
+  static final Global topObject = new Global();
+  static final int cutOffId = 2;
+
+  private HashMap<AbilityObject, Collection<Function>> abilities;
+  private int userId;
+
+  public AuthorizationManager(int userId) {
+    this.userId = userId;
+  }
+
+  public static AuthorizationManager getManager(int userId) {
+    return new AuthorizationManager(userId);
+  }
+
+  public HashMap<AbilityObject, Collection<Function>> getAbilities() {
+    if (abilities == null) {
+      if (logger.isDebugEnabled()) {
+        logger.debug("BUILDING MAP userId=" + userId);
+      }
+      abilities = buildAbilitesMap(this.userId);
+      if (logger.isDebugEnabled()) {
+        logger.debug("END BUILDING MAP userId=" + userId);
+      }
+    }
+    return abilities;
+  }
+
+  public boolean hasPermission(User u, Function f, FunctionableEntity e, Project p) {
+    boolean ret = false;
+    if (logger.isDebugEnabled()) {
+      logger.debug("u=" + u);
+      logger.debug("f=" + f);
+      logger.debug("e=" + e);
+      logger.debug("p=" + p);
+    }
+    if (u != null && u.getPk() == this.userId) {
+      AbilityObject ao = buildAbilityObject(e, p);
+      ret = this.containsWithCollection(f, ao);
+      while (!ret && ao.hasParent()) {
+        ao = ao.getParent();
+        ret = this.containsWithCollection(f, ao);
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("ret=" + ret);
+    }
+    return ret;
+  }
+
+  private AbilityObject buildAbilityObject(FunctionableEntity e, Project p) {
+    AbilityObject ao;
+    if (e == null && p == null) {
+      ao = new AbilityObject(topObject, 0);
+    } else if (e == null) {
+      ao = new AbilityObject(p, p.getPk());
+    } else if (p == null) {
+      ao = new AbilityObject(topObject, 0);
+    } else if (e.getObjectId() > p.getObjectId()) {
+      ao = new AbilityObject(e, p.getPk());
+    } else if (e.getObjectId() == p.getObjectId()) {
+      ao = new AbilityObject(e, p.getPk());
+    } else {
+      ao = new AbilityObject(e, 0);
+    }
+    return ao;
+  }
+
+  public boolean hasPermission(User u, Function f, FunctionableEntity e) {
+    return this.hasPermission(u, f, e, null);
+  }
+
+  public boolean hasPermission(User u, Function f) {
+    return this.hasPermission(u, f, null, null);
+  }
+
+  private boolean containsWithCollection(Function f, AbilityObject ao) {
+    boolean ret = false;
+    if (this.getAbilities() != null && this.getAbilities().keySet() != null && this.getAbilities().keySet().contains(ao))
+    {
+      ret = this.getAbilities().get(ao).contains(f);
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("ret=" + ret);
+      logger.debug("f=" + f);
+      logger.debug("ao=" + ao);
+    }
+    return ret;
+  }
+
+  public Collection<Function> getFunctions() { //todo this is for the help items should return all available functions for the user.
+    Collection<Function> c = null;
+    return c;
+  }
+
+  public Collection<Function> getAvailFunctions(FunctionableEntity fe, Project p) {
+    Collection<Function> c = new TMCSet<Function>();
+    if (fe != null) {
+      int projectId = 0;
+      if (p != null) {
+        projectId = p.getPk();
+      }
+      AbilityObject ao = new AbilityObject(fe, projectId);
+      HashMap<AbilityObject, Collection<Function>> abil = this.getAbilities();
+      if (abil.containsKey(ao)) {
+        c = abil.get(ao);
+      }
+    }
+    return c;
+  }
+
+  public Collection<Function> getAvailFunctions(FunctionableEntity fe) {
+    return this.getAvailFunctions(fe, null);
+  }
+
+  public static HashMap<AbilityObject, Collection<Function>> buildAbilitesMap(User u) {
+    return buildAbilitesMap(u.getPk());
+  }
+
+  public static HashMap<AbilityObject, Collection<Function>> buildAbilitesMap(int uId) {
+    HashMap<AbilityObject, Collection<Function>> h = new HashMap<AbilityObject, Collection<Function>>();
+    try {
+      Collection<AbilityDAO> adaos = broker.getObjects(AbilityDAO.class, true, "core.ability.by.user", uId);
+      for (final AbilityDAO adao : adaos) {
+        Collection<Function> c;
+        if (h.containsKey(adao.getAbilityObject())) {
+          c = h.get(adao.getAbilityObject());
+        } else {
+          c = new TMCSet<Function>();
+        }
+        c.add(adao.getFunction());
+        h.put(adao.getAbilityObject(), c);
+      }
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return h;
+  }
+
+  public static boolean storeAbilitiesMap(HashMap<AbilityObject, Collection<Function>> h) {
+    return false;
+  }
+
+  public static void addAbility(User u, Function f, FunctionableEntity fe, Project p) throws SQLException {
+    AbilityObject ao = new AbilityObject(fe, p);
+    storeAbility(u, f, ao);
+  }
+
+  public static void addAbility(User u, Function f, FunctionableEntity fe, int pId) throws SQLException {
+    AbilityObject ao = new AbilityObject(fe, pId);
+    storeAbility(u, f, ao);
+  }
+
+  /**
+   * @param type
+   * @param id
+   *
+   * @return Appropriate Functionable Entity
+   */
+
+  public static FunctionableEntity getEntity(int type, int id) {//todo make this dynamic
+    FunctionableEntity e;
+    if (type == 1) {
+      e = fillObject(Global.class, id);
+    } else if (type == 2) {
+      e = fillObject(Project.class, id);
+    } else if (type == 3) {
+      e = fillObject(VirtualHost.class, id);
+    } else if (type == 4) {
+      e = fillObject(TomcatContext.class, id);
+    } else {
+      e = null;
+    }
+    return e;
+  }
+
+  private static <T extends FunctionableEntity> T fillObject(Class<T> clazz, int id) {
+    T fe = null;
+    try {
+      fe = clazz.newInstance();
+      if (fe instanceof FunctionableEntity) {
+        fe = (T) fe.get(id);
+      }
+    } catch (SecurityException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("SecurityException: " + e.getMessage());
+      }
+    } catch (InstantiationException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("InstantiationException: " + e.getMessage());
+      }
+    } catch (IllegalAccessException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("IllegalAccessException: " + e.getMessage());
+      }
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("Exception: " + e.getMessage());
+      }
+    }
+    return fe;
+  }
+
+  public static boolean storeAbilities(User u) throws SQLException {
+    boolean ret = false;
+    AuthorizationManager am = u.getAuthManager();
+    HashMap<AbilityObject, Collection<Function>> h = am.getAbilities();
+    for (final AbilityObject ao : h.keySet()) {
+      storeAbilities(u, h.get(ao), ao);
+    }
+    return ret;
+  }
+
+  private static void storeAbilities(User u, Collection<Function> c, AbilityObject ao) throws SQLException {
+    for (final Function f : c) {
+      storeAbility(u, f, ao);
+    }
+  }
+
+  public static void storeAbility(User u, Function f, AbilityObject ao) throws SQLException {
+    try {
+      broker.modifyRelationship("core.ability.rel.user.store", u.getPk(), ao.getPk(), ao.getObjectId(), ao.getProjectId(), f.getPk());
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("SQLException: " + e.getMessage());
+      }
+      throw e;
+    }
+  }
+
+  public static void removeAbilities(User u, int projectId) throws SQLException {
+    try {
+      broker.modifyRelationship("core.ability.rel.user.delete", u.getPk(), projectId);
+    } catch (SQLException e) {
+      if (logger.isInfoEnabled()) {
+        logger.info("SQLException: " + e.getMessage());
+      }
+      throw e;
+    }
+  }
+
+}
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/Global.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/Global.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/Global.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/authorization/Global.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,80 @@
+/*
+* 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.common.authorization;
+
+import lokahi.core.common.interfaces.FunctionableEntity;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: Global.java,v 1.4 2006/03/07 22:05:24 drtobes Exp $
+ */
+public class Global extends FunctionableEntity<Global> {
+  private static final int objectId = 1;
+
+  public int getObjectId() {
+    return objectId;
+  }
+
+  public StringBuilder buildXMLRepresention() {
+    return null;
+  }
+
+  public StringBuilder buildShortXMLRepresentation() {
+    return null;
+  }
+
+  public int getPk() {
+    return 0;
+  }
+
+  public String getName() {
+    return "All Projects";
+  }
+
+  public Global get(int id) {
+    return new Global();
+  }
+
+  public FunctionableEntity getParent() {
+    return this;
+  }
+
+  public Global fillObject(ResultSet r) throws SQLException {
+    return new Global();
+  }
+
+  public Global() {
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (!(o instanceof Global)) return false;
+    return true;
+  }
+
+  public int hashCode() {
+    return 42;
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCache.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCache.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCache.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCache.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,74 @@
+/*
+* 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.common.collection;
+
+import lokahi.core.api.function.Function;
+import lokahi.core.common.interfaces.Collectable;
+
+import java.util.AbstractCollection;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: TMCCache.java,v 1.4 2006/03/07 20:18:56 drtobes Exp $
+ */
+public class TMCCache<T extends Collectable> extends AbstractCollection<T> {
+  protected HashMap<String, Integer> index;
+  protected HashMap<Integer, T> cache;
+
+  public TMCCache() {
+    index = new HashMap<String, Integer>();
+    cache = new HashMap<Integer, T>();
+  }
+
+  public boolean add(T t) {
+    Integer i = t.getPk();
+    String name;
+    if (t instanceof Function) {
+      name = ((Function) t).getCommand();
+    } else {
+      name = t.getName();
+    }
+    index.put(name, i);
+    cache.put(i, t);
+    return true;
+  }
+
+  public T get(int i) {
+    return cache.get(i);
+  }
+
+  public T get(String i) {
+    return cache.get(index.get(i));
+  }
+
+  public Iterator<T> iterator() {
+    return cache.values().iterator();
+  }
+
+  public int size() {
+    return cache.values().size();
+  }
+
+  public Collection<T> values() {
+    return cache.values();
+  }
+
+}
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCollectionImpl.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCollectionImpl.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCollectionImpl.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCCollectionImpl.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,232 @@
+/*
+* 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.common.collection;
+
+import lokahi.core.common.interfaces.Collectable;
+import org.apache.log4j.Logger;
+
+import java.io.Serializable;
+import java.util.AbstractCollection;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: TMCCollectionImpl.java,v 1.3 2006/03/07 20:18:56 drtobes Exp $
+ */
+public class TMCCollectionImpl<ItemType extends Collectable> extends AbstractCollection<ItemType> implements Serializable {
+  static final Logger logger = Logger.getLogger(TMCCollectionImpl.class);
+
+  private HashMap<Integer, ItemType> collection = new HashMap<Integer, ItemType>();
+
+  public TMCCollectionImpl() {
+  }
+
+  public TMCCollectionImpl(Collection<ItemType> c) {
+    this.addAll(c);
+  }
+
+  public Iterator<ItemType> iterator() {
+    return collection.values().iterator();
+  }
+
+  public boolean add(ItemType o) {
+    boolean ret = false;
+    if (o != null) {
+      if (logger.isDebugEnabled()) {
+        logger.debug("o=" + o);
+      }
+      if (collection.put(o.getPk(), o) != null) {
+        ret = true;
+      }
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public boolean remove(Object o) {
+    boolean ret = false;
+    if (logger.isDebugEnabled()) {
+      logger.debug("TMCCollectionImpl remove called with object " + o);
+    }
+    if (o instanceof Collectable) {
+      if (collection.remove(((Collectable) o).getPk()) != null) {
+        ret = true;
+      }
+    } else if (o instanceof String) {
+      if (collection.remove(Integer.parseInt((String) o)) != null) {
+        ret = true;
+      }
+    } else if (o instanceof Integer) {
+      if (collection.remove(o) != null) ret = true;
+    } else {
+      if (logger.isInfoEnabled()) {
+        logger.info("Wrong instance " + o.getClass());
+      }
+    }
+
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+  public int size() {
+    return collection.size();
+  }
+
+  public boolean contains(ItemType o) {
+    boolean ret = false;
+    if (collection.containsValue(o)) {
+      ret = true;
+    }
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + ret);
+    }
+    return ret;
+  }
+
+//  public boolean contains(int id) {
+//    return collection.containsKey(id);
+//  }
+//
+//  public boolean contains(String id) {
+//    return this.contains(Integer.parseInt(id));
+//  }
+
+//  public ItemType getObject(int id) {
+//    if (logger.isEnabledFor(Priority.ERROR)) {
+//      logger.error("Unimplemented method called id=" + id);
+//    }
+//    return null;
+//  }
+
+  private Collection keySet() {
+    if (logger.isDebugEnabled()) {
+      logger.debug("Collection keyset in TMCCollectionImpl called. " + collection.toString());
+    }
+    return this.collection.keySet();
+  }
+
+  public Object clone() {
+    TMCCollectionImpl<ItemType> clone = null;
+//    try {
+//      clone = (TMCCollectionImpl<ItemType>) super.clone();
+//    } catch (CloneNotSupportedException e) {
+//      if (logger.isDebugEnabled()) {
+//        logger.debug("Exception: " + e.getMessage());
+//      }
+//    }
+//    if (clone == null) {
+    clone = new TMCCollectionImpl<ItemType>();
+    clone.addAll(this);
+//    } else {
+//      clone.collection = (HashMap<Integer, ItemType>) this.collection.clone();
+//    }
+    return clone;
+  }
+
+  public Collection<ItemType> values() {
+    return this.collection.values();
+  }
+
+  public TreeMap<Integer, ItemType> orderedList() {
+    TreeMap<Integer, ItemType> tm = new TreeMap<Integer, ItemType>();
+    for (final ItemType newVar : collection.values()) {
+      tm.put(newVar.getPk(), newVar);
+    }
+    return tm;
+  }
+
+  public TreeMap<Integer, ItemType> last(int numberElements) {
+    TreeMap<Integer, ItemType> tm = this.orderedList();
+    TreeMap<Integer, ItemType> ret = new TreeMap<Integer, ItemType>(new Comparator<Integer>() {
+      public int compare(final Integer x, final Integer y) {
+        int ret = 0;
+        ret = y - x;
+        return ret;
+      }
+    });
+    Iterator<Integer> i = tm.keySet().iterator();
+    if (tm.keySet().size() > numberElements) {
+      int placeholder = tm.keySet().size() - numberElements;
+      int count = 0;
+      while (i.hasNext() && count != placeholder) {
+        i.next();
+        count++;
+      }
+      if (logger.isDebugEnabled()) {
+        logger.debug("count=" + count);
+        logger.debug("placeholder=" + placeholder);
+      }
+      if (count == placeholder) {
+        ret.putAll(tm.tailMap(i.next()));
+      }
+    } else {
+      ret.putAll(tm);
+    }
+    return ret;
+  }
+
+  public TreeMap<String, ItemType> orderByName() {
+    TreeMap<String, ItemType> tm = new TreeMap<String, ItemType>();
+    for (final ItemType dao : collection.values()) {
+      tm.put(dao.getName(), dao);
+    }
+    return tm;
+  }
+
+  public SortedSet<ItemType> getSortedSetByDate(Comparator<? super ItemType> c, int topNumber) {
+    SortedSet<ItemType> set = new TreeSet<ItemType>(c);
+    SortedSet<ItemType> ret;
+    topNumber++;
+    set.addAll(this.values());
+    if (this.values().size() > topNumber) {
+      Iterator<ItemType> i = set.iterator();
+      ItemType o = set.first();
+      int counter = 1;
+      while (counter <= topNumber && i.hasNext()) {
+        o = i.next();
+        counter++;
+      }
+      ret = set.headSet(o);
+    } else {
+      ret = set;
+    }
+    return ret;
+  }
+
+  public String toString() {
+    final StringBuffer buf = new StringBuffer();
+    buf.append("TMCCollectionImpl");
+    buf.append("{collection=").append(collection);
+    buf.append('}');
+    return buf.toString();
+  }
+
+}
+
+
+
+
+

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCDataStore.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCDataStore.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCDataStore.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCDataStore.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,25 @@
+package lokahi.core.common.collection;
+
+import lokahi.core.common.interfaces.Collectable;
+
+/**
+ * @author Stephen Toback
+ * @version $Id: TMCDataStore.java,v 1.3 2006/03/07 20:18:56 drtobes Exp $
+ */
+public class TMCDataStore<T extends Collectable> extends TMCCache<T> {
+  private static int i = 1;
+
+  public TMCDataStore() {
+    super();
+  }
+
+  public int store(T t) {
+    Integer j = i++;
+    String name;
+    name = t.getName();
+    index.put(name, j);
+    cache.put(j, t);
+    return j;
+  }
+
+}
\ No newline at end of file

Added: incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCSet.java
URL: http://svn.apache.org/viewcvs/incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCSet.java?rev=392982&view=auto
==============================================================================
--- incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCSet.java (added)
+++ incubator/lokahi/lokahi/trunk/src/java/lokahi/core/common/collection/TMCSet.java Mon Apr 10 09:19:33 2006
@@ -0,0 +1,55 @@
+/*
+* 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.common.collection;
+
+import java.util.Collection;
+import java.util.HashSet;
+
+/**
+ * This Set is an extention of HashSet that does not accept <i>null</i>
+ * elements.  All other properties remain the same.
+ *
+ * @author Stephen Toback
+ * @version $Id: TMCSet.java,v 1.2 2006/03/06 22:31:55 drtobes Exp $
+ */
+public class TMCSet<E> extends HashSet<E> {
+
+  public TMCSet() {
+    super();
+  }
+
+  public TMCSet(Collection<? extends E> c) {
+    super(c);
+  }
+
+  public TMCSet(int initialCapacity) {
+    super(initialCapacity);
+  }
+
+  public TMCSet(int initialCapacity, float loadFactor) {
+    super(initialCapacity, loadFactor);
+  }
+
+  public boolean add(E o) {
+    boolean ret = false;
+    if (o != null) {
+      ret = super.add(o);
+    }
+    return ret;
+  }
+}
+
+



Mime
View raw message