View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package de.tivsource.ejb3plugin;
7   
8   import java.lang.reflect.Field;
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  import javax.naming.InitialContext;
14  import javax.naming.NamingException;
15  
16  import org.apache.logging.log4j.LogManager;
17  import org.apache.logging.log4j.Logger;
18  
19  import com.opensymphony.xwork2.ActionInvocation;
20  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
21  import com.opensymphony.xwork2.interceptor.Interceptor;
22  
23  import de.tivsource.ejb3plugin.cache.AnnotatedField;
24  import de.tivsource.ejb3plugin.cache.InjectEJBCache;
25  
26  /**
27   * Interceptor der nach allen Feldern die mit InjectEJB annotierten sucht und
28   * dann die angegebene EJB in das feld einfügt.
29   * 
30   * @author Marc Michele
31   * 
32   */
33  public class InjectEJBInterceptor extends AbstractInterceptor implements
34  		Interceptor {
35  
36  	/**
37  	 * Serial Version Uid der Klasse.
38  	 */
39  	private static final long serialVersionUID = 7717052849037004429L;
40  
41      /**
42       * Statischer Logger der Klasse.
43       */
44      private static final Logger LOGGER = LogManager.getLogger(InjectEJBInterceptor.class);
45  
46  	/**
47  	 * Einfacher Cache.
48  	 */
49  	private InjectEJBCache cache;
50  
51  	public InjectEJBInterceptor() {
52  		super();
53  		this.cache = InjectEJBCache.getInstance();
54  	}
55  
56  	public String intercept(ActionInvocation actionInvocation) throws Exception {
57  
58  		// Hole das Action Objekt
59  		Object action = actionInvocation.getAction();
60  
61  		// Hol den Namen der Action
62  		String actionClassName = action.getClass().getName();
63  
64  		// Frage ob die Action Klasse sich im Cache befindet und EJBs besitzt.
65  		Boolean hasEJBAnnotations = this.cache.hasEJBAnnotations(actionClassName);
66  
67  		// Wenn das Action Objekt sich im Cache befindet-
68  		if (Boolean.TRUE.equals(hasEJBAnnotations)) {
69  			// Hole die Felder aus dem Cache
70  			List<AnnotatedField> aFields = this.cache.getAnnotatedFields(action.getClass().getName());
71  			// Durchlaufe die Liste der Felder
72  			for (Iterator<AnnotatedField> it = aFields.iterator(); it.hasNext();) {
73  				// EJB Injection
74  				this.injectEJB(action, it.next());
75  			}
76  		} else if (hasEJBAnnotations == null) {
77  			List<AnnotatedField> annotatedFields = new ArrayList<AnnotatedField>();
78  
79  			for (Field f : action.getClass().getDeclaredFields()) {
80  
81  				if (f.isAnnotationPresent(InjectEJB.class)) {
82  					// Annotationen gefunden
83  
84  					AnnotatedField aField = new AnnotatedField(
85  							(InjectEJB) f.getAnnotation(InjectEJB.class), f);
86  					this.injectEJB(action, aField);
87  					annotatedFields.add(aField);
88  				}
89  			}
90  
91  			for (Field f : action.getClass().getSuperclass()
92  					.getDeclaredFields()) {
93  
94  				if (f.isAnnotationPresent(InjectEJB.class)) {
95  					// Annotationen gefunden
96  
97  					AnnotatedField aField = new AnnotatedField(
98  							(InjectEJB) f.getAnnotation(InjectEJB.class), f);
99  					this.injectEJB(action, aField);
100 					annotatedFields.add(aField);
101 				}
102 			}
103 
104 
105 			if (annotatedFields.size() == 0) {
106 				cache.noEJBAnnotations(actionClassName);
107 			} else {
108 				cache.cacheAnnotatedFields(actionClassName, annotatedFields);
109 			}
110 		}
111 
112 		return actionInvocation.invoke();
113 	}
114 
115 	private void injectEJB(Object action, AnnotatedField aField)
116 			throws Exception {
117 		InjectEJB annotation = aField.getAnnotation();
118 		Field f = aField.getField();
119 
120 		StringBuilder serviceName = new StringBuilder(
121 				annotation.name() != null ? annotation.name() : f.getType()
122 						.getName());
123 
124 		if (annotation.appname() != null && !annotation.appname().isEmpty()) {
125 			serviceName.insert(0, annotation.appname() + "/");
126 		} else {
127 			serviceName.insert(0, "dao-0.0.1/");
128 		}
129 
130 		LOGGER.info("Test Inject");
131 		Object service = null;
132 		InitialContext ic = new InitialContext();
133 		try {
134 			LOGGER.info("Try EJB: java:global/tiv-page/"
135 					+ serviceName.toString());
136 			service = ic.lookup("java:global/tiv-page/"
137 					+ serviceName.toString());
138 		} catch (NamingException ex) {
139 			LOGGER.info("Error Inject");
140 			LOGGER.info("Try EJB: " + serviceName.toString());
141 			service = ic.lookup(serviceName.toString());
142 		} finally {
143 			if (service != null) {
144 				LOGGER.info("wasAccessible");
145 				boolean wasAccessible = f.isAccessible();
146 				f.setAccessible(true);
147 				f.set(action, service);
148 				f.setAccessible(wasAccessible);
149 			}
150 		}
151 	}
152 
153 }// Ende class