----------------------------------------------------------------
BEFORE YOU POST, search the faq at <http://java.apache.org/faq/>
WHEN YOU POST, include all relevant version numbers, log files,
and configuration files.  Don't make us guess your problem!!!
----------------------------------------------------------------

Keith Ball wrote:
> 
> > I am using a a servlet engine as a front end for RCS.  (Don't ask me I just
> prorgram here, I was thinking some nice swing front end, but I am not in
> charge!)

I have to admit I am doing the same thing. A Servlet front end to a CVS
repository, that will check in and out projects.

I have managed to get listing a directory, checking out a project and
some other bits and pieces.

Things you should bear in mind, ie what user is jserv running as? Will
that person have permissions to create a directory in the desired
location (for instance without specifying a directory you are working in
the root directory).

Rob
----------------

Below is the source code for one file I am currently using to test the
concept.
If its easier I can mail it to you


import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class CVSProjectsCheckout extends HttpServlet {

        private BufferedReader in = null;
        private BufferedReader error = null;
        private String line = null;

        String username;
        String project;
        String directory;
        
        PrintWriter out = null;
        
        Runtime runtime = null;
                
        Process process = null;
                
        public void init(ServletConfig config) throws ServletException {
                super.init(config);
                runtime = Runtime.getRuntime();
        }


        public void doGet(HttpServletRequest req, HttpServletResponse res) 
throws ServletException, IOException {

                res.setContentType("text/html");

                out = res.getWriter();

                username = req.getRemoteUser();
                project = req.getParameter("project");
                directory = "test";

                String command2End = "cd ~/ ; mkdir " + directory;
                String command3End = "cd ~/ ; ls | grep -x " + directory;
//              String command4End = "cd ~/" + directory + " ; cvs checkout " +
project;
                String command4End = "./tmp/checkout " + project;
                String command5End = "cd ~/ ; ls | grep -x " + username;

                String[] command = {"sudo", "-H", "-u", username, "/bin/csh", "-c",
"cd ~/; pwd"}; //works
                String[] command2 = {"sudo", "-H", "-u", username, "/bin/csh", "-c",
command2End}; //works
                String[] command3 = {"sudo", "-H", "-u", username, "/bin/csh", "-c",
command3End}; //works
                String[] command4 = {"sudo", "-H", "-u", username, "/bin/csh", "-c",
command4End}; //works
                String[] command5 = {"sudo", "-H", "-u", "robertm", "/bin/csh", "-c",
command5End}; //works

                out.println("Welcome " + username);
                out.println("<br><br>");
                                                
                out.println("Checking " + directory + " directory exists in you home
area");
                out.println("<br><br>");
                
                boolean suc = false;
                
                if(checkForDirectory(command5) == true) {
                
                        if(checkForDirectory(command3) == true) {
                                suc = checkoutProject(command4);
                        }
                        else {
                                if (makeDirectory(command2) == true) {
                                        suc = checkoutProject(command4);
                                }
                        }
                
                        if(suc == true) {                                              
 
                                out.println("Project checked out");
                        }
                        else {
                                out.println("error checking project out");
                        }
                }
                else {
                        out.println("User " + username + " does not have a home 
directory.");
                        out.println("<br><br>");
                        out.println("Therefore the project will not be checked out");
                }
        }
        
        private boolean checkForDirectory(String[] command) throws IOException{
                try {
                        if (command != null) {
                                process = runtime.exec(command);
                        
                                in = new BufferedReader(new
InputStreamReader(process.getInputStream()));
                                error = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
                        
                                line = null;
                        
                                while((line = error.readLine()) != null) {
                                        out.println(line);
                                }
                        
                                line = in.readLine();
                                
                                if (line != null) {
                                        in.close();
                                        error.close();
                                        out.println("Directory Exists");
                                        out.println("<br><br>");
                                        process.destroy();
                                        return true;
                                }
                                else {
                                        in.close();
                                        error.close();
                                        process.destroy();
                                        return false;
                                }
                        }
                        
                } catch (Exception e) {
                        out.println("Problem with 1" + e.toString());
                        
                        in.close();
                        error.close();
                }
                        process.destroy();
                        return false;
        }
        
        private boolean checkoutProject(String[] command) throws IOException{
        
                boolean errorFlag = false;
                
                try {
                        if (command != null) {
                                out.println("Checking project out now");
                                out.println("<br><br>");
                                
                                process = runtime.exec(command);
                        
                                in = new BufferedReader(new
InputStreamReader(process.getInputStream()));
                                error = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
                        
                                line = null;
                                
                                out.println("<pre>");

                                while((line = in.readLine()) != null) {
                                        out.println(line);
                                }
                                
                                while((line = error.readLine()) != null) {
                                        out.println(line);
                                }
                                
                                process.waitFor();
                                
                                if (process.exitValue() != 0) {
                                        out.println("</pre>");
                                        
                                        in.close();
                                        error.close();
                                        process.destroy();
                                        return false;
                                }
                                
                                out.println("</pre>");
                                
                                in.close();
                                error.close();
                                process.destroy();
                                return true;
                                }
                        
                        
                } catch (Exception e) {
                        out.println("Problem with 2" + e.toString());
                        
                        in.close();
                        error.close();
                }
                process.destroy();              
                return false;
        }
        
        private boolean makeDirectory(String[] command) throws IOException {
                boolean errorFlag = false;
                
                try {
                        if (command != null) {
                                out.println("Making directory " + directory);
                                out.println("<br><br>");
                                
                                process = runtime.exec(command);
                        
                                in = new BufferedReader(new
InputStreamReader(process.getInputStream()));
                                error = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
                        
                                line = null;
                                while((line = error.readLine()) != null) {
                                        errorFlag = true;
                                        out.println(line);
                                }
                                
                                if ( errorFlag == true) {
                                        in.close();
                                        error.close();
                                        process.destroy();
                                        return false;
                                }
                        
                                while((line = in.readLine()) != null) {
                                        out.println(line);
                                }
                                
                                in.close();
                                error.close();
                                
                                out.println("Directory " + directory + " created");
                                out.println("<br><br>");
                                process.destroy();
                                return true;
                                
                        }
                        
                } catch (Exception e) {
                        out.println("Problem with 3" + e.toString());
                        
                        in.close();
                        error.close();
                }
                process.destroy();

                return false;
        }
}


--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html>
Problems?:           [EMAIL PROTECTED]

Reply via email to