1 17 20 package org.apache.forrest.forrestbot.webapp.util; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.RandomAccessFile ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Date ; 30 import java.util.Iterator ; 31 32 import org.apache.forrest.forrestbot.webapp.Constants; 33 import org.apache.forrest.forrestbot.webapp.Config; 34 import org.apache.forrest.forrestbot.webapp.dto.ProjectDTO; 35 import org.apache.log4j.Logger; 36 37 import com.opensymphony.user.Group; 38 import com.opensymphony.user.UserManager; 39 40 public class Project { 41 protected ProjectDTO dto; 42 private static Logger log = Logger.getLogger(Project.class); 43 44 private String logfile = null; 45 46 public Project() { 47 this(new ProjectDTO()); 48 } 49 public Project(ProjectDTO dto) { 50 this.dto = dto; 51 } 52 public ProjectDTO asDTO() { 53 return dto; 54 } 55 56 public void loadData() { 57 dto.setLastBuilt(getLastBuilt()); 58 dto.setUrl(getUrl()); 59 dto.setLogUrl(getLogUrl()); 60 dto.setStatus(getStatus()); 61 dto.setLogged(isLogged()); 62 } 63 64 public void loadSecurity(String user) { 65 dto.setBuildable(isBuildable(user)); 66 dto.setDeployable(isDeployable(user)); 67 } 68 69 private boolean isBuildable(String user) { 70 UserManager userManager = UserManager.getInstance(); 71 try { 72 for (Iterator i = userManager.getGroups().iterator(); i.hasNext();) { 73 Group g = (Group) i.next(); 74 if (g.containsUser(dto.getName()) && g.containsUser(user)) 75 return true; 76 } 77 } catch (Exception e) { 78 log.warn("error while checking if " + user + " has access to build " + dto.getName(), e); 79 } 80 81 return false; 82 } 83 84 private boolean isDeployable(String user) { 85 return isBuildable(user); 87 } 88 89 private boolean isLogged() { 90 return new File (getLogFile()).isFile(); 91 } 92 93 private String getLogFile() { 94 if (logfile == null) { 95 logfile = Config.getProperty("logs-dir") + "/" + dto.getName() + ".log"; 96 } 97 return logfile; 98 } 99 100 private Date getLastBuilt() { 101 File f = new File (getLogFile()); 102 long lm = f.lastModified(); 103 if (lm == 0) 104 return null; 105 else 106 return new Date (lm); 107 } 108 109 private String getUrl() { 110 return Config.getProperty("build-url") + "/" + dto.getName() + "/"; 111 } 112 113 private String getLogUrl() { 114 return Config.getProperty("logs-url") + "/" + dto.getName() + ".log"; 115 } 116 117 private int getStatus() { 118 RandomAccessFile f; 119 try { 120 f = new RandomAccessFile (getLogFile(), "r"); 121 } catch (FileNotFoundException e) { 122 log.debug("couldn't find log file for: " + getLogFile()); 123 return Constants.STATUS_UNKNOWN; 124 } 125 126 byte[] checkSuccess = new byte[Constants.BUILD_SUCCESS_STRING.length()]; 127 try { 129 f.seek((int) f.length() - checkSuccess.length - 2); 130 f.read(checkSuccess, 0, checkSuccess.length); 131 } catch (IOException e1) { 132 log.debug("couldn't find seek in log file: " + f.toString()); 133 return Constants.STATUS_UNKNOWN; 134 } 135 if (Constants.BUILD_SUCCESS_STRING.equals(new String (checkSuccess))) 136 return Constants.STATUS_SUCCESS; 137 try { 139 f.seek((int) f.length() - checkSuccess.length - 1); 140 f.read(checkSuccess, 0, checkSuccess.length); 141 } catch (IOException e1) { 142 log.debug("couldn't find seek in log file: " + f.toString()); 143 return Constants.STATUS_UNKNOWN; 144 } 145 if (Constants.BUILD_SUCCESS_STRING.equals(new String (checkSuccess))) 146 return Constants.STATUS_SUCCESS; 147 148 149 if (getLastBuilt().getTime() > (new Date ()).getTime() - 60 * 1000) 151 return Constants.STATUS_RUNNING; 152 153 return Constants.STATUS_FAILED; 155 } 156 157 160 public static Collection getAllProjects() { 161 162 163 ArrayList sites = new ArrayList (); 164 File f = new File (Config.getProperty("config-dir")); 165 File [] possibleSites = f.listFiles(); 166 for (int i = 0; i < possibleSites.length; i++) { 167 if (possibleSites[i].isFile()) { 168 String name = possibleSites[i].getName(); 169 if (name.endsWith(".xml")) { 170 ProjectDTO projectDTO = new ProjectDTO(); 171 projectDTO.setName(name.substring(0, name.length() - 4)); 172 (new Project(projectDTO)).loadData(); 173 sites.add(projectDTO); 174 } 175 } 176 } 177 Collections.sort(sites); 178 return sites; 179 } 180 181 182 public static boolean exists(String project) { 183 Collection c = getAllProjects(); 184 for (Iterator i = c.iterator(); i.hasNext();) { 185 if (((ProjectDTO)i.next()).getName().equals(project)) { 186 return true; 187 } 188 } 189 return false; 190 } 191 } 192 | Popular Tags |