KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > HttpListenerVirtualServerAssociationMgr


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 /*
25  * $Id: HttpListenerVirtualServerAssociationMgr.java,v 1.3 2005/12/25 03:42:22 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 //jdk imports
31
import java.util.Set JavaDoc;
32 import java.util.LinkedHashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import com.sun.enterprise.util.i18n.StringManager;
37
38 //config imports
39
import com.sun.enterprise.config.serverbeans.Domain;
40 import com.sun.enterprise.config.serverbeans.Config;
41 import com.sun.enterprise.config.serverbeans.VirtualServer;
42 import com.sun.enterprise.config.serverbeans.HttpListener;
43 import com.sun.enterprise.config.ConfigContext;
44 import com.sun.enterprise.config.ConfigException;
45
46 public class HttpListenerVirtualServerAssociationMgr
47 {
48     /**
49      * i18n strings manager object
50      */

51     private static final StringManager strMgr =
52         StringManager.getManager(HttpListenerVirtualServerAssociationMgr.class);
53
54     private final Config config;
55
56     public HttpListenerVirtualServerAssociationMgr(ConfigContext cctx,
57                                                    String JavaDoc configRef)
58         throws ConfigException
59     {
60         checkArg(cctx, strMgr.getString("http_listener_vs_assoc_mgr.null_context"));
61         checkArg(configRef, strMgr.getString("http_listener_vs_assoc_mgr.null_config_ref"));
62         final Domain domain = (Domain)cctx.getRootConfigBean();
63         config = domain.getConfigs().getConfigByName(configRef);
64         checkArg(config, strMgr.getString("http_listener_vs_assoc_mgr.no_such_element", configRef));
65     }
66
67     public void addHttpListenerRef(String JavaDoc listenerId) throws ConfigException
68     {
69         final String JavaDoc defaultVS = getHttpListener(listenerId).getDefaultVirtualServer();
70         final Set JavaDoc s = getHttpListeners(defaultVS);
71         s.add(listenerId);
72         setHttpListeners(defaultVS, s);
73     }
74
75     public void deleteHttpListenerRef(String JavaDoc listenerId) throws ConfigException
76     {
77         VirtualServer[] servers = config.getHttpService().getVirtualServer();
78         if (servers != null)
79         {
80             for (int i = 0; i < servers.length; i++)
81             {
82                 Set JavaDoc httpListeners = getHttpListeners(servers[i].getId());
83                 if (httpListeners.contains(listenerId))
84                 {
85                     httpListeners.remove(listenerId);
86                     servers[i].setHttpListeners(setToStr(httpListeners));
87                 }
88             }
89         }
90     }
91
92     public void changeHttpListenerRef(String JavaDoc listenerId,
93                                       String JavaDoc oldVs,
94                                       String JavaDoc newVs)
95         throws ConfigException
96     {
97         Set JavaDoc s = getHttpListeners(oldVs);
98         s.remove(listenerId);
99         setHttpListeners(oldVs, s);
100
101         s = getHttpListeners(newVs);
102         s.add(listenerId);
103         setHttpListeners(newVs, s);
104     }
105
106     Set JavaDoc getHttpListeners(String JavaDoc vsID) throws ConfigException
107     {
108         return strToSet(getVirtualServer(vsID).getHttpListeners());
109     }
110
111     void setHttpListeners(String JavaDoc vsID, Set JavaDoc httpListeners)
112         throws ConfigException
113     {
114         getVirtualServer(vsID).setHttpListeners(setToStr(httpListeners));
115     }
116
117     protected HttpListener getHttpListener(String JavaDoc id) throws ConfigException
118     {
119         HttpListener listener = config.getHttpService().getHttpListenerById(id);
120         checkArg(listener, strMgr.getString("http_listener_vs_assoc_mgr.no_such_element", id));
121         return listener;
122     }
123
124     protected VirtualServer getVirtualServer(String JavaDoc id) throws ConfigException
125     {
126         VirtualServer vs = config.getHttpService().getVirtualServerById(id);
127         checkArg(vs, strMgr.getString("http_listener_vs_assoc_mgr.no_such_element", id));
128         return vs;
129     }
130
131     private Set JavaDoc strToSet(String JavaDoc httpListeners)
132     {
133         final Set JavaDoc s = new LinkedHashSet JavaDoc();
134         if (null != httpListeners)
135         {
136             StringTokenizer JavaDoc strTok = new StringTokenizer JavaDoc(httpListeners, ",");
137             while (strTok.hasMoreTokens())
138             {
139                 s.add(strTok.nextToken());
140             }
141         }
142         return s;
143     }
144
145     private String JavaDoc setToStr(Set JavaDoc httpListeners)
146     {
147         String JavaDoc s = null;
148         final Iterator JavaDoc it = httpListeners.iterator();
149         if (it.hasNext())
150         {
151             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
152             while (it.hasNext())
153             {
154                 sb.append((String JavaDoc)it.next());
155                 if (it.hasNext()) { sb.append(','); }
156             }
157             s = sb.toString();
158         }
159         return s;
160     }
161
162     private void checkArg(Object JavaDoc o, Object JavaDoc msg) throws ConfigException
163     {
164         if (null == o)
165         {
166             throw new ConfigException(msg.toString());
167         }
168     }
169 }
170
Popular Tags