KickJava   Java API By Example, From Geeks To Geeks.

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


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.filestorage.IFileStorageAppSoap;
41 import com.vasoftware.sf.soap42.webservices.frs.FrsFileSoapList;
42 import com.vasoftware.sf.soap42.webservices.frs.FrsFileSoapRow;
43 import com.vasoftware.sf.soap42.webservices.frs.IFrsAppSoap;
44 import com.vasoftware.sf.soap42.webservices.sfmain.ISourceForgeSoap;
45 import net.sourceforge.cruisecontrol.CruiseControlException;
46 import net.sourceforge.cruisecontrol.util.ValidationHelper;
47 import org.jdom.Element;
48
49 import javax.activation.DataHandler JavaDoc;
50 import java.io.File JavaDoc;
51 import java.net.MalformedURLException JavaDoc;
52 import java.rmi.RemoteException JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.Collection JavaDoc;
55 import java.util.Iterator JavaDoc;
56
57 /**
58  * <p>Publishes to a SourceForge Enterprise Edition File Release System.</p>
59  *
60  * @author <a HREF="mailto:kspillne@thoughtworks.com">Kent Spillner</a>
61  * @author <a HREF="mailto:pj@thoughtworks.com">Paul Julius</a>
62  */

63 public class SfeeFrsPublisher extends SfeePublisher {
64     private File JavaDoc file;
65     private String JavaDoc releaseID;
66     private String JavaDoc uploadName;
67
68     public void setFile(String JavaDoc filename) {
69         this.file = new File JavaDoc(filename);
70     }
71
72     public void setReleaseID(String JavaDoc releaseID) {
73         this.releaseID = releaseID;
74     }
75
76     public void setUploadname(String JavaDoc uploadname) {
77         uploadName = uploadname;
78     }
79
80     public void publish(Element cruisecontrolLog) throws CruiseControlException {
81         ValidationHelper.assertExists(getFile(), "file", this.getClass());
82         ValidationHelper.assertIsNotDirectory(getFile(), "file", this.getClass());
83         ValidationHelper.assertIsReadable(getFile(), "file", this.getClass());
84
85         if (uploadName == null) {
86             uploadName = getFile().getName();
87         }
88
89         ISourceForgeSoap soap = (ISourceForgeSoap) ClientSoapStubFactory
90                 .getSoapStub(ISourceForgeSoap.class, getServerURL());
91         try {
92             String JavaDoc sessionID = soap.login(getUsername(), getPassword());
93
94             IFrsAppSoap frsApp = (IFrsAppSoap) ClientSoapStubFactory.getSoapStub(IFrsAppSoap.class, getServerURL());
95
96             FrsFileSoapList fileList = frsApp.getFrsFileList(sessionID, releaseID);
97             Collection JavaDoc existingFiles = findExistingFiles(fileList, uploadName);
98             for (Iterator JavaDoc i = existingFiles.iterator(); i.hasNext();) {
99                 String JavaDoc id = (String JavaDoc) i.next();
100                 frsApp.deleteFrsFile(sessionID, id);
101             }
102
103             DataHandler JavaDoc dataHandler = new DataHandler JavaDoc(getFile().toURL());
104             IFileStorageAppSoap fileStorageApp =
105                     (IFileStorageAppSoap) ClientSoapStubFactory.getSoapStub(IFileStorageAppSoap.class, getServerURL());
106             String JavaDoc storedFileId = fileStorageApp.uploadFile(sessionID, dataHandler);
107             frsApp.createFrsFile(sessionID, releaseID, uploadName, dataHandler.getContentType(), storedFileId);
108         } catch (RemoteException JavaDoc e) {
109             throw new CruiseControlException(e);
110         } catch (MalformedURLException JavaDoc e) {
111             throw new CruiseControlException(e);
112         }
113
114
115     }
116
117     protected File JavaDoc getFile() {
118         return file;
119     }
120
121     private static Collection JavaDoc findExistingFiles(FrsFileSoapList fileList, String JavaDoc filename) {
122         FrsFileSoapRow[] files = fileList.getDataRows();
123         ArrayList JavaDoc duplicates = new ArrayList JavaDoc();
124         for (int i = 0; i < files.length; i++) {
125             FrsFileSoapRow nextFile = files[i];
126             if (nextFile.getFilename().equals(filename)) {
127                 duplicates.add(nextFile.getId());
128             }
129         }
130         return duplicates;
131     }
132
133     public void subValidate() throws CruiseControlException {
134         ValidationHelper.assertIsSet(releaseID, "releaseid", this.getClass());
135         ValidationHelper.assertIsSet(getFile(), "file", this.getClass());
136     }
137 }
138
Popular Tags