KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > html > ClusteredConsoleServlet


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.jmx.adaptor.html;
23
24 import java.io.IOException JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.BufferedReader JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.RequestDispatcher JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import javax.naming.NamingEnumeration JavaDoc;
40 import javax.naming.NameClassPair JavaDoc;
41 import javax.management.ObjectName JavaDoc;
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 /** A servlet that provides the cluster view bootstrap index for the
50  * jmx-console cluster frame.
51  *
52  * @author Scott.Stark@jboss.org
53  * @version $Revision: 37459 $
54  */

55 public class ClusteredConsoleServlet extends HttpServlet JavaDoc
56 {
57    private static Logger log = Logger.getLogger(ClusteredConsoleServlet.class);
58
59    private static final String JavaDoc ACTION_PARAM = "action";
60    private static final String JavaDoc CLUSTER_BOOTSTRAP_ACTION = "bootstrap";
61    private static final String JavaDoc CLUSTER_INDEX_ACTION = "index";
62
63    private static final String JavaDoc BOOTSTRAP_PARAM = "bootstrap";
64    private static final String JavaDoc PARTITION_PARAM = "partition";
65    private static final String JavaDoc HOSTNAME_PARAM = "hostname";
66    private static final String JavaDoc PORT_PARAM = "port";
67    private static final String JavaDoc DISCOVERY_GROUP_PARAM = "discoveryGroup";
68    private static final String JavaDoc DISCOVERY_TIMEOUT_PARAM = "discoveryTimeout";
69
70    private String JavaDoc jgProps;
71
72    /** Creates a new instance of HtmlAdaptor */
73    public ClusteredConsoleServlet()
74    {
75    }
76
77    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
78    {
79       super.init(config);
80
81       jgProps = config.getInitParameter("jgProps");
82       if( jgProps == null )
83          throw new ServletException JavaDoc("No jgProps init-param specified");
84       StringBuffer JavaDoc trimedProps = new StringBuffer JavaDoc();
85       StringReader JavaDoc sr = new StringReader JavaDoc(jgProps);
86       BufferedReader JavaDoc br = new BufferedReader JavaDoc(sr);
87       String JavaDoc protocol = null;
88       try
89       {
90          while( (protocol = br.readLine()) != null )
91             trimedProps.append(protocol.trim());
92       }
93       catch(IOException JavaDoc e)
94       {
95          throw new ServletException JavaDoc("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 JavaDoc request, HttpServletResponse JavaDoc response)
106       throws ServletException JavaDoc, IOException JavaDoc
107    {
108       processRequest(request, response);
109    }
110    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
111       throws ServletException JavaDoc, IOException JavaDoc
112    {
113       processRequest(request, response);
114    }
115
116    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
117       throws ServletException JavaDoc, IOException JavaDoc
118    {
119       String JavaDoc 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    /** cluster index view
132     */

133    private void clusterIndex(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
134       throws ServletException JavaDoc, IOException JavaDoc
135    {
136       try
137       {
138          // Query for the membership of the partition cluster
139
String JavaDoc[] hosts = {};
140          request.setAttribute("partition", "none");
141          request.setAttribute("partitionHosts", hosts);
142          RequestDispatcher JavaDoc rd = this.getServletContext().getRequestDispatcher("/cluster/clusterView.jsp");
143          rd.forward(request, response);
144       }
145       catch(Exception JavaDoc 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    /** bootstrap cluster node view
154     */

155    private void clusterBootstrap(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
156       throws ServletException JavaDoc, IOException JavaDoc
157    {
158       String JavaDoc bootstrap = request.getParameter(BOOTSTRAP_PARAM);
159       log.debug("processRequest, parameters:");
160       Enumeration JavaDoc params = request.getParameterNames();
161       while( params.hasMoreElements() )
162       {
163          String JavaDoc name = (String JavaDoc) params.nextElement();
164          log.debug(name+"="+request.getParameter(name));
165       }
166
167       if( bootstrap == null )
168          bootstrap = "discovery";
169       String JavaDoc hostname = request.getParameter(HOSTNAME_PARAM);
170       if( hostname == null || hostname.equalsIgnoreCase("localhost") )
171          hostname = request.getServerName();
172       String JavaDoc partition = request.getParameter(PARTITION_PARAM);
173       String JavaDoc port = request.getParameter(PORT_PARAM);
174       String JavaDoc discoveryGroup = request.getParameter(DISCOVERY_GROUP_PARAM);
175       String JavaDoc discoveryTimeout = request.getParameter(DISCOVERY_TIMEOUT_PARAM);
176
177       log.debug("bootstrap: "+bootstrap);
178       String JavaDoc[] hosts = {};
179       Properties JavaDoc env = new Properties JavaDoc();
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 JavaDoc("Unkown bootstrap mode specified: "+bootstrap);
201          }
202       }
203       catch(Exception JavaDoc e)
204       {
205          throw new ServletException JavaDoc("Failed to bootstrap hosts", e);
206       }
207
208       try
209       {
210          // Query for the membership of the partition cluster
211
partition = env.getProperty(NamingContext.JNP_PARTITION_NAME);
212          request.setAttribute("partition", partition);
213          request.setAttribute("partitionHosts", hosts);
214          RequestDispatcher JavaDoc rd = this.getServletContext().getRequestDispatcher("/cluster/clusterView.jsp");
215          rd.forward(request, response);
216       }
217       catch(Exception JavaDoc 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 JavaDoc[] discoverHosts(Properties JavaDoc env)
226       throws NamingException JavaDoc, IOException JavaDoc
227    {
228       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
229
230       String JavaDoc[] hosts = {};
231       log.debug("Querying HAJNDI: "+env);
232       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
233       String JavaDoc partitionName = env.getProperty(NamingContext.JNP_PARTITION_NAME);
234       if( partitionName != null )
235       {
236          String JavaDoc partitionJndiName = "/HAPartition/" + partitionName;
237          HAPartition partition = (HAPartition) ctx.lookup(partitionJndiName);
238          Vector JavaDoc view = partition.getCurrentView();
239          log.debug("Found HAPartition: "+partitionName);
240          hosts = new String JavaDoc[view.size()];
241          for(int v = 0; v < view.size(); v ++)
242          {
243             Object JavaDoc addr = view.get(v);
244             log.debug(addr);
245             hosts[v] = addr.toString();
246          }
247       }
248       else
249       {
250          NamingEnumeration JavaDoc iter = ctx.list("/HAPartition");
251          while( iter.hasMore() )
252          {
253             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) iter.next();
254             partitionName = pair.getName();
255             String JavaDoc partitionJndiName = "/HAPartition/" + partitionName;
256             HAPartition partition = (HAPartition) ctx.lookup(partitionJndiName);
257             env.setProperty(NamingContext.JNP_PARTITION_NAME, partitionName);
258             Vector JavaDoc view = partition.getCurrentView();
259             log.debug("Found HAPartition: "+partitionName);
260             hosts = new String JavaDoc[view.size()];
261             for(int v = 0; v < view.size(); v ++)
262             {
263                Object JavaDoc 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 JavaDoc[] queryHost(String JavaDoc hostname, String JavaDoc port, Properties JavaDoc env)
276       throws Exception JavaDoc
277    {
278       String JavaDoc[] hosts = {};
279       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
280       env.setProperty(Context.PROVIDER_URL, hostname+":"+port);
281       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
282       NamingEnumeration JavaDoc iter = ctx.list("/HAPartition");
283       String JavaDoc partitionName = null;
284       while( iter.hasMore() )
285       {
286          NameClassPair JavaDoc pair = (NameClassPair JavaDoc) iter.next();
287          partitionName = pair.getName();
288          break;
289       }
290       if( partitionName == null )
291          throw new NamingException JavaDoc("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 JavaDoc clusterPartition = new ObjectName JavaDoc("jboss:service="+partitionName);
296       Vector JavaDoc view = (Vector JavaDoc) adaptor.getAttribute(clusterPartition, "CurrentView");
297       log.debug("Found ClusterPartition: "+clusterPartition);
298       hosts = new String JavaDoc[view.size()];
299       for(int v = 0; v < view.size(); v ++)
300       {
301          Object JavaDoc 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