KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > framework > server > util > PingJndi


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.ha.framework.server.util;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.Context JavaDoc;
30
31 import org.jboss.system.ServiceMBeanSupport;
32 import org.jboss.logging.Logger;
33 import org.jboss.ha.framework.server.util.TopologyMonitorService.AddressPort;
34
35 /** A utility MBean that can be used as the trigger target of the
36  * TopologyMonitorService to probe the state of JNDI on the cluster nodes.
37  *
38  * @author Scott.Stark@jboss.org
39  * @version $Revision: 58095 $
40  */

41 public class PingJndi extends ServiceMBeanSupport
42    implements PingJndiMBean
43 {
44    private String JavaDoc urlPrefix;
45    private String JavaDoc urlSuffix;
46    private String JavaDoc urlPattern;
47    private String JavaDoc[] lookupNames;
48
49    /** Get the names of JNDI bindings that should be queried on each host
50     * @return the array of target names to test
51     * @jmx:managed-attribute
52     */

53    public String JavaDoc[] getLookupNames()
54    {
55       return lookupNames;
56    }
57    /** Set the names of JNDI bindings that should be queried on each host
58     * @param names
59     * @jmx:managed-attribute
60     */

61    public void setLookupNames(String JavaDoc[] names)
62    {
63       this.lookupNames = names;
64    }
65
66    /** Get the Context.PROVIDER_URL regular expression.
67     * @return the regular expression containing the host, for example
68     * 'jnp://(host):1099/'
69     * @jmx:managed-attribute
70     */

71    public String JavaDoc getProviderURLPattern()
72    {
73       return urlPattern;
74    }
75
76    /** Set the regular expression containing the hostname/IP address of
77     * the JNDI provider. This expression is used to build the JNDI
78     * Context.PROVIDER_URL for each node in the cluster. The expression
79     * should contain a "(host)" component that will be replaced with the
80     * cluster node hostname.
81     *
82     * @param regex the regular expression containing the host, for example
83     * 'jnp://(host):1099/'
84     * @jmx:managed-attribute
85     */

86    public void setProviderURLPattern(String JavaDoc regex)
87    {
88       this.urlPattern = regex;
89       this.urlPrefix = regex;
90       this.urlSuffix = "";
91       String JavaDoc hostExp = "{host}";
92       int hostIndex = regex.indexOf(hostExp);
93       if( hostIndex >= 0 )
94       {
95          urlPrefix = regex.substring(0, hostIndex);
96          int endIndex = hostIndex + hostExp.length();
97          urlSuffix = regex.substring(endIndex);
98       }
99    }
100
101    /** The TopologyMonitorService trigger callback operation.
102     *
103     * @param removed ArrayList<AddressPort> of nodes that were removed
104     * @param added ArrayList<AddressPort> of nodes that were added
105     * @param members ArrayList<AddressPort> of nodes currently in the cluster
106     * @param logLoggerName the log4j category name used by the
107     * TopologyMonitorService. This is used for logging to integrate with
108     * the TopologyMonitorService output.
109     */

110    public void membershipChanged(ArrayList JavaDoc removed, ArrayList JavaDoc added,
111       ArrayList JavaDoc members, String JavaDoc logLoggerName)
112    {
113       log.debug("membershipChanged");
114       Logger tmsLog = Logger.getLogger(logLoggerName);
115       Hashtable JavaDoc localEnv = null;
116       try
117       {
118          InitialContext JavaDoc localCtx = new InitialContext JavaDoc();
119          localEnv = localCtx.getEnvironment();
120       }
121       catch(NamingException JavaDoc e)
122       {
123          tmsLog.error("Failed to obtain InitialContext env", e);
124          return;
125       }
126
127       tmsLog.info("Checking removed hosts JNDI binding");
128       doLookups(localEnv, tmsLog, removed);
129       tmsLog.info("Checking added hosts JNDI binding");
130       doLookups(localEnv, tmsLog, added);
131       tmsLog.info("Checking members hosts JNDI binding");
132       doLookups(localEnv, tmsLog, members);
133    }
134
135    private void doLookups(Hashtable JavaDoc localEnv, Logger tmsLog, ArrayList JavaDoc nodes)
136    {
137       for(int n = 0; n < nodes.size(); n ++)
138       {
139          AddressPort addrInfo = (AddressPort) nodes.get(n);
140          String JavaDoc providerURL = urlPrefix + addrInfo.getHostName() + urlSuffix;
141          Hashtable JavaDoc env = new Hashtable JavaDoc(localEnv);
142          env.put(Context.PROVIDER_URL, providerURL);
143          tmsLog.info("Checking names on: "+addrInfo);
144          try
145          {
146             InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
147             for(int s = 0; s < lookupNames.length; s ++)
148             {
149                String JavaDoc name = lookupNames[s];
150                Object JavaDoc value = ctx.lookup(name);
151                tmsLog.info("lookup("+name+"): "+value);
152             }
153          }
154          catch(Exception JavaDoc e)
155          {
156             tmsLog.error("Failed lookups on: "+addrInfo, e);
157          }
158       }
159    }
160 }
161
Popular Tags