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.support.action;
014:
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.lang.reflect.Constructor;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import com.eviware.soapui.SoapUI;
023: import com.eviware.soapui.config.ActionMappingPositionTypeConfig;
024: import com.eviware.soapui.config.SoapUIActionConfig;
025: import com.eviware.soapui.config.SoapUIActionGroupConfig;
026: import com.eviware.soapui.config.SoapUIActionMappingConfig;
027: import com.eviware.soapui.config.SoapUIActionsConfig;
028: import com.eviware.soapui.config.SoapuiActionsDocumentConfig;
029: import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
030: import com.eviware.soapui.model.ModelItem;
031: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
032: import com.eviware.soapui.support.action.support.DefaultActionMapping;
033: import com.eviware.soapui.support.action.support.DefaultSoapUIActionGroup;
034: import com.eviware.soapui.support.action.support.StandaloneActionMapping;
035:
036: /**
037: * Global SoapUIAction Registry
038: *
039: * @author ole.matzura
040: */
041:
042: @SuppressWarnings("unchecked")
043: public class SoapUIActionRegistry {
044: private Map<String, SoapUIAction> actions = new HashMap<String, SoapUIAction>();
045: private Map<String, SoapUIActionGroup> actionGroups = new HashMap<String, SoapUIActionGroup>();
046:
047: public void addAction(String soapuiActionID, SoapUIAction action) {
048: actions.put(soapuiActionID, action);
049: }
050:
051: public void removeAction(String soapuiActionID) {
052: actions.remove(soapuiActionID);
053: }
054:
055: public static class SeperatorAction extends AbstractSoapUIAction {
056: public static final String SOAPUI_ACTION_ID = "SeperatorAction";
057: public static SeperatorAction INSTANCE = new SeperatorAction();
058: private static SoapUIActionMapping defaultMapping = new DefaultActionMapping(
059: SeperatorAction.SOAPUI_ACTION_ID, null, null, false,
060: null);
061:
062: private SeperatorAction() {
063: super (null, null);
064: }
065:
066: public void perform(ModelItem target, Object param) {
067: }
068:
069: public static SoapUIActionMapping<WsdlMessageAssertion> getDefaultMapping() {
070: return defaultMapping;
071: }
072: }
073:
074: @SuppressWarnings("unused")
075: public static class SoapUIActionGroupAction<T extends ModelItem>
076: extends AbstractSoapUIAction<T> {
077: private SoapUIActionGroup actionGroup;
078: private final String actionGroupId;
079:
080: public SoapUIActionGroupAction(String name, String description,
081: String actionGroupId) {
082: super (name, description);
083: this .actionGroupId = actionGroupId;
084: }
085:
086: public SoapUIActionGroup getActionGroup() {
087: if (actionGroup == null)
088: actionGroup = SoapUI.getActionRegistry()
089: .getActionGroup(actionGroupId);
090: return actionGroup;
091: }
092:
093: public void perform(T target, Object param) {
094: SoapUIActionGroup group = getActionGroup();
095: List<SoapUIActionMapping<T>> mappings = group
096: .getActionMappings(target);
097: for (SoapUIActionMapping<T> mapping : mappings) {
098: if (mapping.isDefault()) {
099: ((SoapUIAction<T>) mapping.getAction()).perform(
100: target, param);
101: }
102: }
103: }
104: }
105:
106: public <T extends ModelItem> SoapUIAction<T> getAction(
107: String soapUIActionId) {
108: SoapUIAction soapUIAction = actions.get(soapUIActionId);
109: if (soapUIAction == null)
110: System.err.println("Missing action [" + soapUIActionId
111: + "]");
112: return soapUIAction;
113: }
114:
115: public SoapUIActionRegistry(InputStream config) {
116: // default actions
117: addAction(SeperatorAction.SOAPUI_ACTION_ID,
118: SeperatorAction.INSTANCE);
119:
120: if (config != null)
121: addConfig(config, getClass().getClassLoader());
122: }
123:
124: public void addConfig(InputStream config, ClassLoader classLoader) {
125: try {
126: SoapuiActionsDocumentConfig configDocument = SoapuiActionsDocumentConfig.Factory
127: .parse(config);
128: SoapUIActionsConfig soapuiActions = configDocument
129: .getSoapuiActions();
130:
131: for (SoapUIActionConfig action : soapuiActions
132: .getActionList()) {
133: try {
134: String id = action.getId();
135: Class<?> actionClass = Class.forName(action
136: .getActionClass(), true, classLoader);
137:
138: addAction(id, (SoapUIAction) actionClass
139: .newInstance());
140: } catch (Exception e) {
141: System.err.println("Error initializing Action: "
142: + e);
143: }
144: }
145:
146: for (SoapUIActionGroupConfig group : soapuiActions
147: .getActionGroupList()) {
148: SoapUIActionGroup actionGroup = null;
149:
150: // modify existing?
151: if (actionGroups.containsKey(group.getId())) {
152: actionGroup = actionGroups.get(group.getId());
153: addMappings(actionGroup, group);
154: } else {
155: if (group.isSetClass1()) {
156: Class<SoapUIActionGroup> actionGroupClass = (Class<SoapUIActionGroup>) Class
157: .forName(group.getClass1());
158:
159: Constructor<SoapUIActionGroup> constructor = actionGroupClass
160: .getConstructor(new Class[] {
161: String.class, String.class });
162: if (constructor != null) {
163: actionGroup = constructor
164: .newInstance(new Object[] {
165: group.getId(),
166: group.getName() });
167: } else {
168: actionGroup = actionGroupClass
169: .newInstance();
170: }
171: } else {
172: actionGroup = new DefaultSoapUIActionGroup(
173: group.getId(), group.getName());
174: }
175:
176: addMappings(actionGroup, group);
177: actionGroups.put(group.getId(), actionGroup);
178: }
179: }
180: } catch (Exception e) {
181: SoapUI.logError(e);
182: } finally {
183: try {
184: config.close();
185: } catch (IOException e) {
186: SoapUI.logError(e);
187: }
188: }
189: }
190:
191: private void addMappings(SoapUIActionGroup actionGroup,
192: SoapUIActionGroupConfig groupConfig) {
193: for (SoapUIActionMappingConfig mapping : groupConfig
194: .getActionMappingList()) {
195: try {
196: int insertIndex = -1;
197: if (mapping.isSetPosition()
198: && mapping.isSetPositionRef()) {
199: insertIndex = actionGroup.getMappingIndex(mapping
200: .getPositionRef());
201: if (mapping.getPosition() == ActionMappingPositionTypeConfig.AFTER)
202: insertIndex++;
203: }
204:
205: if (mapping.isSetGroupId()) {
206: SoapUIActionGroupAction actionListAction = new SoapUIActionGroupAction(
207: mapping.getName(),
208: mapping.getDescription(), mapping
209: .getGroupId());
210: StandaloneActionMapping actionMapping = new StandaloneActionMapping(
211: actionListAction);
212:
213: actionGroup.addMapping(mapping.getGroupId(),
214: insertIndex, actionMapping);
215:
216: if (mapping.isSetName())
217: actionMapping.setName(mapping.getName());
218:
219: if (mapping.isSetDescription())
220: actionMapping.setDescription(mapping
221: .getDescription());
222: } else if (mapping.getActionId().equals(
223: SeperatorAction.SOAPUI_ACTION_ID)) {
224: actionGroup.addMapping(
225: SeperatorAction.SOAPUI_ACTION_ID,
226: insertIndex,
227: (SoapUIActionMapping) SeperatorAction
228: .getDefaultMapping());
229: } else {
230: DefaultActionMapping actionMapping = new DefaultActionMapping(
231: mapping.getActionId(), mapping
232: .getKeyStroke(), mapping
233: .getIconPath(), mapping
234: .getActionId().equals(
235: groupConfig.getDefault()),
236: mapping.getParam());
237: actionGroup.addMapping(mapping.getActionId(),
238: insertIndex, actionMapping);
239:
240: if (mapping.isSetName())
241: actionMapping.setName(mapping.getName());
242:
243: if (mapping.isSetDescription())
244: actionMapping.setDescription(mapping
245: .getDescription());
246: }
247: } catch (Exception e) {
248: System.err.println("Error initializing ActionMapping: "
249: + e);
250: SoapUI.logError(e);
251: }
252: }
253: }
254:
255: public <T extends ModelItem> SoapUIActionGroup<T> getActionGroup(
256: String groupId) {
257: return actionGroups.get(groupId);
258: }
259: }
|