PageRenderTime 146ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/jain-slee/tools/eclipslee/plugin/plugins/org.mobicents.eclipslee.servicecreation/src/org/mobicents/eclipslee/servicecreation/popup/actions/EditRaTypeEventsAction.java

http://mobicents.googlecode.com/
Java | 272 lines | 180 code | 50 blank | 42 comment | 33 complexity | a22541e8e2acc8a4a36162edd1428b38 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors by the
  4. * @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.eclipslee.servicecreation.popup.actions;
  23. import java.util.HashMap;
  24. import org.eclipse.core.resources.IFile;
  25. import org.eclipse.core.runtime.IProgressMonitor;
  26. import org.eclipse.jdt.core.ICompilationUnit;
  27. import org.eclipse.jdt.core.JavaCore;
  28. import org.eclipse.jface.action.IAction;
  29. import org.eclipse.jface.dialogs.MessageDialog;
  30. import org.eclipse.jface.viewers.ISelection;
  31. import org.eclipse.jface.viewers.IStructuredSelection;
  32. import org.eclipse.jface.window.Window;
  33. import org.eclipse.swt.widgets.Shell;
  34. import org.eclipse.ui.IActionDelegate;
  35. import org.eclipse.ui.IWorkbenchPart;
  36. import org.mobicents.eclipslee.servicecreation.util.EclipseUtil;
  37. import org.mobicents.eclipslee.servicecreation.util.ResourceAdaptorTypeFinder;
  38. import org.mobicents.eclipslee.servicecreation.wizards.ratype.RaTypeEventsDialog;
  39. import org.mobicents.eclipslee.util.SLEE;
  40. import org.mobicents.eclipslee.util.slee.xml.components.ComponentNotFoundException;
  41. import org.mobicents.eclipslee.util.slee.xml.components.ResourceAdaptorTypeEventXML;
  42. import org.mobicents.eclipslee.util.slee.xml.components.ResourceAdaptorTypeXML;
  43. import org.mobicents.eclipslee.xml.ResourceAdaptorTypeJarXML;
  44. /**
  45. *
  46. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  47. */
  48. public class EditRaTypeEventsAction implements IActionDelegate {
  49. public EditRaTypeEventsAction() {
  50. }
  51. public EditRaTypeEventsAction(String raTypeID) {
  52. this.raTypeID = raTypeID;
  53. }
  54. public void setActivePart(IAction action, IWorkbenchPart targetPart) {}
  55. public void run(IAction action) {
  56. initialize();
  57. if (dialog == null) {
  58. MessageDialog.openError(new Shell(), "Error Modifying Resource Adaptor Type", getLastError());
  59. return;
  60. }
  61. if (dialog.open() == Window.OK) {
  62. try {
  63. IProgressMonitor monitor = null;
  64. HashMap newEvents[] = dialog.getSelectedEvents();
  65. // foreach event in xml
  66. // if not in newEvents
  67. // delete event from xml
  68. ResourceAdaptorTypeEventXML[] oldEvents = raType.getEvents();
  69. for (int old = 0; old < oldEvents.length; old++) {
  70. ResourceAdaptorTypeEventXML oldEvent = oldEvents[old];
  71. if (findEvent(oldEvent, newEvents) == null) {
  72. // Nuke this event.
  73. raType.removeEvent(oldEvent);
  74. }
  75. }
  76. // foreach new event
  77. // if not getEvent(name, vendor, version) -- create event
  78. for (int i = 0; i < newEvents.length; i++) {
  79. HashMap event = newEvents[i];
  80. ResourceAdaptorTypeEventXML raTypeEventXML = raType.getEvent((String) event.get("Name"), (String) event.get("Vendor"), (String) event.get("Version"));
  81. if (raTypeEventXML == null) {
  82. raTypeEventXML = raType.addEvent((String) event.get("Name"), (String) event.get("Vendor"), (String) event.get("Version"));
  83. }
  84. }
  85. // Save the XML
  86. xmlFile.setContents(raTypeJarXML.getInputStreamFromXML(), true, true, monitor);
  87. }
  88. catch (Exception e) {
  89. MessageDialog.openError(new Shell(), "Error Modifying Resource Adaptor Type", "An error occurred while modifying the resource adaptor type. It must be modified manually.");
  90. e.printStackTrace();
  91. System.err.println(e.toString() + ": " + e.getMessage());
  92. return;
  93. }
  94. }
  95. }
  96. /**
  97. * Get the RaTypeXML data object for the current selection.
  98. *
  99. */
  100. private void initialize() {
  101. String projectName = null;
  102. raType = null;
  103. raTypeJarXML = null;
  104. if (selection == null && selection.isEmpty()) {
  105. setLastError("Please select a Resource Adaptor Type's Java or XML file first.");
  106. return;
  107. }
  108. if (!(selection instanceof IStructuredSelection)) {
  109. setLastError("Please select a Resource Adaptor Type's Java or XML file first.");
  110. return;
  111. }
  112. IStructuredSelection ssel = (IStructuredSelection) selection;
  113. if (ssel.size() > 1) {
  114. setLastError("This plugin only supports editing of one resource adaptor type at a time.");
  115. return;
  116. }
  117. // Get the first (and only) item in the selection.
  118. Object obj = ssel.getFirstElement();
  119. if (obj instanceof IFile) {
  120. ICompilationUnit unit = null;
  121. try {
  122. unit = JavaCore.createCompilationUnitFrom((IFile) obj);
  123. } catch (Exception e) {
  124. // Suppress Exception. The next check checks for null unit.
  125. }
  126. if (unit != null) { // .java file
  127. raTypeJarXML = ResourceAdaptorTypeFinder.getResourceAdaptorTypeJarXML(unit);
  128. if (raTypeJarXML == null) {
  129. setLastError("Unable to find the corresponding resource-adaptor-type-jar.xml for this Resource Adaptor Type.");
  130. return;
  131. }
  132. try {
  133. raType = raTypeJarXML.getResourceAdaptorType(EclipseUtil.getClassName(unit));
  134. } catch (org.mobicents.eclipslee.util.slee.xml.components.ComponentNotFoundException e) {
  135. setLastError("Unable to find the corresponding resource-adaptor-type-jar.xml for this Resource Adaptor Type.");
  136. return;
  137. }
  138. // Set 'file' to the Resource Adaptor Type XML file, not the Java file.
  139. xmlFile = ResourceAdaptorTypeFinder.getResourceAdaptorTypeJarXMLFile(unit);
  140. acifFile = ResourceAdaptorTypeFinder.getResourceAdaptorTypeActivityContextInterfaceFactoryFile(unit);
  141. if (xmlFile == null) {
  142. setLastError("Unable to find Resource Adaptor Type XML.");
  143. return;
  144. }
  145. if (acifFile == null) {
  146. setLastError("Unable to find Resource Adaptor Type ACIF class file.");
  147. return;
  148. }
  149. projectName = unit.getJavaProject().getProject().getName();
  150. }
  151. else {
  152. IFile file = (IFile) obj;
  153. String name = SLEE.getName(raTypeID);
  154. String vendor = SLEE.getVendor(raTypeID);
  155. String version = SLEE.getVersion(raTypeID);
  156. try {
  157. raTypeJarXML = new ResourceAdaptorTypeJarXML(file);
  158. }
  159. catch (Exception e) {
  160. setLastError("Unable to find the corresponding resource-adaptor-type-jar.xml for this Resource Adaptor Type.");
  161. return;
  162. }
  163. try {
  164. raType = raTypeJarXML.getResourceAdaptorType(name, vendor, version);
  165. }
  166. catch (ComponentNotFoundException e) {
  167. setLastError("This Resource Adaptor Type is not defined in this XML file.");
  168. return;
  169. }
  170. xmlFile = file;
  171. acifFile = ResourceAdaptorTypeFinder.getResourceAdaptorTypeActivityContextInterfaceFactoryFile(xmlFile, name, vendor, version);
  172. if (acifFile == null) {
  173. setLastError("Unable to find Resource Adaptor Type ACIF class file.");
  174. return;
  175. }
  176. unit = (ICompilationUnit) JavaCore.create(acifFile);
  177. projectName = unit.getJavaProject().getProject().getName();
  178. }
  179. } else {
  180. setLastError("Unsupported object type: " + obj.getClass().toString());
  181. return;
  182. }
  183. ResourceAdaptorTypeEventXML[] events = raType.getEvents();
  184. dialog = new RaTypeEventsDialog(new Shell(), events, projectName);
  185. return;
  186. }
  187. /**
  188. * @see IActionDelegate#selectionChanged(IAction, ISelection)
  189. */
  190. public void selectionChanged(IAction action, ISelection selection) {
  191. this.selection = selection;
  192. }
  193. private void setLastError(String error) {
  194. lastError = (error == null) ? "Success" : error;
  195. }
  196. private String getLastError() {
  197. String error = lastError;
  198. setLastError(null);
  199. return error;
  200. }
  201. private HashMap findEvent(ResourceAdaptorTypeEventXML oldEvent, HashMap selectedEvents[]) {
  202. for (int i = 0; i < selectedEvents.length; i++) {
  203. String name = (String) selectedEvents[i].get("Name");
  204. String vendor = (String) selectedEvents[i].get("Vendor");
  205. String version = (String) selectedEvents[i].get("Version");
  206. if (oldEvent.getName().equals(name)
  207. && oldEvent.getVendor().equals(vendor)
  208. && oldEvent.getVersion().equals(version)) {
  209. return selectedEvents[i];
  210. }
  211. }
  212. return null;
  213. }
  214. private String raTypeID;
  215. private ResourceAdaptorTypeJarXML raTypeJarXML;
  216. private ResourceAdaptorTypeXML raType;
  217. private String lastError;
  218. private ISelection selection;
  219. private RaTypeEventsDialog dialog;
  220. private IFile xmlFile;
  221. private IFile acifFile;
  222. }