KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > wsmgmt > lifecycle > reconfig > TransformationRuleEventListenerImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.wsmgmt.lifecycle.reconfig;
24
25 import com.sun.enterprise.admin.event.wsmgmt.TransformationRuleEvent;
26 import com.sun.enterprise.admin.event.wsmgmt.TransformationRuleEventListener;
27
28 import com.sun.enterprise.admin.event.AdminEventListenerException;
29 import com.sun.enterprise.config.ConfigBean;
30 import com.sun.enterprise.config.ConfigContext;
31 import com.sun.enterprise.config.ConfigException;
32
33 import com.sun.enterprise.config.serverbeans.WebServiceEndpoint;
34 import com.sun.enterprise.config.serverbeans.TransformationRule;
35 import com.sun.enterprise.config.serverbeans.J2eeApplication;
36 import com.sun.enterprise.config.serverbeans.WebModule;
37 import com.sun.enterprise.config.serverbeans.EjbModule;
38
39 import com.sun.enterprise.admin.wsmgmt.transform.TransformHandler;
40 import com.sun.enterprise.admin.wsmgmt.transform.TransformFilter;
41 import com.sun.enterprise.admin.wsmgmt.filter.spi.Filter;
42 import com.sun.enterprise.admin.wsmgmt.config.impl.WebServiceConfigImpl;
43 import com.sun.enterprise.admin.wsmgmt.config.spi.Constants;
44
45 /**
46  * Listener impl to handle web-service-endpoint/transformation-rule
47  * element events.
48  */

49 public class TransformationRuleEventListenerImpl implements
50         TransformationRuleEventListener {
51
52     /**
53      * Handles web-service-endpoint/transformation-rule element removal.
54      *
55      * @param event Event to be processed.
56      *
57      * @throws AdminEventListenerException when the listener is unable to
58      * process the event.
59      */

60     public void handleDelete(TransformationRuleEvent event)
61              throws AdminEventListenerException {
62         handleReconfiguration(event,true, true);
63     }
64
65     private void handleReconfiguration(TransformationRuleEvent event, boolean
66             takeOld, boolean isRemove)
67              throws AdminEventListenerException {
68        try {
69            ConfigBean bean = getTRBean(event, takeOld);
70            if ( bean instanceof TransformationRule ) {
71                 TransformationRule tr = (TransformationRule) bean;
72                 String JavaDoc appId = getApplicationId(tr);
73                 WebServiceConfigImpl wsc = new WebServiceConfigImpl (
74                     (WebServiceEndpoint) tr.parent());
75                 TransformHandler trh = new TransformHandler( wsc , appId);
76                 Filter f = trh.getFilter(appId, wsc);
77                 TransformFilter tf = null;
78                 if ( f != null) {
79                     tf = (TransformFilter) f;
80                 }
81                 ConfigBean newBean = getTRBean(event, false);
82                 WebServiceConfigImpl nwsc = null;
83                 if ( newBean == null) {
84                        if ( ! isRemove) {
85                             // only remove operation can not have
86
// new element in the new config context
87
throw new AdminEventListenerException();
88                        } else {
89                             nwsc = wsc;
90                        }
91                 } else {
92                     nwsc = new WebServiceConfigImpl(
93                         (WebServiceEndpoint) newBean.parent());
94                 }
95                 if ( tf == null) {
96                     // create new filter to handle transformation
97
tf = (TransformFilter) trh.registerFilter(wsc);
98                 } else {
99                     if ( isRemove) {
100                         String JavaDoc applyTo = tr.getApplyTo();
101                         if ( applyTo.equals(Constants.BOTH) ||
102                             applyTo.equals(Constants.REQUEST)) {
103                             com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[] rtrs =
104                             nwsc.getRequestTransformationRule();
105                             tf.resetRequestChain( pruneList( rtrs,
106                             tr.getName()));
107                         }
108                         if ( applyTo.equals(Constants.BOTH) ||
109                             applyTo.equals(Constants.RESPONSE)) {
110                             com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[] rtrs =
111                             nwsc.getResponseTransformationRule();
112                             tf.resetResponseChain( pruneList( rtrs,
113                             tr.getName()));
114                         }
115                     } else {
116                         tf.resetRequestChain(
117                             nwsc.getRequestTransformationRule());
118                         tf.resetResponseChain(
119                             nwsc.getResponseTransformationRule());
120                     }
121                 }
122            }
123
124         } catch (Exception JavaDoc e) {
125             throw new AdminEventListenerException(e);
126         }
127     }
128
129
130     private com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[]
131      pruneList (
132         com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[] tRules,
133     String JavaDoc name) {
134
135         com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[]
136         newRules =
137         new com.sun.enterprise.admin.wsmgmt.config.spi.TransformationRule[
138         tRules.length-1];
139
140         int newIndex = 0;
141         for ( int index =0; index < tRules.length; index++) {
142             if ( tRules[index].getName().equals(name)) {
143                  // found the match, do not add to the new list
144
} else {
145                  if ( newIndex >= tRules.length-1) {
146                     // removed rule should exist and should not cause array
147
// overflow.
148
throw new RuntimeException JavaDoc();
149                  }
150                  newRules[newIndex++] = tRules[index];
151             }
152         }
153         return newRules;
154     }
155
156     /**
157      * Handles web-service-endpoint/transformation-rule element modification
158      * (attributes/properties values changed).
159      *
160      * @param event Event to be processed.
161      *
162      * @throws AdminEventListenerException when the listener is unable to
163      * process the event.
164      */

165     public void handleUpdate(TransformationRuleEvent event)
166              throws AdminEventListenerException {
167
168         handleReconfiguration(event,false,false);
169     }
170
171     /**
172      * Handles element additions.
173      *
174      * @param event Event to be processed.
175      *
176      * @throws AdminEventListenerException when the listener is unable to
177      * process the event.
178      */

179     public void handleCreate(TransformationRuleEvent event)
180              throws AdminEventListenerException {
181
182         handleReconfiguration(event,false, false);
183     }
184
185     private ConfigBean getTRBean(TransformationRuleEvent event, boolean old)
186             throws ConfigException {
187
188         if (event == null) {
189             throw new IllegalArgumentException JavaDoc();
190         }
191
192         ConfigBean bean = null;
193         ConfigContext ctx = null;
194         String JavaDoc xpath = event.getElementXPath();
195         if (old) {
196             ctx = event.getOldConfigContext();
197         } else {
198             ctx = event.getConfigContext();
199         }
200         if (ctx != null) {
201             bean = ctx.exactLookup(xpath);
202         }
203
204         return bean;
205     }
206
207     private String JavaDoc getApplicationId(TransformationRule trBean) {
208         String JavaDoc name = null;
209
210         ConfigBean bean = null;
211         if ( trBean != null) {
212             bean = (ConfigBean) trBean.parent();
213             if (!( bean instanceof WebServiceEndpoint)) {
214                 throw new RuntimeException JavaDoc();
215             }
216         }
217
218         if (bean != null) {
219             ConfigBean parent = (ConfigBean) bean.parent();
220             if (parent instanceof J2eeApplication) {
221                 J2eeApplication app = (J2eeApplication) parent;
222                 name = app.getName();
223             } else if (parent instanceof WebModule) {
224                 WebModule wm = (WebModule) parent;
225                 name = wm.getName();
226             } else if (parent instanceof EjbModule) {
227                 EjbModule em = (EjbModule) parent;
228                 name = em.getName();
229             }
230         }
231         return name;
232     }
233 }
234
Popular Tags