001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.weblogic;
005:
006: import weblogic.management.descriptors.webapp.FilterMBean;
007: import weblogic.management.descriptors.webapp.FilterMappingMBean;
008: import weblogic.management.descriptors.webapp.ParameterMBean;
009: import weblogic.management.descriptors.webapp.ServletMBean;
010: import weblogic.management.descriptors.webapp.UIMBean;
011: import weblogic.servlet.internal.WebAppServletContext;
012:
013: import com.tc.aspectwerkz.joinpoint.StaticJoinPoint;
014: import com.tc.object.bytecode.hook.impl.ClassProcessorHelper;
015: import com.terracotta.session.SessionFilter;
016:
017: public class SessionAspect {
018:
019: private final ThreadLocal dsoFilter = new ThreadLocal();
020:
021: public Object addFilterIfNeeded(StaticJoinPoint jp,
022: weblogic.servlet.internal.WebAppServletContext context)
023: throws Throwable {
024: FilterMBean[] filters = (FilterMBean[]) jp.proceed();
025: if (filters == null) {
026: filters = new FilterMBean[] {};
027: }
028:
029: if (isDSOSesisons(context)) {
030: FilterMBean filter = new DSOFilterMBean();
031: setFilterInstance(context, filter);
032:
033: FilterMBean[] withDSO = new FilterMBean[filters.length + 1];
034: System.arraycopy(filters, 0, withDSO, 1, filters.length);
035: withDSO[0] = filter;
036: filters = withDSO;
037: }
038: return filters;
039: }
040:
041: public Object addFilterMappingIfNeeded(StaticJoinPoint jp,
042: weblogic.servlet.internal.WebAppServletContext context)
043: throws Throwable {
044: FilterMappingMBean[] mappings = (FilterMappingMBean[]) jp
045: .proceed();
046: if (mappings == null) {
047: mappings = new FilterMappingMBean[] {};
048: }
049:
050: if (isDSOSesisons(context)) {
051: FilterMappingMBean[] withDSO = new FilterMappingMBean[mappings.length + 1];
052: System.arraycopy(mappings, 0, withDSO, 1, mappings.length);
053: withDSO[0] = new DSOFilterMappingMBean(
054: getFilterInstance(context));
055: mappings = withDSO;
056: }
057: return mappings;
058: }
059:
060: private FilterMBean getFilterInstance(WebAppServletContext context) {
061: FilterMBean filter = (FilterMBean) dsoFilter.get();
062: if (filter == null) {
063: throw new AssertionError("No filter found for "
064: + context.getName());
065: }
066: dsoFilter.set(null);
067: return filter;
068: }
069:
070: private void setFilterInstance(WebAppServletContext context,
071: FilterMBean filter) {
072: if (dsoFilter.get() != null) {
073: throw new AssertionError("Filter already present for "
074: + context.getName());
075: }
076: dsoFilter.set(filter);
077: }
078:
079: private static boolean isDSOSesisons(WebAppServletContext context) {
080: String appName = context.getName();
081: boolean rv = ClassProcessorHelper.isDSOSessions(appName);
082: return rv;
083: }
084:
085: private static class DSOFilterMappingMBean implements
086: FilterMappingMBean {
087:
088: private final FilterMBean filter;
089:
090: public DSOFilterMappingMBean(FilterMBean filter) {
091: this .filter = filter;
092: }
093:
094: public FilterMBean getFilter() {
095: return filter;
096: }
097:
098: public ServletMBean getServlet() {
099: return null;
100: }
101:
102: public String getUrlPattern() {
103: return "/*";
104: }
105:
106: public void setFilter(FilterMBean filtermbean) {
107: throw new AssertionError();
108: }
109:
110: public void setServlet(ServletMBean servletmbean) {
111: throw new AssertionError();
112: }
113:
114: public void setUrlPattern(String s) {
115: throw new AssertionError();
116: }
117:
118: public void addDescriptorError(String s) {
119: throw new AssertionError();
120: }
121:
122: public String[] getDescriptorErrors() {
123: throw new AssertionError();
124: }
125:
126: public boolean isValid() {
127: throw new AssertionError();
128: }
129:
130: public void removeDescriptorError(String s) {
131: throw new AssertionError();
132: }
133:
134: public void setDescriptorErrors(String[] as) {
135: throw new AssertionError();
136: }
137:
138: public String getName() {
139: throw new AssertionError();
140: }
141:
142: public void register() {
143: throw new AssertionError();
144: }
145:
146: public void setName(String s) {
147: throw new AssertionError();
148: }
149:
150: public String toXML(int i) {
151: throw new AssertionError();
152: }
153:
154: public void unregister() {
155: throw new AssertionError();
156: }
157:
158: }
159:
160: private static class DSOParameterMBean implements ParameterMBean {
161: public String getParamName() {
162: return SessionFilter.APP_SERVER_PARAM_NAME;
163: }
164:
165: public void setParamName(String s) {
166: throw new AssertionError();
167: }
168:
169: public String getParamValue() {
170: return SessionFilter.BEA_WEBLOGIC;
171: }
172:
173: public void setParamValue(String s) {
174: throw new AssertionError();
175: }
176:
177: public String getDescription() {
178: return "Session Filter parameters for BEA Weblogic";
179: }
180:
181: public void setDescription(String s) {
182: throw new AssertionError();
183: }
184: }
185:
186: private static class DSOFilterMBean implements FilterMBean {
187:
188: public void addInitParam(ParameterMBean parametermbean) {
189: //
190: }
191:
192: public String getFilterClass() {
193: return SessionFilter.FILTER_CLASS;
194: }
195:
196: public String getFilterName() {
197: return SessionFilter.FILTER_NAME;
198: }
199:
200: public ParameterMBean[] getInitParams() {
201: return new ParameterMBean[] { new DSOParameterMBean() };
202: }
203:
204: public UIMBean getUIData() {
205: throw new AssertionError();
206: }
207:
208: public void removeInitParam(ParameterMBean parametermbean) {
209: throw new AssertionError();
210: }
211:
212: public void setFilterClass(String s) {
213: throw new AssertionError();
214: }
215:
216: public void setFilterName(String s) {
217: throw new AssertionError();
218: }
219:
220: public void setInitParams(ParameterMBean[] aparametermbean) {
221: throw new AssertionError();
222: }
223:
224: public void setUIData(UIMBean uimbean) {
225: throw new AssertionError();
226: }
227:
228: public void addDescriptorError(String s) {
229: throw new AssertionError();
230: }
231:
232: public String[] getDescriptorErrors() {
233: throw new AssertionError();
234: }
235:
236: public boolean isValid() {
237: throw new AssertionError();
238: }
239:
240: public void removeDescriptorError(String s) {
241: throw new AssertionError();
242: }
243:
244: public void setDescriptorErrors(String[] as) {
245: throw new AssertionError();
246: }
247:
248: public String getName() {
249: throw new AssertionError();
250: }
251:
252: public void register() {
253: throw new AssertionError();
254: }
255:
256: public void setName(String s) {
257: throw new AssertionError();
258: }
259:
260: public String toXML(int i) {
261: throw new AssertionError();
262: }
263:
264: public void unregister() {
265: throw new AssertionError();
266: }
267:
268: }
269:
270: }
|