001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: SimpleInterface.java$
021: * $FileID: 3958$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 9/11/03 11:14 AM$
026: * $Comment: Add setConfDirectory(String) method.$
027: */
028:
029: package org.sourcejammer.client.simpletools;
030:
031: import org.sourcejammer.client.gui.CommandCentral;
032: import org.sourcejammer.client.gui.conf.GuiConf;
033: import org.sourcejammer.util.AppConfig;
034: import org.sourcejammer.util.RepeatingResponse;
035: import org.sourcejammer.project.view.Project;
036: import org.sourcejammer.client.HistoryTypeMapper;
037: import org.sourcejammer.util.SourceJammerConnectionException;
038: import org.sourcejammer.client.gui.GUICommandException;
039: import org.sourcejammer.client.NoSessionException;
040: import org.sourcejammer.client.SourceJammerClient;
041: import org.sourcejammer.client.DisplayTextLibrary;
042: import org.sourcejammer.project.NodeIterator;
043: import org.sourcejammer.project.view.NodeInfo;
044:
045: import java.net.MalformedURLException;
046: import java.io.IOException;
047: import java.io.OutputStream;
048: import java.io.PrintStream;
049:
050: /**
051: * Title: $FileName: SimpleInterface.java$<br>
052: * @author $AuthorName: Rob MacGrogan$<br>
053: * @version $VerNum: 12$<br>
054: *
055: * $Description: $<br>
056: * $KeyWordsOff: $<br><br>
057: *
058: *
059: * This singleton class provides a simplified interface into the SourceJammer client
060: * functionality for the purpose of building plugins to integrate SourceJammer into
061: * 3rd party development tools such as JBuilder and Eclipse.<br><br>
062: *
063: * To use this class, get the singleton instance, call the <code>setConfDirectory()</code>
064: * method to set the configuration, call the <code>connect()</code> method to
065: * connect to an archive, and then call the provided methods as needed.
066: *
067: */
068: public class SimpleInterface {
069:
070: private static SimpleInterface instance = new SimpleInterface();
071:
072: /**
073: * Returns the singleton instance.
074: */
075: public static SimpleInterface getInstance() {
076: return instance;
077: }
078:
079: private SimpleInterface() {
080: }
081:
082: /**
083: * Sets the configuration directory. Should be the directory that contains the
084: * conf.xml and other sourcejammer configuration files. This method must be called
085: * before any of the other methods of this class can be used.
086: */
087: public void setConfDirectory(java.io.File confDir)
088: throws IOException {
089: AppConfig.getInstance(confDir.getAbsolutePath());
090: SourceJammerClient client = SourceJammerClient
091: .getInstance(confDir.getAbsolutePath());
092: DisplayTextLibrary.initializeInstance(client
093: .getDisplayTextPropertiesFileName());
094: }
095:
096: /**
097: * Sets the configuration directory. Should be the directory that contains the
098: * conf.xml and other sourcejammer configuration files. This method must be called
099: * before any of the other methods of this class can be used.
100: */
101: public void setConfDirectory(String confDir) throws IOException {
102: java.io.File flDir = new java.io.File(confDir);
103: setConfDirectory(flDir);
104: }
105:
106: /**
107: * Connects the SimpleInterface to the specified archive. Returns the root
108: * project for the archive.
109: *
110: * @param archiveName -- Name of archive to connect to.
111: * @param url -- URL of SourceJammer server.
112: * @param userName -- name of user to use when connecting.
113: * @param password -- user's password.
114: */
115: public Project connect(String archiveName, String url,
116: String userName, String password)
117: throws MalformedURLException, IOException,
118: SourceJammerConnectionException, GUICommandException,
119: NoSessionException {
120: CommandCentral.getInstance().setServerURL(url);
121: CommandCentral.getInstance().setArchiveConnectInfo(archiveName,
122: userName, password);
123: return CommandCentral.getInstance().connectToArchive();
124: }
125:
126: /**
127: * Returns the Project object for the specified project.
128: */
129: public Project getProjectObj(long projectID)
130: throws SourceJammerConnectionException, GUICommandException {
131: return CommandCentral.getInstance().retrieveProject(projectID);
132: }
133:
134: /**
135: * Get File object for the specified file.
136: */
137: public org.sourcejammer.project.view.File getFileObj(long fileID)
138: throws SourceJammerConnectionException, GUICommandException {
139: NodeInfo flInfo = new NodeInfo();
140: flInfo.setUniqueID(fileID);
141: return CommandCentral.getInstance().getFileInfo(flInfo, null);
142: }
143:
144: /**
145: * Check in the file. Uses default after checkin action from SourceJammerClient (clientconf.xml).
146: */
147: public void checkInFile(long fileID, String fileName,
148: java.io.File targetDir, String comment) throws IOException,
149: SourceJammerConnectionException, GUICommandException {
150: checkInFile(fileID, fileName, targetDir, comment,
151: SourceJammerClient.getInstance()
152: .getOnCheckInFileAction());
153: }
154:
155: /**
156: * Check in the file. afterCheckInAction specifies action to perform on local
157: * copy of file after it is checked in. Possible options are the same as
158: * after add action options on addFile().
159: */
160: public void checkInFile(long fileID, String fileName,
161: java.io.File targetDir, String comment,
162: String afterCheckInAction) throws IOException,
163: SourceJammerConnectionException, GUICommandException {
164:
165: NodeInfo flInfo = new NodeInfo();
166: flInfo.setUniqueID(fileID);
167: flInfo.setNodeName(fileName);
168: flInfo.setNodeType(AppConfig.NodeTypes.FILE);
169: CommandCentral.getInstance().checkInFile(flInfo, targetDir,
170: comment, afterCheckInAction, null);
171: }
172:
173: /**
174: * Get the file to the specified target directory without checking it out.
175: *
176: * @param fileID -- unique id of the file.
177: * @param fileName -- name of the file (File.java, for example).
178: * @param targetDir -- directory whe the file will be "got" to.
179: * @param eolType -- the eol type to apply to all text files. This value must be
180: * in AppConfig.EndOfLineType.
181: * @param printMessage -- if <code>true</code>, messages will be printed to output stream.
182: * @param setReadOnly -- if <code>true</code>, file will be set to read-only after it's been downloaded.
183: */
184: public void getFile(long fileID, String fileName,
185: java.io.File targetDir, int eolType, boolean printMessages,
186: boolean setReadOnly) throws IOException,
187: SourceJammerConnectionException, GUICommandException {
188: NodeInfo flInfo = new NodeInfo();
189: flInfo.setFileName(fileName);
190: flInfo.setUniqueID(fileID);
191: CommandCentral.getInstance().getFile(flInfo, targetDir,
192: eolType, printMessages, setReadOnly,
193: new RepeatingResponse(), null);
194: }
195:
196: /**
197: * Streams the requested file into the passed in OutputStream.
198: *
199: * @param fileID -- unique id of the file.
200: * @param eolType -- the eol type to apply to all text files. This value must be
201: * in AppConfig.EndOfLineType.
202: * @param stmOut -- an OutputStream to receive the file.
203: */
204: public void getFileToStream(long fileID, int eolType,
205: OutputStream stmOut) throws GUICommandException,
206: IOException, SourceJammerConnectionException {
207: CommandCentral.getInstance().getFileToStream(fileID, stmOut,
208: eolType);
209: }
210:
211: /**
212: * Check out the file.
213: * @param fileID -- unique id of the file.
214: * @param fileName -- name of the file (File.java, for example).
215: * @param targetDir -- directory whe the file will be "got" to.
216: * @param eolType -- the eol type to apply to all text files. This value must be
217: * in AppConfig.EndOfLineType.
218: */
219: public void checkOutFile(long fileID, String fileName,
220: java.io.File targetDir, int eolType)
221: throws SourceJammerConnectionException,
222: GUICommandException, IOException {
223: CommandCentral.getInstance().checkOutFile(fileID, fileName,
224: targetDir, eolType, null);
225: }
226:
227: /**
228: * Check out the file and stream file to the passed in OutputStream.
229: *
230: * @param fileID -- unique id of the file.
231: * @param eolType -- the eol type to apply to all text files. This value must be
232: * in AppConfig.EndOfLineType.
233: * @param stmOut -- an OutputStream to receive the file.
234: */
235: public void checkOutFileToStream(long fileID, int eolType,
236: OutputStream stmOut)
237: throws SourceJammerConnectionException,
238: GUICommandException, IOException {
239: CommandCentral.getInstance().checkOutFileToStream(fileID,
240: stmOut, eolType);
241: }
242:
243: /**
244: * Add specified file to specified project in SourceJammer archive.
245: *
246: * @param projectID -- id of parent project for file being added.
247: * @param fileName -- full name of file.
248: * @param localPath -- java.io.File object pointing file to be added.
249: * @param historyType -- History type. Must be a value in AppConfig.FileHistoryStorageTypes.
250: * @param fileType -- File type. Must be a value in AppConfig.FileTypes, but not LABEL.
251: * @param description -- a description of the file.
252: */
253: public void addFile(long projectID, String fileName,
254: java.io.File localPath, int historyType, int fileType,
255: String description) throws SourceJammerConnectionException,
256: GUICommandException, IOException {
257: addFile(projectID, fileName, localPath, historyType, fileType,
258: description, SourceJammerClient.getInstance()
259: .getOnAddFileAction());
260: }
261:
262: /**
263: * Add specified file to specified project in SourceJammer archive.
264: *
265: * @param projectID -- id of parent project for file being added.
266: * @param fileName -- full name of file.
267: * @param localPath -- java.io.File object pointing file to be added.
268: * @param historyType -- History type. Must be a value in AppConfig.FileHistoryStorageTypes.
269: * @param fileType -- File type. Must be a value in AppConfig.FileTypes, but not LABEL.
270: * @param description -- a description of the file.
271: * @param afterAddAction -- Action to take on local file after it has been added.
272: * Choices are in SourceJammerClient--lfa_DELETE, lfa_NO_SPECIAL_ACTION, and lfa_SET_READ_ONLY.
273: */
274: public void addFile(long projectID, String fileName,
275: java.io.File localPath, int historyType, int fileType,
276: String description, String afterAddAction)
277: throws SourceJammerConnectionException,
278: GUICommandException, IOException {
279: CommandCentral.getInstance().addFile(projectID, fileName,
280: localPath.getAbsolutePath(), fileType, historyType,
281: description, afterAddAction, null);
282: }
283:
284: /**
285: * Move a file from one project to another within the SourceJammer archive.
286: *
287: * @param fileID -- unique ID of the file to be moved.
288: * @param fromProjectID -- unique ID of current parent project of file to be moved.
289: * @param toProjectID -- uniquie ID of new parent project of file to be moved.
290: */
291: public void moveFile(long fileID, long fromProjectID,
292: long toProjectID) throws SourceJammerConnectionException,
293: GUICommandException {
294: NodeInfo nd = new NodeInfo();
295: nd.setUniqueID(fileID);
296: CommandCentral.getInstance().moveFile(nd, fromProjectID,
297: toProjectID, null);
298: }
299:
300: /**
301: * Copy a file from one project to another within the SourceJammer archive.
302: *
303: * @param fileID -- unique ID of the file to be copied.
304: * @param fromProjectID -- unique ID of current parent project of file to be copied.
305: * @param toProjectID -- uniquie ID of new parent project of file to be copied.
306: */
307: public void copyFile(long fileID, long fromProjectID,
308: long toProjectID) throws SourceJammerConnectionException,
309: GUICommandException {
310: NodeInfo nd = new NodeInfo();
311: nd.setUniqueID(fileID);
312: CommandCentral.getInstance().copyFile(nd, fromProjectID,
313: toProjectID, null);
314: }
315:
316: /**
317: * Copy a project and all of its contents to a new location within a SourceJammer archive. All
318: * files within new project will start over with no history. Existing project will not
319: * be changed.
320: *
321: * @param projectID -- unique id of project to be copied.
322: * @param toProjectID -- unique id of new parent project of project to be copied.
323: * @param newProjectName -- new name to use for new project that is created by this copy action.
324: * If null, current name of project to be copied will be used.
325: */
326: public void copyProject(long projectID, long toProjectID,
327: String newProjectName)
328: throws SourceJammerConnectionException, GUICommandException {
329: CommandCentral.getInstance().copyProject(projectID,
330: toProjectID, newProjectName);
331: }
332:
333: /**
334: * Move a project and all of its contents to a new location within a SourceJammer archive. All
335: * files will maintain their exact same history once move operation is complete.
336: *
337: * @param projectID -- unique id of project to be moved.
338: * @param toProjectID -- unique id of new parent project of project to be moved.
339: * @param newProjectName -- new name to use for new project that is created by this move action.
340: * If null, current name of project to be moved will be used.
341: */
342: public void moveProject(long projectID, long toProjectID,
343: String newProjectName)
344: throws SourceJammerConnectionException, GUICommandException {
345: CommandCentral.getInstance().moveProject(projectID,
346: toProjectID, newProjectName);
347: }
348:
349: /**
350: * Add specified file to specified project in SourceJammer archive. File type and
351: * history type will be set automatically using HistoryTypeMapper.
352: *
353: * @param projectID -- id of parent project for file being added.
354: * @param fileName -- full name of file.
355: * @param localPath -- java.io.File object pointing file to be added.
356: * @param description -- a description of the file.
357: */
358: public void addFile(long projectID, String fileName,
359: java.io.File localPath, String description)
360: throws SourceJammerConnectionException,
361: GUICommandException, IOException {
362: addFile(projectID, fileName, localPath, description,
363: SourceJammerClient.getInstance().getOnAddFileAction());
364: }
365:
366: /**
367: * Add specified file to specified project in SourceJammer archive. File type and
368: * history type will be set automatically using HistoryTypeMapper.
369: *
370: * @param projectID -- id of parent project for file being added.
371: * @param fileName -- full name of file.
372: * @param localPath -- java.io.File object pointing file to be added.
373: * @param description -- a description of the file.
374: * @param afterAddAction -- Action to take on local file after it has been added.
375: * Choices are in SourceJammerClient--lfa_DELETE, lfa_NO_SPECIAL_ACTION, and lfa_SET_READ_ONLY.
376: */
377: public void addFile(long projectID, String fileName,
378: java.io.File localPath, String description,
379: String afterAddAction)
380: throws SourceJammerConnectionException,
381: GUICommandException, IOException {
382: HistoryTypeMapper typeMapper = HistoryTypeMapper.getInstance();
383: String sType = typeMapper
384: .getDefaultHistoryTypeForFile(fileName);
385: //Interpret type to file type and history type.
386: int historyType = -1;
387: int fileType = -1;
388: if (sType.equals(HistoryTypeMapper.TEXT_LIST)) {
389: fileType = AppConfig.FileTypes.TEXT;
390: historyType = AppConfig.FileHistoryStorageTypes.DIFF;
391: } else {
392: fileType = AppConfig.FileTypes.BINARY;
393: if (sType.equals(HistoryTypeMapper.BIN_COMPRESSED_LIST)) {
394: historyType = AppConfig.FileHistoryStorageTypes.ZIP;
395: } else if (sType
396: .equals(HistoryTypeMapper.BIN_CURR_ONLY_LIST)) {
397: historyType = AppConfig.FileHistoryStorageTypes.CURR_VERSION_SOURCE_ONLY;
398: } else if (sType.equals(HistoryTypeMapper.BIN_DIFF_LIST)) {
399: historyType = AppConfig.FileHistoryStorageTypes.DIFF;
400: } else if (sType.equals(HistoryTypeMapper.BIN_FULL_LIST)) {
401: historyType = AppConfig.FileHistoryStorageTypes.FULL_SOURCE;
402: } else {
403: throw new org.sourcejammer.util.BadMethodArgumentException(
404: "Unrecognized history type: " + sType);
405: }
406: }
407: addFile(projectID, fileName, localPath, historyType, fileType,
408: description, afterAddAction);
409: }
410:
411: /**
412: * Add a new project to the archive.
413: */
414: public void addProject(long parentProjectID, String projectName)
415: throws SourceJammerConnectionException, GUICommandException {
416: CommandCentral.getInstance().makeProject(parentProjectID,
417: projectName);
418: }
419:
420: /**
421: * Gets the specified label to the content directory.
422: *
423: * @param labelID -- id of the label.
424: * @param verNumber -- version number of the label that you want to get. Use
425: * <code>-1</code> to use the latest version of the label.
426: * @param targetDir -- directory where the files will be downloaded to.
427: * @param buildSubDires -- if <code>true</code>, builds all needed subdirectories.
428: * If <code>false</code>, does not download files that need to go into
429: * sub directories that do not already exist.
430: * @param eolType -- the eol type to apply to all text files. This value must be
431: * in AppConfig.EndOfLineType.
432: */
433: public void getLabel(long labelID, int verNumber,
434: java.io.File targetDir, boolean buildSubDirs, int eolType)
435: throws GUICommandException,
436: SourceJammerConnectionException, IOException {
437:
438: getLabel(labelID, verNumber, targetDir, buildSubDirs, eolType,
439: isDefaultReadOnlyOnGet());
440: }
441:
442: private boolean isDefaultReadOnlyOnGet() {
443: boolean isReadOnly = false;
444: String s = SourceJammerClient.getInstance()
445: .getOnGetFileAction();
446: if (s.equals(SourceJammerClient.lfa_SET_READ_ONLY)) {
447: isReadOnly = true;
448: }
449: return isReadOnly;
450: }
451:
452: public void getLabel(long labelID, int verNumber,
453: java.io.File targetDir, boolean buildSubDirs, int eolType,
454: boolean setReadOnly) throws GUICommandException,
455: SourceJammerConnectionException, IOException {
456: if (verNumber < 0) {
457: //get the latest version number.
458: NodeInfo flInfo = new NodeInfo();
459: flInfo.setUniqueID(labelID);
460: org.sourcejammer.project.view.File labelFile = CommandCentral
461: .getInstance().getFileInfo(flInfo, null);
462: verNumber = labelFile.childCount();
463: }
464: CommandCentral.getInstance().getLabel(labelID, verNumber,
465: targetDir, buildSubDirs, eolType, setReadOnly,
466: new RepeatingResponse());
467: }
468:
469: /**
470: * Gets the specified label to the content directory. All required subdirectories
471: * will be created if they do not already exist. All text files will be returned
472: * with the default EOL type specified in the clientconf.xml file. The latest
473: * version of the label will be used.
474: */
475: public void getLabel(long labelID, java.io.File targetDir)
476: throws GUICommandException,
477: SourceJammerConnectionException, IOException {
478: int eolType = SourceJammerClient.getInstance()
479: .getDefaultEOLType();
480: getLabel(labelID, -1, targetDir, true, eolType);
481: }
482:
483: /**
484: * Returns unique id of File based on full sourcejammer path.
485: */
486: public long getFileUniqueID(String sjPath)
487: throws GUICommandException, SourceJammerConnectionException {
488: return CommandCentral.getInstance().getFileUniqueID(sjPath);
489: }
490:
491: /**
492: * Returns unique id of Project based on full sourcejammer path.
493: */
494: public long getProjectUniqueID(String sjPath)
495: throws GUICommandException, SourceJammerConnectionException {
496: return CommandCentral.getInstance().getFileUniqueID(sjPath);
497: }
498:
499: /**
500: * Disconnect.
501: */
502: public void disconnect() throws GUICommandException,
503: SourceJammerConnectionException {
504: CommandCentral.getInstance().disconnect();
505: }
506:
507: public void setOut(PrintStream out) {
508: CommandCentral.getInstance().setAlternateOut(out);
509: }
510:
511: /**
512: * Retrieves all files in the specified Project to the specified targetDirectory.
513: */
514: public void getProject(java.io.File targetDir, Project proj,
515: boolean bRecursive, boolean bBuildSubDirs, int iEOLType,
516: boolean makeReadOnly, boolean verifyCheckIn)
517: throws GUICommandException,
518: SourceJammerConnectionException, IOException {
519: boolean bSkipThisProj = false;
520: if (!targetDir.exists()) {
521: if (bBuildSubDirs) {
522: targetDir.mkdir();
523: } else {
524: bSkipThisProj = true;
525: }
526: }
527: if (!bSkipThisProj) {
528: NodeIterator oChildren = proj.childNodes();
529: CommandCentral oCommand = CommandCentral.getInstance();
530: while (oChildren.hasMoreNodes()) {
531: NodeInfo nd = (NodeInfo) oChildren.getNextNode();
532: if (nd.getNodeType() == AppConfig.NodeTypes.FILE) {
533: if (verifyCheckIn && nd.isCheckedOut()) {
534: String msg = "File (" + nd.getNodeName()
535: + ") is checked out to user ("
536: + nd.getCheckedOutToUser()
537: + ") on date ("
538: + nd.getCheckedOutDate().toString()
539: + "). Halting project get...";
540: throw new GUICommandException(msg);
541: }
542: oCommand
543: .getFile(nd, targetDir, iEOLType,
544: makeReadOnly,
545: new RepeatingResponse(), null);
546: } else if (nd.getNodeType() == AppConfig.NodeTypes.PROJECT
547: && bRecursive) {
548: //This also may be a temporary fix.
549: java.io.File flChildDirectory = new java.io.File(
550: targetDir, nd.getNodeName());
551: Project ndChildProject = oCommand
552: .retrieveProject(nd.getUniqueID());
553: getProject(flChildDirectory, ndChildProject,
554: bRecursive, bBuildSubDirs, iEOLType,
555: makeReadOnly, verifyCheckIn);
556: }
557: }
558: }
559: }
560:
561: /**
562: * Return GuiConf object for current archive/user. GuiConf is used to
563: * obtain the user's default directory for each project in the archive.
564: */
565: public GuiConf getGuiConf() {
566: return CommandCentral.getInstance().getGuiConf();
567: }
568:
569: }
|