1 22 package org.jboss.jmx.adaptor.html; 23 24 import java.io.IOException ; 25 import java.io.StringReader ; 26 import java.io.BufferedReader ; 27 import java.util.Properties ; 28 import java.util.Vector ; 29 import java.util.Enumeration ; 30 import javax.servlet.http.HttpServlet ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.RequestDispatcher ; 36 import javax.naming.InitialContext ; 37 import javax.naming.Context ; 38 import javax.naming.NamingException ; 39 import javax.naming.NamingEnumeration ; 40 import javax.naming.NameClassPair ; 41 import javax.management.ObjectName ; 42 43 import org.jboss.ha.framework.interfaces.HAPartition; 44 import org.jboss.jmx.adaptor.control.AddressPort; 45 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 46 import org.jboss.logging.Logger; 47 import org.jnp.interfaces.NamingContext; 48 49 55 public class ClusteredConsoleServlet extends HttpServlet 56 { 57 private static Logger log = Logger.getLogger(ClusteredConsoleServlet.class); 58 59 private static final String ACTION_PARAM = "action"; 60 private static final String CLUSTER_BOOTSTRAP_ACTION = "bootstrap"; 61 private static final String CLUSTER_INDEX_ACTION = "index"; 62 63 private static final String BOOTSTRAP_PARAM = "bootstrap"; 64 private static final String PARTITION_PARAM = "partition"; 65 private static final String HOSTNAME_PARAM = "hostname"; 66 private static final String PORT_PARAM = "port"; 67 private static final String DISCOVERY_GROUP_PARAM = "discoveryGroup"; 68 private static final String DISCOVERY_TIMEOUT_PARAM = "discoveryTimeout"; 69 70 private String jgProps; 71 72 73 public ClusteredConsoleServlet() 74 { 75 } 76 77 public void init(ServletConfig config) throws ServletException 78 { 79 super.init(config); 80 81 jgProps = config.getInitParameter("jgProps"); 82 if( jgProps == null ) 83 throw new ServletException ("No jgProps init-param specified"); 84 StringBuffer trimedProps = new StringBuffer (); 85 StringReader sr = new StringReader (jgProps); 86 BufferedReader br = new BufferedReader (sr); 87 String protocol = null; 88 try 89 { 90 while( (protocol = br.readLine()) != null ) 91 trimedProps.append(protocol.trim()); 92 } 93 catch(IOException e) 94 { 95 throw new ServletException ("Failed to process jgProps", e); 96 } 97 jgProps = trimedProps.toString(); 98 log.debug("Using jbPropgs: "+jgProps); 99 } 100 101 public void destroy() 102 { 103 } 104 105 protected void doGet(HttpServletRequest request, HttpServletResponse response) 106 throws ServletException , IOException 107 { 108 processRequest(request, response); 109 } 110 protected void doPost(HttpServletRequest request, HttpServletResponse response) 111 throws ServletException , IOException 112 { 113 processRequest(request, response); 114 } 115 116 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 117 throws ServletException , IOException 118 { 119 String action = request.getParameter(ACTION_PARAM); 120 121 if( action == null ) 122 action = CLUSTER_INDEX_ACTION; 123 124 if( action.equals(CLUSTER_INDEX_ACTION) ) 125 clusterIndex(request, response); 126 else if( action.equals(CLUSTER_BOOTSTRAP_ACTION) ) 127 clusterBootstrap(request, response); 128 } 129 130 131 133 private void clusterIndex(HttpServletRequest request, HttpServletResponse response) 134 throws ServletException , IOException 135 { 136 try 137 { 138 String [] hosts = {}; 140 request.setAttribute("partition", "none"); 141 request.setAttribute("partitionHosts", hosts); 142 RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/cluster/clusterView.jsp"); 143 rd.forward(request, response); 144 } 145 catch(Exception e) 146 { 147 log.debug("Failed to get partition view", e); 148 response.sendError(HttpServletResponse.SC_NO_CONTENT, "No partition view found"); 149 } 150 } 151 152 153 155 private void clusterBootstrap(HttpServletRequest request, HttpServletResponse response) 156 throws ServletException , IOException 157 { 158 String bootstrap = request.getParameter(BOOTSTRAP_PARAM); 159 log.debug("processRequest, parameters:"); 160 Enumeration params = request.getParameterNames(); 161 while( params.hasMoreElements() ) 162 { 163 String name = (String ) params.nextElement(); 164 log.debug(name+"="+request.getParameter(name)); 165 } 166 167 if( bootstrap == null ) 168 bootstrap = "discovery"; 169 String hostname = request.getParameter(HOSTNAME_PARAM); 170 if( hostname == null || hostname.equalsIgnoreCase("localhost") ) 171 hostname = request.getServerName(); 172 String partition = request.getParameter(PARTITION_PARAM); 173 String port = request.getParameter(PORT_PARAM); 174 String discoveryGroup = request.getParameter(DISCOVERY_GROUP_PARAM); 175 String discoveryTimeout = request.getParameter(DISCOVERY_TIMEOUT_PARAM); 176 177 log.debug("bootstrap: "+bootstrap); 178 String [] hosts = {}; 179 Properties env = new Properties (); 180 try 181 { 182 if( bootstrap.equals("discovery") ) 183 { 184 if( partition != null && partition.length() > 0 ) 185 env.setProperty(NamingContext.JNP_PARTITION_NAME, partition); 186 if( port != null && port.length() > 0 ) 187 env.setProperty(NamingContext.JNP_DISCOVERY_PORT, port); 188 if( discoveryGroup != null && discoveryGroup.length() > 0 ) 189 env.setProperty(NamingContext.JNP_DISCOVERY_GROUP, discoveryGroup); 190 if( discoveryTimeout != null && discoveryTimeout.length() > 0 ) 191 env.setProperty(NamingContext.JNP_DISCOVERY_TIMEOUT, discoveryTimeout); 192 hosts = discoverHosts(env); 193 } 194 else if( bootstrap.equals("byhost") ) 195 { 196 queryHost(hostname, port, env); 197 } 198 else 199 { 200 throw new ServletException ("Unkown bootstrap mode specified: "+bootstrap); 201 } 202 } 203 catch(Exception e) 204 { 205 throw new ServletException ("Failed to bootstrap hosts", e); 206 } 207 208 try 209 { 210 partition = env.getProperty(NamingContext.JNP_PARTITION_NAME); 212 request.setAttribute("partition", partition); 213 request.setAttribute("partitionHosts", hosts); 214 RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/cluster/clusterView.jsp"); 215 rd.forward(request, response); 216 } 217 catch(Exception e) 218 { 219 log.debug("Failed to get partition view", e); 220 response.sendError(HttpServletResponse.SC_NO_CONTENT, "No partition view found"); 221 } 222 } 223 224 225 private String [] discoverHosts(Properties env) 226 throws NamingException , IOException 227 { 228 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 229 230 String [] hosts = {}; 231 log.debug("Querying HAJNDI: "+env); 232 InitialContext ctx = new InitialContext (env); 233 String partitionName = env.getProperty(NamingContext.JNP_PARTITION_NAME); 234 if( partitionName != null ) 235 { 236 String partitionJndiName = "/HAPartition/" + partitionName; 237 HAPartition partition = (HAPartition) ctx.lookup(partitionJndiName); 238 Vector view = partition.getCurrentView(); 239 log.debug("Found HAPartition: "+partitionName); 240 hosts = new String [view.size()]; 241 for(int v = 0; v < view.size(); v ++) 242 { 243 Object addr = view.get(v); 244 log.debug(addr); 245 hosts[v] = addr.toString(); 246 } 247 } 248 else 249 { 250 NamingEnumeration iter = ctx.list("/HAPartition"); 251 while( iter.hasMore() ) 252 { 253 NameClassPair pair = (NameClassPair ) iter.next(); 254 partitionName = pair.getName(); 255 String partitionJndiName = "/HAPartition/" + partitionName; 256 HAPartition partition = (HAPartition) ctx.lookup(partitionJndiName); 257 env.setProperty(NamingContext.JNP_PARTITION_NAME, partitionName); 258 Vector view = partition.getCurrentView(); 259 log.debug("Found HAPartition: "+partitionName); 260 hosts = new String [view.size()]; 261 for(int v = 0; v < view.size(); v ++) 262 { 263 Object addr = view.get(v); 264 AddressPort ap = AddressPort.getMemberAddress(addr); 265 log.debug(ap); 266 hosts[v] = ap.getHostAddress(); 267 } 268 break; 269 } 270 } 271 272 return hosts; 273 } 274 275 private String [] queryHost(String hostname, String port, Properties env) 276 throws Exception 277 { 278 String [] hosts = {}; 279 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 280 env.setProperty(Context.PROVIDER_URL, hostname+":"+port); 281 InitialContext ctx = new InitialContext (env); 282 NamingEnumeration iter = ctx.list("/HAPartition"); 283 String partitionName = null; 284 while( iter.hasMore() ) 285 { 286 NameClassPair pair = (NameClassPair ) iter.next(); 287 partitionName = pair.getName(); 288 break; 289 } 290 if( partitionName == null ) 291 throw new NamingException ("Failed to find any parition"); 292 env.setProperty(NamingContext.JNP_PARTITION_NAME, partitionName); 293 294 RMIAdaptor adaptor = (RMIAdaptor) ctx.lookup("jmx/rmi/RMIAdaptor"); 295 ObjectName clusterPartition = new ObjectName ("jboss:service="+partitionName); 296 Vector view = (Vector ) adaptor.getAttribute(clusterPartition, "CurrentView"); 297 log.debug("Found ClusterPartition: "+clusterPartition); 298 hosts = new String [view.size()]; 299 for(int v = 0; v < view.size(); v ++) 300 { 301 Object addr = view.get(v); 302 AddressPort ap = AddressPort.getMemberAddress(addr); 303 log.debug(ap); 304 hosts[v] = ap.getHostAddress(); 305 } 306 return hosts; 307 } 308 } 309 | Popular Tags |