001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.util.ArrayList;
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Set;
022:
023: import javax.swing.ImageIcon;
024:
025: import org.apache.log4j.Logger;
026: import org.apache.xmlbeans.XmlException;
027: import org.apache.xmlbeans.XmlOptions;
028:
029: import com.eviware.soapui.SoapUI;
030: import com.eviware.soapui.config.SoapuiWorkspaceDocumentConfig;
031: import com.eviware.soapui.config.WorkspaceProjectConfig;
032: import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
033: import com.eviware.soapui.impl.wsdl.WsdlProject;
034: import com.eviware.soapui.model.ModelItem;
035: import com.eviware.soapui.model.project.Project;
036: import com.eviware.soapui.model.settings.Settings;
037: import com.eviware.soapui.model.support.AbstractModelItem;
038: import com.eviware.soapui.model.workspace.Workspace;
039: import com.eviware.soapui.model.workspace.WorkspaceListener;
040: import com.eviware.soapui.support.SoapUIException;
041: import com.eviware.soapui.support.UISupport;
042:
043: /**
044: * Default Workspace implementation
045: *
046: * @author Ole.Matzura
047: */
048:
049: public class WorkspaceImpl extends AbstractModelItem implements
050: Workspace {
051: private final static Logger log = Logger
052: .getLogger(WorkspaceImpl.class);
053: private List<Project> projectList = new ArrayList<Project>();
054: private SoapuiWorkspaceDocumentConfig workspaceConfig;
055: private String path = null;
056: private Set<WorkspaceListener> listeners = new HashSet<WorkspaceListener>();
057: private ImageIcon workspaceIcon;
058: private XmlBeansSettingsImpl settings;
059:
060: public WorkspaceImpl(String path) throws XmlException, IOException {
061: File file = new File(path);
062: this .path = file.getAbsolutePath();
063: loadWorkspace(file);
064: workspaceIcon = UISupport.createImageIcon("/workspace.gif");
065: }
066:
067: public void changeWorkspace(File file) throws SoapUIException {
068: // check first if valid workspace file
069: if (file.exists()) {
070: try {
071: SoapuiWorkspaceDocumentConfig.Factory.parse(file);
072: } catch (Exception e) {
073: throw new SoapUIException("Failed to load workspace: "
074: + e.toString());
075: }
076: }
077:
078: while (projectList.size() > 0) {
079: Project project = projectList.remove(0);
080: try {
081: fireProjectRemoved(project);
082: } finally {
083: project.release();
084: }
085: }
086:
087: try {
088: String oldName = getName();
089:
090: loadWorkspace(file);
091: this .path = file.getAbsolutePath();
092:
093: for (Project project : projectList) {
094: fireProjectAdded(project);
095: }
096:
097: notifyPropertyChanged(ModelItem.NAME_PROPERTY, oldName,
098: getName());
099: } catch (Exception e) {
100: SoapUI.logError(e);
101: }
102: }
103:
104: private void loadWorkspace(File file) throws XmlException,
105: IOException {
106: if (file.exists()) {
107: log.info("Loading workspace from ["
108: + file.getAbsolutePath() + "]");
109: workspaceConfig = SoapuiWorkspaceDocumentConfig.Factory
110: .parse(file);
111: if (workspaceConfig.getSoapuiWorkspace().getSettings() == null)
112: workspaceConfig.getSoapuiWorkspace().addNewSettings();
113:
114: settings = new XmlBeansSettingsImpl(this , SoapUI
115: .getSettings(), workspaceConfig
116: .getSoapuiWorkspace().getSettings());
117:
118: List<WorkspaceProjectConfig> projects = workspaceConfig
119: .getSoapuiWorkspace().getProjectList();
120: for (int i = 0; i < projects.size(); i++) {
121: WorkspaceProjectConfig wsc = projects.get(i);
122: String str = wsc.getStringValue();
123:
124: if (new File(str).exists()) {
125: try {
126: WsdlProject project = new WsdlProject(str, this );
127: projectList.add(project);
128: } catch (Exception e) {
129: UISupport
130: .showErrorMessage("Failed to load project ["
131: + str
132: + "]\nfrom workspace; "
133: + e.getMessage());
134:
135: SoapUI.logError(e);
136: }
137: } else {
138: UISupport.showErrorMessage("project [" + str
139: + "]\nnot found, disabling in workspace");
140:
141: try {
142: WsdlProject project = new WsdlProject(str,
143: this , false);
144: projectList.add(project);
145: } catch (Exception e) {
146: SoapUI.logError(e);
147: }
148: }
149: }
150: } else {
151: workspaceConfig = SoapuiWorkspaceDocumentConfig.Factory
152: .newInstance();
153: workspaceConfig.addNewSoapuiWorkspace().setName("Projects");
154: workspaceConfig.getSoapuiWorkspace().addNewSettings();
155:
156: settings = new XmlBeansSettingsImpl(this , SoapUI
157: .getSettings(), workspaceConfig
158: .getSoapuiWorkspace().getSettings());
159: }
160: }
161:
162: public void setPath(String path) {
163: this .path = path;
164: }
165:
166: public Project[] getProjects() {
167: return projectList.toArray(new Project[projectList.size()]);
168: }
169:
170: public void setName(String name) {
171: String oldName = getName();
172:
173: workspaceConfig.getSoapuiWorkspace().setName(name);
174: notifyPropertyChanged(ModelItem.NAME_PROPERTY, oldName, name);
175: }
176:
177: public void setDescription(String description) {
178: String oldDescription = getDescription();
179:
180: workspaceConfig.getSoapuiWorkspace()
181: .setDescription(description);
182: notifyPropertyChanged(ModelItem.DESCRIPTION_PROPERTY,
183: oldDescription, description);
184: }
185:
186: public String getName() {
187: return workspaceConfig.getSoapuiWorkspace().isSetName() ? workspaceConfig
188: .getSoapuiWorkspace().getName()
189: : "Projects";
190: }
191:
192: public Project getProjectAt(int index) {
193: return projectList.get(index);
194: }
195:
196: public Project getProjectByName(String projectName) {
197: for (Project project : projectList) {
198: if (project.getName().equals(projectName))
199: return project;
200: }
201:
202: return null;
203: }
204:
205: public int getProjectCount() {
206: return projectList.size();
207: }
208:
209: public void onClose() {
210: save(false);
211: }
212:
213: public void save(boolean workspaceOnly) {
214: try {
215: List<WorkspaceProjectConfig> projects = new ArrayList<WorkspaceProjectConfig>();
216:
217: // save projects first
218: for (int c = 0; c < getProjectCount(); c++) {
219: WsdlProject project = (WsdlProject) getProjectAt(c);
220:
221: if (!workspaceOnly) {
222: if (SoapUI.getTestMonitor()
223: .hasRunningTests(project)) {
224: log
225: .warn("Project ["
226: + project.getName()
227: + "] has running tests.. skipping save");
228: } else {
229: project.save();
230: }
231: }
232:
233: String path = project.getPath();
234: if (path != null) {
235: WorkspaceProjectConfig wpc = WorkspaceProjectConfig.Factory
236: .newInstance();
237: wpc.setStringValue(path);
238: projects.add(wpc);
239: }
240: }
241:
242: if (path == null) {
243: File file = UISupport.getFileDialogs().saveAs(this ,
244: "Save workspace", ".xml", "XML Files (*.xml)",
245: null);
246: if (file == null)
247: return;
248:
249: path = file.getAbsolutePath();
250: }
251:
252: workspaceConfig
253: .getSoapuiWorkspace()
254: .setProjectArray(
255: projects
256: .toArray(new WorkspaceProjectConfig[projects
257: .size()]));
258: workspaceConfig.getSoapuiWorkspace().setSoapuiVersion(
259: SoapUI.SOAPUI_VERSION);
260:
261: File workspaceFile = new File(path);
262: workspaceConfig.save(workspaceFile, new XmlOptions()
263: .setSavePrettyPrint());
264:
265: log.info("Saved workspace to ["
266: + workspaceFile.getAbsolutePath() + "]");
267: } catch (IOException e) {
268: log.error("Failed to save workspace: " + e.getMessage(), e);
269: }
270: }
271:
272: public void addWorkspaceListener(WorkspaceListener listener) {
273: listeners.add(listener);
274: }
275:
276: public void removeWorkspaceListener(WorkspaceListener listener) {
277: listeners.remove(listener);
278: }
279:
280: public Project importProject(String fileName)
281: throws SoapUIException {
282: File projectFile = new File(fileName);
283:
284: WsdlProject project = new WsdlProject(projectFile
285: .getAbsolutePath(), this );
286: projectList.add(project);
287:
288: fireProjectAdded(project);
289:
290: save(true);
291:
292: return project;
293: }
294:
295: public WsdlProject createProject(String name)
296: throws SoapUIException {
297: File projectFile = new File(createProjectFileName(name));
298: File file = UISupport.getFileDialogs().saveAs(this ,
299: "Create Project", ".xml", "XML Files (*.xml)",
300: projectFile);
301: if (file == null)
302: return null;
303:
304: return (WsdlProject) createProject(name, file);
305: }
306:
307: public Project createProject(String name, File file)
308: throws SoapUIException {
309: File projectFile = file;
310: while (projectFile.exists()) {
311: Boolean result = Boolean.FALSE;
312: while (!result.booleanValue()) {
313: result = UISupport.confirmOrCancel(
314: "Project File exists, overwrite?",
315: "Overwrite Project?");
316: if (result == null)
317: return null;
318: if (result.booleanValue()) {
319: projectFile.delete();
320: } else {
321: projectFile = UISupport.getFileDialogs().saveAs(
322: this , "Create Project", ".xml",
323: "XML Files (*.xml)", projectFile);
324: if (projectFile == null)
325: return null;
326: else
327: break;
328: }
329: }
330: }
331:
332: WsdlProject project = new WsdlProject(projectFile
333: .getAbsolutePath(), this );
334:
335: project.setName(name);
336: projectList.add(project);
337:
338: fireProjectAdded(project);
339:
340: try {
341: project.save();
342: } catch (IOException e) {
343: log.error("Failed to save project: " + e.getMessage(), e);
344: }
345: save(true);
346:
347: return project;
348: }
349:
350: private void fireProjectAdded(Project project) {
351: for (Iterator<WorkspaceListener> iter = listeners.iterator(); iter
352: .hasNext();) {
353: WorkspaceListener listener = iter.next();
354: listener.projectAdded(project);
355: }
356: }
357:
358: private String createProjectFileName(String name) {
359: StringBuffer result = new StringBuffer();
360: result.append(name);
361: result.append("-soapui-project.xml");
362: return result.toString();
363: }
364:
365: public void removeProject(Project project) {
366: int ix = projectList.indexOf(project);
367: if (ix == -1)
368: throw new RuntimeException("Project [" + project.getName()
369: + "] not available in workspace for removal");
370:
371: projectList.remove(ix);
372:
373: try {
374: fireProjectRemoved(project);
375: } finally {
376: project.release();
377: workspaceConfig.getSoapuiWorkspace().removeProject(ix);
378: }
379: }
380:
381: public void reloadProject(WsdlProject project)
382: throws SoapUIException {
383: int ix = projectList.indexOf(project);
384: if (ix == -1)
385: throw new RuntimeException("Project [" + project.getName()
386: + "] not available in workspace for removal");
387:
388: projectList.remove(ix);
389: fireProjectRemoved(project);
390:
391: project.release();
392: project = new WsdlProject(project.getPath(), this );
393:
394: projectList.add(ix, project);
395:
396: fireProjectAdded(project);
397:
398: workspaceConfig.getSoapuiWorkspace().getProjectArray(ix)
399: .setStringValue(project.getPath());
400: save(true);
401: }
402:
403: private void fireProjectRemoved(Project project) {
404: WorkspaceListener[] listenerArray = listeners
405: .toArray(new WorkspaceListener[listeners.size()]);
406: for (int c = 0; c < listenerArray.length; c++) {
407: listenerArray[c].projectRemoved(project);
408: }
409: }
410:
411: public ImageIcon getIcon() {
412: return workspaceIcon;
413: }
414:
415: public Settings getSettings() {
416: return settings;
417: }
418:
419: public int getIndexOfProject(Project project) {
420: return projectList.indexOf(project);
421: }
422:
423: public String getPath() {
424: return path;
425: }
426:
427: public void release() {
428: settings.release();
429:
430: for (Project project : projectList)
431: project.release();
432: }
433:
434: public List<? extends ModelItem> getProjectList() {
435: return projectList;
436: }
437:
438: public String getDescription() {
439: return workspaceConfig.getSoapuiWorkspace().getDescription();
440: }
441:
442: }
|