001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud.connection.local;
023:
024: import gruntspud.Constants;
025: import gruntspud.StreamSink;
026: import gruntspud.connection.CVSRootDefaultConnection;
027:
028: import java.io.IOException;
029: import java.util.Locale;
030: import java.util.ResourceBundle;
031:
032: import org.netbeans.lib.cvsclient.connection.AuthenticationException;
033: import org.netbeans.lib.cvsclient.connection.ConnectionModifier;
034: import org.netbeans.lib.cvsclient.util.LoggedDataInputStream;
035: import org.netbeans.lib.cvsclient.util.LoggedDataOutputStream;
036:
037: /**
038: * A <strong>:local</strong> connection is similar to <strong>:ext</strong> except it
039: * assumes the CVS command is avaiable locally. This class executes that command
040: * and manages the streams its produces.
041: *
042: * @author magicthize
043: */
044: public class CVSRootLocalConnection extends CVSRootDefaultConnection {
045: public final static String PROFILE_PROPERTY_ALTERNATIVE_CVS_COMMAND = "alternativeCVSCommand";
046: static ResourceBundle res = ResourceBundle.getBundle(
047: "gruntspud.connection.local.ResourceBundle", Locale
048: .getDefault(), CVSRootLocalConnection.class
049: .getClassLoader());
050:
051: protected LoggedDataInputStream inputStream = null;
052: protected LoggedDataOutputStream outputStream = null;
053: protected Process process = null;
054:
055: /* (non-Javadoc)
056: * @see org.netbeans.lib.cvsclient.connection.Connection#getInputStream()
057: */
058: public LoggedDataInputStream getInputStream() {
059: return inputStream;
060: }
061:
062: /* (non-Javadoc)
063: * @see org.netbeans.lib.cvsclient.connection.Connection#getOutputStream()
064: */
065: public LoggedDataOutputStream getOutputStream() {
066: return outputStream;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.netbeans.lib.cvsclient.connection.Connection#isOpen()
071: */
072: public boolean isOpen() {
073: return process != null;
074: }
075:
076: private void openConnection() throws AuthenticationException {
077: try {
078: String[] envp = null;
079:
080: // Hack to get windows to work correctly (see http://www.cs.helsinki.fi/u/mjkarki/prog.html)
081: if (System.getProperty("os.name").toLowerCase().startsWith(
082: "win")) {
083: envp = new String[] {
084: "HOME=" + System.getProperty("user.home"),
085: "USERPROFILE="
086: + System.getProperty("user.home") };
087: }
088:
089: process = Runtime
090: .getRuntime()
091: .exec(
092: new String[] {
093: getProfile()
094: .getProperty(
095: PROFILE_PROPERTY_ALTERNATIVE_CVS_COMMAND,
096: "cvs"), "server" },
097: envp,
098: getContext().getViewManager().getHome());
099: outputStream = new LoggedDataOutputStream(process
100: .getOutputStream());
101: inputStream = new LoggedDataInputStream(process
102: .getInputStream());
103: new StreamSink(process.getErrorStream(), true).start();
104: } catch (Throwable throwable) {
105: try {
106: close();
107: } catch (IOException ioe) {
108: Constants.CVS_LOG.error(
109: "Failed to close local CVS process.", ioe);
110: }
111: throw new AuthenticationException(
112: "Connection error",
113: throwable,
114: res
115: .getString("cvsRootLocalConnection.error.connectionError.text"));
116: }
117: }
118:
119: private void reset() {
120: process = null;
121: inputStream = null;
122: outputStream = null;
123: }
124:
125: /* (non-Javadoc)
126: * @see org.netbeans.lib.cvsclient.connection.Connection#verify()
127: */
128: public void verify() throws AuthenticationException {
129: }
130:
131: /* (non-Javadoc)
132: * @see org.netbeans.lib.cvsclient.connection.Connection#open()
133: */
134: public void open() throws AuthenticationException {
135: openConnection();
136: }
137:
138: /* (non-Javadoc)
139: * @see org.netbeans.lib.cvsclient.connection.Connection#close()
140: */
141: public void close() throws IOException {
142: if (process != null) {
143: Constants.CONNECTION_LOG.debug("Closing local process");
144: process.destroy();
145: process = null;
146: Constants.CONNECTION_LOG.debug("Local process closed");
147: }
148: reset();
149: }
150:
151: /* (non-Javadoc)
152: * @see org.netbeans.lib.cvsclient.connection.Connection#modifyInputStream(org.netbeans.lib.cvsclient.connection.ConnectionModifier)
153: */
154: public void modifyInputStream(ConnectionModifier connectionmodifier)
155: throws IOException {
156: connectionmodifier.modifyInputStream(inputStream);
157: }
158:
159: /* (non-Javadoc)
160: * @see org.netbeans.lib.cvsclient.connection.Connection#modifyOutputStream(org.netbeans.lib.cvsclient.connection.ConnectionModifier)
161: */
162: public void modifyOutputStream(ConnectionModifier connectionmodifier)
163: throws IOException {
164: connectionmodifier.modifyOutputStream(outputStream);
165: }
166: }
|