001 /* 002 * Copyright 1999-2001,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.workflow.web; 018 019 020 import java.util.Enumeration; 021 import java.util.HashMap; 022 import javax.servlet.ServletRequest; 023 import org.apache.commons.beanutils.BeanUtils; 024 import org.apache.commons.workflow.Context; 025 import org.apache.commons.workflow.Descriptor; 026 import org.apache.commons.workflow.StepException; 027 import org.apache.commons.workflow.base.DescriptorStep; 028 029 030 /** 031 * <p>For each associated <code>Descriptor</code>, populate the properties 032 * of the bean specified by that descriptor from the request parameters of 033 * the current request.</p> 034 * 035 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ 036 * @author Craig R. McClanahan 037 */ 038 039 public class PopulateStep extends DescriptorStep { 040 041 042 // ----------------------------------------------------------= Constructors 043 044 045 /** 046 * Construct a default instance of this Step. 047 */ 048 public PopulateStep() { 049 050 super(); 051 052 } 053 054 055 /** 056 * Construct an instance of this Step with the specified identifier. 057 * 058 * @param id Step identifier 059 */ 060 public PopulateStep(String id) { 061 062 super(); 063 setId(id); 064 065 } 066 067 068 /** 069 * Construct an instance of this Step with the specified identifier 070 * and associated Descriptor. 071 * 072 * @param id Step identifier 073 * @param descriptor Initial descriptor 074 */ 075 public PopulateStep(String id, Descriptor descriptor) { 076 077 super(); 078 setId(id); 079 addDescriptor(descriptor); 080 081 } 082 083 084 // --------------------------------------------------------- Public Methods 085 086 087 /** 088 * Perform the executable actions related to this Step, in the context of 089 * the specified Context. 090 * 091 * @param context The Context that is tracking our execution state 092 * 093 * @exception StepException if a processing error has occurred 094 */ 095 public void execute(Context context) throws StepException { 096 097 // Make sure our executing Context is a WebContext 098 if (!(context instanceof WebContext)) 099 throw new StepException("Execution context is not a WebContext", 100 this); 101 WebContext webContext = (WebContext) context; 102 ServletRequest request = webContext.getServletRequest(); 103 104 // Prepare a Map of our request parameter names and values 105 // (in Servlet 2.3 we would just call request.getParameterMap()) 106 HashMap map = new HashMap(); 107 Enumeration names = request.getParameterNames(); 108 while (names.hasMoreElements()) { 109 String name = (String) names.nextElement(); 110 map.put(name, request.getParameterValues(name)); 111 } 112 113 // Process all associated descriptors 114 Descriptor descriptors[] = findDescriptors(); 115 for (int i = 0; i < descriptors.length; i++) { 116 Object value = descriptors[i].get(context); 117 if (value == null) 118 throw new StepException 119 ("Cannot retrieve object for " + descriptors[i], this); 120 try { 121 BeanUtils.populate(value, map); 122 } catch (Throwable t) { 123 throw new StepException("Populate exception", t, this); 124 } 125 } 126 127 } 128 129 130 /** 131 * Render a string representation of this Step. 132 */ 133 public String toString() { 134 135 StringBuffer sb = new StringBuffer("<web:populate"); 136 if (getId() != null) { 137 sb.append(" id=\""); 138 sb.append(getId()); 139 sb.append("\""); 140 } 141 sb.append(">"); 142 Descriptor descriptors[] = findDescriptors(); 143 for (int i = 0; i < descriptors.length; i++) 144 sb.append(descriptors[i].toString()); 145 sb.append("</web:populate>"); 146 return (sb.toString()); 147 148 } 149 150 151 }