1 23 24 27 28 package com.sun.enterprise.admin.mbeans; 29 30 import java.util.Set ; 32 import java.util.LinkedHashSet ; 33 import java.util.Iterator ; 34 import java.util.StringTokenizer ; 35 36 import com.sun.enterprise.util.i18n.StringManager; 37 38 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 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 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 listenerId) throws ConfigException 68 { 69 final String defaultVS = getHttpListener(listenerId).getDefaultVirtualServer(); 70 final Set s = getHttpListeners(defaultVS); 71 s.add(listenerId); 72 setHttpListeners(defaultVS, s); 73 } 74 75 public void deleteHttpListenerRef(String 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 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 listenerId, 93 String oldVs, 94 String newVs) 95 throws ConfigException 96 { 97 Set 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 getHttpListeners(String vsID) throws ConfigException 107 { 108 return strToSet(getVirtualServer(vsID).getHttpListeners()); 109 } 110 111 void setHttpListeners(String vsID, Set httpListeners) 112 throws ConfigException 113 { 114 getVirtualServer(vsID).setHttpListeners(setToStr(httpListeners)); 115 } 116 117 protected HttpListener getHttpListener(String 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 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 strToSet(String httpListeners) 132 { 133 final Set s = new LinkedHashSet (); 134 if (null != httpListeners) 135 { 136 StringTokenizer strTok = new StringTokenizer (httpListeners, ","); 137 while (strTok.hasMoreTokens()) 138 { 139 s.add(strTok.nextToken()); 140 } 141 } 142 return s; 143 } 144 145 private String setToStr(Set httpListeners) 146 { 147 String s = null; 148 final Iterator it = httpListeners.iterator(); 149 if (it.hasNext()) 150 { 151 StringBuffer sb = new StringBuffer (); 152 while (it.hasNext()) 153 { 154 sb.append((String )it.next()); 155 if (it.hasNext()) { sb.append(','); } 156 } 157 s = sb.toString(); 158 } 159 return s; 160 } 161 162 private void checkArg(Object o, Object msg) throws ConfigException 163 { 164 if (null == o) 165 { 166 throw new ConfigException(msg.toString()); 167 } 168 } 169 } 170 | Popular Tags |