KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > reconfig > VirtualServerReconfig


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
24 package com.sun.enterprise.web.reconfig;
25
26 import com.sun.enterprise.admin.event.AdminEventListener;
27 import com.sun.enterprise.admin.event.AdminEventListenerException;
28 import com.sun.enterprise.admin.event.http.HSVirtualServerEvent;
29 import com.sun.enterprise.admin.event.http.HSVirtualServerEventListener;
30 import com.sun.enterprise.config.ConfigAdd;
31 import com.sun.enterprise.config.ConfigContext;
32 import com.sun.enterprise.config.ConfigException;
33 import com.sun.enterprise.config.ConfigChange;
34 import com.sun.enterprise.config.ConfigBean;
35 import com.sun.enterprise.config.serverbeans.Config;
36 import com.sun.enterprise.config.serverbeans.ElementProperty;
37 import com.sun.enterprise.config.serverbeans.VirtualServer;
38 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
39 import com.sun.enterprise.config.serverbeans.Server;
40 import com.sun.enterprise.web.PEWebContainer;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Set JavaDoc;
44
45 import org.apache.catalina.LifecycleException;
46
47
48 /**
49  * This class handles dynamic reconfiguration notification related to
50  * virtual-server creation, update and deletion.
51  *
52  * @author Jean-Francois Arcand
53  */

54 public class VirtualServerReconfig implements HSVirtualServerEventListener{
55    
56     private static PEWebContainer webContainer =
57                                  ((PEWebContainer)PEWebContainer.getInstance());
58     
59     /**
60      * Create a new virtual-server.
61      */

62     public void handleCreate(HSVirtualServerEvent event)
63                                             throws AdminEventListenerException {
64         
65         if ( webContainer == null) return;
66            
67         try{
68             ConfigContext configContext = event.getConfigContext();
69             Config config = ServerBeansFactory.getConfigBean(configContext);
70                                     
71             if ( config == null) return;
72             
73             ConfigAdd configAdd = null;
74             ArrayList JavaDoc configChangeList = event.getConfigChangeList();
75             VirtualServer vsBean = null;
76             String JavaDoc xpath = null;
77             Server serverBean = ServerBeansFactory.getServerBean(configContext);
78             ElementProperty elementProperty = null;
79             Object JavaDoc object;
80             Object JavaDoc configObject;
81             
82             for (int i=0; i < configChangeList.size(); i++){
83                 configObject = configChangeList.get(i);
84
85                 if ( configObject instanceof ConfigAdd) {
86                     configAdd = (ConfigAdd)configObject;
87                     xpath = configAdd.getXPath();
88                     if( xpath != null){
89                         object = configContext.exactLookup(xpath);
90                         // always a VirtualServer first and then ElementProperty.
91
if ( object instanceof VirtualServer){
92                             vsBean = (VirtualServer)object;
93                             webContainer.createHost(vsBean,configContext,true);
94                         } else if (object instanceof ElementProperty) {
95                             // Lookup the server beans.
96
xpath = xpath.substring(0,xpath.lastIndexOf("/"));
97                             vsBean = (VirtualServer)configContext.exactLookup(xpath);
98                             elementProperty = (ElementProperty)object;
99                             webContainer.updateHostProperties(
100                                        vsBean,
101                                        elementProperty.getName(),
102                                        elementProperty.getValue(),
103                                        config.getHttpService());
104                         }
105                     }
106                 }
107             }
108         } catch (Exception JavaDoc ex) {
109            throw new AdminEventListenerException(ex);
110         }
111     }
112     
113     
114     /**
115      * Update an existing virtual-server.
116      */

117     public void handleUpdate(HSVirtualServerEvent event)
118                                             throws AdminEventListenerException {
119         
120         if ( webContainer == null) return;
121            
122         try{
123             ConfigContext configContext = event.getConfigContext();
124             Config config = ServerBeansFactory.getConfigBean(configContext);
125             
126             if ( config == null) return;
127             
128             ConfigChange configChange = null;
129             ArrayList JavaDoc<ConfigChange> configChangeList =
130                                                     event.getConfigChangeList();
131             VirtualServer vsBean = null;
132             String JavaDoc xpath = null;
133             Server serverBean = ServerBeansFactory.getServerBean(configContext);
134             ElementProperty elementProperty = null;
135             Object JavaDoc object;
136
137             for (int i=0; i < configChangeList.size(); i++){
138                 configChange = configChangeList.get(i);
139                 
140                 xpath = configChange.getXPath();
141                 if( xpath != null){
142                     object = configContext.exactLookup(xpath);
143                     if ( object instanceof VirtualServer){
144                         vsBean = (VirtualServer)object;
145                         webContainer.updateHost(vsBean,
146                                                 config.getHttpService(),
147                                                 serverBean);
148                     } else if (object instanceof ElementProperty) {
149                         // Lookup the server beans.
150
xpath = xpath.substring(0,xpath.lastIndexOf("/"));
151                         vsBean = (VirtualServer)configContext.exactLookup(xpath);
152                         elementProperty = (ElementProperty)object;
153                         webContainer.updateHostProperties(
154                                    vsBean,
155                                    elementProperty.getName(),
156                                    elementProperty.getValue(),
157                                    config.getHttpService());
158                     }
159                 }
160             }
161         } catch (Exception JavaDoc ex) {
162            throw new AdminEventListenerException(ex);
163         }
164     }
165     
166     
167     /**
168      * Delete an existing virtual-server.
169      *
170      * Implementation note: we cannot use the same approach used when
171      * handling create/update event since the element is deleted before this
172      * method is invoked (strange behaviour). Instead we need to find by
173      * ourself which virtual-server has been deleted.
174      */

175     public void handleDelete(HSVirtualServerEvent event)
176                                             throws AdminEventListenerException {
177          
178         if ( webContainer == null) return;
179            
180         try{
181             if( event != null){
182                 ConfigContext configContext = event.getConfigContext();
183                 Config config = ServerBeansFactory.getConfigBean(configContext);
184                 
185                 if ( config!= null)
186                     webContainer.deleteHost(config.getHttpService());
187             }
188         } catch( Exception JavaDoc ex){
189            throw new AdminEventListenerException(ex);
190         }
191     }
192 }
193
194
195
Popular Tags