1 24 package org.riotfamily.common.web.dwr; 25 26 import java.lang.reflect.Method ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 import org.directwebremoting.Container; 33 import org.directwebremoting.extend.AccessControl; 34 import org.directwebremoting.extend.Configurator; 35 import org.directwebremoting.extend.ConverterManager; 36 import org.directwebremoting.extend.Creator; 37 import org.directwebremoting.extend.CreatorManager; 38 import org.directwebremoting.impl.SignatureParser; 39 import org.directwebremoting.spring.BeanCreator; 40 import org.springframework.beans.factory.BeanCreationException; 41 import org.springframework.util.StringUtils; 42 43 public class SpringConfigurator implements Configurator { 44 45 private Map serviceBeans; 46 47 private Class [] serviceInterfaces; 48 49 private Properties converterTypes; 50 51 private String signatures; 52 53 57 public void setServiceBeans(Map serviceBeans) { 58 this.serviceBeans = serviceBeans; 59 } 60 61 68 public void setServiceInterfaces(Class [] serviceInterfaces) { 69 this.serviceInterfaces = serviceInterfaces; 70 } 71 72 public void setConverterTypes(Properties converterTypes) { 73 this.converterTypes = converterTypes; 74 } 75 76 public void setSignatures(String signatures) { 77 this.signatures = signatures; 78 } 79 80 public void configure(Container container) { 81 ConverterManager converterManager = (ConverterManager) 82 container.getBean(ConverterManager.class.getName()); 83 84 if (converterTypes != null) { 85 Iterator it = converterTypes.entrySet().iterator(); 86 while (it.hasNext()) { 87 Map.Entry entry = (Map.Entry ) it.next(); 88 String match = (String ) entry.getKey(); 89 String type = (String ) entry.getValue(); 90 try { 91 converterManager.addConverter(match, type, Collections.EMPTY_MAP); 92 } 93 catch (IllegalArgumentException e) { 94 throw new BeanCreationException("Error adding converter", e); 95 } 96 catch (InstantiationException e) { 97 throw new BeanCreationException("Error adding converter", e); 98 } 99 catch (IllegalAccessException e) { 100 throw new BeanCreationException("Error adding converter", e); 101 } 102 } 103 } 104 105 CreatorManager creatorManager = (CreatorManager) 106 container.getBean(CreatorManager.class.getName()); 107 108 109 if (serviceBeans != null) { 110 Iterator it = serviceBeans.entrySet().iterator(); 111 while (it.hasNext()) { 112 Map.Entry entry = (Map.Entry ) it.next(); 113 String scriptName = (String ) entry.getKey(); 114 BeanCreator creator = new BeanCreator(); 115 creator.setScope(Creator.APPLICATION); 116 creator.setBean(entry.getValue()); 117 creator.afterPropertiesSet(); 118 creatorManager.addCreator(scriptName, creator); 119 } 120 } 121 122 if (serviceInterfaces != null) { 123 AccessControl accessControl = (AccessControl) container.getBean( 124 AccessControl.class.getName()); 125 126 for (int i = 0; i < serviceInterfaces.length; i++) { 127 includeMethods(accessControl, serviceInterfaces[i]); 128 } 129 } 130 131 if (StringUtils.hasText(signatures)) { 132 SignatureParser sigp = new SignatureParser(converterManager, creatorManager); 133 sigp.parse(signatures); 134 } 135 } 136 137 private void includeMethods(AccessControl accessControl, 138 Class serviceInterface) { 139 140 Iterator it = serviceBeans.entrySet().iterator(); 141 while (it.hasNext()) { 142 Map.Entry entry = (Map.Entry ) it.next(); 143 if (serviceInterface.isInstance(entry.getValue())) { 144 String scriptName = (String ) entry.getKey(); 145 includeMethods(accessControl, serviceInterface, scriptName); 146 } 147 } 148 } 149 150 private void includeMethods(AccessControl accessControl, 151 Class serviceInterface, String scriptName) { 152 153 Method [] methods = serviceInterface.getDeclaredMethods(); 154 for (int i = 0; i < methods.length; i++) { 155 accessControl.addIncludeRule(scriptName, methods[i].getName()); 156 } 157 } 158 159 } 160 | Popular Tags |