001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.ha;
023:
024: import org.jboss.ha.singleton.HASingletonSupport;
025: import org.jboss.invocation.Invocation;
026: import org.jboss.naming.Util;
027:
028: import javax.management.ObjectName;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import java.lang.reflect.Method;
032: import java.util.Properties;
033:
034: /**
035: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
036: * @version $Revision: 8784 $
037: */
038: public class HASingletonInvoker extends HASingletonSupport implements
039: HASingletonInvokerMBean {
040: /** The proxy factory. */
041: private ObjectName proxyFactory;
042:
043: /** The cached proxy. */
044: private Proxy proxy;
045:
046: /** Where is bound the ha proxy. */
047: private String jndiName;
048:
049: /** The callback service which this service depends on. */
050: private Callback callback;
051:
052: /** The JNDI properties. */
053: private Properties jndiProperties;
054:
055: /** How many time we retry on fail over. */
056: private int maxRetries;
057:
058: /** How long we wait before trying a jndi lookup during fail over. */
059: private long retryWaitingTimeMS;
060:
061: public long getRetryWaitingTimeMS() {
062: return retryWaitingTimeMS;
063: }
064:
065: public void setRetryWaitingTimeMS(long retryWaitingTimeMS) {
066: this .retryWaitingTimeMS = retryWaitingTimeMS;
067: }
068:
069: public int getMaxRetries() {
070: return maxRetries;
071: }
072:
073: public void setMaxRetries(int maxRetries) {
074: this .maxRetries = maxRetries;
075: }
076:
077: public Properties getJNDIProperties() {
078: return jndiProperties;
079: }
080:
081: public void setJNDIProperties(Properties jndiProperties) {
082: this .jndiProperties = jndiProperties;
083: }
084:
085: public ObjectName getProxyFactory() {
086: return proxyFactory;
087: }
088:
089: public void setProxyFactory(ObjectName proxyFactory) {
090: this .proxyFactory = proxyFactory;
091: }
092:
093: public Callback getCallback() {
094: return callback;
095: }
096:
097: public void setCallback(Callback callback) {
098: this .callback = callback;
099: }
100:
101: public String getJNDIName() {
102: return jndiName;
103: }
104:
105: public void setJNDIName(String jndiName) {
106: this .jndiName = jndiName;
107: }
108:
109: public Proxy lookupProxy() {
110: if (proxy == null) {
111: try {
112: InitialContext ctx = new InitialContext(jndiProperties);
113: proxy = (Proxy) ctx.lookup(jndiName);
114: } catch (NamingException e) {
115: log.error("Proxy " + jndiName + " not bound");
116: }
117: }
118: return proxy;
119: }
120:
121: public Object invoke(String methodName, Class[] types, Object[] args)
122: throws Exception {
123: int retries = maxRetries;
124: while (retries-- > 0) {
125: try {
126: Proxy proxy = lookupProxy();
127: return proxy.invoke(methodName, types, args);
128: } catch (Throwable t) {
129: if (!(t instanceof Error)) {
130: proxy = null;
131: if (retries > 0) {
132: log.error("Cannot invoke proxy will retry "
133: + retries + " times", t);
134: try {
135: log.debug("Sleeping for "
136: + retryWaitingTimeMS
137: + " before trying to fail over");
138: Thread.sleep(retryWaitingTimeMS);
139: } catch (InterruptedException ignore) {
140: // Get out of the loop
141: retries = 0;
142: }
143: } else {
144: log
145: .error(
146: "Cannot invoke proxy will no more retry",
147: t);
148: }
149: } else {
150: throw (Error) t;
151: }
152: }
153: }
154: // Todo : throw exception
155: return null;
156: }
157:
158: protected void startService() throws Exception {
159: callback.setInvoker(this );
160:
161: //
162: super .startService();
163: }
164:
165: protected void stopService() throws Exception {
166: super .stopService();
167:
168: //
169: callback.setInvoker(null);
170: }
171:
172: public void startSingleton() {
173: String name = callback.getDisplayName();
174:
175: //
176: try {
177:
178: // Start the service.
179: log.debug("Starting singleton " + name);
180: callback.startSingleton();
181: log.debug("Singleton " + name + " started");
182:
183: //
184: log.debug("Binding singleton proxy " + name);
185: Object proxy = server.getAttribute(proxyFactory, "Proxy");
186: Util.bind(new InitialContext(), jndiName, proxy);
187: log.debug("Singleton proxy " + name + " bound");
188: } catch (Exception e) {
189: log.error("Not able to start singleton " + name + " bound",
190: e);
191: }
192: }
193:
194: public void stopSingleton() {
195: // Stop the singleton
196: if (callback != null) {
197: //
198: String name = callback.getDisplayName();
199:
200: //
201: log.debug("Stopping singleton proxy " + name);
202: callback.stopSingleton();
203: log.debug("Singleton " + name + " stopped");
204: }
205: }
206:
207: public Object invoke(Invocation mi) throws Exception {
208: Method m = mi.getMethod();
209: Object[] args = mi.getArguments();
210: return callback
211: .invoke(m.getName(), m.getParameterTypes(), args);
212: }
213: }
|