KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > sfee > SfeeDocumentManagerPublisher


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.publishers.sfee;
38
39 import com.vasoftware.sf.soap42.webservices.ClientSoapStubFactory;
40 import com.vasoftware.sf.soap42.webservices.docman.DocumentFolderSoapDO;
41 import com.vasoftware.sf.soap42.webservices.docman.DocumentFolderSoapList;
42 import com.vasoftware.sf.soap42.webservices.docman.DocumentFolderSoapRow;
43 import com.vasoftware.sf.soap42.webservices.docman.DocumentSoapDO;
44 import com.vasoftware.sf.soap42.webservices.docman.DocumentSoapList;
45 import com.vasoftware.sf.soap42.webservices.docman.DocumentSoapRow;
46 import com.vasoftware.sf.soap42.webservices.docman.IDocumentAppSoap;
47 import com.vasoftware.sf.soap42.webservices.filestorage.IFileStorageAppSoap;
48 import com.vasoftware.sf.soap42.webservices.sfmain.ISourceForgeSoap;
49 import net.sourceforge.cruisecontrol.CruiseControlException;
50 import net.sourceforge.cruisecontrol.util.ValidationHelper;
51 import net.sourceforge.cruisecontrol.util.XPathAwareChild;
52 import org.jdom.Element;
53
54 import javax.activation.DataHandler JavaDoc;
55 import javax.activation.DataSource JavaDoc;
56 import javax.activation.FileDataSource JavaDoc;
57 import java.rmi.RemoteException JavaDoc;
58 import java.util.StringTokenizer JavaDoc;
59
60 /**
61  * @author <a HREF="mailto:pj@thoughtworks.com">Paul Julius</a>
62  * @author <a HREF="mailto:krs@thoughtworks.com">Kent Spillner</a>
63  */

64 public class SfeeDocumentManagerPublisher extends SfeePublisher {
65
66     private String JavaDoc projectName;
67     private XPathAwareChild documentName;
68     private XPathAwareChild description;
69     private DataSource JavaDoc dataSrc;
70     private String JavaDoc path;
71     private Status status;
72     private XPathAwareChild versionComment;
73     private boolean lock;
74     private String JavaDoc documentPath;
75
76     public Status createStatus() {
77         status = new Status();
78         return status;
79     }
80
81     public XPathAwareChild createVersionComment() {
82         versionComment = new XPathAwareChild();
83         return versionComment;
84     }
85
86     public void setLock(boolean lock) {
87         this.lock = lock;
88     }
89
90     public void setData(DataSource JavaDoc dataSrc) {
91         this.dataSrc = dataSrc;
92     }
93
94     public XPathAwareChild createDocumentName() {
95         documentName = new XPathAwareChild();
96         return documentName;
97     }
98
99     public XPathAwareChild createDescription() {
100         description = new XPathAwareChild();
101         return description;
102     }
103
104     public void setProjectName(String JavaDoc name) {
105         projectName = name;
106     }
107
108     public void setFolder(String JavaDoc path) {
109         this.path = path;
110     }
111
112     public void publish(Element cruisecontrolLog) throws CruiseControlException {
113
114         ISourceForgeSoap sfee = (ISourceForgeSoap) ClientSoapStubFactory
115                 .getSoapStub(ISourceForgeSoap.class, getServerURL());
116         String JavaDoc sessionID;
117         try {
118             sessionID = sfee.login(getUsername(), getPassword());
119         } catch (RemoteException JavaDoc e) {
120             throw new CruiseControlException(e);
121         }
122
123         IFileStorageAppSoap fileStorage = (IFileStorageAppSoap) ClientSoapStubFactory
124                 .getSoapStub(IFileStorageAppSoap.class, getServerURL());
125         String JavaDoc fileID;
126         String JavaDoc mimeType;
127         try {
128             if (documentPath != null) {
129                 dataSrc = new FileDataSource JavaDoc(documentPath);
130             }
131             DataHandler JavaDoc dataHandler = new DataHandler JavaDoc(dataSrc);
132             fileID = fileStorage.uploadFile(sessionID, dataHandler);
133             mimeType = dataHandler.getContentType();
134         } catch (RemoteException JavaDoc e) {
135             throw new CruiseControlException(e);
136         }
137
138         DocumentFolderSoapDO folder = findFolder(path);
139         String JavaDoc intendedDocumentName =
140                 (documentName != null ? documentName.lookupValue(cruisecontrolLog) : dataSrc.getName());
141         String JavaDoc intendedVersionComment =
142                 (versionComment != null ? versionComment.lookupValue(cruisecontrolLog) : null);
143
144         DocumentSoapDO existingDocument;
145         try {
146             existingDocument = findExistingDocument(sessionID, folder, intendedDocumentName);
147         } catch (RemoteException JavaDoc e) {
148             throw new CruiseControlException(e);
149         }
150
151         if (existingDocument != null) {
152             updateExistingDocument(sessionID, existingDocument, intendedVersionComment, mimeType, fileID,
153                     cruisecontrolLog);
154         } else {
155             createNewDocument(cruisecontrolLog, sessionID, intendedDocumentName, intendedVersionComment, folder,
156                     mimeType, fileID);
157         }
158     }
159
160     private void updateExistingDocument(String JavaDoc sessionID, DocumentSoapDO existingDocument, String JavaDoc versionComment,
161                                         String JavaDoc mimeType,
162                                         String JavaDoc fileID, Element cruiseControlLog) throws CruiseControlException {
163         IDocumentAppSoap docApp = (IDocumentAppSoap) ClientSoapStubFactory
164                 .getSoapStub(IDocumentAppSoap.class, getServerURL());
165         existingDocument.setDescription(description.lookupValue(cruiseControlLog));
166         existingDocument.setStatus(status.lookupValue(cruiseControlLog));
167         if (lock) {
168             existingDocument.setLockedBy(getUsername());
169         } else {
170             existingDocument.setLockedBy(null);
171         }
172         existingDocument.setMimeType(mimeType);
173         existingDocument.setVersionComment(versionComment);
174
175         try {
176             docApp.setDocumentData(sessionID, existingDocument, fileID);
177         } catch (RemoteException JavaDoc e) {
178             throw new CruiseControlException(e);
179         }
180     }
181
182     private DocumentSoapDO findExistingDocument(String JavaDoc sessionID, DocumentFolderSoapDO folder,
183                                                 String JavaDoc intendedDocumentName)
184             throws RemoteException JavaDoc {
185         IDocumentAppSoap docApp = (IDocumentAppSoap) ClientSoapStubFactory
186                 .getSoapStub(IDocumentAppSoap.class, getServerURL());
187         DocumentSoapList documentList = docApp.getDocumentList(sessionID, folder.getId(), null);
188         DocumentSoapRow[] existingDocuments = documentList.getDataRows();
189         for (int i = 0; i < existingDocuments.length; i++) {
190             DocumentSoapRow existingDocument = existingDocuments[i];
191             if (existingDocument.getTitle().equals(intendedDocumentName)) {
192                 return docApp
193                         .getDocumentData(sessionID, existingDocument.getId(), existingDocument.getCurrentVersion());
194             }
195         }
196         return null;
197     }
198
199     private void createNewDocument(Element cruisecontrolLog, String JavaDoc sessionID, String JavaDoc intendedDocumentName,
200                                    String JavaDoc intendedVersionComment, DocumentFolderSoapDO folder, String JavaDoc mimeType,
201                                    String JavaDoc fileID) throws
202             CruiseControlException {
203         IDocumentAppSoap docApp = (IDocumentAppSoap) ClientSoapStubFactory
204                 .getSoapStub(IDocumentAppSoap.class, getServerURL());
205         try {
206             docApp.createDocument(sessionID, folder.getId(), intendedDocumentName,
207                     description.lookupValue(cruisecontrolLog), intendedVersionComment,
208                     status.lookupValue(cruisecontrolLog), lock, intendedDocumentName, mimeType,
209                     fileID);
210         } catch (RemoteException JavaDoc e) {
211             throw new CruiseControlException(e);
212         }
213     }
214
215     public void subValidate() throws CruiseControlException {
216         ValidationHelper.assertIsSet(path, "folder", SfeeDocumentManagerPublisher.class);
217         ValidationHelper.assertIsSet(projectName, "projectName", SfeeDocumentManagerPublisher.class);
218         if (documentPath == null && dataSrc == null) {
219             throw new CruiseControlException("Either a document or a datasource must be specified.");
220         }
221         ValidationHelper.assertHasChild(description, "description", SfeeDocumentManagerPublisher.class);
222         description.validate();
223         ValidationHelper.assertHasChild(status, "status", SfeeDocumentManagerPublisher.class);
224         status.validate();
225         if (documentName != null) {
226             documentName.validate();
227         }
228         if (versionComment != null) {
229             versionComment.validate();
230         }
231     }
232
233     DocumentFolderSoapDO findFolder(String JavaDoc path) throws CruiseControlException {
234         if (path == null) {
235             throw new IllegalArgumentException JavaDoc("path can not be null.");
236         }
237
238         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(path, "/");
239
240         DocumentFolderSoapDO folder;
241         try {
242             ISourceForgeSoap soap = (ISourceForgeSoap) ClientSoapStubFactory
243                     .getSoapStub(ISourceForgeSoap.class, getServerURL());
244             String JavaDoc sessionID = soap.login(getUsername(), getPassword());
245             String JavaDoc projectID = SfeeUtils.findProjectID(soap, sessionID, projectName);
246
247             IDocumentAppSoap docApp = (IDocumentAppSoap) ClientSoapStubFactory
248                     .getSoapStub(IDocumentAppSoap.class, getServerURL());
249
250             folder = findFolderInternal(sessionID, projectID, tokens, docApp);
251
252         } catch (RemoteException JavaDoc e) {
253             throw new CruiseControlException(e);
254         }
255
256         if (folder == null) {
257             throw new CruiseControlException("Unable to find a folder for the path specified [" + path + "].");
258         }
259
260         return folder;
261     }
262
263     /**
264      * Recursively traverses the folder tree until the desired one is found.
265      *
266      * @return null if folder not found.
267      */

268     private static DocumentFolderSoapDO findFolderInternal(String JavaDoc sessionID, String JavaDoc parentID,
269                                                            StringTokenizer JavaDoc pathTokens, IDocumentAppSoap docApp)
270             throws RemoteException JavaDoc {
271
272         if (!pathTokens.hasMoreTokens()) {
273             return null;
274         }
275
276         DocumentFolderSoapList folderList = docApp.getDocumentFolderList(sessionID, parentID, false);
277         DocumentFolderSoapRow[] folders = folderList.getDataRows();
278         String JavaDoc nextPathElement = pathTokens.nextToken();
279         boolean isFinalPathElement = !pathTokens.hasMoreTokens();
280
281         for (int i = 0; i < folders.length; i++) {
282             DocumentFolderSoapRow nextFolder = folders[i];
283             String JavaDoc nextFolderTitle = nextFolder.getTitle();
284             boolean folderNamesMatch = nextFolderTitle.equals(nextPathElement);
285             if (folderNamesMatch && isFinalPathElement) {
286                 return docApp.getDocumentFolderData(sessionID, nextFolder.getId());
287             } else if (folderNamesMatch) {
288                 return findFolderInternal(sessionID, nextFolder.getId(), pathTokens, docApp);
289             }
290         }
291
292         return null;
293     }
294
295     public void setDocument(String JavaDoc documentPath) {
296         this.documentPath = documentPath;
297     }
298
299
300     public static class Status extends XPathAwareChild {
301         public void validate() throws CruiseControlException {
302
303             if (getXpathExpression() == null && getFixedValue() != null
304                     && !(getFixedValue().equalsIgnoreCase("final") || getFixedValue().equalsIgnoreCase("draft"))) {
305                 throw new CruiseControlException(
306                         "Status value specified [] is not valid. Valid values are 'final' and 'draft'.");
307             }
308
309             super.validate();
310         }
311     }
312 }
313
Popular Tags