KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > forrestbot > webapp > util > Project


1 /*
2 * Copyright 2002-2004 The Apache Software Foundation or its licensors,
3 * as applicable.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */

17 /*
18  * Created on Feb 10, 2004
19  */

20 package org.apache.forrest.forrestbot.webapp.util;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.RandomAccessFile JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
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 JavaDoc 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 JavaDoc user) {
65         dto.setBuildable(isBuildable(user));
66         dto.setDeployable(isDeployable(user));
67     }
68
69     private boolean isBuildable(String JavaDoc user) {
70         UserManager userManager = UserManager.getInstance();
71         try {
72             for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc user) {
85         // for now we don't need to seperate deployable and buildable security
86
return isBuildable(user);
87     }
88
89     private boolean isLogged() {
90         return new File JavaDoc(getLogFile()).isFile();
91     }
92     
93     private String JavaDoc getLogFile() {
94         if (logfile == null) {
95             logfile = Config.getProperty("logs-dir") + "/" + dto.getName() + ".log";
96         }
97         return logfile;
98     }
99
100     private Date JavaDoc getLastBuilt() {
101         File JavaDoc f = new File JavaDoc(getLogFile());
102         long lm = f.lastModified();
103         if (lm == 0)
104             return null;
105         else
106             return new Date JavaDoc(lm);
107     }
108
109     private String JavaDoc getUrl() {
110         return Config.getProperty("build-url") + "/" + dto.getName() + "/";
111     }
112
113     private String JavaDoc getLogUrl() {
114         return Config.getProperty("logs-url") + "/" + dto.getName() + ".log";
115     }
116
117     private int getStatus() {
118         RandomAccessFile JavaDoc f;
119         try {
120             f = new RandomAccessFile JavaDoc(getLogFile(), "r");
121         } catch (FileNotFoundException JavaDoc 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 for 2-byte eol
128
try {
129             f.seek((int) f.length() - checkSuccess.length - 2);
130             f.read(checkSuccess, 0, checkSuccess.length);
131         } catch (IOException JavaDoc 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 JavaDoc(checkSuccess)))
136             return Constants.STATUS_SUCCESS;
137         // try for 1-byte eol
138
try {
139             f.seek((int) f.length() - checkSuccess.length - 1);
140             f.read(checkSuccess, 0, checkSuccess.length);
141         } catch (IOException JavaDoc 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 JavaDoc(checkSuccess)))
146             return Constants.STATUS_SUCCESS;
147         
148         
149         // if date is in last minute, consider it still running
150
if (getLastBuilt().getTime() > (new Date JavaDoc()).getTime() - 60 * 1000)
151             return Constants.STATUS_RUNNING;
152         
153         // default
154
return Constants.STATUS_FAILED;
155     }
156
157     /**
158      * @return Collection of type ProjectDTO
159      */

160     public static Collection JavaDoc getAllProjects() {
161
162         /* based on config files */
163         ArrayList JavaDoc sites = new ArrayList JavaDoc();
164         File JavaDoc f = new File JavaDoc(Config.getProperty("config-dir"));
165         File JavaDoc[] possibleSites = f.listFiles();
166         for (int i = 0; i < possibleSites.length; i++) {
167             if (possibleSites[i].isFile()) {
168                 String JavaDoc 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 JavaDoc project) {
183         Collection JavaDoc c = getAllProjects();
184         for (Iterator JavaDoc 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