1 16 17 package com.google.inject.struts2; 18 19 import com.google.inject.AbstractModule; 20 import com.google.inject.Guice; 21 import com.google.inject.Injector; 22 import com.google.inject.Module; 23 import com.google.inject.servlet.ServletModule; 24 import com.opensymphony.xwork2.ActionInvocation; 25 import com.opensymphony.xwork2.ObjectFactory; 26 import com.opensymphony.xwork2.config.ConfigurationException; 27 import com.opensymphony.xwork2.config.entities.InterceptorConfig; 28 import com.opensymphony.xwork2.inject.Inject; 29 import com.opensymphony.xwork2.interceptor.Interceptor; 30 import java.util.HashSet ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.logging.Logger ; 34 35 public class GuiceObjectFactory extends ObjectFactory { 36 37 static final Logger logger = 38 Logger.getLogger(GuiceObjectFactory.class.getName()); 39 40 Module module; 41 Injector injector; 42 boolean developmentMode = false; 43 44 @Override  45 public boolean isNoArgConstructorRequired() { 46 return false; 47 } 48 49 @Inject(value = "guice.module", required = false) 50 void setModule(String moduleClassName) { 51 try { 52 @SuppressWarnings ({"unchecked"}) 54 Class <? extends Module> moduleClass = 55 (Class <? extends Module>) Class.forName(moduleClassName); 56 this.module = moduleClass.newInstance(); 57 } catch (Exception e) { 58 throw new RuntimeException (e); 59 } 60 } 61 62 @Inject(value = "struts.devMode", required = false) 63 void setDevelopmentMode(String developmentMode) { 64 this.developmentMode = developmentMode.trim().equals("true"); 65 } 66 67 Set <Class <?>> boundClasses = new HashSet <Class <?>>(); 68 69 public Class getClassInstance(String name) throws ClassNotFoundException { 70 Class <?> clazz = super.getClassInstance(name); 71 72 synchronized (this) { 73 if (injector == null) { 74 if (!boundClasses.contains(clazz)) { 76 try { 77 clazz.getDeclaredFields(); 80 clazz.getDeclaredMethods(); 81 82 boundClasses.add(clazz); 83 } catch (Throwable t) { 84 return clazz; 88 } 89 } 90 } 91 } 92 93 return clazz; 94 } 95 96 public Object buildBean(Class clazz, Map extraContext) { 97 synchronized (this) { 98 if (injector == null) { 99 try { 100 logger.info("Creating injector..."); 101 this.injector = Guice.createInjector(new AbstractModule() { 102 protected void configure() { 103 install(new ServletModule()); 104 105 for (Class <?> boundClass : boundClasses) { 108 bind(boundClass); 109 } 110 } 111 }); 112 } catch (Throwable t) { 113 t.printStackTrace(); 114 System.exit(1); 115 } 116 logger.info("Injector created successfully."); 117 } 118 } 119 120 return injector.getInstance(clazz); 121 } 122 123 public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, 124 Map interceptorRefParams) throws ConfigurationException { 125 try { 126 getClassInstance(interceptorConfig.getClassName()); 127 } catch (ClassNotFoundException e) { 128 throw new RuntimeException (e); 129 } 130 131 return new LazyLoadedInterceptor(interceptorConfig, interceptorRefParams); 135 } 136 137 Interceptor superBuildInterceptor(InterceptorConfig interceptorConfig, 138 Map interceptorRefParams) throws ConfigurationException { 139 return super.buildInterceptor(interceptorConfig, interceptorRefParams); 140 } 141 142 class LazyLoadedInterceptor implements Interceptor { 143 144 final InterceptorConfig config; 145 final Map params; 146 147 LazyLoadedInterceptor(InterceptorConfig config, Map params) { 148 this.config = config; 149 this.params = params; 150 } 151 152 Interceptor delegate = null; 153 154 synchronized Interceptor getDelegate() { 155 if (delegate == null) { 156 delegate = superBuildInterceptor(config, params); 157 delegate.init(); 158 } 159 return delegate; 160 } 161 162 public void destroy() { 163 getDelegate().destroy(); 164 } 165 166 public void init() { 167 throw new AssertionError (); 168 } 169 170 public String intercept(ActionInvocation invocation) throws Exception { 171 return getDelegate().intercept(invocation); 172 } 173 } 174 } 175 | Popular Tags |